功能:获取文件元数据

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);

stat结构体

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 filesystem 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 */
};
//示例   - err_exit函数如前
// 其实可以通过Linux 系统调用major, minor来替换(如下)
#define MAJOR(a) (int)((unsigned short)a >> 8)  //主设备号: 获取高8位
#define MINOR(a) (int)((unsigned short)a & 0xFF)//次设备号: 获取低8位
bool fileType(const struct stat &fileStat);
void filePerm(const struct stat &fileStat, char *perm);

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cerr << "Usage: " << argv[0] << " <file-name>" << endl;
        exit(EXIT_FAILURE);
    }

    struct stat fileStat;
    if (lstat(argv[1], &fileStat) == -1)
        err_exit("stat error");

    cout << "file-name: " << argv[1] << endl;
    cout << "st_ino = " << fileStat.st_ino << endl;
    cout << "device major: " << major(fileStat.st_dev)
         << ", minor: " << minor(fileStat.st_dev) << endl;
    if (fileType(fileStat))
    {
        cout << "----------------------------" << endl;
        cout << "major: " << MAJOR(fileStat.st_dev)
             << ", minor: " << MINOR(fileStat.st_rdev) << endl;
//        cout << "major: " << major(fileStat.st_dev)
//             << ", minor: " << minor(fileStat.st_rdev) << endl;
    }
    //获取文件的权限: 但要注意需要&上07777
    fprintf(stdout, "file permission: %o", fileStat.st_mode&07777);
    char perm[11];
    filePerm(fileStat, perm);
    cout << ", msg: " << perm << endl;

    cout << "st_nlink = " << fileStat.st_nlink << endl;
    cout << "st_uid = " << fileStat.st_uid << endl;
    cout << "st_gid = " << fileStat.st_gid << endl;
    cout << "st_size = " << fileStat.st_size << endl;
    cout << "st_blksize = " << fileStat.st_blksize << endl;
    cout << "st_blocks = " << fileStat.st_blocks << endl;
    cout << "st_atime = " << fileStat.st_atime << endl;
    cout << "st_ctime = " << fileStat.st_ctime << endl;
    cout << "st_mtime = " << fileStat.st_mtime << endl;
}

bool fileType(const struct stat &fileStat)
{
    cout << "file-type: ";
    switch(fileStat.st_mode & S_IFMT)
    {
    case S_IFSOCK:
        cout << "socket";
        break;
    case S_IFLNK:
        cout << "symbolic link";
        break;
    case S_IFREG:
        cout << "regular file";
        break;
    case S_IFBLK:
        cout << "block device" << endl;
        return true;
        break;
    case S_IFDIR:
        cout << "directory";
        break;
    case S_IFCHR:
        cout << "character device" << endl;
        return true;
        break;
    case S_IFIFO:
        cout << "FIFO" << endl;
        break;
    default:
        cout << "unknown?";
        break;
    }
    cout << endl;
    return false;
}

void filePerm(const struct stat &fileStat, char *perm)
{
    strcpy(perm, "----------");
    switch(fileStat.st_mode & S_IFMT)
    {
    case S_IFSOCK:
        perm[0] = 's';
        break;
    case S_IFLNK:
        perm[0] = 'l';
        break;
    case S_IFREG:
        perm[0] = '-';
        break;
    case S_IFBLK:
        perm[0] = 'b';
        break;
    case S_IFDIR:
        perm[0] = 'd';
        break;
    case S_IFCHR:
        perm[0] = 'c';
        break;
    case S_IFIFO:
        perm[0] = 'p';
        break;
    default:
        perm[0] = '?';
        break;
    }

    if (fileStat.st_mode & S_IRUSR)
        perm[1] = 'r';
    if (fileStat.st_mode & S_IWUSR)
        perm[2] = 'w';
    if (fileStat.st_mode & S_IXUSR)
        perm[3] = 'x';
    if (fileStat.st_mode & S_IRGRP)
        perm[4] = 'r';
    if (fileStat.st_mode & S_IWGRP)
        perm[5] = 'w';
    if (fileStat.st_mode & S_IXGRP)
        perm[6] = 'x';
    if (fileStat.st_mode & S_IROTH)
        perm[7] = 'r';
    if (fileStat.st_mode & S_IWOTH)
        perm[8] = 'w';
    if (fileStat.st_mode & S_IXOTH)
        perm[9] = 'x';
}


[拓展]

1.getpwuid 

struct passwd *getpwuid(uid_t uid);
//passwd结构体
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 */
};

2.getgrgid

