標籤:c style class blog code java
1. 檔案和中繼資料
每個檔案都是通過inode引用,每個inode索引節點都具有檔案系統中唯一的inode number一個inode索引節點是儲存在Linux檔案系統的磁碟介質上的物理對象,也是LInux核心通過資料結構表示的實體inode儲存相關聯檔案的中繼資料 ls -i 命令擷取檔案的inode number
/* obtaining the metadata of a file */#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>int stat (const char *path, struct stat *buf);int fstat (int fd, struct stat *buf);int lstat (const char *path, struct stat *buf);
注意:lstat函數可以擷取 符號連結的檔案中繼資料,lstat() returns information about the link itself and not the target file
struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* permissions */ 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 blocks allocated */ time_t st_atime; /* last access time */ time_t st_mtime; /* last modification time */ time_t st_ctime; /* last status change time */};
2. 目錄a directory contains a list of filenames, each of which maps to an inode number. Each name is called a directory entry, and each name-to-inode mapping is called a link當開啟給定目錄中的檔案,核心查詢相應的inode編號,然後將inode編號傳遞給檔案系統,檔案系統使用inode編號來尋找相應的儲存在物理介質中的檔案 Reading a Directory’s Contents :
/* creates a directory stream representing the directory given by name */#include <sys/types.h>#include <dirent.h>DIR * opendir (const char *name);
/* returns the next entry in the directory represented by dir */#include <sys/types.h>#include <dirent.h>struct dirent * readdir (DIR *dir);
/* closes the directory stream represented by dir */#include <sys/types.h>#include <dirent.h>int closedir (DIR *dir);
/* * find_file_in_dir - searches the directory ‘path‘ for a * file named ‘file‘. * * Returns 0 if ‘file‘ exists in ‘path‘ and a nonzero * value otherwise. */int find_file_in_dir (const char *path, const char *file){ struct dirent *entry; int ret = 1; DIR *dir; dir = opendir (path); errno = 0; while ((entry = readdir (dir)) != NULL) { if (strcmp(entry->d_name, file) == 0) { ret = 0; break; } } if (errno && !entry) perror ("readdir"); closedir (dir); return ret;}
3. 連結each name-to-inode mapping in a directory is called a link.Most files have a link count of 1,that is, they are pointed at by a single directory entry當一個檔案的連結計數減為0時,檔案被標記為free,如果存在進程仍在使用此檔案,則該檔案將保留在檔案系統中 Linux核心使用 a link count and a usage count 實現,一個檔案只有其link count和usage count都為0時,才會從檔案系統移除 永久連結:
/* creates a new link under the path newpath for the existing file oldpath */#include <unistd.h>int link (const char *oldpath, const char *newpath);
成功調用之後,oldpath and newpath refer to the same file 符號連結(軟連結):軟連結可以
跨檔案系統,連結到任何檔案
/* creates the symbolic link newpath pointing at the target oldpath */#include <unistd.h>int symlink (const char *oldpath, const char *newpath);
解鏈:
#include <unistd.h>int unlink (const char *pathname);
Once no process has the file open, it is deleted 4. Copying and Moving Files Unix沒有提供可以直接複製檔案和目錄的系統調用 copying a file src to a file named dst:
1). Open src.2). Open dst, creating it if it does not exist, and truncating it to zero length if it does exist.3). Read a chunk of src into memory.4). Write the chunk to dst.5). Continue until all of src has been read and written to dst.6). Close dst.7). Close src.
5. 塊裝置The null device lives at /dev/null. The kernel silently discards all write requests to the device. All read requests to the file return end-of-file (EOF).The zero device lives at /dev/zero. 讀此裝置返回null字元,寫此裝置被丟棄The full device lives at /dev/full. 讀此裝置返回null字元,寫此裝置將觸發錯誤表示裝置已滿The kernel‘s random number generators live at /dev/random.