我的程序打开一个文件,想要为它设置O_DIRECT。但程序假定块大小为512。所以,我需要检查一下那个尺码。man 2 open建议这样做,ioctl(filesystem_fd, BLKSSZGET, &block_size)。
我的问题是:如何从普通文件的fd中获取此filesystem_fd?
发布于 2020-10-07 22:59:59
你不需要这样做。如果想知道文件所在文件系统的块大小,只需在st_blksize中查找即可。这就是:
#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;
}https://stackoverflow.com/questions/64241149
复制相似问题