Linux stat函數和stat命令

stat函數和stat命令

linux文件裏的【inode = index node】解釋:要理解inode必須瞭解磁盤和【目錄項】,inode實際是鏈接【目錄項】和磁盤的中間物質。node

  • 圖裏的大圈表明硬件的磁盤,裏面的小圈表明某個文件存儲在磁盤上了。
  • 【inode = index node】的node(承載node信息的結構體是:stat,stat的定義在後面 )裏面有:
    • 文件大小
    • 文件的最後修改時間
    • 文件的所屬用戶
    • 文件的權限
    • 硬連接計數(ls -l 顯示出來的數字)
    • 塊位置:指定文件存儲在磁盤的具體位置。
  • 下圖中的hello是個普通文件,hello.hard是hello的硬連接
  • 文件夾裏放的就是每一個文件的【目錄項】以下圖,【目錄項】裏有:
    • 文件名
    • 該目錄項的大小
    • 文件的類型
    • inode

  • 如何查看文件的【inode】呢?使用【-i】選項linux

    ls -li 文件名

    執行結果:c++

    ys@ys-VirtualBox:~/lianxi1$ ls -li hello hello.hard 
    3801352 -rw-rw-r-- 2 ys ys 0 4月  24 11:01 hello
    3801352 -rw-rw-r-- 2 ys ys 0 4月  24 11:01 hello.hard

    發現hello和hello.hard的inode(3801352)是相同的,也就說明了,只在磁盤上存了一份。shell

  • 如何查看目錄項呢?用emacs或者vim打開目錄(lianxi1),截圖以下。可是看不到文件的【inode】。編程

1,stat函數:取得指定文件的文件屬性,文件屬性存儲在結構體stat裏。vim

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);

struct stat 結構體:微信

struct stat {
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* Inode number */
               mode_t    st_mode;        /* File type and mode */
               nlink_t   st_nlink;       /* Number of hard links */
               uid_t     st_uid;         /* User ID of owner */
               gid_t     st_gid;         /* Group ID of owner */
               dev_t     st_rdev;        /* Device ID (if special file) */
               off_t     st_size;        /* Total size, in bytes */
               blksize_t st_blksize;     /* Block size for filesystem I/O */
               blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

               /* Since Linux 2.6, the kernel supports nanosecond
                  precision for the following timestamp fields.
                  For the details before Linux 2.6, see NOTES. */

               struct timespec st_atim;  /* Time of last access */
               struct timespec st_mtim;  /* Time of last modification */
               struct timespec st_ctim;  /* Time of last status change */

