天天看點

stat結構體 詳解

#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;

structstatbuf;

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);

}