Linux下的文件和文件夹
首先简单介绍一下Linux下的文件属性的结构体
struct stat{
mode_t st_mode; //属性类型
ino_t st_ino; //i节点s
dev_t st_dev; //文件存储设备的编号
dev_t st_rdev; //特殊文件设备编号
nlink_t st_nlink; //链接数量
uid_t st_uid; //文件拥有者ID
gid_t st_git; //文件拥有者的ID
off_t st_size; //文件大小
time_t st_atime; //最后一次访问时间
time_t st_mtime; //最后一次修改时间
time_t st_ctime; //文件状态时间
blksize_t st_blksize; //IO块的大小
blkcnt_t st_blocks; //系统分配的磁盘块的数量
}
在上面的结构体中可以看到关于文件的各个属性,其中有几个相对来说是比较重要的,如st_mode,st_nlink,st_size等,在上面其类型都是用typedef 类型别名重新定义后的
然后介绍一下文件属性相关的操作函数
#include<sys/types.h>
#include<sys/stat.h>
int stat(const char *pathname,struct stat*buf);
int fstat(int fd,struct stat *buf);
int lstat(const char *pathname,struct stat *buf);
返回:若成功返回0,若出错则为-1
功能:返回一个与pathname或fd指定的文件属性,存储在结构体buf中
参数:pathname:文件路径名字
buf:struct stat结构体指针
lstat函数类似stat,但是当命名的文件是一个符号链接是,
lstat返回该符号连接的有关信息,而不是由该符号链接引用的文件的信息。
inux中的七种文件和七种宏
普通文件 S_ISREG() 以-开头
目录文件 S_ISDIR() 以d开头
块特殊文件 S_ISBLK() 以b开头
字符特殊文件 S_ISCHR() 以c开头
FIFO S_ISFIFO() 以p开头
套接字 S_ISSOCK() 以s开头
符号链接 S_ISLNK() //符号链接 以l开头
如:if(S_ISREG(stat.st_mode)){
}else{
}
标注:如何去创建管道文件和链接文件
创建fifo文件 mkfifo s.pipe
创建符号链接 ln -s date.txt l_date
Linux中的设备文件一般都是存放在dev目录下
套接字的设备文件是以s开头
关于文件的权限:
9种文件访问权限位:USR,GRP,OTH
如 -rw-r--r--
读写执行
rw-用户权限:USR
S_IRUSR,S_IWUSR,S_IXUSR
r--组权限:GRP
S_IRGRP,S_IWGRP,S_IXGRP
r--其他权限:OTH
S_IROTH,S_IWOTH,S_IXOTH
文件权限通过按位或方式构造
常用777,664
int fd =open("date.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|...);
int fd =open("date.txt",O_WRONLY|O_CREAT,0777);
文件权限检测函数access
assess函数
#include<unistd.h>
int access(const char*pathname,int mode);
返回:成功执行返回0,若出错为-1
功能:检查 是否可以对指定文件进行某种操作
参数:pathname 文件路径
mode:文件的访问权限
R_OK 判断文件是否有读权限
W_OK 判断文件是否有写权限
X_OK 判断文件是否有可执行权限
F_OK 判断文件是否存在
使用getpid获取我们的进程号码:
getpid();获得当前的进程;
以下是代码部分:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<memory.h>
#include<malloc.h>
#include<unistd.h>
void accessFile(char *pathname);
void outPrint(struct stat * stat_pointer,char * filename);
void outFileAuth(struct stat* stat_pointer);
int main(int argc,char *argv[]){
struct stat *stat_pointer = (struct stat*)malloc(sizeof(struct stat)) ;
int get_result1;
int i = 0;
if(argc == 1){
fprintf(stdout,"缺少参数\n");
exit(EXIT_FAILURE);
}
for(i = 1; i<argc;i++){
memset(stat_pointer,0,sizeof(stat_pointer));
get_result1 = lstat(argv[i],stat_pointer);
if(get_result1 == 0){
printf("stat get result success\n");
outPrint(stat_pointer,argv[i]);
}else{
printf("stat get result failed\n");
}
outPrint(stat_pointer,argv[i]);
accessFile(argv[i]);
}
free(stat_pointer);
return 0;
}
void accessFile(char * filepath){
if(access(filepath,F_OK)){
printf("%s is not file\n",filepath);
return;
}
if(!access(filepath,R_OK)){
printf("%s can read ok\n",filepath);
}else{
printf("%s can not read ok\n",filepath);
}
if(!access(filepath,W_OK)){
printf("%s can write ok\n",filepath);
}else{
printf("%s can not write ok\n",filepath);
}
if(!access(filepath,X_OK)){
printf("%s can exce ok\n",filepath);
}else{
printf("%s can not exce ok\n",filepath);
}
}
void outPrint(struct stat * stat_pointer,char *pathname){
printf("file name:%s:\t",pathname);
if(S_ISREG(stat_pointer->st_mode)){
printf("-");
}else if(S_ISDIR(stat_pointer->st_mode)){
printf("d");
}else if(S_ISBLK(stat_pointer->st_mode)){
printf("b");
}else if(S_ISCHR(stat_pointer->st_mode)){
printf("c");
}else if(S_ISFIFO(stat_pointer->st_mode)){
printf("p");
}else if(S_ISSOCK(stat_pointer->st_mode)){
printf("s");
}else if(S_ISLNK(stat_pointer->st_mode)){
printf("l");
}else{
printf("other file\n");
}
outFileAuth(stat_pointer);
printf("\n");
}
void outFileAuth(struct stat * stat_pointer){
mode_t st_mode = stat_pointer->st_mode;
if(S_IRUSR & st_mode){
printf("r");
}else{
printf("-");
}
if(S_IWUSR & st_mode){
printf("w");
}else{
printf("-");
}
if(S_IXUSR & st_mode){
printf("x");
}else{
printf("-");
}
if(S_IRGRP & st_mode){
printf("r");
}else{
printf("-");
}
if(S_IWGRP & st_mode){
printf("w");
}else{
printf("-");
}
if(S_IXGRP & st_mode){
printf("x");
}else{
printf("-");
}
if(S_IROTH & st_mode){
printf("r");
}else{
printf("-");
}
if(S_IWOTH & st_mode){
printf("w");
}else{
printf("-");
}
if(S_IXOTH & st_mode){
printf("x");
}else{
printf("-");
}
printf("\t");
printf("pid:%d",getpid());
}
代码是可以直接进行run的,结合了上述的所有函数的,相对来说都是比较简单的用法。希望对大家能有点帮助,写的不行的地方,希望能够及时指出,谢谢访问。有兴趣一起学习的朋友可以粉下,一起交流,谢谢