7 c linux pipe splice zero-copy
这是关于splice()的另一个问题.我希望用它来复制文件,我试图使用两个拼接调用,通过像splice维基百科页面上的例子一样的管道连接.我写了一个简单的测试用例,它只试图从一个文件读取前32K字节并将它们写入另一个文件:
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main(int argc, char **argv) {
int pipefd[2];
int result;
FILE *in_file;
FILE *out_file;
result = pipe(pipefd);
in_file = fopen(argv[1], "rb");
out_file = fopen(argv[2], "wb");
result = splice(fileno(in_file), 0, pipefd[1], NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE);
printf("%d\n", result);
result = splice(pipefd[0], NULL, fileno(out_file), 0, 32768, SPLICE_F_MORE | SPLICE_F_MOVE);
printf("%d\n", result);
if (result == -1)
printf("%d - %s\n", errno, strerror(errno));
close(pipefd[0]);
close(pipefd[1]);
fclose(in_file);
fclose(out_file);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,输入文件似乎正确读取,但第二次拼接调用失败了EINVAL.谁知道我在这里做错了什么?
谢谢!
从拼接手册页:
EINVAL Target file system doesn't support splicing; target file is
opened in append mode; neither of the descriptors refers to a
pipe; or offset given for non-seekable device.
Run Code Online (Sandbox Code Playgroud)
我们知道描述符之一是管道,并且文件不是以追加模式打开的。我们还知道没有给出偏移量(0相当于NULL- 您的意思是传递一个指向零偏移量的指针吗?),所以这不是问题。因此,您使用的文件系统不支持文件拼接。
您要复制到哪种文件系统或从哪种文件系统复制?
当两个文件都在 ext3 上时,您的示例在我的系统上运行,但当我使用外部驱动器时失败(我立即忘记了它是 DOS 还是 NTFS)。我的猜测是您的一个或两个文件位于 splice 不支持的文件系统上。