繁体   English   中英

从该设备上文件的名称/描述符获取存储设备块大小

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM