sendfile64只复制约2GB

Ste*_*Ste 5 c c++ linux sendfile

我需要使用sendfile64来复制大约16GB的文件.到目前为止我取得的成就是

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/sendfile.h>
#include <sys/stat.h>

int main (int argc, char** argv)
{
  long long src;
  long long dest;
  struct stat64 stat_buf;
  off64_t offset = 0LL;
  long long rc;

  if (argc != 3) {
    fprintf(stderr, "usage: %s <source> <destination>\n", argv[0]);
    exit(1);
  }

  src = open64(argv[1], O_RDONLY);
  if (src == -1) {
    fprintf(stderr, "unable to open '%s': %s\n", argv[1], strerror(errno));
    exit(1);
  }

  fstat64(src, &stat_buf);

  dest = open64(argv[2], O_WRONLY|O_CREAT, stat_buf.st_mode);
  if (dest == -1) {
    fprintf(stderr, "unable to open '%s': %s\n", argv[2], strerror(errno));
    exit(1);
  }

 /* copy file using sendfile */
 rc = sendfile64 (dest, src, &offset, stat_buf.st_size);
 if (rc == -1) {
    fprintf(stderr, "error from sendfile: %s\n", strerror(errno));
    exit(1);
 }
 if (rc != stat_buf.st_size) {
   fprintf(stderr, "incomplete transfer from sendfile: %lld of %lld bytes\n",
           rc,
           (long long)stat_buf.st_size);
   exit(1);
 }

 /* clean up and exit */
 close(dest);
 close(src);

 return 0;
}
Run Code Online (Sandbox Code Playgroud)

我已编译使用

g++ BigCopy2.cpp -o BigCopy2 -D_FILE_OFFSET_BITS=64 -DLARGEFILE64_SOURCE
Run Code Online (Sandbox Code Playgroud)

问题是我仍然无法复制超过2GB的文件.

有人能指出我的错误在哪里?

nos*_*nos 8

您应该使用循环来复制它,sendfile()可能由于各种原因,不能通过一次调用复制所有数据.正如janneb所指出的,sendfile64的返回值是ssize_t,所以我们不应该将超过SSIZE_MAX传递给sendfile,而且sendfile的最后一个参数是size_t,它在32位平台上是32位.

 /* copy file using sendfile */
while (offset < stat_buf.st_size) {
  size_t count;
  off64_t remaining = stat_buf.st_size- offset;
  if (remaining > SSIZE_MAX)
      count = SSIZE_MAX;
   else 
      count = remaining;
  rc = sendfile64 (dest, src, &offset, count);
  if (rc == 0) {
     break;
  }
  if (rc == -1) {
     fprintf(stderr, "error from sendfile: %s\n", strerror(errno));
     exit(1);
  }
}

 if (offset != stat_buf.st_size) {
   fprintf(stderr, "incomplete transfer from sendfile: %lld of %lld bytes\n",
           rc,
           (long long)stat_buf.st_size);
   exit(1);
 }
Run Code Online (Sandbox Code Playgroud)

请注意,您可以使用off_t,stat,sendfile替换所有64位变体off64_t,stat64,sendfile64.只要你有-D_FILE_OFFSET_BITS=64标志,那个定义就会做正确的事情并将off_t转换为off64_t,sendfile转换为sendfile64,依此类推,如果那些类型和函数还不是64位(例如32位架构).

  • 不仅是size_t类型的最后一个参数,而且返回值的类型为ssize_t,其最大值小于size_t.因此,您需要一次传输最多SSIZE_MAX个字节,以便正确跟踪每个呼叫实际传输的数量. (2认同)