Whamcloud - gitweb
subst: work around an NFS bug
authorTheodore Ts'o <tytso@mit.edu>
Sat, 19 Sep 2015 01:37:53 +0000 (21:37 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Sat, 19 Sep 2015 01:37:53 +0000 (21:37 -0400)
When running on NFS, opening files with 0444 perms for writing can
sometimes fail.  This is arguably an NFS server bug, but work around
it by creating the file with 0644 permissions, and only change the
permissions to be 0444 right before we close the file.

URL: https://bugs.gentoo.org/550986
Reported-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
util/subst.c

index f36adb4..70dc0bc 100644 (file)
@@ -319,7 +319,7 @@ int main(int argc, char **argv)
 {
        char    line[2048];
        int     c;
-       int     fd;
+       int     fd, ofd = -1;
        FILE    *in, *out, *old = NULL;
        char    *outfn = NULL, *newfn = NULL;
        int     verbose = 0;
@@ -370,12 +370,12 @@ int main(int argc, char **argv)
                }
                strcpy(newfn, outfn);
                strcat(newfn, ".new");
-               fd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0444);
-               if (fd < 0) {
+               ofd = open(newfn, O_CREAT|O_TRUNC|O_RDWR, 0644);
+               if (ofd < 0) {
                        perror(newfn);
                        exit(1);
                }
-               out = fdopen(fd, "w+");
+               out = fdopen(ofd, "w+");
                if (!out) {
                        perror("fdopen");
                        exit(1);
@@ -429,12 +429,16 @@ int main(int argc, char **argv)
                                        printf("Using original atime\n");
                                set_utimes(outfn, fileno(old), tv);
                        }
+                       if (ofd >= 0)
+                               (void) fchmod(ofd, 0444);
                        fclose(out);
                        if (unlink(newfn) < 0)
                                perror("unlink");
                } else {
                        if (verbose)
                                printf("Creating or replacing %s.\n", outfn);
+                       if (ofd >= 0)
+                               (void) fchmod(ofd, 0444);
                        fclose(out);
                        if (old)
                                fclose(old);