From f467f59897f330d2a493e919108e62dffaa1f736 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 4 Sep 2021 11:38:03 +0200 Subject: Communicate whether file transfers were completed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- copier.c | 46 +++++++++------------------------------------- 1 file changed, 9 insertions(+), 37 deletions(-) (limited to 'copier.c') diff --git a/copier.c b/copier.c index 231e515..3e7a6c6 100644 --- a/copier.c +++ b/copier.c @@ -1,46 +1,14 @@ /* See LICENSE file for copyright and license details. */ -#include -#include -#include -#include -#include -#include -#include +#include "common.h" -static const char *argv0 = "editasroot/copier"; - - -static void -copy_file(int destfd, const char *destfname, int srcfd, const char *srcfname) -{ - char buf[BUFSIZ]; - ssize_t r, w, p; - - for (;;) { - r = read(srcfd, buf, sizeof(buf)); - if (r <= 0) { - if (!r) - break; - fprintf(stderr, "%s: read %s: %s", argv0, srcfname, strerror(errno)); - exit(1); - } - - for (p = 0; p < r; p += w) { - w = write(destfd, buf, (size_t)(r - p)); - if (r <= 0) { - fprintf(stderr, "%s: write %s: %s", argv0, destfname, strerror(errno)); - exit(1); - } - } - } -} +const char *argv0 = "editasroot/copier"; int main(int argc, char *argv[]) { - int filefd, parentfd; + int filefd, parentfd, ok; const char *path; ssize_t r; char b; @@ -59,7 +27,7 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: open %s O_RDWR: %s\n", argv0, path, strerror(errno)); exit(1); } - copy_file(parentfd, "", filefd, path); + copy_file(parentfd, "", filefd, path, NULL); if (close(filefd)) { fprintf(stderr, "%s: read %s: %s\n", argv0, path, strerror(errno)); exit(1); @@ -81,7 +49,7 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: open %s O_WRONLY|O_TRUNC: %s\n", argv0, path, strerror(errno)); exit(1); } - copy_file(filefd, path, parentfd, ""); + copy_file(filefd, path, parentfd, "", &ok); if (close(filefd)) { fprintf(stderr, "%s: write %s: %s\n", argv0, path, strerror(errno)); exit(1); @@ -90,6 +58,10 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: read : %s\n", argv0, strerror(errno)); exit(1); } + if (!ok) { + fprintf(stderr, "%s: parent exited before sending file termination message, file may be truncated\n", argv0); + exit(1); + } return 0; } -- cgit v1.2.3-70-g09d2