?1 File Times
每個文件會維護三個時間字段,每個字段代表的時間都不同。如下表所示:
字段說明:
i-node中的信息和文件的實際內容是分離的,所以當更新i-node時,需要更新的時st_ctim(the changed-status time),而不是st_mtim(the modification time)。
命令ls各個參數排序時使用的時間標準:
下表總結了會影響這三種時間的部分函數,在看到表之前,我們需要了解:
?
函數作用:修改文件的access time和modification time。
函數聲明:
#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);
參數說明:
strcut timespec結構體中至少包含兩個時間字段:time_t tv_sec(秒)和long tv_nsec(毫微秒)。
數組times包含兩個時間:第一個元素是access time,第二個元素是modification time。
函數行為受參數times的取值影響,需要參考時可以自行查詢。
?
同時,執行上面的一對函數也會對權限有要求,要求如下:
函數細節:
函數utimes通過制定一個文件路徑pathname,來修改文件的相關時間。
函數聲明:
#include <sys/time.h>
int utimes(const char *pathname, const struct timeval times[2]);
strcut timeval {
? ? time_t tv_sec;
? ? long tv_usec;
};
我們不能指定修改時間字段st_ctim(changed-status time),但是在調用utimes函數時,該字段被自動更新。
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);
}
運行結果:(由于我用的mac os不支持的原因,并沒有編譯成功該例,所以直接用書上的結果)
從結果中可以看到,last-modification time和last-access time沒有改變,而changed-status time發生了改變。
?
3 mkdir、mkdirat和rmdir函數mkdir和mkdirat函數創建一個新的空的目錄,rmdir函數用來刪除目錄。
函數聲明:
#include <sys/stat.h>
int mkdir(const char* pathname, mode_t mode);
int mkdirat(int fd, const char* pathname, mode_t mode);
參數mode取值為基于在前面一篇提到過的文件創建掩碼(file creation mask of PRocess)。
?rmdir函數用來刪除一個空的目錄,空目錄中只含有兩個記錄(dot和dot-dot)。
函數聲明:
#include <sys/stat.h>
int rmdir(const char* pathname);
上面的三個函數,調用成功返回0,失敗返回-1.
?
4 讀取目錄文件(reading directories)對于目錄文件,只要有相應的權限,任何人都可以讀取目錄文件的內容。但是為了保證系統正常工作,只有內核可以對目錄文件執行寫操作。
在前面的章節中,我們了解到,目錄文件的寫權限位和執行權限位決定了我們是否可以在該目錄下創建和刪除文件,但是它們并不能允許我們寫目錄文件本身。
讀文件操作依賴于系統實現。相關函數聲明如下:
#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);
細節說明:
?
5 chdir、fchdir和getcwd函數每個進程都有一個工作目錄(current working directory),工作目錄是進程的一個屬性。
函數聲明:
#include <unistd.h>
int chdir(const char* pathname);
int fchdir(int fd);
?比較簡單,不做贅述。
如果我們希望獲取當前工作目錄的完整信息(絕對路徑),無法直接從內核獲取,因為內核只是維護了一個指向該目錄的指針。
如果我們要獲取絕對路徑,需要通過dot-dot進入上級目錄,讀取該級目錄的信息,獲取目錄名,然后再依次訪問上級目錄一直到根目錄,最后拼出絕對路徑。
函數getcwd就是實現了這個功能。
函數聲明:
#include <unistd.h>
char* getcwd(char *buf, size_t size);
需要注意的一點是,這里的buf需要足夠大,size為它的大小。buf需要容納絕對路徑加上一個字節的null終止符。
?Example:
函數功能:修改當前工作目錄(chdir)并獲取該工作目錄絕對路徑(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 文件權限位總結如下表所示
??
7 小結這一章包括了三篇內容,主要圍繞stat函數,了解了:
下一章我們將會學習標準IO庫。?
?
參考資料:
《Advanced Programming in the UNIX Envinronment 3rd》
?
|
新聞熱點
疑難解答