]> git.cameronkatri.com Git - pw-darwin.git/commitdiff
If (flags & O_TRUNC), don't truncate the file until we've successfully
authorDag-Erling Smørgrav <des@FreeBSD.org>
Wed, 23 May 2007 08:12:34 +0000 (08:12 +0000)
committerDag-Erling Smørgrav <des@FreeBSD.org>
Wed, 23 May 2007 08:12:34 +0000 (08:12 +0000)
locked it.

MFC after: 3 weeks

libutil/flopen.c

index 687ebac5ed92d4f55d651626ddf855c2c6342e25..92254cc1c3cf51b577e925f561532721d635b091 100644 (file)
@@ -41,7 +41,7 @@ __FBSDID("$FreeBSD$");
 int
 flopen(const char *path, int flags, ...)
 {
-       int fd, operation, serrno;
+       int fd, operation, serrno, truncate;
        struct stat sb, fsb;
        mode_t mode;
 
@@ -62,6 +62,9 @@ flopen(const char *path, int flags, ...)
        if (flags & O_NONBLOCK)
                operation |= LOCK_NB;
 
+       truncate = (flags & O_TRUNC);
+       flags |= ~O_TRUNC;
+
        for (;;) {
                if ((fd = open(path, flags, mode)) == -1)
                        /* non-existent or no access */
@@ -91,6 +94,13 @@ flopen(const char *path, int flags, ...)
                        close(fd);
                        continue;
                }
+               if (truncate && ftruncate(fd, 0) != 0) {
+                       /* can't happen [tm] */
+                       serrno = errno;
+                       close(fd);
+                       errno = serrno;
+                       return (-1);
+               }
                return (fd);
        }
 }