]> err.no Git - varnish/commitdiff
From FreeBSD: if (flags & O_TRUNC), don't truncate the file until we've
authordes <des@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Wed, 23 May 2007 08:14:11 +0000 (08:14 +0000)
committerdes <des@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Wed, 23 May 2007 08:14:11 +0000 (08:14 +0000)
successfully locked it.

git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1467 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/lib/libvarnish/flopen.c

index 009d898af9769e8b484235b6a0c042853991c2f3..d0c03f7c2089dc967f937754cab4c32a198605a7 100644 (file)
@@ -26,7 +26,7 @@
  *
  * $Id$
  * Derived from:
- * $FreeBSD: src/lib/libutil/flopen.c,v 1.4 2007/05/10 15:01:42 des Exp $
+ * $FreeBSD: src/lib/libutil/flopen.c,v 1.5 2007/05/23 08:12:34 des Exp $
  */
 
 #include <sys/file.h>
@@ -42,7 +42,7 @@
 int
 flopen(const char *path, int flags, ...)
 {
-       int fd, operation, serrno;
+       int fd, operation, serrno, truncate;
        struct stat sb, fsb;
        mode_t mode;
 
@@ -63,6 +63,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 */
@@ -92,6 +95,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);
        }
 }