summaryrefslogtreecommitdiff
path: root/usr.bin/cvs/buf.c
diff options
context:
space:
mode:
authorJoris Vink <joris@cvs.openbsd.org>2006-06-14 14:10:51 +0000
committerJoris Vink <joris@cvs.openbsd.org>2006-06-14 14:10:51 +0000
commit53c3e011c0e9b0a02a8c7ae04ba37fb4b2286574 (patch)
treef2297476a16d8c4c3f2b689b054e7d894174d98b /usr.bin/cvs/buf.c
parenta160bec77bb30d38663d833116b460595af2b9c8 (diff)
add cvs_buf_load_fd() which does the same as cvs_buf_load()
except it takes a decriptor as argument instead of a path. modified cvs_buf_load() to open the descriptor then pass it to cvs_buf_load_fd(). change all the calls to cvs_buf_load() that have a descriptor open for the path to cvs_buf_load_fd() to prevent races.
Diffstat (limited to 'usr.bin/cvs/buf.c')
-rw-r--r--usr.bin/cvs/buf.c35
1 files changed, 19 insertions, 16 deletions
diff --git a/usr.bin/cvs/buf.c b/usr.bin/cvs/buf.c
index 397f30760bf..92bf9ccb65d 100644
--- a/usr.bin/cvs/buf.c
+++ b/usr.bin/cvs/buf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buf.c,v 1.53 2006/05/29 04:47:28 joris Exp $ */
+/* $OpenBSD: buf.c,v 1.54 2006/06/14 14:10:50 joris Exp $ */
/*
* Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
* All rights reserved.
@@ -78,30 +78,35 @@ cvs_buf_alloc(size_t len, u_int flags)
return (b);
}
-/*
- * cvs_buf_load()
- *
- * Open the file specified by <path> and load all of its contents into a
- * buffer.
- * Returns the loaded buffer on success.
- */
BUF *
cvs_buf_load(const char *path, u_int flags)
{
int fd;
+ BUF *bp;
+
+ if ((fd = open(path, O_RDONLY, 0600)) == -1)
+ fatal("cvs_buf_load: failed to load '%s' : %s", path,
+ strerror(errno));
+
+ bp = cvs_buf_load_fd(fd, flags);
+ (void)close(fd);
+ return (bp);
+}
+
+BUF *
+cvs_buf_load_fd(int fd, u_int flags)
+{
ssize_t ret;
size_t len;
u_char *bp;
struct stat st;
BUF *buf;
- if ((fd = open(path, O_RDONLY, 0600)) == -1) {
- cvs_log(LP_ERR, "%s", path);
- return (NULL);
- }
-
if (fstat(fd, &st) == -1)
- fatal("cvs_buf_load: fstat: %s", strerror(errno));
+ fatal("cvs_buf_load_fd: fstat: %s", strerror(errno));
+
+ if (lseek(fd, 0, SEEK_SET) == -1)
+ fatal("cvs_buf_load_fd: lseek: %s", strerror(errno));
buf = cvs_buf_alloc(st.st_size, flags);
for (bp = buf->cb_cur; ; bp += (size_t)ret) {
@@ -115,8 +120,6 @@ cvs_buf_load(const char *path, u_int flags)
buf->cb_len += (size_t)ret;
}
- (void)close(fd);
-
return (buf);
}