我如何使用Linux拼接()函数将文件复制到另一个文件? [英] How can I use Linux's splice() function to copy a file to another file?

查看:247
本文介绍了我如何使用Linux拼接()函数将文件复制到另一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是关于拼接另一个问题()。我希望用它来复制文件,并正尝试使用由像剪接的维基百科页面上的例子管道连接的两个接头电话。我写这只是试图从一个文件中读取第一32K字节,并将其写入到另一个简单的测试用例:

here's another question about splice(). I'm hoping to use it to copy files, and am trying to use two splice calls joined by a pipe like the example on splice's Wikipedia page. I wrote a simple test case which only tries to read the first 32K bytes from one file and write them to another:

#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;
}

当我运行它,输入文件似乎被正确读取,但第二拼接调用失败EINVAL。任何人都知道我在做什么错在这里?

When I run this, the input file seems to be read properly, but the second splice call fails with EINVAL. Anybody know what I'm doing wrong here?

谢谢!

推荐答案

,您在复制到/什么样的文件系统(S)的?

What kind of file system(s) are you copying to/from?

您例如我的系统上运行时,这两个文件都在EXT3,但未能当我使用一个外部驱动器(我忘了副手,如果它是DOS或NTFS)。我的猜测是,你的文件的一个或两个是拼接不支持的文件系统上。

Your example runs on my system when both files are on ext3 but fails when I use an external drive (I forget offhand if it is DOS or NTFS). My guess is that one or both of your files are on a file system that splice does not support.

这篇关于我如何使用Linux拼接()函数将文件复制到另一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