關(guān)于屬性的結(jié)構(gòu)
在linux下文件和文件夾都被認(rèn)為是文件, 所以以下的這個(gè)屬性對(duì)文件和文件夾通用
獲取屬性的函數(shù)有stat/fstat/lstat/fstat
struct stat{
mode_t st_mode; //文件類(lèi)型和讀寫(xiě)權(quán)限
ino_t st_ino;
dev_t st_dev;
dev_t st_rdev;
nlink_t st_nlink;
uid_t st_uid; //文件擁有者的ID
gid_t st_gid; //文件擁所在的用戶組
offt_t st_size; //文件大小
struct timespec st_atime;
struct timespec st_mtime;
struct timespec st_ctime;
blksize_t st_blksize;
blkcnt_t st_blocks;
}
struct stat buf;
char *pathname="./test.txt";
if(lstat(pathname, &buf) < 0){
printf("lstat error");
exit(1);
}
文件類(lèi)型
linux下文件分為以下幾種:
1.普通文件(regular file), 判斷函數(shù)為S_ISREG();
2.目錄文件(directory file), 判斷函數(shù)為S_ISDIR();
3.塊特殊文件(block special file), S_ISBLK();
3.字符特殊文件(character special file), S_ISCHR();
5.進(jìn)程通信管道文件(FIFO), S_ISFIFO();
6.套接字(socket), S_ISSOCK();
7.符號(hào)鏈接(symbolic link), S_ISLNK();
struct stat buf;
char *pathname="./test.txt";
if(lstat(pathname, &buf) < 0){
printf("lstat error");
exit(1);
}
if(S_ISREG(buf.st_mode))
printf("this is a regular file");
else if(S_ISDIR(buf.st_mode))
printf("this is a directory");
else if(..)
...
else
printf("unknown file type");
測(cè)試訪問(wèn)權(quán)限
文件有讀/寫(xiě)/執(zhí)行三種權(quán)限, 文件的擁有者對(duì)文件可能有讀/寫(xiě)/執(zhí)行的權(quán)限, 同組的可能有讀/執(zhí)行的權(quán)限, 而不同組的可能連讀的權(quán)限都沒(méi)有
所以在我們對(duì)已存在的文件進(jìn)行讀寫(xiě)操作時(shí), 可以先進(jìn)行訪問(wèn)權(quán)限判斷
int access(const char *pathname, int mode), mode有三個(gè)供選擇參數(shù):R_OK, W_OK, X_OK, 分別是讀/寫(xiě)/執(zhí)行
char *pathname="./test.txt";
if(access(pathname,R_OK) < 0){
perror("access error");
exit(1);
}else
printf("read access");
int fd;
if((fd=open(pathname,O_RDONLY))<0){
printf("open error");
exit(1);
}else
printf("open for reading");
更改訪問(wèn)權(quán)限
更改文件的訪問(wèn)權(quán)限條件需至少需滿足一項(xiàng):1.超級(jí)用戶進(jìn)程進(jìn)行更改; 2.文件擁有者的進(jìn)程進(jìn)行更改
文件權(quán)限的設(shè)置分成三部分, 分別作用于:
1.文件的擁有者,S_IRUSR,S_IWUSR,S_IXUSR,三合一的寫(xiě)法為S_IRWXU
2.同組的用戶,S_IRGRP,S_IWGRP,S_IXGRP,三合一的寫(xiě)法為S_IRWXG
3.其它組的用戶,S_IROTH,S_IWOTH,S_IXOTH,三合一的寫(xiě)法為S_IRWXO
以上權(quán)限依次為讀, 寫(xiě), 執(zhí)行, 三合一的包含三個(gè)權(quán)限
更改的函數(shù)有chmod/fchmod/fchmodat
struct stat buf;
char *pathname="./test.txt";
if(stat(pathname,&buf)<0){
printf("stat error");
exit(1);
}
//只關(guān)掉S_IXGRP
if(chmod(pathname,buf.st_mode & ~S_IXGRP)<0)
printf("chmod error");
//不管當(dāng)前的權(quán)限, 以絕對(duì)的方式設(shè)置
if(chmod(pathname,S_IRUSR|S_IWUSR|S_IRGRP)<0)
printf("chmod error");
更改文件擁有者
該操作在大多數(shù)linux系統(tǒng)中需要root來(lái)執(zhí)行
chown/fchown/fchownat/lchown
int chown (const char *pathname, uid_t owner, gid_t group);
//假設(shè)當(dāng)前系統(tǒng)有個(gè)普通用戶組的用戶,ID和組ID都為1000
//以root身份運(yùn)行, sudo root
char *pathname="./test.txt";
if(chown(pathname,1000,1000)<0)
printf("chown failed");
文件大小
struct stat buf;
char *pathname="./test.txt";
if(lstat(pathname,&buf)<0){
printf("lstat error");
exit(1);
}
printf("file size: %lu",buf.st_size);
另外可以用truncate(pathname,0)將文件大小設(shè)置成0,也就是擦除文件內(nèi)容
其它
創(chuàng)建文件: int creat(const char *path, mode_t mode) mode為訪問(wèn)權(quán)限,用open也可以創(chuàng)建文件
文件重命名: int rename(const char *oldname, const char *newname)
刪除文件: int remove(const char *pathname)