Commit 14451ed3 authored by Pavel Emelyanov's avatar Pavel Emelyanov

util: Add fn for copying file from one fd to another

Will be required to dump completely unlinked files.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 09d95980
......@@ -275,4 +275,6 @@ static inline dev_t kdev_to_odev(u32 kdev)
return (kdev_major(kdev) << 8) | kdev_minor(kdev);
}
int copy_file(int fd_in, int fd_out, size_t bytes);
#endif /* UTIL_H_ */
......@@ -12,7 +12,7 @@
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <sys/sendfile.h>
#include <fcntl.h>
#include <sys/param.h>
......@@ -270,3 +270,32 @@ int get_service_fd(int type)
return rlimit.rlim_cur - type;
}
int copy_file(int fd_in, int fd_out, size_t bytes)
{
ssize_t written = 0;
size_t chunk = bytes ? bytes : 4096;
while (1) {
ssize_t ret;
ret = sendfile(fd_out, fd_in, NULL, chunk);
if (ret < 0) {
pr_perror("Can't send data to ghost file");
return -1;
}
if (ret == 0) {
if (bytes && (written != bytes)) {
pr_err("Ghost file size mismatch %lu/%lu\n",
written, bytes);
return -1;
}
break;
}
written += ret;
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment