嵌入式 Linux系统编程(四)——文件属性

一、文件属性概述

Linux 文件的属性主要包括:文件的节点、种类、权限模式、链接数量、所归属的用户和用户组、最近访问或修改的时间等内容。文件属性示例如下:

多个文件属性查看:

ls -lih

1341714 -rw-r--r-- 1 root root 2.5K May 28 10:24 bit_marco.c

1341718 -rw-r--r-- 1 root root 2.1K May 28 09:08 bit_marco.c~

1341706 -rw-r--r-- 1 root root 2.6K May 28 08:54 bit_operaion.c

1335906 -rwxr-xr-x 1 root root 7.2K May 30 04:06 file

1341722 -rw-r--r-- 1 root root 2.7K May 30 04:06 file.c

1321584 -rw-r--r-- 1 root root 2.7K May 30 04:04 file.c~

1341708 -rwxr-xr-x 1 root root 6.4K May 28 09:08 main

1335846 -rw-r--r-- 1 root root 2.6K May 28 08:55 main.c

第一字段:inode

第二字段:文件类型和权限

第三字段:硬链接数

第四字段:所属用户

第五字段:所属用户组

第六字段:文件大小

第七字段:最后访问或修改时间

第八字段:文件名

单个文件属性详细查看:

root#stat file

