[英]Get storage device block size from name/descriptor of a file on that device
假設我有一個文件名或一個駐留在存儲設備(硬盤,USB閃存,DVD等)上的文本文件的打開文件描述符。 如何在Linux中以編程方式從C中的文件名/描述符中獲取該設備的塊大小。我知道ioctl系統調用,但是它接受設備專用文件的打開描述符,而不是該設備上文件的打開描述符。
例如,我在某個存儲設備(例如/ dev / sda1)上有一個文件名“ /home/hrant/file1.txt”(或該文件上的打開文件描述符)。 我不知道文件在哪個設備上。 如何獲取該設備的塊大小以塊為單位讀取文件“ /home/hrant/file1.txt”的內容。
正如fstat()
手冊頁所述:
int fstat(int fildes, struct stat *buf);
int stat(const char *path, struct stat *buf);
stat()
函數獲取有關路徑指向的文件的信息。 不需要對命名文件的讀取,寫入或執行權限,但是指向該文件的路徑名中列出的所有目錄都必須是可搜索的。
fstat()
獲取有關文件描述符fildes已知的打開文件的相同信息。
buf
參數是一個指向stat結構的指針,該結構由與文件有關的信息定義並放入其中。 統計資料結構定義為:
struct stat {
dev_t st_dev; /* device inode resides on */
ino_t st_ino; /* inode's number */
mode_t st_mode; /* inode protection mode */
nlink_t st_nlink; /* number of hard links to the file */
uid_t st_uid; /* user-id of owner */
gid_t st_gid; /* group-id of owner */
dev_t st_rdev; /* device type, for special file inode */
struct timespec st_atimespec; /* time of last access */
struct timespec st_mtimespec; /* time of last data modification */
struct timespec st_ctimespec; /* time of last file status change */
off_t st_size; /* file size, in bytes */
quad_t st_blocks; /* blocks allocated for file */
u_long st_blksize;/* optimal file sys I/O ops blocksize */
};
希望對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.