[英]filesystem fd from file fd
My program opens a file and wants to set O_DIRECT
for it.我的程序打开一个文件并希望为其设置O_DIRECT
。 But the program assumes block size of 512. So, I need to check for that size.但程序假定块大小为 512。因此,我需要检查该大小。 man 2 open
suggests to do it like this ioctl(filesystem_fd, BLKSSZGET, &block_size)
. man 2 open
建议这样做ioctl(filesystem_fd, BLKSSZGET, &block_size)
。
And my question is: how to get this filesystem_fd
from fd
of an ordinary file?我的问题是:如何从普通文件的fd
中获取这个filesystem_fd
?
You don't need to do that.你不需要这样做。 If you want to know the block size of the filesystem in which your file resides, just look in st_blksize.如果您想知道文件所在文件系统的块大小,只需查看 st_blksize。 That is:那是:
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char **argv)
{
struct stat sb;
char *name = argc > 1 ? argv[1] : argv[0];
if( stat(name, &sb) == -1 ) {
perror(name);
return EXIT_FAILURE;
}
printf("Block size: %d\n", (int)sb.st_blksize);
return EXIT_SUCCESS;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.