Linux 高并发学习笔记 - Linux 文件属性函数

1.6.3 Linux 文件属性函数

Linux 高并发学习笔记 - 笔记索引

  • Linux中一切皆文件,你同样可以用下面这些函数操作目录等特殊文件。
前言

关于文件操作函数这一块主要用英文文档的形势书写,因为凉皮在写文档的时候发现Markdown用起来太繁琐了。那么关于函数这一块主要也是读文档就能解决的了。

关于文档可以使用Linux命令man 2/3 xxx查看,在后面也写了查看每个文档的命令。

返回文件属性
  • statlstat

  • 常用属性:

    st_mode:文件属性编码。
    在这里插入图片描述
    st_size:文件大小Bytes

    st_atime:访问时间。

    st_mtime:修改时间。

    st_ctime:修改时间,指属性修改。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
// 	get file status
// 		pathname:
// 			the path of target file
// 		statbuf:
// 			the container to restore data
// 			struct stat {
// 			    dev_t     st_dev;         /* ID of device containing file */
// 			    ino_t     st_ino;         /* Inode number */
// 			    mode_t    st_mode;        /* File type and mode */
// 			    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;     /* Block size for filesystem I/O */
// 			    blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */
// 	
// 			    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
// 			    #define st_mtime st_mtim.tv_sec
// 			    #define st_ctime st_ctim.tv_sec
// 			};
// 		return value:
// 			return 0 for success, -1 for error
int stat(const char *pathname, struct stat *statbuf);
// similar to stat
int lstat(const char *pathname, struct stat *statbuf);

// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("stat");

// About more
// $ man 2 stat
  • lstatstat的区别:stat在访问软链接时,返回软链接属性;lstat在访问软链接时,返回指向文件属性。
$ ln -s src.txt link.lnk  // 建立软链接 link.lnk -> src.txt
判断文件访问
  • access
#include <unistd.h>
//  check permissions to the file for current process
// 		pathname:
// 			the file path
// 		mode:
// 			optional and limitless:
// 				F_OK for file exists, 
// 				R_OK for read permission for process, 
// 				W_OK for write permission for process, 
// 				X_OK for execute permission for process
// 		return value:
// 			return 0 for all permission is available, -1 for other case and error
int access(const char *pathname, int mode);

// if return value is -1, you can then call to print the error message
#include <stdio.h>
perror("access");

// About more
// $ man 2 access
修改文件属性
  • chmod
#include <sys/stat.h>
// 	change mode of file
// 		pathname:
// 			the file path
// 		mode:
// 			three digits of oct-based number
// 			UMASK work here
// 		return value:
// 			return 0 for success, -1 for error
int chmod(const char *pathname, mode_t mode);

// if return value is -1, you can then call to print the error message
#include <stdio.h>
void perror("chmod");

// About more
// $ man 2 chmod
修改文件所有
  • chown
#include <unistd.h>
// 	change owner of file
// 		pathname:
// 			the file path
// 		owner:
// 			the UID of owner
// 		group:
// 			the GID of group
// 		return 0 for success, -1 for error
int chown(const char *pathname, uid_t owner, gid_t group);

// if return value is -1, you can then call to print the error message
#include <stdio.h>
void perror("chown");

// About more
// $ man 2 chown
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值