diff options
author | Mattias Andrée <maandree@kth.se> | 2021-09-04 11:41:21 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-09-04 11:41:38 +0200 |
commit | fc2201769e850bca4b718305a1f8e776f5d0786b (patch) | |
tree | e37090b9b1cde35a208604e45df6efb0a68feb41 /copier.c | |
parent | Communicate whether file transfers were completed (diff) | |
download | editasroot-fc2201769e850bca4b718305a1f8e776f5d0786b.tar.gz editasroot-fc2201769e850bca4b718305a1f8e776f5d0786b.tar.bz2 editasroot-fc2201769e850bca4b718305a1f8e776f5d0786b.tar.xz |
Use MSG_PEEK instead of sending unnecessary data
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | copier.c | 12 |
1 files changed, 9 insertions, 3 deletions
@@ -22,6 +22,7 @@ main(int argc, char *argv[]) path = argv[0]; parentfd = atoi(argv[1]); + /* Send content of file to parent */ filefd = open(path, O_RDWR); /* O_RDWR establishes that we can indeed write to the file */ if (filefd < 0) { fprintf(stderr, "%s: open %s O_RDWR: %s\n", argv0, path, strerror(errno)); @@ -37,13 +38,18 @@ main(int argc, char *argv[]) exit(1); } - r = read(parentfd, &b, 1); - if (r != 1) { - if (r) + /* Wait for parent to start sending the new file content, + * so that O_TRUNC does not cause the file to be truncated + * without new content being sent in case the parent exited + * abnormally. */ + r = recv(parentfd, &b, 1, MSG_PEEK); + if (r <= 0) { + if (r < 0) fprintf(stderr, "%s: read %s: %s\n", argv0, path, strerror(errno)); exit(1); } + /* Rewrite file with content sent from parent */ filefd = open(path, O_WRONLY | O_TRUNC); if (filefd < 0) { fprintf(stderr, "%s: open %s O_WRONLY|O_TRUNC: %s\n", argv0, path, strerror(errno)); |