File: `file'

Size: 7308            Blocks: 16         IO Block: 4096   regular file

Device: fd00h/64768d    Inode: 1335906     Links: 1

Access: (0755/-rwxr-xr-x)  Uid: (0/root)   Gid: (0/root)

Access: 2016-05-30 04:06:10.151098056 -0400

Modify: 2016-05-30 04:06:07.227089617 -0400

Change: 2016-05-30 04:06:07.227089617 -0400

二、文件属性函数

#include

#include

#include

int stat(const char *path, struct stat *buf);

int fstat(int fd, struct stat *buf);

int lstat(const char *path, struct stat *buf);

成功返回0,错误返回-1,并设置errno全局变量

文件属性数据结构:

struct stat {

dev_t     st_dev;     /* ID of device containing file */

ino_t     st_ino;     /* inode number */

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 ID (if special file) */

off_t     st_size;    /* total size, in bytes */

blksize_t st_blksize; /* blocksize for file system I/O */

blkcnt_t  st_blocks;  /* number of 512B 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 */

};

1、stat

int stat(const char *path, struct stat *buf);

path:文件的路径,buf:指向存储文件信息的数据结构的指针

2、fstat

int fstat(int fd, struct stat *buf);

fd:文件描述符,buf:指向存储文件信息的数据结构的指针

3、lstat

int lstat(const char *path, struct stat *buf);

path:文件的路径,buf:指向存储文件信息的数据结构的指针

用于查阅链接文件的文件属性

三、文件权限信息提取

文件的文件类型和文件权限存在于文件属性数据结构中的st_mode成员。为了使用st_mode检查文件类型,POSIX标准定义了如下的宏:

S_ISREG(m)  is it a regular file?

S_ISDIR(m)  directory?

S_ISCHR(m)  character device?

S_ISBLK(m)  block device?

S_ISFIFO(m) FIFO (named pipe)?

S_ISLNK(m)  symbolic link? (Not in POSIX.1-1996.)

S_ISSOCK(m) socket? (Not in POSIX.1-1996.)

S_IFMT     0170000   bit mask for the file type bit fields

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-group-ID 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 //掩码

S_IROTH    00004     others have read permission

S_IWOTH    00002     others have write permission

S_IXOTH    00001     others have execute permission

文件类型的获取:

int get_stat_type(const struct stat *st, char *buf)

{

if(S_ISREG(st->st_mode))

{

sprintf(buf, "regular file");

}

if(S_ISDIR(st->st_mode))

{

sprintf(buf, "directory");

}

if(S_ISCHR(st->st_mode))

{

sprintf(buf, "charactor device");

}

if(S_ISBLK(st->st_mode))

{

sprintf(buf, "block device");

}

if(S_ISFIFO(st->st_mode))

{

sprintf(buf, "fifo file");

}

if(S_ISLNK(st->st_mode))

{

sprintf(buf, "symbloc link");

}

if(S_ISSOCK(st->st_mode))

{

sprintf(buf, "socket");

}

return 0;

}

四、程序实例#include

#include

#include

#include

#include

#include

#include

int print_stat(struct stat *st);

int main(int argc, char **argv)

{

if(argc

{

fprintf(stderr, "argc is less than 2\n");

fprintf(stdout, "please enter: appname filename");

return -1;

}

struct stat s;

bzero(&s, sizeof(struct stat));

stat(argv[1], &s);

print_stat(&s);

return 0;

}

int print_stat(struct stat *st)

{

if(NULL == st)

{

fprintf(stderr, "struct stat *st is NULL\n");

exit(-1);

}

printf("The device no is: %d\n", st->st_dev);

printf("The file's node number is: %d\n", st->st_ino);

printf("The file's access mode is: %d\n", st->st_mode);

printf("The file's hard link number is: %d\n", st->st_nlink);

printf("The file's user id is: %d\n", st->st_uid);

printf("The file's group id is: %d\n", st->st_gid);

printf("The file's size is: %d\n", st->st_size);

printf("The block size is: %d\n", st->st_blksize);

printf("The number of allocated blocks is: %d\n", st->st_blocks);

struct tm* pAccess_time=localtime(&(st->st_atime));

struct tm* pModify_time=localtime(&(st->st_mtime));

struct tm* pChange_time=localtime(&(st->st_ctime));

char aBuffer[64] = {0};

char mBuffer[64] = {0};

char cBuffer[64] = {0};

strftime(aBuffer, 64, "The last access time is: %Y-%m-%d %H:%M:%S \n", pAccess_time);

strftime(mBuffer, 64, "The last modify time is: %Y-%m-%d %H:%M:%S \n", pModify_time);

strftime(cBuffer, 64, "The last change time is: %Y-%m-%d %H:%M:%S \n", pChange_time);

printf(aBuffer);

printf(mBuffer);

printf(cBuffer);

return 0;

}

嵌入式Linux文件提取,嵌入式 Linux系统编程(四)——文件属性相关推荐

  1. delphi 监控文件变化_监控Linux文件变化,防止系统被黑

    运维服务器比较头疼的一个问题是系统被黑,沦为肉鸡或者矿机.除了加强安全基线配置,加强网络和端口加固,系统和应用bug修复,上IDS/IPS(入侵检测/防御系统)之外,另一个方面就是系统监控,一个完善准 ...

  2. 解压出来的文件md5会改变吗_监控Linux文件变化,防止系统被黑

    运维服务器比较头疼的一个问题是系统被黑,沦为肉鸡或者矿机.除了加强安全基线配置,加强网络和端口加固,系统和应用bug修复,上IDS/IPS(入侵检测/防御系统)之外,另一个方面就是系统监控,一个完善准 ...

  3. 文件上传linux服务器,Linux 文件上传Linux服务器

    进入命令行 在图形化桌面出现之前,与Unix系统进行交互的唯一方式就是借助由shell所提供的文本命令行界面(command line interface,CLI).CLI只能接受文本输入,也只能显示 ...

  4. linux文件权限754,Linux文件权限

    Linux文件权限 在Linux中,对系统服务的文件通常只有root才能读写或执行. 说明:Linux记录用户身份的是UID(用户ID)和GID(群组ID),root的两个ID号均为0,一般用户的两个 ...

  5. Linux shell、内核及系统编程精品资料下载汇总 topsage

    shell编程.sed.awk.grep相关: UNIX shell by Example 第四版 UNIX shell范例精解 (第4版) 中文高清PDF下载 Shell脚本学习指南(Classic ...

  6. linux文件操作管理,linux 文件管理操作入门

    mkdir -p /root/kali/bp/shell  一路创建文件夹直到生成文件夹shell,中间没有kali文件夹的话也会自动创建生成 tar解压缩 范例一:将整个 /etc 目录下的文件全部 ...

  7. linux文件命令介绍,linux文件搜索及其它基础命令介绍(3)

    1.linux中包含大量的文件,对于文件查找,linux提供了find命令. find是一个非常有效的工具,它可以遍历目标目录甚至整个文件系统来查找某些文件或目录: find [path...] [e ...

  8. linux 文件类型 管理,Linux的文件类型及用户管理

    Linux的文件类型 -,f:普通文件 d:目录文件,路径映射 l:链接文件(符号链接)软连接 设备文件: c:字符设备,一次存取一个字符 b:块设备 p:管道文件 fi,fo s:套接字文件,soc ...

  9. linux文件系统启动流程,linux 内核启动过程以及挂载android 根文件系统的过程

    转载 作者:汕头大学-黄珠唐 时间:2009 年10 月29 日 主要介绍linux 内核启动过程以及挂载android 根文件系统的过程,以及介绍android 源代码中文件系统部分的浅析. 主要源 ...

最新文章

  1. Bioinformatics: Assembling Genomes (week 1-2)
  2. 最新调查:等这一波COBOL程序员退休,很多关键岗位就后继无人了
  3. Layout两列定宽中间自适应三列布局
  4. 使用 matlab 产生GK101任意波数据文件的方法
  5. [云炬ThinkPython阅读笔记]1.3 第一个程序
  6. java求s a aa aaa_Java求s=a+aa+aaa+aaaa+aa...a的值
  7. 宁波医院计算机试题及答案,(宁波市第25届小学生计算机程序设计竞赛试题及答案.doc...
  8. oracle如何把字符集改回默认,更改oracle字符集
  9. 一针一线皆关“云” 报喜鸟以匠心融合科技
  10. python接口自动化(三十三)-python自动发邮件总结及实例说明番外篇——下
  11. vm 虚拟机 删除 权限_虚拟机win7一键傻瓜式安装
  12. 五分钟快速过完Verilog HDL基本概念(1)
  13. 使用Windows service创建一个简单的定时器
  14. HashMap排序(java)
  15. 什么是数据恢复工具,一款一键恢复软件
  16. 第二章 常用半导体器件原理
  17. web漏扫问题处理1
  18. dell 服务器 重装Linux系统
  19. java表格标题栏_java使用poi自定义excel标题头并导出(springmvc+poi)
  20. IMUGPS融合定位::IMU姿态解算

热门文章

  1. 关于有源晶振倍频干扰的问题
  2. 2013.10u-boot移植之增加nand保存环境变量
  3. 11.3 free:查看系统内存信息
  4. 微信小程序支付java视频_【原创】微信小程序支付(普通模式,公众号支付同适用)java后台案例...
  5. 求职特训营火热来袭,阿里大咖教你制作专业简历
  6. 【下载!】实时计算正当时!助你轻松get Apache Flink 动向!
  7. 封神-性能容量分析报告
  8. 云原生中间件与开源自建TCO对比
  9. linux查看java运行日志,Linux下查看日志用到的经常使用命令
  10. XMLHttpRequest对象在IE和Firefox中创建方式有没有不同?