stat结构体 详解

来源:互联网 发布:apache json解析 编辑:程序博客网 时间:2024/05/07 23:19

#include <sys/stat.h>

char file1[1024]="/file1.log"

char file2[1024]="/file2.log"

struct stat fileStat;

if(stat(file1, &fileStat) < 0)

    error();

};

if(S_ISDIR(fileStat.st_mode))

{
   continue;
  };
  if(fileStat.st_size == 0)

{
   continue;
  }


   struct stat mystat;

now=time(0);
   if(stat(file2,&mystat)<0 || now-mystat.st_ctime<1800)
  {

  continue;
  }

 

stat结构体  stat 结构定义于:/usr/include/sys/stat.h 文件中

  struct stat finfo;

  stat( sFileName, &finfo );

  int size = finfo.st_size;

  struct stat {

  mode_t st_mode; //文件对应的模式,文件,目录等

  ino_t st_ino; //i-node节点号

  dev_t st_dev; //设备号码

  dev_t st_rdev; //特殊设备号码

  nlink_t st_nlink; //文件的连接数

  uid_t st_uid; //文件所有者

  gid_t st_gid; //文件所有者对应的组

  off_t st_size; //普通文件,对应的文件字节数

  time_t st_atime; //文件最后被访问的时间

  time_t st_mtime; //文件内容最后被修改的时间

  time_t st_ctime; //文件状态(属性)改变时间

  blksize_t st_blksize; //文件内容对应的块大小

  blkcnt_t st_blocks; //文件内容对应的块数量

  }; stat命令  stat是linux中经常被忽略的一个命令,常被用来显示文件的详细信息,请注意,这个命令是区别于ls命令的,下面是Linus中--help的帮助内容:Usage: stat [OPTION] FILE... Display file or filesystem status. -f, --filesystem display filesystem status instead of file status -c --format=FORMAT use the specified FORMAT instead of the default -L, --dereference follow links -t, --terse print the information in terse form --help displ ...

 

学习,stat,lstat,fstat

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

函数原型

#include <sys/stat.h>

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 文件对应的属性

struct stat {

        mode_t     st_mode;       //文件对应的模式,文件,目录等

        ino_t      st_ino;       //inode节点号

        dev_t      st_dev;        //设备号码

        dev_t      st_rdev;       //特殊设备号码

        nlink_t    st_nlink;      //文件的连接数

        uid_t      st_uid;        //文件所有者

        gid_t      st_gid;        //文件所有者对应的组

        off_t      st_size;       //普通文件,对应的文件字节数

        time_t     st_atime;      //文件最后被访问的时间

        time_t     st_mtime;      //文件内容最后被修改的时间

        time_t     st_ctime;      //文件状态改变时间

        blksize_t st_blksize;    //文件内容对应的块大小

        blkcnt_t   st_blocks;     //伟建内容对应的块数量

      };

可以通过上面提供的函数,返回一个结构体,保存着文件的信息。

 

stat结构中最常用到的属性是st_mode(文件的类型及文件的访问权限)、st_nlink(硬链接数,表示有几个链接到该文件上)、st_uid、st_gid、st_size(以字节为单位的文件长度,只对普通文件、目录文件和符号连接有意义)、st_atime、st_mtime、st_ctime。

       我们曾一再提到Unix系统中一切皆可视为文件,不过细而化之的话又可以分为多种类型,那么在程序中如何去对文件类型进行判别呢?这就需要用到下表中所示的一些宏:

      

作用

S_ISREG()

测试是否为普通文件

S_ISDIR()

测试是否为目录文件

S_ISCHR()

测试是否为字符特殊文件

S_ISBLK()

测试是否为块特殊文件

S_ISFIFO()

测试是否为FIFO文件

S_ISLNK()

测试是否为链接文件

S_ISSOCK()

测试是否为socket文件

S_ISUID()

测试是否设置了“设置-用户-ID”位

S_ISGID()

