[Linux][2011-5-25]Linux "struct stat" 结构 & stat(),lstat(),fstat()

来源:互联网 发布:千里眼淘宝插件mac 编辑:程序博客网 时间:2024/06/14 06:25

struct stat
{
      dev_t st_dev; /* ID of device containing file -文件所在设备的ID*/
      ino_t st_ino; /* inode number -inode节点号*/
      mode_t st_mode; /* protection -保护模式?*/
      nlink_t st_nlink; /* number of hard links -链向此文件的连接数(硬连接)*/
      uid_t st_uid; /* user ID of owner -user id*/
      gid_t st_gid; /* group ID of owner - group id*/
      dev_t st_rdev; /* device ID (if special file) -设备号,针对设备文件*/
      off_t st_size; /* total size, in bytes -文件大小,字节为单位*/
      blksize_t st_blksize; /* blocksize for filesystem I/O -系统块的大小*/
      blkcnt_t st_blocks; /* number of blocks allocated -文件所占块数*/
      time_t st_atime; /* time of last access -文件数据的最后访问时间 eg:read*/
     time_t st_mtime; /* time of last modification -文件数据的最后修改时间 eg:write*/
     time_t st_ctime; /* time of last status change -i节点状态的最后更改时间 eg:chmod chown*/
};

 

缩写小结: atime: 最后访问时间

               mtime:最后修改时间

               ctime:最后状态转换时间

 

函数原型#include <sys/stat.h>

 

 

stat,lstat,fstat1 函数都是获取文件(普通文件,目录,管道,socket,字符,块()的属性。

stat 通过文件名              fname

fstat通过文件描述符        fd

lstat通过连接文件名        ln

获取文件属性

int stat(const char *restrict pathname, struct stat *restrict buf);提供文件名字,获取文件对应属性。
int fstat(int filedes, struct stat *buf);通过文件描述符获取文件对应的属性。
int lstat(const char *restrict pathname, struct stat *restrict buf);连接文件描述命,获取文件属性。2 文件对应的属性

 

 

 

#include <unsitd.h>
#inlcude <sys/stat.h>
#include <sys/types.h>
int fstat(int filedes,struct stat *buf);
int stat(const char *path,struct stat *buf);
int lstat(const char *path,struct stat *buf);
这三个系统调用都可以返回指定文件的状态信息,这些信息被写到结构struct stat的缓冲区中。通过分析这个结构可以获得指定文件的信息。

 

返回说明:  
成功执行时,返回0。失败返回-1,errno被设为以下的某个值  
EBADF:  文件描述词无效
EFAULT: 地址空间不可访问
ELOOP:  遍历路径时遇到太多的符号连接
ENAMETOOLONG:文件路径名太长
ENOENT:路径名的部分组件不存在,或路径名是空字串
ENOMEM:内存不足
ENOTDIR:路径名的部分组件不是目录

 

 

 

void report(struct stat *ptr)
{
printf("The major device no is:%d/n",major(ptr->st_dev));//主设备号
printf("The minor device no is:%d/n",minor(ptr->st_dev));//从设备号
printf("The file's node number is:%d/n",ptr->st_ino);//文件节点号
printf("The file's access mode is:%d/n",ptr->st_mode);//文件的访问模式
printf("The file's hard link number is:%d/n",ptr->st_nlink);//文件的硬链接数目
printf("The file's user id is:%d/n",ptr->uid);//文件拥有者的ID
printf("The file's group id is:%d/n",ptr->gid);//文件的组ID
printf("The file's size is:%d/n",ptr->st_size);//文件的大小
printf("The block size is:%d/n",ptr->blksize);//文件占用的块数量
printf("The number of allocated blocks is:%d/n",ptr->st_blocks);//文件分配块数量
struct tm*accesstime,*lmodifytime,*lchangetime;//访问时间,修改时间,最后一个改变时间(属性)
accesstime=localtime(&(ptr->st_atime));
accesstime=localtime(&(ptr->st_mtime));
accesstime=localtime(&(ptr->st_ctime));
printf("The last access time is: %d::%d::%d/n",accesstime->hour,accesstime->min,accesstime->sec);
printf("The last modify time is:%d::%d::%d/n",lmodifytime->hour,lmodifytime->min,lmodifytime->sec);
printf("The last change time is:%d::%d::%d/n",lchangetime->hour,lchangetime->min,lchangetime->sec);
}

结构time_t可用用localtime转换成tm结构,获得本地时间。

 

1.普通文件(Regular file)。这是最常见的文件类型,这种文件包含了某种形式的数据。至于这种数据是文本还是二进制数据对于系统核而言并无区别。对普通文件内容的解释由处理该文件的应用程序进行。

  2.目录文件(Directory file)。这种文件包含了其它文件的名字以及指向与这些文件有关信息的指针。对一个目录文件具有读许可数的任一进程都可以读该目录的内容,但只有系统核可以写目录文件。

  3.字符特殊文件(Charocter special file)。这种文件用于系统中的某些类型的设备。

  4.块特殊文件(Block special file)。这种文件典型地用于磁盘设备。系统中的所有设备或者是字符特殊文件,或者是块特殊文件。

  5.FIFO。这种文件用于进程间的通信,有时也将其称为命名管道。在14.5对其进行说明。

  6.套接口(socket)。这种文件用于进程间的网络通信。套接口也可用于在一台宿主机上的进程之间的非网络通信。在第十五章,我们将用套接口进行进程间的通信。只有4.3+BSD才返回套接口文件类型,虽然 SVR4支持用套接口进行进程间通信,但现在是经由套接口函数库实现的,而不是通过系统核内的套接口文件类型,将来的SVR4版本可能会支持套接口文件类型。

  7.符号连接(Symboliclink)。这种文件指向另一个文件。我们在4.16中将更多地述及符号连接。