           #define st_atime st_atim.tv_sec      /* Backward compatibility */
           #define st_mtime st_mtim.tv_sec
           #define st_ctime st_ctim.tv_sec
           };
  • st_dev:設備ID,不太經常使用socket

  • st_ino:【inode】,【inode】是啥?不知道就看上面關於【inode】的解釋ide

  • st_mode:文件的類型和權限,共16位,以下圖。函數

    • 0-11位控制文件的權限

    • 12-15位控制文件的類型

    0-2比特位:其餘用戶權限

    3-5比特位:組用戶權限

    6-8比特位:本用戶權限

    9-11比特位:特殊權限

    12-15比特位:文件類型(由於文件類型只有7中,因此用12-14位就夠了

文件類型的宏以下(下面的數字是8進制):

  • S_IFSOCK 0140000 socket
  • S_IFLNK 0120000 symbolic link(軟鏈接)
  • S_IFREG 0100000 regular file(普通文件)
  • S_IFBLK 0060000 block device(塊設備文件)
  • S_IFDIR 0040000 directory(目錄)
  • S_IFCHR 0020000 character device(字符設備文件)
  • S_IFIFO 0010000 FIFO(管道)
判斷文件類型的函數,返回true,false       
   S_ISREG(stat.st_mode)  is it a regular file?
   S_ISDIR(stat.st_mode)  directory?
   S_ISCHR(stat.st_mode)  character device?
   S_ISBLK(stat.st_mode)  block device?
   S_ISFIFO(m) FIFO (named pipe)?
   S_ISLNK(stat.st_mode)  symbolic link?  (Not in POSIX.1-1996.)
   S_ISSOCK(stat.st_mode) socket?  (Not in POSIX.1-1996.)

文件權限的宏以下:

S_ISUID     04000   set-user-ID bit
       S_ISGID     02000   set-group-ID bit (see below)
       S_ISVTX     01000   sticky bit (see below)

       S_IRWXU     00700   owner has read, write, and execute permission
       S_IRUSR     00400   owner has read permission
       S_IWUSR     00200   owner has write permission
       S_IXUSR     00100   owner has execute permission

       S_IRWXG     00070   group has read, write, and execute permission
       S_IRGRP     00040   group has read permission
       S_IWGRP     00020   group has write permission
       S_IXGRP     00010   group has execute permission

       S_IRWXO     00007   others (not in group) have read,  write,  and
                           execute permission
       S_IROTH     00004   others have read permission
       S_IWOTH     00002   others have write permission
       S_IXOTH     00001   others have execute permission
  • st_nlink:硬鏈接計數

  • st_uid:這個文件所屬用戶的ID

  • st_gid:這個文件所屬用戶的組ID

  • st_rdev:特殊設備的ID,不太經常使用

  • st_size:文件的大小

  • st_blksize:不明是幹啥的

  • st_blocks:不明是幹啥的

  • struct timespec st_atim:最後訪問的時間

  • struct timespec st_mtim:最後修改的時間

  • struct timespec st_ctim:最後狀態改變的時間

    struct timespec {
      __kernel_time_t tv_sec;  /* seconds */當前時間到1970.1.1 00:00:00的秒數
      long        tv_nsec;    /* nanoseconds *//納秒數(不知道從哪到哪的)
    };
    1s  秒   = 1000ms 毫秒
    1ms 毫秒 = 1000us 微秒
    1us 微秒 = 1000ns 納秒

pathname:文件名

返回值:0表明成功;-1表明失敗,並設置error

例子:statbuf是結構體stat,能夠看出來st_mode是個10進制的數字。

  • st_mode

    用gdb顯示st_mode,發現返回的st_mode是個10進制的數字,用gdb的【p/o】(o表明用8進製表示)命令把10進制的33204轉換成了8進制的【0100664】,第一個0代筆是8進制,後三位的【100】表明文件類型,從上面的說明能夠看出來【100】表明普通文件,最後三位的【664】表明這個文件的權限(本用戶:rw-,組用戶:rw-,其餘用戶:r--)。因此從st_mode裏就能夠得知文件的類型和權限設置(只使用了16個比特位,真的好節省空間,牛逼!)

  • st_uid

  • st_gid

    發現st_uid和st_gid是1000,但這個1000怎麼和用戶對應上呢,查看/etc/passwd文件,發現用於ys的uid和gid都是1000,因此就對應上了。

stat命令,是stat函數對應,執行結果以下:

ys@ys-VirtualBox:~/lianxi1$ stat hello
  File: hello
  Size: 11          Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d  Inode: 3801352     Links: 2
Access: (0764/-rwxrw-r--)  Uid: ( 1000/      ys)   Gid: ( 1000/      ys)
Access: 2019-04-24 17:02:39.199461489 +0800
Modify: 2019-04-24 16:54:16.407461489 +0800
Change: 2019-04-24 17:03:44.927461489 +0800

2,getpwuid函數:返回/etc/passwd文件裏指定uid的行,把這一行的信息放入結構體passwd中。雖然返回值是指針,但不須要調用free函數。

#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);

struct passwd {
  char   *pw_name;       /* username */
  char   *pw_passwd;     /* user password */
  uid_t   pw_uid;        /* user ID */
  gid_t   pw_gid;        /* group ID */
  char   *pw_gecos;      /* user information */
  char   *pw_dir;        /* home directory */
  char   *pw_shell;      /* shell program */
};

3,getgrgid函數:返回/etc/group文件裏指定gid的行,把這一行的信息放入結構體group中。雖然返回值是指針,但不須要調用free函數。

#include <sys/types.h>
#include <grp.h>
struct group *getgrnam(const char *name);
struct group *getgrgid(gid_t gid);

struct group {
  char   *gr_name;        /* group name */
  char   *gr_passwd;      /* group password */
  gid_t   gr_gid;         /* group ID */
  char  **gr_mem;         /* NULL-terminated array of pointers
                             to names of group members */
};

4,localtime函數:傳入從stat函數裏獲得的st_mtim.tv_sec(當前時間到1970.1.1 00:00:00的秒數),獲得結構體tm。雖然返回值是指針,但不須要調用free函數。

#include <time.h>
struct tm *localtime(const time_t *timep);
struct tm {
  int tm_sec;    /* Seconds (0-60) */
  int tm_min;    /* Minutes (0-59) */
  int tm_hour;   /* Hours (0-23) */
  int tm_mday;   /* Day of the month (1-31) */
  int tm_mon;    /* Month (0-11) */
  int tm_year;   /* Year - 1900 */
  int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
  int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
  int tm_isdst;  /* Daylight saving time */
};

5,lstat函數:stat碰到軟連接,會追述到源文件,穿透;lstat並不會穿透。

例子:模仿ls -l 文件

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>//getpwuid
#include <stdlib.h>
#include <time.h>//localtime
#include <grp.h>//getgrgid

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

  struct stat sbuf;
  //stat(argv[1], &sbuf);
  lstat(argv[1], &sbuf);

  char str[11] = {0};
  memset(str, '-', (sizeof str - 1));
    
  //文件類型
  if(S_ISREG(sbuf.st_mode))  str[0] = '-';
  if(S_ISDIR(sbuf.st_mode))  str[0] = 'd';
  if(S_ISCHR(sbuf.st_mode))  str[0] = 'c';
  if(S_ISBLK(sbuf.st_mode))  str[0] = 'b';
  if(S_ISFIFO(sbuf.st_mode)) str[0] = 'p';
  if(S_ISLNK(sbuf.st_mode))  str[0] = 'l';
  if(S_ISSOCK(sbuf.st_mode)) str[0] = 's';

  //本用戶的文件權限
  if(sbuf.st_mode & S_IRUSR) str[1] = 'r';
  if(sbuf.st_mode & S_IWUSR) str[2] = 'w';
  if(sbuf.st_mode & S_IXUSR) str[3] = 'x';
  
  //本用戶的組的文件權限
  if(sbuf.st_mode & S_IRGRP) str[4] = 'r';
  if(sbuf.st_mode & S_IWGRP) str[5] = 'w';
  if(sbuf.st_mode & S_IXGRP) str[6] = 'x';
  
  //其餘用戶的文件權限
  if(sbuf.st_mode & S_IROTH) str[7] = 'r';
  if(sbuf.st_mode & S_IWOTH) str[8] = 'w';
  if(sbuf.st_mode & S_IXOTH) str[9] = 'x';

  char ymd[20] = {0};
  //取得日期和時間
  struct tm* tm = localtime(&sbuf.st_atim.tv_sec);
  sprintf(ymd, "%2d月  %2d %02d:%02d", tm->tm_mon + 1, tm->tm_mday,
      tm->tm_hour + 1,tm->tm_sec);
    
  //-rw-r--r-- 1 ys ys 134 4月  25 09:21 st2.c
  printf("%s %ld %s %s %ld %s %s\n", str, sbuf.st_nlink,
     getpwuid(sbuf.st_uid)->pw_name, getgrgid(sbuf.st_gid)->gr_name,
     sbuf.st_size, ymd, argv[1]);
  return 0;
}