struct group *getgrgid(gid_t gid);
//group结构体
struct group
{
    char   *gr_name;       /* group name */
    char   *gr_passwd;     /* group password */
    gid_t   gr_gid;        /* group ID */
    char  **gr_mem;        /* group members */
};

3. readlink

ssize_t readlink(const char *path, char *buf, size_t bufsiz);

4. localtime

struct tm *localtime(const time_t *timep);
//tm结构体
struct tm
{
    int tm_sec;         /* seconds */
    int tm_min;         /* minutes */
    int tm_hour;        /* hours */
    int tm_mday;        /* day of the month */
    int tm_mon;         /* month */
    int tm_year;        /* year */
    int tm_wday;        /* day of the week */
    int tm_yday;        /* day in the year */
    int tm_isdst;       /* daylight saving time */
};

//示例: 实现简单的ls -l功能
#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <grp.h>
#include <pwd.h>
#include <time.h>

using namespace std;

inline void err_exit(std::string message);
bool isDevice(const struct stat &fileStat);
bool isLink(const struct stat &fileStat);
void filePerm(const struct stat &fileStat, char *perm);

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cerr << "Usage: " << argv[0] << " <file-name>" << endl;
        exit(EXIT_FAILURE);
    }

    struct stat fileStat;
    if (lstat(argv[1], &fileStat) == -1)
        err_exit("lstat error");

    //获取权限
    char perm[11];
    filePerm(fileStat, perm);
    cout << perm << ' ';

    //获取文件链接数
    cout << fileStat.st_nlink << ' ';

    //获取文件所有者
    struct passwd *ps = getpwuid(fileStat.st_uid);
    cout << ps->pw_name << ' ';

    //获取文件所属组
    struct group *gp = getgrgid(fileStat.st_gid);
    cout << gp->gr_name << ' ';

    if (isDevice(fileStat))
        cout << major(fileStat.st_dev) << ", "
             << minor(fileStat.st_rdev) << ' ';
    else
        cout << fileStat.st_size << ' ';

    // 打印最后一次修改时间
    time_t mtime = fileStat.st_mtime;
    struct tm *pTime = localtime(&mtime);
    cout << pTime->tm_mon+1 << "月 " << pTime->tm_mday << ' '
         << pTime->tm_hour << ':' << pTime->tm_min << ' ';

    // 打印文件名
    cout << argv[1];
    if (isLink(fileStat))
    {
        cout << " -> ";
        char name[1024] = {0};
        if (readlink(argv[1], name, sizeof(name)) == -1)
            err_exit("readlink error");
        cout << name;
    }
    cout << endl;

}

inline void err_exit(std::string message)
{
    perror(message.c_str());
    exit(EXIT_FAILURE);
}
bool isDevice(const struct stat &fileStat)
{
    switch(fileStat.st_mode & S_IFMT)
    {
    case S_IFBLK:
    case S_IFCHR:
        return true;
        break;
    default:
        return false;
        break;
    }
    return false;
}
bool isLink(const struct stat &fileStat)
{
    if ((fileStat.st_mode & S_IFMT) == S_IFLNK)
        return true;
    return false;
}
void filePerm(const struct stat &fileStat, char *perm)
{
    strcpy(perm, "----------");
    switch(fileStat.st_mode & S_IFMT)
    {
    case S_IFSOCK:
        perm[0] = 's';
        break;
    case S_IFLNK:
        perm[0] = 'l';
        break;
    case S_IFREG:
        perm[0] = '-';
        break;
    case S_IFBLK:
        perm[0] = 'b';
        break;
    case S_IFDIR:
        perm[0] = 'd';
        break;
    case S_IFCHR:
        perm[0] = 'c';
        break;
    case S_IFIFO:
        perm[0] = 'p';
        break;
    default:
        perm[0] = '?';
        break;
    }

    if (fileStat.st_mode & S_IRUSR)
        perm[1] = 'r';
    if (fileStat.st_mode & S_IWUSR)
        perm[2] = 'w';
    if (fileStat.st_mode & S_IXUSR)
        perm[3] = 'x';
    if (fileStat.st_mode & S_IRGRP)
        perm[4] = 'r';
    if (fileStat.st_mode & S_IWGRP)
        perm[5] = 'w';
    if (fileStat.st_mode & S_IXGRP)
        perm[6] = 'x';
    if (fileStat.st_mode & S_IROTH)
        perm[7] = 'r';
    if (fileStat.st_mode & S_IWOTH)
        perm[8] = 'w';
    if (fileStat.st_mode & S_IXOTH)
        perm[9] = 'x';
}

