aboutsummaryrefslogtreecommitdiffstats
path: root/copier.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2021-09-04 11:41:21 +0200
committerMattias Andrée <maandree@kth.se>2021-09-04 11:41:38 +0200
commitfc2201769e850bca4b718305a1f8e776f5d0786b (patch)
treee37090b9b1cde35a208604e45df6efb0a68feb41 /copier.c
parentCommunicate whether file transfers were completed (diff)
downloadeditasroot-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 'copier.c')
-rw-r--r--copier.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/copier.c b/copier.c
index 3e7a6c6..5488f03 100644
--- a/copier.c
+++ b/copier.c
@@ -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));