6,access函數:判斷調用程序的用戶對於指定文件的權限(可讀?可寫?可執行?)

#include <unistd.h>
int access(const char *pathname, int mode);
  • pathname:文件
  • mode
    • R_OK:可讀?
    • W_OK:可寫?
    • X_OK:可執行?
    • F_OK:文件存在?
  • 返回值
    • 查詢的權限存在或者文件存在:返回0。
    • 查詢的權限不存在或者文件不存在:返回-1。

例子:

#include <stdio.h>
#include <unistd.h>//access

int main(int argc, char* argv[]){
  if(access(argv[1], R_OK) == 0)
    printf("read ok\n");
  if(access(argv[1], W_OK) == 0)
    printf("write ok\n");
  if(access(argv[1], X_OK) == 0)
    printf("exe ok\n");
  if(access(argv[1], F_OK) == 0)
    printf("exists\n");
}
  • 先用ls -l 查看/usr/include/time.h文件的權限,結果以下

    ys@ys-VirtualBox:~/lianxi$ ls -l /usr/include/time.h
    -rw-r--r-- 1 root root 10360 4月  17  2018 /usr/include/time.h
  • 用ys用戶執行例子程序,查看/usr/include/time.h文件,結果以下。由於time.h是屬於root用戶的,對於其餘用戶來講是[r--],因此得出下面的結果。

    ys@ys-VirtualBox:~/lianxi$ ./ac /usr/include/time.h
    read ok
    exists
  • 仍是用ys用戶執行,可是加上sudo,結果以下。發現結果和root用戶相同。由於加了sudo,就編程了root用戶。

    ys@ys-VirtualBox:~/lianxi$ sudo ./ac /usr/include/time.h
    [sudo] password for ys: 
    read ok
    write ok
    exists

7,truncate函數:截斷文件和擴展文件的大小

#include <unistd.h>
#include <sys/types.h>
int truncate(const char *path, off_t length);
  • path:文件
  • length:
    • length大於原來文件的大小,則擴展文件的大小至length
    • length小於原來文件的大小,則截斷文件的大小至length

8,link函數:建立硬連接

#include <unistd.h>
int link(const char *oldpath, const char *newpath);

返回值:成功返回0,失敗返回-1,並設置errno。

9,symlink函數:建立軟連接

#include <unistd.h>
int symlink(const char *target, const char *linkpath);

返回值:成功返回0,失敗返回-1,並設置errno。

10,readlink函數:找到軟連接對應的實際文件,把文件的名字放入buf裏。注意:硬連接不行。

#include <unistd.h>
ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);

返回值:成功返回寫入buf的字節數,失敗返回-1,並設置errno。

11,unlink函數:刪除軟硬連接,也能夠刪除文件。

#include <unistd.h>
int unlink(const char *pathname);

返回值:成功返回0,失敗返回-1,並設置errno。

有個特殊用法:下面的open代碼想要建立hello文件,而後直接用unlink刪除,可是能寫入成功,ret是大於0的,程序執行完,發現沒有作成hello文件。

結論:當執行unlink後,計數爲0後,但,發現別的進程還引用這個文件,這個時間點,unlink不會刪除這個文件,等這個進程結束後,再刪除,因此下面的write代碼可以寫入成功。
利用這個特色能夠實現:在線觀看視頻時,實際是把視頻文件下載到了本地(而後代碼裏,使用unlink),看完後視頻文件的計數爲0,就自動刪除了,不怕視頻被泄露出去。

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){
  int fd = open("hello", O_WRONLY | O_CREAT, 0666);
  unlink("hello");
  int ret = write(fd, "aaa", 4);
  if(ret > 0){
    printf("write OK\n");
  }
  
}

12,chown函數:改變文件的所屬用戶和組

#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
  • pathname:文件
  • owner:用戶ID(數字的)/etc/passwd
  • group:組ID(數字的)/etc/group
  • 返回值:0成功,-1失敗。

13,rename函數:重命名

#include <stdio.h>
int rename(const char *oldpath, const char *newpath);
  • oldpath :原來的文件名後者目錄
  • newpath:新的文件名後者目錄
  • 返回值:0成功,-1失敗。

14,getcwd函數:得到當前工做的目錄

#include <unistd.h>
char *getcwd(char *buf, size_t size);
  • buf:當前工做的目錄
  • size:緩衝區大小
  • 返回值:
    • 成功返回當前工做的目錄
    • 失敗返回NULL

15,chdir函數:改變進程的工做目錄

#include <unistd.h>
int chdir(const char *path);
  • path:目標工做目錄
  • 返回值:0成功,-1失敗

16,mkdir函數:建立目錄

#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
  • pathname:目標工做目錄
  • mode:mode & ~umask & 0777 。注意,若是沒有x權限,則沒法cd進入這個目錄。
  • 返回值:0成功,-1失敗

17,rmdir函數:刪除目錄,目錄必須是空目錄,也就是裏面沒有任何文件。

#include <unistd.h>
int rmdir(const char *pathname);

18,opendir函數:打開目錄

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
  • name:目錄名
  • 返回值:a pointer to the directory stream

19,readdir函數:讀目錄

#include <dirent.h>
struct dirent *readdir(DIR *dirp);

struct dirent {
  ino_t          d_ino;       /* Inode number */
  off_t          d_off;       /* Not an offset; see below */
  unsigned short d_reclen;    /* Length of this record */
  unsigned char  d_type;      /* Type of file; not supported
                                 by all filesystem types */
  char           d_name[256]; /* Null-terminated filename */
};
  • dirp:opendir函數的返回值
  • 返回值:結構體dirent,能夠理解成最上面說的【目錄項】
    • NULL表明讀到末尾或者有錯誤
    • NULL之外表明目錄項的內容

20,closedir函數:關閉目錄

#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
  • dirp:opendir函數的返回值

21,strerron函數:打印出errno對應的文字信息。

#include <string.h>
char *strerror(int errnum);
  • errnum的宏放在文件:/usr/include/asm-generic/errno.h

例子:

#include <string.h>
#include <stdio.h>
#include <asm-generic/errno.h>//EDEADLK
int main(){
  char* buf = strerror(EDEADLK);
  printf("%s\n", buf);//Resource deadlock avoided
}

22,dup和dup2函數:文件描述符的重定向

#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
  • dup:和open相似,先打開一個新的文件描述符,讓新的文件描述符也指向:oldfd指向的地方。
    • 成功返回新打開的文件描述符;失敗返回-1.
  • dup2:
    • 先消除newfd的指向
    • 再讓newfd指向oldfd指向的地方
    • 成功返回newfd;失敗返回-1.

例子:調用printf2次,第一次printf把內容寫到文件;第二次printf把內容打印到屏幕。

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){

  int oldfd = dup(STDOUT_FILENO);
  int fd = open("www", O_WRONLY | O_CREAT, 0666);
  dup2(fd, STDOUT_FILENO);
  printf("aaaa\n");
  fflush(stdout);
  int ret = dup2(oldfd, STDOUT_FILENO);
  //int ret = dup2(oldfd, 6);
  //perror("dup2:");
  printf("reg:%d\n", ret);
  printf("aaaa\n");
  close(fd);
}

c/c++ 學習互助QQ羣:877684253

本人微信:xiaoshitou5854

相關文章
相關標籤/搜索