精品免费在线观看-精品欧美-精品欧美成人bd高清在线观看-精品欧美高清不卡在线-精品欧美日韩一区二区

17站長網(wǎng)

17站長網(wǎng) 首頁 網(wǎng)站 服務(wù)器 查看內(nèi)容

詳解Linux中stat函數(shù)和stat命令的使用

2022-10-24 14:47| 查看: 2238 |來源: 互聯(lián)網(wǎng)

stat函數(shù)和stat命令 linux文件里的【inode = index node】解釋:要理解inode必須了解磁盤和【目錄項】,inode實際是連接【目錄項】和磁盤的中間物質(zhì)。 圖里的 ...

stat函數(shù)和stat命令

linux文件里的【inode = index node】解釋:要理解inode必須了解磁盤和【目錄項】,inode實際是連接【目錄項】和磁盤的中間物質(zhì)。

圖里的大圈代表硬件的磁盤,里面的小圈代表某個文件存儲在磁盤上了。

【inode = index node】的node(承載node信息的結(jié)構(gòu)體是:stat,stat的定義在后面 )里面有:

  1. 文件大小

  2. 文件的最后修改時間

  3. 文件的所屬用戶

  4. 文件的權(quán)限

  5. 硬鏈接計數(shù)(ls -l 顯示出來的數(shù)字)

  6. 塊位置:指定文件存儲在磁盤的具體位置。

下圖中的hello是個普通文件,hello.hard是hello的硬鏈接

文件夾里放的就是每個文件的【目錄項】如下圖,【目錄項】里有:

  1. 文件名

  2. 該目錄項的大小

  3. 文件的類型

  4. inode

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

ls -li 文件名

執(zhí)行結(jié)果:

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

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

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

1,stat函數(shù):取得指定文件的文件屬性,文件屬性存儲在結(jié)構(gòu)體stat里。

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

struct stat 結(jié)構(gòu)體:

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:設(shè)備ID,不太常用

st_ino:【inode】,【inode】是啥?不知道就看上面關(guān)于【inode】的解釋
st_mode:文件的類型和權(quán)限,共16位,如下圖。

  • 0-11位控制文件的權(quán)限

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

0-2比特位:其他用戶權(quán)限
3-5比特位:組用戶權(quán)限
6-8比特位:本用戶權(quán)限
9-11比特位:特殊權(quán)限
12-15比特位:文件類型(因為文件類型只有7中,所以用12-14位就夠了

文件類型的宏如下(下面的數(shù)字是8進(jìn)制):

  • S_IFSOCK 0140000 socket

  • S_IFLNK 0120000 symbolic link(軟連接)

  • S_IFREG 0100000 regular file(普通文件)

  • S_IFBLK 0060000 block device(塊設(shè)備文件)

  • S_IFDIR 0040000 directory(目錄)

  • S_IFCHR 0020000 character device(字符設(shè)備文件)

  • S_IFIFO 0010000 FIFO(管道)

判斷文件類型的函數(shù),返回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.)

文件權(quán)限的宏如下:

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:硬連接計數(shù)

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

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

  • st_rdev:特殊設(shè)備的ID,不太常用

  • st_size:文件的大小

  • st_blksize:不明是干啥的

  • st_blocks:不明是干啥的

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

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

  • struct timespec st_ctim:最后狀態(tài)改變的時間

struct timespec {
__kernel_time_ttv_sec; /* seconds */當(dāng)前時間到1970.1.1 00:00:00的秒數(shù)
longtv_nsec;/* nanoseconds *//納秒數(shù)(不知道從哪到哪的)
};
1s 秒  = 1000ms 毫秒
1ms 毫秒 = 1000us 微秒
1us 微秒 = 1000ns 納秒

pathname:文件名

返回值:0代表成功;-1代表失敗,并設(shè)置error

例子:statbuf是結(jié)構(gòu)體stat,可以看出來st_mode是個10進(jìn)制的數(shù)字。

st_mode

用gdb顯示st_mode,發(fā)現(xiàn)返回的st_mode是個10進(jìn)制的數(shù)字,用gdb的【p/o】(o代表用8進(jìn)制表示)命令把10進(jìn)制的33204轉(zhuǎn)換成了8進(jìn)制的【0100664】,第一個0代筆是8進(jìn)制,后三位的【100】代表文件類型,從上面的說明可以看出來【100】代表普通文件,最后三位的【664】代表這個文件的權(quán)限(本用戶:rw-,組用戶:rw-,其他用戶:r--)。所以從st_mode里就可以得知文件的類型和權(quán)限設(shè)置(只使用了16個比特位,真的好節(jié)省空間,牛逼!)

st_uid

st_gid

發(fā)現(xiàn)st_uid和st_gid是1000,但這個1000怎么和用戶對應(yīng)上呢,查看/etc/passwd文件,發(fā)現(xiàn)用于ys的uid和gid都是1000,所以就對應(yīng)上了。

stat命令,是stat函數(shù)對應(yīng),執(zhí)行結(jié)果如下:

