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
。