]> err.no Git - moreutils/commitdiff
fix mode of new file renamed from temp file
authorJoey Hess <joey@kodama.kitenet.net>
Fri, 11 Apr 2008 23:11:33 +0000 (19:11 -0400)
committerJoey Hess <joey@kodama.kitenet.net>
Fri, 11 Apr 2008 23:11:33 +0000 (19:11 -0400)
sponge.c

index 0dd676aa1d8dc0d04cab50cc36c1b676e76ccd12..80733a22387f8290b77434f04a6a5dae099cc6b9 100644 (file)
--- a/sponge.c
+++ b/sponge.c
@@ -318,9 +318,19 @@ int main (int argc, char **argv) {
                              ! S_ISLNK(statbuf.st_mode)
                             ) || errno == ENOENT) &&
                            rename(tmpname, outname) == 0) {
-                               /* Fix renamed file mode. */
-                               if (errno != ENOENT &&
-                                   chmod(outname, statbuf.st_mode) != 0) {
+                               /* Fix renamed file mode to match either
+                                * the old file mode, or the default file
+                                * mode for a newly created file. */
+                               mode_t mode;
+                               if (errno != ENOENT) {
+                                       mode = statbuf.st_mode;
+                               }
+                               else {
+                                       mode_t mask = umask(0);
+                                       umask(mask);
+                                       mode = 0666 & ~mask;
+                               }
+                               if (chmod(outname, mode) != 0) {
                                        perror("chmod");
                                        exit(1);
                                }