ys@ys-VirtualBox:~/lianxi1$ stat hello
 File: hello
 Size: 11  Blocks: 8   IO Block: 4096 regular file
Device: 801h/2049dInode: 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函數(shù):返回/etc/passwd文件里指定uid的行,把這一行的信息放入結(jié)構(gòu)體passwd中。雖然返回值是指針,但不需要調(diào)用free函數(shù)。

#include
#include
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函數(shù):返回/etc/group文件里指定gid的行,把這一行的信息放入結(jié)構(gòu)體group中。雖然返回值是指針,但不需要調(diào)用free函數(shù)。

#include
#include
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函數(shù):傳入從stat函數(shù)里得到的st_mtim.tv_sec(當(dāng)前時間到1970.1.1 00:00:00的秒數(shù)),得到結(jié)構(gòu)體tm。雖然返回值是指針,但不需要調(diào)用free函數(shù)。

#include
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函數(shù):stat碰到軟鏈接,會追述到源文件,穿透;lstat并不會穿透。

例子:模仿ls -l 文件

#include
#include
#include
#include
#include
#include
//getpwuid
#include
#include
//localtime
#include
//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';

 //本用戶的文件權(quán)限
 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';
 
 //本用戶的組的文件權(quán)限
 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';
 
 //其他用戶的文件權(quán)限
 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函數(shù):判斷調(diào)用程序的用戶對于指定文件的權(quán)限(可讀?可寫?可執(zhí)行?)

#includeint access(const char *pathname, int mode);

例子:

#include
#include
//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文件的權(quán)限,結(jié)果如下

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用戶執(zhí)行例子程序,查看/usr/include/time.h文件,結(jié)果如下。因為time.h是屬于root用戶的,對于其他用戶來說是[r--],所以得出下面的結(jié)果。

ys@ys-VirtualBox:~/lianxi$ ./ac /usr/include/time.h
read ok
exists

還是用ys用戶執(zhí)行,但是加上sudo,結(jié)果如下。發(fā)現(xiàn)結(jié)果和root用戶相同。因為加了sudo,就編程了root用戶。

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

7,truncate函數(shù):截斷文件和擴(kuò)展文件的大小

#include
#include
int truncate(const char *path, off_t length);

path:文件

length:
length大于原來文件的大小,則擴(kuò)展文件的大小至length
length小于原來文件的大小,則截斷文件的大小至length

8,link函數(shù):創(chuàng)建硬鏈接

#include
int link(const char *oldpath, const char *newpath);

返回值:成功返回0,失敗返回-1,并設(shè)置errno。

9,symlink函數(shù):創(chuàng)建軟鏈接

#include
int symlink(const char *target, const char *linkpath);

返回值:成功返回0,失敗返回-1,并設(shè)置errno。

10,readlink函數(shù):找到軟鏈接對應(yīng)的實際文件,把文件的名字放入buf里。注意:硬鏈接不行。

#include
ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);

返回值:成功返回寫入buf的字節(jié)數(shù),失敗返回-1,并設(shè)置errno。

11,unlink函數(shù):刪除軟硬鏈接,也可以刪除文件。

#include
int unlink(const char *pathname);

返回值:成功返回0,失敗返回-1,并設(shè)置errno。

有個特殊用法:下面的open代碼想要創(chuàng)建hello文件,然后直接用unlink刪除,但是能寫入成功,ret是大于0的,程序執(zhí)行完,發(fā)現(xiàn)沒有做成hello文件。

結(jié)論:當(dāng)執(zhí)行unlink后,計數(shù)為0后,但,發(fā)現(xiàn)別的進(jìn)程還引用這個文件,這個時間點,unlink不會刪除這個文件,等這個進(jìn)程結(jié)束后,再刪除,所以下面的write代碼能夠?qū)懭氤晒Α?br/>利用這個特點可以實現(xiàn):在線觀看視頻時,實際是把視頻文件下載到了本地(然后代碼里,使用unlink),看完后視頻文件的計數(shù)為0,就自動刪除了,不怕視頻被泄露出去。

#include
#include
#include
#include
#include
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函數(shù):改變文件的所屬用戶和組

#include
int chown(const char *pathname, uid_t owner, gid_t group);

pathname:文件

owner:用戶ID(數(shù)字的)/etc/passwd

group:組ID(數(shù)字的)/etc/group

返回值:0成功,-1失敗。

13,rename函數(shù):重命名

#include
int rename(const char *oldpath, const char *newpath);

oldpath :原來的文件名后者目錄

newpath:新的文件名后者目錄

返回值:0成功,-1失敗。

14,getcwd函數(shù):獲得當(dāng)前工作的目錄

#includechar *getcwd(char *buf, size_t size);

buf:當(dāng)前工作的目錄

size:緩沖區(qū)大小

返回值:成功返回當(dāng)前工作的目錄 失敗返回NULL

15,chdir函數(shù):改變進(jìn)程的工作目錄

#includeint chdir(const char *path);

path:目標(biāo)工作目錄

