Linux檔案系統及屬性
Linux檔案系統及屬性
宗旨:技術的學習是有限的,分享的精神是無限的。
一、Linux系統下檔案型別及屬性
1、inode結構
/*索引節點物件由inode結構體表示,定義檔案在linux/fs.h中*/ struct inode { struct hlist_node i_hash; /* 雜湊表 */ struct list_head i_list; /* 索引節點連結串列 */ struct list_head i_dentry; /* 目錄項鍊表 */ unsigned long i_ino; /* 節點號 */ atomic_t i_count; /* 引用記數 */ umode_t i_mode; /* 訪問許可權控制 */ unsigned int i_nlink; /* 硬連結數 */ uid_t i_uid; /* 使用者id */ gid_t i_gid; /* 使用者id組 */ kdev_t i_rdev; /* 實裝置識別符號 */ loff_t i_size; /* 以位元組為單位的檔案大小 */ struct timespec i_atime; /* 最後訪問時間 */ struct timespec i_mtime; /* 最後修改(modify)時間 */ struct timespec i_ctime; /* 最後改變(change)時間 */ unsigned int i_blkbits; /* 以位為單位的塊大小 */ unsigned long i_blksize; /* 以位元組為單位的塊大小 */ unsigned long i_version; /* 版本號 */ unsigned long i_blocks; /* 檔案的塊數 */ unsigned short i_bytes; /* 使用的位元組數 */ spinlock_t i_lock; /* 自旋鎖 */ struct rw_semaphore i_alloc_sem; /* 索引節點訊號量 */ struct inode_operations *i_op; /* 索引節點操作表 */ struct file_operations *i_fop; /* 預設的索引節點操作 */ struct super_block *i_sb; /* 相關的超級塊 */ struct file_lock *i_flock; /* 檔案鎖鏈表 */ struct address_space *i_mapping; /* 相關的地址對映 */ struct address_space i_data; /* 裝置地址對映 */ struct dquot *i_dquot[MAXQUOTAS]; /* 節點的磁碟限額 */ struct list_head i_devices; /* 塊裝置連結串列 */ struct pipe_inode_info *i_pipe; /* 管道資訊 */ struct block_device *i_bdev; /* 塊裝置驅動 */ unsigned long i_dnotify_mask; /* 目錄通知掩碼 */ struct dnotify_struct *i_dnotify; /* 目錄通知 */ unsigned long i_state; /* 狀態標誌 */ unsigned long dirtied_when; /* 首次修改時間 */ unsigned int i_flags; /* 檔案系統標誌 */ unsigned char i_sock; /* 可能是個套接字吧 */ atomic_t i_writecount; /* 寫者記數 */ void *i_security; /* 安全模組 */ __u32 i_generation; /* 索引節點版本號 */ union { void *generic_ip; /* 檔案特殊資訊 */ } u; };<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
2、Linux檔案型別(/usr/include/bits/stat.h)
-:表示常規檔案; ls –l /etc/exports
d:目錄檔案 ls –ld /etc/rc.d
c:字元裝置檔案 ls –l /dev/null
b:塊裝置檔案 ls –l /dev/sda1
l:符號連結
s:套接字檔案
p:管道檔案 【mknod pipe p ls–l pipe】
二、獲取檔案屬性函式
1、stat
——讀取任意型別檔案屬性
(1)函式原型
#include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int stat(const char *filename, struct stat buf); struct stat { dev_t st_dev; /* 裝置號 */ ino_t st_ino; /* inode值 */ mode_t st_mode; /* 檔案型別及許可權 */ nlink_t st_nlink; /* 硬連線數 */ uid_t st_uid; /* 使用者ID */ gid_t st_gid; /* 使用者組ID */ dev_t st_rdev; /* 裝置號 */ off_t st_size; /* 檔案大小 */ blksize_t st_blksize; /* 資料塊大小 */ blkcnt_t st_blocks; /* 資料塊數量 */ time_t st_atime; /* 最後一次訪問的時間 */ time_t st_mtime; /* 最後一次修改的時間 */ time_t st_ctime; /* 最後一次改變屬性時間 */ };
(2)函式引數
filename:欲讀取狀態的檔案(路徑字串)
buf:檔案屬性臨時存放位置
(3)返回值
執行成功,將在第二個引數中儲存該檔案的基本資訊,並返回0;出錯返回-1
2、fstat
——讀取已開啟的檔案的狀態
(1) 函式原型
int fstat(int fd, structstat *buf);
(2)函式引數
fd:如果第一個引數是符號連結檔案,其讀取的屬性是原始檔的屬性,因此要獲取連結檔案的屬性,需要呼叫lstat函式。
int lstat(const char *filename,struct stat *buf);
(3)返回值
執行成功,將在第二個引數中儲存該檔案的基本資訊,並返回0;出錯返回-1
三、使用者名稱/組名與uid/gid的轉換
函式stat()返回的某檔案屬性中,其擁有者所在的組以UID和GID值存在,在輸出某檔案使用者屬性時有必要將其轉換為特定使用者名稱和組名。
getpwuid()/getpwnam()
——通過使用者UID或使用者名稱檢視某特定使用者的基本資訊
(1) 函式原型 —— 都是從/etc/passwd下讀取該使用者的基本資訊
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
struct passwd
{
char *pw_name; // 使用者名稱
char *pw_paawd; // 密碼
uid_t pw_uid; //uid
gid_t pw_gid; // gid
char *pw_gecos; // 註釋
char *pw_dir; // 主目錄
char *pw_shell; // 預設shell
};
(2)函式引數
uid:使用者uid
(3)返回值
成功返回struct passwd的結構體