?1 File Times
每個(gè)文件會(huì)維護(hù)三個(gè)時(shí)間字段,每個(gè)字段代表的時(shí)間都不同。如下表所示:

字段說(shuō)明:
i-node中的信息和文件的實(shí)際內(nèi)容是分離的,所以當(dāng)更新i-node時(shí),需要更新的時(shí)st_ctim(the changed-status time),而不是st_mtim(the modification time)。
命令ls各個(gè)參數(shù)排序時(shí)使用的時(shí)間標(biāo)準(zhǔn):
下表總結(jié)了會(huì)影響這三種時(shí)間的部分函數(shù),在看到表之前,我們需要了解:
?
函數(shù)作用:修改文件的access time和modification time。
函數(shù)聲明:
#include <sys/stat.h>
int futimens(int fd, const struct timespec times[2]);
int utimensat(int fd ,const char *path, const struct timespec times[2], int flag);
參數(shù)說(shuō)明:
strcut timespec結(jié)構(gòu)體中至少包含兩個(gè)時(shí)間字段:time_t tv_sec(秒)和long tv_nsec(毫微秒)。
數(shù)組times包含兩個(gè)時(shí)間:第一個(gè)元素是access time,第二個(gè)元素是modification time。
函數(shù)行為受參數(shù)times的取值影響,需要參考時(shí)可以自行查詢(xún)。
?
同時(shí),執(zhí)行上面的一對(duì)函數(shù)也會(huì)對(duì)權(quán)限有要求,要求如下:
函數(shù)細(xì)節(jié):
函數(shù)utimes通過(guò)制定一個(gè)文件路徑pathname,來(lái)修改文件的相關(guān)時(shí)間。
函數(shù)聲明:
#include <sys/time.h>
int utimes(const char *pathname, const struct timeval times[2]);
strcut timeval {
? ? time_t tv_sec;
? ? long tv_usec;
};
我們不能指定修改時(shí)間字段st_ctim(changed-status time),但是在調(diào)用utimes函數(shù)時(shí),該字段被自動(dòng)更新。
Example:
?例子情景:
Code:
#include "apue.h"
#include <fcntl.h>
?
int
main(int argc, char *argv[])
{
? ? int ? ? ? ? ? ? i, fd;
? ? struct stat ? ? statbuf;
? ? struct timespec times[2];
?
? ? for (i = 1; i < argc; i++) {
? ? ? ? if (stat(argv[i], &statbuf) < 0) {? /* fetch current times */
? ? ? ? ? ? err_ret("%s: stat error", argv[i]);
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? if ((fd = open(argv[i], O_RDWR | O_TRUNC)) < 0) { /* truncate */
? ? ? ? ? ? err_ret("%s: open error", argv[i]);
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? times[0] = statbuf.st_atim;
? ? ? ? times[1] = statbuf.st_mtim;
? ? ? ? if (futimens(fd, times) < 0)? ? ? ? /* reset times */
? ? ? ? ? ? err_ret("%s: futimens error", argv[i]);
? ? ? ? ? ??continue;
? ? ? ? }
? ? ? ? times[0] = statbuf.st_atim;
? ? ? ? times[1] = statbuf.st_mtim;
? ? ? ? if (futimens(fd, times) < 0)? ? ? ? /* reset times */
? ? ? ? ? ? err_ret("%s: futimens error", argv[i]);
? ? ? ? close(fd);
? ? }
? ? exit(0);
}
運(yùn)行結(jié)果:(由于我用的mac os不支持的原因,并沒(méi)有編譯成功該例,所以直接用書(shū)上的結(jié)果)

從結(jié)果中可以看到,last-modification time和last-access time沒(méi)有改變,而changed-status time發(fā)生了改變。
?
3 mkdir、mkdirat和rmdir函數(shù)mkdir和mkdirat函數(shù)創(chuàng)建一個(gè)新的空的目錄,rmdir函數(shù)用來(lái)刪除目錄。
函數(shù)聲明:
#include <sys/stat.h>
int mkdir(const char* pathname, mode_t mode);
int mkdirat(int fd, const char* pathname, mode_t mode);
參數(shù)mode取值為基于在前面一篇提到過(guò)的文件創(chuàng)建掩碼(file creation mask of PRocess)。
?rmdir函數(shù)用來(lái)刪除一個(gè)空的目錄,空目錄中只含有兩個(gè)記錄(dot和dot-dot)。
函數(shù)聲明:
#include <sys/stat.h>
int rmdir(const char* pathname);
上面的三個(gè)函數(shù),調(diào)用成功返回0,失敗返回-1.
?
4 讀取目錄文件(reading directories)對(duì)于目錄文件,只要有相應(yīng)的權(quán)限,任何人都可以讀取目錄文件的內(nèi)容。但是為了保證系統(tǒng)正常工作,只有內(nèi)核可以對(duì)目錄文件執(zhí)行寫(xiě)操作。
在前面的章節(jié)中,我們了解到,目錄文件的寫(xiě)權(quán)限位和執(zhí)行權(quán)限位決定了我們是否可以在該目錄下創(chuàng)建和刪除文件,但是它們并不能允許我們寫(xiě)目錄文件本身。
讀文件操作依賴(lài)于系統(tǒng)實(shí)現(xiàn)。相關(guān)函數(shù)聲明如下:
#include <dirent.h>
DIR *opendir(const char* pathname);
DIR *fdopendir(int fd); ? // return : pointer if OK, NULL on error
struct dirent *readdir(DIR *dp); ? ?// return : pointer if OK, at end of directory or error
void rewinddir(DIR *dp);
int closed(DIR *dp); ? ?// return : 0 if OK, -1 on error
long telluride(DIR *dp); ? ?// return : current location in directory associated with dp
void seekdir(DIR *dp, long loc);
細(xì)節(jié)說(shuō)明:
?
5 chdir、fchdir和getcwd函數(shù)每個(gè)進(jìn)程都有一個(gè)工作目錄(current working directory),工作目錄是進(jìn)程的一個(gè)屬性。
函數(shù)聲明:
#include <unistd.h>
int chdir(const char* pathname);
int fchdir(int fd);
?比較簡(jiǎn)單,不做贅述。
如果我們希望獲取當(dāng)前工作目錄的完整信息(絕對(duì)路徑),無(wú)法直接從內(nèi)核獲取,因?yàn)閮?nèi)核只是維護(hù)了一個(gè)指向該目錄的指針。
如果我們要獲取絕對(duì)路徑,需要通過(guò)dot-dot進(jìn)入上級(jí)目錄,讀取該級(jí)目錄的信息,獲取目錄名,然后再依次訪問(wèn)上級(jí)目錄一直到根目錄,最后拼出絕對(duì)路徑。
函數(shù)getcwd就是實(shí)現(xiàn)了這個(gè)功能。
函數(shù)聲明:
#include <unistd.h>
char* getcwd(char *buf, size_t size);
需要注意的一點(diǎn)是,這里的buf需要足夠大,size為它的大小。buf需要容納絕對(duì)路徑加上一個(gè)字節(jié)的null終止符。
?Example:
函數(shù)功能:修改當(dāng)前工作目錄(chdir)并獲取該工作目錄絕對(duì)路徑(getcwd)
Code:
#include "apue.h"
?
int
main(void)
{
? ? char? ? *ptr;
? ? size_t? ? ? size;
?
? ? if (chdir("/usr/") < 0)
? ? ? ? err_sys("chdir failed");
?
? ? ptr = path_alloc(&size);? ? /* our own function */
? ? if (getcwd(ptr, size) == NULL)
? ? ? ? err_sys("getcwd failed");
?
? ? printf("cwd = %s/n", ptr);
? ? exit(0);
}
切換工作目錄到/usr/并打印工作目錄。
?
6 文件權(quán)限位總結(jié)如下表所示

??
7 小結(jié)這一章包括了三篇內(nèi)容,主要圍繞stat函數(shù),了解了:
下一章我們將會(huì)學(xué)習(xí)標(biāo)準(zhǔn)IO庫(kù)。?
?
參考資料:
《Advanced Programming in the UNIX Envinronment 3rd》
?
新聞熱點(diǎn)
疑難解答
圖片精選