测试是否设置了“设置-组-ID”位

      

  [程序6]

     

  #include <stdio.h>

  #include <stdlib.h>

  #include <sys/types.h>

  #include <sys/stat.h>

  #include <fcntl.h>

  #include <unistd.h>

 

  int main(int argc, char * argv[])

       {

    int i;

    struct stat buf;

    char *ptr;

    for(i=1; i< argc; i++)

    {

        printf("%s: ", argv[i]);

      if(lstat(argv[i],&buf)<0)

      {

                     printf("lstat error.");

                     continue;

      }

      if(S_ISREG(buf.st_mode)) ptr = "regular";

      else if(S_ISDIR(buf.st_mode)) ptr = "directory";

      else if(S_ISCHR(buf.st_mode)) ptr = "char special";

      else if(S_ISBLK(buf.st_mode)) ptr = "block special";

      else if(S_ISFIFO(buf.st_mode)) ptr = "fifo";

      else ptr = "Others";

      printf(" %s\n",ptr );

    }

    return 0;

  }

      

  编译程序后,我们先来使用命令mkfifo myfifo.pipe建一个管道文件,然后运行:

       filetype myfifo.pipe . /etc/passwd

       屏幕会显示:

         myfifo.pipe : fifo   

       . : directory

       /etc/passwd : regular

     

  此外,st_mode属性还包含了可用于对文件的属主及权限进行判断的宏,如下表所示:   

       

意义

S_IRUSR

所有者-读

S_IWUSR

所有者-写

S_IXUSR

所有者-执行

S_IRGRP

组-读

S_IWGRP

组-写

S_IXGRP

组-执行

S_IROTH

其他-读

S_IWOTH

其他-写

S_IXOTH

其他-执行

 

 

这三个函数的功能是一致的,都用于获取文件相关信息,但应用于不同的文件对象。对于函数中给出pathname参数,stat函数返回与此命名文件有关的信息结构,fstat函数获取已在描述符fields上打开文件的有关信息,lstat函数类似于stat但是当命名的文件是一个符号链接时,lstat返回该符号链接的有关信息,而不是由该符号链接引用文件的信息。第二个参数buf是指针,它指向一个用于保存文件描述信息的结构,由函数填写结构内容。该结构的实际定义可能随实现有所不同.

用法:

#include

int stat(const char *path, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *path, struct stat *buf);

参数:
path:文件路径名。
filedes:文件描述词。
buf:是以下结构体的指针


structstat{
mode_t st_mode; //(文件保护模式)文件类型和权限信息
ino_t st_ino; //文件结点号
dev_t st_dev; //文件所在设备的文件系统标识号 device number (file system)
dev_t st_rdev; //文件所表示的特殊设备文件的设备标识 device number for special files
nlink_t st_nlink; //符号链接数
uid_t st_uid; //文件用户标识 用户ID
gid_t st_gid; //文件用户组标识 组ID
off_t st_size; // 总大小,字节为单位 size in bytes,for regular files
time_t st_st_atime; //文件内容最后访问的时间
time_t st_mtime; //文件内容最后修改时间
time_t st_ctime; //文件结构最后状态改变时间
blksize_t st_blksize; // 文件系统的最优I/O块大小 best I/O block size
blkcnt_t st_blocks; //分配给文件的块的数量,512字节为1单元 number of disk blocks allocated
};

文件类型:
普通文件,目录文件,块特殊文件,字符特殊文件,套接字,FIFO,符号链接.
文件类型信息包含在stat结构的st_mode成员中,可以用如下的宏确定文件类型,这些宏是stat结构中st_mode的成员.
S_ISREG();

S_ISDIR();

S_ISBLK();

S_ISCHR();

S_ISSOCK();

S_ISFIFO();

S_ISLNK();

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

示例:
#include
int main(int argc,char* argv[])
{
int i;
struct
statbuf;
char * ptr;

for(i=1;i
{
if(lstat(argv[i],&buf)<0)
{
perror(”错误原因是:”);
continue;
}

if (S_ISREG(buf.st_mode))
ptr=”普通文件”;
if (S_ISDIR(buf.st_mode))
ptr=”目录”;

//……and so on…

cout<<”参数为:”<<<”的标识是一个”<<
}
exit(0);
}

原创粉丝点击