Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有关文件新接口的buf大小的疑问 #632

Closed
chanchann opened this issue Nov 8, 2021 · 2 comments
Closed

有关文件新接口的buf大小的疑问 #632

chanchann opened this issue Nov 8, 2021 · 2 comments

Comments

@chanchann
Copy link
Contributor

chanchann commented Nov 8, 2021

Hello,请教一下

WFFileIOTask *WFTaskFactory::create_pwrite_task(const std::string& pathname, const void *buf, size_t count, off_t offset, fio_callback_t callback)

在tutorial里面,我们调用这个老文件接口的时候,

int fd = open(abs_path.c_str(), O_RDONLY);

size_t size = lseek(fd, 0, SEEK_END);
void *buf = malloc(size); /* As an example, assert(buf != NULL); */

我们通过lseek知道文件大小

在我们调用新接口的时候,如果说我要读入一个文件,传入文件名,此处buf大小是我们自己预估一个大小吗?

WFFileIOTask *WFTaskFactory::create_pread_task(const std::string& pathname, const void *buf, size_t count, off_t offset, fio_callback_t callback)
@Barenboim
Copy link
Contributor

没错,这两个接口除了一个以fd为参数,另一个以文件名为参数之外没有区别。其实我更建议用文件名的接口,除非你自己要复用fd来创建下一个任务。
如果有用文件名的接口,并且想读取整个文件,可以先调用操作系统的stat(2)系统调用得到文件的大小:

       #include <sys/types.h>
       #include <sys/stat.h>
       #include <unistd.h>

       int stat(const char *path, struct stat *buf);

       struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

然后根据这个文件大小来分配内存。剩下的就和示例里没什么区别了。

@chanchann
Copy link
Contributor Author

好的,非常感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants