From 4e505a8eccafe3defaf0e491ec6c347ee0e87830 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sun, 28 Feb 2016 18:49:06 +0100 Subject: sfeed_frames: fix open file permission (write only). check errno EEXIST (file exist? -> ignore), handle other errno codes as errors. ... also make sure to fflush write before modifying file access and modification timestamps or it will be overwritten again. --- sfeed_frames.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/sfeed_frames.c b/sfeed_frames.c index 673b6dc..3411187 100644 --- a/sfeed_frames.c +++ b/sfeed_frames.c @@ -155,11 +155,11 @@ printfeed(FILE *fpitems, FILE *fpin, struct feed *f) strtotime(fields[FieldUnixTimestamp], &parsedtime); /* content file doesn't exist yet and has write access */ - if ((fd = open(filepath, O_CREAT | O_EXCL)) == -1) { - if (errno == EACCES) + if ((fd = open(filepath, O_CREAT | O_EXCL | O_WRONLY)) == -1) { + if (errno != EEXIST) err(1, "open: %s", filepath); } else { - if (!(fpcontent = fdopen(fd, "w+b"))) + if (!(fpcontent = fdopen(fd, "wb"))) err(1, "fdopen: %s", filepath); fputs("" "" @@ -185,6 +185,11 @@ printfeed(FILE *fpitems, FILE *fpin, struct feed *f) /* set modified and access time of file to time of item. */ if (parsedtime) { + /* flush writes before setting atime and mtime + else the remaining (buffered) write can occur at + fclose() and overwrite our time again. */ + fflush(fpcontent); + times[0].tv_sec = parsedtime; times[1].tv_sec = parsedtime; -- cgit v1.2.3