返回值:0成功,-1失敗

16,mkdir函數(shù):創(chuàng)建目錄

#include#includeint mkdir(const char *pathname, mode_t mode);

pathname:目標(biāo)工作目錄mode:mode & ~umask & 0777 。注意,如果沒有x權(quán)限,則無法cd進(jìn)入這個目錄。返回值:0成功,-1失敗

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

#includeint rmdir(const char *pathname);

18,opendir函數(shù):打開目錄

#include#includeDIR *opendir(const char *name);

name:目錄名

返回值:a pointer to the directory stream

19,readdir函數(shù):讀目錄

#include#includeDIR *opendir(const char *name);

dirp:opendir函數(shù)的返回值

返回值:結(jié)構(gòu)體dirent,可以理解成最上面說的【目錄項】NULL代表讀到末尾或者有錯誤  NULL以外代表目錄項的內(nèi)容

20,closedir函數(shù):關(guān)閉目錄

#include#includeint closedir(DIR *dirp);

dirp:opendir函數(shù)的返回值

21,strerron函數(shù):打印出errno對應(yīng)的文字信息。

#includechar *strerror(int errnum);

errnum的宏放在文件:/usr/include/asm-generic/errno.h

例子:

#include
#include
#include
//EDEADLK
int main(){
 char* buf = strerror(EDEADLK);
 printf("%s\n", buf);//Resource deadlock avoided
}

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

#includeint dup(int oldfd); int dup2(int oldfd, int newfd);

dup:和open類似,先打開一個新的文件描述符,讓新的文件描述符也指向:oldfd指向的地方。成功返回新打開的文件描述符;失敗返回-1.

dup2:先消除newfd的指向再讓newfd指向oldfd指向的地方成功返回newfd;失敗返回-1.

例子:調(diào)用printf2次,第一次printf把內(nèi)容寫到文件;第二次printf把內(nèi)容打印到屏幕。

#include
#include
#include
#include
#include
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);
}
本文最后更新于 2022-10-24 14:47,某些文章具有時效性,若有錯誤或已失效,請在網(wǎng)站留言或聯(lián)系站長:17tui@17tui.com
·END·
站長網(wǎng)微信號:w17tui,關(guān)注站長、創(chuàng)業(yè)、關(guān)注互聯(lián)網(wǎng)人 - 互聯(lián)網(wǎng)創(chuàng)業(yè)者營銷服務(wù)中心

免責(zé)聲明:本站部分文章和圖片均來自用戶投稿和網(wǎng)絡(luò)收集,旨在傳播知識,文章和圖片版權(quán)歸原作者及原出處所有,僅供學(xué)習(xí)與參考,請勿用于商業(yè)用途,如果損害了您的權(quán)利,請聯(lián)系我們及時修正或刪除。謝謝!

17站長網(wǎng)微信二維碼

始終以前瞻性的眼光聚焦站長、創(chuàng)業(yè)、互聯(lián)網(wǎng)等領(lǐng)域,為您提供最新最全的互聯(lián)網(wǎng)資訊,幫助站長轉(zhuǎn)型升級,為互聯(lián)網(wǎng)創(chuàng)業(yè)者提供更加優(yōu)質(zhì)的創(chuàng)業(yè)信息和品牌營銷服務(wù),與站長一起進(jìn)步!讓互聯(lián)網(wǎng)創(chuàng)業(yè)者不再孤獨!

掃一掃,關(guān)注站長網(wǎng)微信

大家都在看

熱門排行

    最近更新

      返回頂部
      主站蜘蛛池模板: 水果污视频 | 欧美最新一区二区三区四区 | 免费特黄一级欧美大片在线看 | 人妖另类亚洲xxxx | 久久久免费观看视频 | 亚洲国产成人久久77 | 激情视频一区 | 国产福利视频奶水在线 | 香蕉钻洞视频 | 尤物视频网站在线 | 非洲一级毛片又粗又长aaaa | 国产福利在线观看永久视频 | 欧美日韩国产在线人 | 涩色网| 青青国产成人久久91 | 黄色片在线免费观看 | 国产亚洲精品一区久久 | 欧美日韩成人午夜免费 | 亚洲一区欧美一区 | 一级毛片美国aaj毛片 | 激情五月综合婷婷 | 国产在线精品成人一区二区三区 | 91高清国产经典在线观看 | 性欧美一级毛片在线播放 | 久久大尺度 | 1000部国产拍拍拍拍在线观看 | 成人三级做爰在线观看男女 | 亚洲毛片 | 一级毛片特级毛片黄毛片 | 久久国产三级 | 欧美日韩国产在线观看 | 欧美色欧美亚洲高清在线视频 | 久久国内免费视频 | 亚洲一级影院 | 最近中文日本字幕免费完整 | 国产精品每日在线观看男人的天堂 | 亚洲欧美日韩高清一区二区一 | 美国特黄一级片 | 久久久久国产成人精品亚洲午夜 | 欧美精品一区二区三区视频 | 色婷婷国产 |