1

sendfile()コピープログラムを実装するために使用しようとしています。

ただし、ディレクトリをコピーしようとすると失敗しました。ディレクトリはLinuxの特別なファイルタイプではありませんか?

ここに私が今使っているコードがあります。StackOverflow からの別の回答からコピーされます。

int copy_file(const char *to, const char *from) {
    int read_fd; int write_fd;
    struct stat stat_buf;
    off_t offset = 0;
    /* Open the input file. */
    read_fd = open(from, O_RDONLY);
    /* Stat the input file to obtain its size. */
    fstat (read_fd, &stat_buf);
    /* Open the output file for writing, with the same permissions as the source file. */
    write_fd = open(to, O_WRONLY | O_CREAT, stat_buf.st_mode);
    /* Blast the bytes from one file to the other. */
    int err = sendfile(write_fd, read_fd, &offset, stat_buf.st_size);
    /* Close up. */
    close (read_fd);
    close (write_fd);
    return err;
}

追記中

私が得た戻り値は -1 です。そして、パスを持つディレクトリではなくファイルを取得しました。to

Ubuntu 12.04、64ビットを使用しています。

の出力はuname -rです3.11.0-20-generic

4

2 に答える 2

0

Linux での copy コマンドの実装を次に示します。これに従ってください。

http://cboard.cprogramming.com/c-programming/143382-implementation-linux-cp-copy-command-c-language.html

于 2014-04-21T06:00:53.927 に答える