aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Paul Weber <singpolyma@singpolyma.net>2014-10-18 11:36:09 -0500
committerMarc André Tanner <mat@brain-dump.org>2014-10-19 12:48:55 +0200
commitb1b73eac95a08d5284d21b43e2d4c1ec9cecd5da (patch)
tree86d6fbe5db4c29454dbe3ea67defbf75829c0951
parent27195e0b6f135d87629cd6c3f4e88fcb18487a8f (diff)
downloadvis-b1b73eac95a08d5284d21b43e2d4c1ec9cecd5da.tar.gz
vis-b1b73eac95a08d5284d21b43e2d4c1ec9cecd5da.tar.xz
Preserve file permissions when saving
-rw-r--r--text.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/text.c b/text.c
index 235c98c..6a213bf 100644
--- a/text.c
+++ b/text.c
@@ -601,15 +601,22 @@ size_t text_redo(Text *txt) {
* using rename(2).
*/
int text_save(Text *txt, const char *filename) {
+ int fd = -1;
size_t len = strlen(filename) + 10;
char *tmpname = malloc(len);
if (!tmpname)
return -1;
snprintf(tmpname, len, "%s~", filename);
- // TODO file ownership, permissions etc
+ // TODO preserve user/group
+ struct stat meta;
+ if (stat(filename, &meta) == -1) {
+ if (errno == ENOENT)
+ meta.st_mode = S_IRUSR|S_IWUSR;
+ else
+ goto err;
+ }
/* O_RDWR is needed because otherwise we can't map with MAP_SHARED */
- int fd = open(tmpname, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
- if (fd == -1)
+ if ((fd = open(tmpname, O_CREAT|O_RDWR|O_TRUNC, meta.st_mode)) == -1)
goto err;
if (ftruncate(fd, txt->size) == -1)
goto err;