zoukankan      html  css  js  c++  java
  • linux c编程:文件的操作

    Linux系统中,系统是通过inode来获得这个文件的信息。在Linux系统中,inode的信息都是封装在stat这个结构体中。可以通过man 2 stat来查看stat的具体结构。从中可以看到包含了文件的UIDGID,大小,以及访问,修改,改变文件状态的时间

    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 */


    /* 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

    };

    下面的这个例子打开一个文件并输出文件的统计信息

    #include <sys/stat.h>
    #include <malloc.h>

    char *path="/home/zhf/test1.txt";

    void check_file(){
        struct stat *buf=NULL;
        buf=(struct stat *)malloc(sizeof(struct stat));
        stat(path,buf);
        printf("the file size is %ld ",buf->st_size);
        printf("the user id is %d ",buf->st_uid);
        printf("the user id is %d ",buf->st_gid);

    }

    运行结果

    the file size is 7
    the user id is 0
    the user id is 0

    对应的是root用户

    root@zhf-maple:/home/zhf# cat /etc/passwd
    root:x:0:0:root:/root:/bin/bash

     

    文件夹类型信息包含在stat结构的st_mode成员中,可以用下面的宏来确定文件类型,这些宏参数都是stat结构中的st_mode成员。如果判断为真,则返回1,否则返回0

    S_ISREG(): 普通文件

    S_ISDIR():目录文件

    S_ISCHR(): 字符特殊文件

    S_ISBLK():块特殊文件

    S_ISFIFO():管道或FIFO

    S_ISLNK():符号链接

    S_ISSOCK():套接字

    前面的check_file添加如下判断就可以进行判断文件是否是一个普通的文件

    if (S_ISREG(buf->st_mode)){
            printf("it is a normal file");
        }

     

     

    在创建文件或者文件夹的时候,创建的文件和文件夹都有三个类别的权限:用户的读,写,执行门限 同一个组的用户读,写,执行门限 其他用户的读,写,执行门限

    st_mode中也包含了这些权限位:

    S_IRUSR:用户读

    S_IWUSR:用户写

    S_IXUSR:用户执行

    S_IRGRP:组读

    S_IWGRP:组写

    S_IXGRP:组执行

    S_IROTH:其他读

    S_IWOTH:其他写

    S_IXOTH :其他执行

    那么在创建文件的时候,我们是如何指定这些文件的权限的呢。在手动创建文件的时候,文件的权限取决于umask。文件和文件夹的默认完成权限分别是666777. 在创建的时候,是采用默认权限减去umask的值。比如如果umask的值是022. 那么创建的文件和文件夹的权限分别是644755。测试如下

    root@zhf-maple:/home/zhf# touch mask.txt

    root@zhf-maple:/home/zhf# ls -al mask.txt

    -rw-r--r-- 1 root root 0 4月  20 16:58 mask.txt

    root@zhf-maple:/home/zhf# umask

    0022

    在用代码创建的时候同样也可以使用umask函数。在这里首先定义了RWRWRW, 权限是666. umask中屏蔽了S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH。因此最终创建的文件应该是600.

    #define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOT

    void mask_function_try(){

    int mode;

    char *path="/home/zhf/mask.txt";

    umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);

    creat(path,RWRWRW);

    }

    实际结果:

    root@zhf-maple:/home/zhf# ls -al | grep mask.txt

    -rw-------  1 root         root         0 4月  20 17:22 mask.txt

     

     

     

    下面来看下具体操作文件的操作。首先来看下修改文件属性。系统提供了chown,fchown来修改指定文件的所有者以及用户组。函数原型如下:调用成功时,返回值为0,失败返回-1.并设置相应的errno值。

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

    int fchown(int fd, uid_t owner,gid_t group);

    两个函数不同的是chown参数pathname是文件的路径,而fchownfd是文件的描述符

    其中uid_tgid_t如果是-1的话就代表不改变。我们来看一个例子。

    /home/zhf/zhf下有个文件test.png 用户和用户组分别属于zhf. 现在我们将用户改为zhf_test

    -rw-rw-r-- 1 zhf zhf 38844 Aug 6 15:28 test.png


    查看zhf_test用户的用户ID和用户组ID,分别是11001101.

    root@zhf-linux:/home/zhf/zhf# cat /etc/passwd | grep zhf_test

    zhf_test:x:1100:1101::/home/zhf_test:


    修改代码如下:

    #include <sys/types.h>

    #include <unistd.h>

    #include <stdio.h>

    #include <stdlib.h>


    void change_file_owner()

    {

    char *path="/home/zhf/zhf/test.png";

    chown(path,1100,-1);

    }


    void main()

    {

    change_file_owner()

    }

    执行过后查看test.png发现用户名已经被修改为zhf_test.

    -rw-rw-r-- 1 zhf_test zhf 38844 Aug 6 15:28 test.png


    来看下fchown的用法。fchown是通过文件属性来修改文件的。文件属性通过open函数来获得。open函数返回一个整数的文件描述符。代码如下, O_RDONLY代表的是以只读的方式打开文件。具体的打开方式可以通过man 2 open来查看

    void change_file_owner_by_fchown()

    {

    int fd;

    fd=open("/home/zhf/zhf/test.png",O_RDONLY);

    fchown(fd,1100,-1);

    close(fd);

    }


    改变文件的名称:

    修改文件名称采用rename函数

    int rename(const char *oldpath, const char *newpath)

    两个参数都为指针,第一个指向原来文件的名称,第二个指向新的文件。调用成功返回0。否则函数返回-1


    void change_file_name()

    {

    char *oldname="/home/zhf/zhf/test.png";

    char *newname="/home/zhf/zhf/test_rename.png";

    rename(oldname,newname);

    }



    获取文件信息:

    采用stat函数。int stat(const char *path, struct stat *buf)

    path表示指向需要获取信息的文件的路径名

    参数buf表示指向一个stat结构体类型的指针。

    注意打印buf.st_size的时候需要用长整型。因为st_sizeoff_t结构,也就是长整型的数值

    void get_file_informaton()

    {

    struct stat buf;

    stat("file_try.c",&buf);

    printf("file_try size=%ld ",buf.st_size);

    printf("file_try UID=%d ",buf.st_uid);

    }

    否则会出现如下的警告:

    file_try.c:32:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘__off_t {aka long int}’ [-Wformat=]

  • 相关阅读:
    3.1C#中的命名空间
    2章总结
    2.4冒泡排序
    2.3 C#中的数组
    2.2二重循环
    2.1c#中的循环语句
    1章总结
    docker内外数据拷贝
    搭建docker环境
    centos7 部署Apache的httpd服务器
  • 原文地址:https://www.cnblogs.com/zhanghongfeng/p/7719076.html
Copyright © 2011-2022 走看看