Linux学习之文件系统篇(三)

基于inode函数介绍

1.stat

表头文件:    #include <sys/stat.h>
                   #include <unistd.h>
定义函数:    int stat(const char *file_name, struct stat *buf);

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

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

函数说明:    通过文件名filename获取文件信息,并保存在buf所指的结构体stat中
返回值:      执行成功则返回0,失败返回-1,错误代码存于errno

struct stat {
    dev_t         st_dev;       //文件的设备编号
    ino_t         st_ino;       //节点
    mode_t        st_mode;      //文件的类型和存取的权限
    nlink_t       st_nlink;     //连到该文件的硬连接数目,刚建立的文件值为1
    uid_t         st_uid;       //用户ID
    gid_t         st_gid;       //组ID
    dev_t         st_rdev;      //(设备类型)若此文件为设备文件,则为其设备编号
    off_t         st_size;      //文件字节数(文件大小)
    unsigned long st_blksize;   //块大小(文件系统的I/O 缓冲区大小4096)
    unsigned long st_blocks;    //块数
    time_t        st_atime;     //最后一次访问时间
    time_t        st_mtime;     //最后一次修改时间
    time_t        st_ctime;     //最后一次改变时间(指属性)
};

 

stat 既有命令也有同名函数,用来获取文件Inode里主要信息,stat跟踪符号链接,lstat不跟踪符号理解

stat里边时间辨析:

atime(最近访问时间):

mtime(最近更改时间):指的是修改文件内容的时间

ctime(最近改动时间):指最近改动Inode的时间,比如更改文件权限位。文件更改必然会导致文件改动时间 改变,因为inode里边有文件属性一栏包括更改时间。

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
    struct stat s_buf;
    if(argc<2)
    {
        printf("./app filename\n");
        exit(1);
     }
     if(stat(argv[1],&s_buf)<0
     {
            perror("stat");
            exit(1);
       }
       printf("%s\t%ld\n",argv[1],s_buf.st_size);
       return 0;
}

2.access

access函数的原型如下:

#include<unistd.h>

int access(const char *pathname, int mode);

此函数是用来获得调用进程对pathname所指向的文件(regular)或者是目录(directory)的访问权限。

pathname: 文件或者是目录路径

mode:访问模式。可以F_OK,或者是F_OK和R_OK, W_OK,and X_OK的或(|)。F_OK,表示pathname所指向文件是否存;R,W,X判断实际用户ID是否有读写执行权限。按实际用户ID和实际组ID测试,跟踪符号链接。

实际用户ID:

有效用户ID:sudo执行时,有效用户ID是root,实际用户ID是用户名 

 3.chmod

#include<sys/stat.h>

int chmod(const char *path,mode_t mode);

int fchmod(int fd,mode_t mode);

 注意:如果使用Linux的chmod命令时,得有root权限

 关于mode_t的定义;

  A:mode_t的定义实际就是unsigned int 形式的

  B:但是函数chmod(const char *path,mode_t mode)在解释mode_t时时将这里的mode当成8进制去解释

参数mode有以下几种组合:

    

   S_ISUID  (04000)  set-user-ID  (set  process  effective  user   ID   on
                         execve(2))                                                                      //执行时设置用户ID

       S_ISGID  (02000)  set-group-ID  (set  process  effective  group  ID  on        
                         execve(2);  mandatory  locking,   as   described   in
                         fcntl(2);  take a new file's group from parent direc‐
                         tory, as described in chown(2) and mkdir(2))                                    //执行时设置组ID

       S_ISVTX  (01000)  sticky bit (restricted deletion flag, as described in
                         unlink(2))                                                                       //文件的黏着位

       S_IRUSR  (00400)  read by owner                                                             //文件所有者可读取权限                                                                    

       S_IWUSR  (00200)  write by owner                                                            //文件所有者具有可写入权限

       S_IXUSR  (00100)  execute/search  by owner ("search" applies for direc‐
                         tories, and means that entries within  the  directory
                         can be accessed)                                                           //文件所有者具有可执行权限

 

       S_IRGRP  (00040)  read by group                                                             //用户组具有可读取权限

       S_IWGRP  (00020)  write by group                                                            //用户组具有可执行权限

       S_IXGRP  (00010)  execute/search by group                                                   //用户组具有可执行权限

 

       S_IROTH  (00004)  read by others                                                             //其他用户具有可读取权限               

       S_IWOTH  (00002)  write by others                                                            //其他用户具有可写入权限

       S_IXOTH  (00001)  execute/search by others                                                  //其他用户具有可执行权限

 

 

      如要将文件test的权限修改为644,那么可以采用以下几种方法:

       chmod("文件名",S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);

       chmod("文件名",0644);

       chmod("文件名",420);

       说明:

       一、第一种方法是将00400和00200和00040和00004进行或运算,最终得到的结果就是0644(八进制),而八进制的0644就等于十进制的420,所以上面的几种方式等价的。

       二、当我们给chmod函数传递参数时他会将对应的十进制的mode参数转换为相应的八进制进行运算。所以,当我们要给函数中传入的文件的权限改为644时传递给函数chmod的参数不能直接是644.而应该是420.这是因为十进制的420就等于八进制的644.  

注意:密码保存在shadow文件中,加密后显示。

4.chown

#include<unistd.h>

int chown(const char *path,uid_t owner,gitd_t group);

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

int lchown(const char *path,uid_t owner,gitd_t group);

chown 使用时必须拥有root权限。

 

5.utime

#include<sys/time.h>

int utimes(const char *filename,const struct timeval times[2]);

 

6.truncate

#include<unistd.h>

#include<sys/types.h>

int truncate(concst char *path,off_t length);

int ftruncate(int fd,off_t length);

 

转载于:https://www.cnblogs.com/rainbow1122/p/7809785.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值