summaryrefslogtreecommitdiffstats
path: root/lib/libutil
diff options
context:
space:
mode:
authordes <des@FreeBSD.org>2007-05-23 08:12:34 +0000
committerdes <des@FreeBSD.org>2007-05-23 08:12:34 +0000
commit253a9fb8b6b9af702912dffa3b9addcd79d4618b (patch)
treef4f427c3ee13f1f389149e46b5bcff5076b239d3 /lib/libutil
parentba07e2634f220052936b4039804dcd5822dc556e (diff)
downloadFreeBSD-src-253a9fb8b6b9af702912dffa3b9addcd79d4618b.zip
FreeBSD-src-253a9fb8b6b9af702912dffa3b9addcd79d4618b.tar.gz
If (flags & O_TRUNC), don't truncate the file until we've successfully
locked it. MFC after: 3 weeks
Diffstat (limited to 'lib/libutil')
-rw-r--r--lib/libutil/flopen.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/lib/libutil/flopen.c b/lib/libutil/flopen.c
index 687ebac..92254cc 100644
--- a/lib/libutil/flopen.c
+++ b/lib/libutil/flopen.c
@@ -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);
}
}
OpenPOWER on IntegriCloud