文件I/O实践(2) --文件stat的更多相关文章

  1. Python编程从入门到实践笔记——文件

    Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...

  2. Linux实践:文件破解

    Linux实践:文件破解 标签(空格分隔): 20135321余佳源 一.掌握NOP.JNE.JE.JMP.CMP汇编指令的机器码 NOP:NOP指令即"空指令".执行到NOP指令 ...

  3. 转 Jmeter测试实践:文件上传接口

    Jmeter测试实践:文件上传接口   1.打开jmeter4.0,新建测试计划,添加线程组.根据实际情况配置线程属性. 2.添加HTTP请求. Basic部分修改如下: Advanced部分我做任何 ...

  4. vue 单文件组件最佳实践

    vue 单文件组件最佳实践 生命周期 template <template> <section> <h1>vue single file components te ...

  5. 文件I/O实践(3) --文件共享与fcntl

    文件共享 一个进程打开了两个文件 文件表条目(file-table-entry): 1.文件状态标志(file-status-flags): 读/写/追加/同步/非阻塞等; 2.当前文件偏移量 3.v ...

  6. 文件I/O实践(1) --基础API

    什么是I/O 输入/输出是内存和外设之间拷贝数据的过程: 设备->内存: 输入操作 内存->设备: 输出操作 高级I/O: ANSI C提供的标准I/O库函数成为高级I/O, 也称为带缓冲 ...

  7. [项目实践] python文件路径引用的规则,记一次使用sys.path[0]的问题,及如何区分 ../与 ./的使用场景

    下面是一个获取配置的代码 def getValue(self,section,option): """ @file: string,the name of the con ...

  8. Linux下恢复误删文件:思路+实践

    周五篮球群里有人问误删文件了怎么恢复,得知是ext4文件系统之后我推荐了ext4magic这个工具,然后又有人提到了xfs的话怎么办,正好前几天看到Dave Chinner在邮件列表里提到了这个问题, ...

  9. Linux文件权限与文件夹权限实践

    文件权限在基础中有介绍,不在重复 一.文件夹权限: 示例: 解释说明: r --read 既ls w --write     既创建新的目录或者文件 x --execute 既cd 现在有4个用户分属 ...

随机推荐

  1. Python List insert()方法详解

    1.功能insert()函数用于将指定对象插入列表的指定位置. 2.语法list.insert(index, obj) 3.参数index: 对象obj需要插入的索引位置.obj: 插入列表中的对象. ...

  2. map函数、filer函数、reduce函数的用法和区别

    Map函数 map函数的用法如下: def add_one(x): return x+1 #使用普通函数 v1 = map(add_one,[1,2,3]) v1 = list(v1) print(v ...

  3. PHP 包含

    PHP 包含文件 PHP include 和 require 语句 在 PHP 中,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容. include 和 require 语句用于在执 ...

  4. 作业02-Java基本语法与类库

    1. 本周学习总结 以几个关键词描述本周的学习内容.并将关键词之间的联系描述或绘制出来. 原则:少而精,自己写.即使不超过5行也可,但请一定不要简单的复制粘贴. 2. 书面作业 1. String-使 ...

  5. IOS开发初体验

    IOS开发初体验 搭建开发环境 不多说什么了,开发环境的搭建太简单了,上App Store搜索XCode下载就行了,说多了都是眼泪 创建第一个IOS项目--HolleIOS 创建工程 选择工程创建位置 ...

  6. Android studio - Failed to find target android-18

    看了一下国外的解决方案,好多人也都遇到此类问题.看老外的聊天,由衷觉得着实的可爱,同时外国的月亮也不见得比国内的圆.以下是他们的对话(最后有一个小总结):   I have a problem wit ...

  7. Markdown对应Yelee主题语法

    概述 这里说的是Yelee主题的语法和原生语法是有些区别的:更多的基础语法可以到Cmd Markdown上面去查看:但是我觉得都会各有不同吧 注意这里说的不是真正意义上的Markdown语法 标题 一 ...

  8. Python Tkinter小试

    前两天看到一篇关于Python使用Tkinter 的博文,写的很好.就拿来研究了一下,改了改.现分享如下: 参考 代码 # coding:utf8 # python2.73 winxp ''''' 天 ...

  9. 数据标准化/归一化normalization

    http://blog.csdn.net/pipisorry/article/details/52247379 基础知识参考: [均值.方差与协方差矩阵] [矩阵论:向量范数和矩阵范数] 数据的标准化 ...

  10. iOS9中如何注册远程通知

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在以往的版本中,我们可以通过: [[UIApplication ...