st_mode 内部剖析

从博客园看到的文章:

先看一下struct stat的结构。

struct stat {
dev_t         st_dev;      /* device */
ino_t         st_ino;      /* inode */
mode_t        st_mode;     /* protection */
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 type (if inode device) */
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 */
time_t        st_mtime;    /* time of last modification */
time_t        st_ctime;    /* time of last status change */
};


其中,st_mode的类型 mode_t.


mode_t其实就是普通的unsigned int.


目前,st_mode使用了其低19bit. 0170000 => 1+ 3*5 = 16.
其中,最低的9位(0-8)是权限,9-11是id,12-15是类型。
具体定义如下:
S_IFMT     0170000   bitmask for the file type bitfields
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
S_ISUID    0004000   set UID bit
S_ISGID    0002000   set GID bit (see below)
S_ISVTX    0001000   sticky bit (see below)
S_IRWXU    00700     mask for file owner permissions
S_IRUSR    00400     owner has read permission
S_IWUSR    00200     owner has write permission
S_IXUSR    00100     owner has execute permission
S_IRWXG    00070     mask for group permissions
S_IRGRP    00040     group has read permission
S_IWGRP    00020     group has write permission
S_IXGRP    00010     group has execute permission
S_IRWXO    00007     mask for permissions for others (not in group)
S_IROTH    00004     others have read permission
S_IWOTH    00002     others have write permisson
S_IXOTH    00001     others have execute permission


当我们需要快速获得文件类型或访问权限时,最好的方法就是使用glibc定义的宏。
如:S_ISDIR,S_IRWXU等。


例:
如果我们需要知道一个文件类型
struct stat tmpStat;
memset(&tmpStat, 0, sizeof(struct stat));
stat("/tmp", &tmpStat);
cout.setf(ios::oct, ios::basfield);
cout << (tmpStat.st_mode & S_IFMT) << endl;
输出:40000
根据之前的定义,我们知道40000表示目录;


同理,如果我们需要知道一个文件权限
只需
cout << (tmpStat.st_mode & ALLPERMS) << endl;
输出:1777
为什么会多出前面的1呢?我暂时认为可能还9-11位的id field有关。
如果别的目录,显示是正常的。如755.
#0 0xb658f9ca in __malloc_consolidate () from /home/wuyilun/Desktop/CAMEOS/platform/staging_dir/target-arm-sigmastar-linux-uclibcgnueabihf/root-model_sstar_ssr920/lib/libc.so.0 No symbol table info available. #1 0xb658fe96 in malloc () from /home/wuyilun/Desktop/CAMEOS/platform/staging_dir/target-arm-sigmastar-linux-uclibcgnueabihf/root-model_sstar_ssr920/lib/libc.so.0 No symbol table info available. #2 0xb6582dee in _stdio_fopen () from /home/wuyilun/Desktop/CAMEOS/platform/staging_dir/target-arm-sigmastar-linux-uclibcgnueabihf/root-model_sstar_ssr920/lib/libc.so.0 No symbol table info available. #3 0x00099702 in drop_caches () at storage_common.c:1831 fp = <optimized out> fp = <optimized out> #4 storage_get_dir_usage (pSd=pSd@entry=0xc87060, path=path@entry=0xb414eb30 "/mnt/sdcard//encrypt", depth=depth@entry=1) at storage_common.c:1070 total_size = 8192 st = {st_dev = 26, __pad1 = 0, st_ino = 258, st_mode = 32769, st_nlink = 1, st_uid = 0, st_gid = 0, st_rdev = 0, __pad2 = 0, st_size = 0, st_blksize = 4096, st_blocks = 16, st_atim = {tv_sec = 1762064248, tv_nsec = 400239000}, st_mtim = {tv_sec = 1761736386, tv_nsec = 872000002}, st_ctim = {tv_sec = 1761736386, tv_nsec = 872000002}, __unused4 = 0, __unused5 = 0} dir = <optimized out> entry = <optimized out> sub_path = '\000' <repeats 255 times> next_depth = 2 ret = 0 __FUNCTION__ = "storage_get_dir_usage" final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> #5 0x000997c0 in storage_get_dir_usage (pSd=pSd@entry=0xc87060, path=path@entry=0x15c708 "/mnt/sdcard/", depth=depth@entry=0) at storage_common.c:1057 total_size = 2473984 st = {st_dev = 26, __pad1 = 0, st_ino = 256, st_mode = 16877, st_nlink = 1, st_uid = 0, st_gid = 0, st_rdev = 0, __pad2 = 0, st_size = 184, st_blksize = 4096, st_blocks = 32, st_atim = {tv_sec = 1762080705, tv_nsec = 642949013}, st_mtim = {tv_sec = 1762080727, tv_nsec = 970949016}, st_ctim = {tv_sec = 1762080727, tv_nsec = 970949016}, __unused4 = 0, __unused5 = 0} dir = 0xc865b8 entry = <optimized out> sub_path = "/mnt/sdcard//encrypt", '\000' <repeats 235 times> next_depth = 1 ret = <optimized out> __FUNCTION__ = "storage_get_dir_usage" final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> #6 0x00099d7c in storage_sdcard_extra_cmd_child_cb (handler=<optimized out>, buf=0xc85de4, event=8) at storage_common.c:1203 data = <optimized out> cmd = 0 used_bytes = '\000' <repeats 19 times> final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> sock = <optimized out> nbuf = <optimized out> pSd = <optimized out> pCtx = <optimized out> dev_info = 0xcf69c0 ret = <optimized out> __FUNCTION__ = "storage_sdcard_extra_cmd_child_cb" final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> final_file = <optimized out> #7 0xb6bb69b4 in tpsocket_event (sock=sock@entry=0xc85c98, event=event@entry=8) at tpsocket_fd.c:112 ret = true empty_write = 1 __func__ = "tpsocket_event" --Type <RET> for more, q to quit, c to continue without paging--
最新发布
11-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值