commit: 11eabcaa6061cbc016498037c576d9d52373a141
parent ffa5bb3da37734168c867ac5780b34fe099abff7
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 15 Aug 2024 21:41:12 +0200
lib/fs: make manual_file_copy and auto_file_copy return written bytes
Diffstat:
2 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/lib/fs.c b/lib/fs.c
@@ -47,9 +47,10 @@ path_split_static(char *path, bool trim)
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
-int
+ssize_t
manual_file_copy(int fd_in, int fd_out, off_t len, int flags)
{
+ ssize_t wrote = 0;
do
{
char buf[BUFSIZ];
@@ -61,15 +62,17 @@ manual_file_copy(int fd_in, int fd_out, off_t len, int flags)
if(nwrite < 0) return nwrite;
len -= nwrite;
+ wrote += nwrite;
} while(len > 0);
- return 0;
+ return wrote;
}
#ifdef HAS_COPY_FILE_RANGE
-int
+ssize_t
auto_file_copy(int fd_in, int fd_out, off_t len, int flags)
{
+ ssize_t wrote = 0;
off_t ret = -1;
do
{
@@ -85,8 +88,9 @@ auto_file_copy(int fd_in, int fd_out, off_t len, int flags)
return ret;
}
len -= ret;
+ wrote += ret;
} while(len > 0 && ret > 0);
- return 0;
+ return wrote;
}
#endif
diff --git a/lib/fs.h b/lib/fs.h
@@ -14,11 +14,11 @@ char *static_basename(char *path);
char *path_split_static(char *path, bool trim);
// Copy data from fd_in to fd_out, with a max of len (flag is ignored for now)
-int manual_file_copy(int fd_in, int fd_out, off_t len, int flags);
+ssize_t manual_file_copy(int fd_in, int fd_out, off_t len, int flags);
#ifndef HAS_COPY_FILE_RANGE
#define auto_file_copy manual_file_copy
#else
// Copy data with copy_file_range(2) for <len> amount of data, with fallback to manual_file_copy when cross-device
-int auto_file_copy(int fd_in, int fd_out, off_t len, int flags);
+ssize_t auto_file_copy(int fd_in, int fd_out, off_t len, int flags);
#endif