在Linux系統中,系統是通過inode來獲得這個文件的信息。在Linux系統中,inode的信息都是封裝在stat這個結構體中。可以通過man 2 stat來查看stat的具體結構。從中可以看到包含了文件的UID,GID,大小,以及訪問,修改,改變文件狀態的時間
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\n",buf->st_size);
printf("the user id is %d\n",buf->st_uid);
printf("the user id is %d\n",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");
}
在創建文件或者文件夾的時候,創建的文件和文件夾都有三個類別的權限:1 用戶的讀,寫,執行門限 2 同一個組的用戶讀,寫,執行門限 3 其他用戶的讀,寫,執行門限
在st_mode中也包含了這些權限位:
S_IRUSR:用戶讀
S_IWUSR:用戶寫
S_IXUSR:用戶執行
S_IRGRP:組讀
S_IWGRP:組寫
S_IXGRP:組執行
S_IROTH:其他讀
S_IWOTH:其他寫
S_IXOTH :其他執行
那么在創建文件的時候,我們是如何指定這些文件的權限的呢。在手動創建文件的時候,文件的權限取決於umask。文件和文件夾的默認完成權限分別是666和777. 在創建的時候,是采用默認權限減去umask的值。比如如果umask的值是022. 那么創建的文件和文件夾的權限分別是644和755。測試如下
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是文件的路徑,而fchown中fd是文件的描述符
其中uid_t和gid_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,分別是1100和1101.
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_size是off_t結構,也就是長整型的數值
void get_file_informaton()
{
struct stat buf;
stat("file_try.c",&buf);
printf("file_try size=%ld\n",buf.st_size);
printf("file_try UID=%d\n",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=]