From 4352f54c11314ec7c4670095d6aa3d977832355f Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Fri, 1 Mar 2024 22:59:23 +0100 Subject: [PATCH] [scan] Use copy_file_range for Linux/FreeBSD file copy FreeBSD doesn't seem to actually have copyfile.h --- src/library/filescanner_ffmpeg.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/library/filescanner_ffmpeg.c b/src/library/filescanner_ffmpeg.c index dadec411..6918ea24 100644 --- a/src/library/filescanner_ffmpeg.c +++ b/src/library/filescanner_ffmpeg.c @@ -33,10 +33,8 @@ // For file copy #include -#if defined(__APPLE__) || defined(__FreeBSD__) +#if defined(__APPLE__) #include -#else -#include #endif #include @@ -812,14 +810,20 @@ static int fast_copy(int fd_dst, int fd_src) { // Here we use kernel-space copying for performance reasons -#if defined(__APPLE__) || defined(__FreeBSD__) +#if defined(__APPLE__) // fcopyfile works on FreeBSD and OS X 10.5+ return fcopyfile(fd_src, fd_dst, 0, COPYFILE_ALL); #else // sendfile will work with non-socket output (i.e. regular file) on Linux 2.6.33+ struct stat fileinfo = { 0 }; + ssize_t bytes_copied; + fstat(fd_src, &fileinfo); - return sendfile(fd_dst, fd_src, NULL, fileinfo.st_size); + bytes_copied = copy_file_range(fd_src, NULL, fd_dst, NULL, fileinfo.st_size, 0); + if (bytes_copied < 0 || bytes_copied != fileinfo.st_size) + return -1; + + return 0; #endif }