summaryrefslogtreecommitdiff
path: root/sponge.c
diff options
context:
space:
mode:
authorJoey Hess <joey@kodama.kitenet.net>2008-04-11 19:11:33 -0400
committerJoey Hess <joey@kodama.kitenet.net>2008-04-11 19:11:33 -0400
commitc525af9d17aacb226d72495d1bd401ce001b03f6 (patch)
tree115bb4ce35bee5297fe9599500c240254eafab8b /sponge.c
parentb6d635bc28bf7d94201bcd167905cebc48abf7cb (diff)
downloadmoreutils-c525af9d17aacb226d72495d1bd401ce001b03f6.tar.gz
fix mode of new file renamed from temp file
Diffstat (limited to 'sponge.c')
-rw-r--r--sponge.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/sponge.c b/sponge.c
index 0dd676a..80733a2 100644
--- 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);
}