admin 管理员组

文章数量: 887021

文件属性

文件的属性

文件属性

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 file,得到
  File: "a.out"Size: 11113           Blocks: 24         IO Block: 4096   普通文件
Device: 803h/2051d      Inode: 1312078     Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (  504/  zijing)   Gid: (    0/    root)
Access: 2019-01-08 18:41:56.783423353 +0800
Modify: 2018-04-26 20:43:50.037682582 +0800
Change: 2018-04-26 20:43:50.037682582 +0800
  • IO Block = 4096
  • 一个扇区的大小为512Byte, fdisk -l查看,units中能看到
  • 一个文件至少占用4096 / 512 = 8个Block
  • Blocks = 11113 / 512 = 21.7 < 24 = 3 * 8
  • fdisk -l例子
WARNING: GPT (GUID Partition Table) detected on '/dev/sda'! The util fdisk doesn't support GPT. Use GNU Parted.Disk /dev/sda: 27591.7 GB, 27591675215872 bytes
255 heads, 63 sectors/track, 3354496 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1      267350  2147483647+  ee  GPT

代码

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) { //get staterr_ret("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 = "character special";else if (S_ISBLK(buf.st_mode))ptr = "block special";else if (S_ISFIFO(buf.st_mode))ptr = "fifo";else if (S_ISLNK(buf.st_mode))ptr = "symbolic link";else if (S_ISSOCK(buf.st_mode))ptr = "socket";else if (S_ISCHR(buf.st_mode))ptr = "directory";elseptr = "**unkown mode**";printf("%s\n", ptr);}exit(0);
}

本文标签: 文件属性