diff options
author | Joris Vink <joris@cvs.openbsd.org> | 2006-06-14 14:10:51 +0000 |
---|---|---|
committer | Joris Vink <joris@cvs.openbsd.org> | 2006-06-14 14:10:51 +0000 |
commit | 53c3e011c0e9b0a02a8c7ae04ba37fb4b2286574 (patch) | |
tree | f2297476a16d8c4c3f2b689b054e7d894174d98b | |
parent | a160bec77bb30d38663d833116b460595af2b9c8 (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.
-rw-r--r-- | usr.bin/cvs/buf.c | 35 | ||||
-rw-r--r-- | usr.bin/cvs/buf.h | 3 | ||||
-rw-r--r-- | usr.bin/cvs/commit.c | 6 | ||||
-rw-r--r-- | usr.bin/cvs/diff.h | 4 | ||||
-rw-r--r-- | usr.bin/cvs/diff3.c | 9 | ||||
-rw-r--r-- | usr.bin/cvs/file.c | 5 | ||||
-rw-r--r-- | usr.bin/cvs/import.c | 10 | ||||
-rw-r--r-- | usr.bin/cvs/update.c | 6 |
8 files changed, 40 insertions, 38 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); } diff --git a/usr.bin/cvs/buf.h b/usr.bin/cvs/buf.h index 708afc77423..d56eb2963e2 100644 --- a/usr.bin/cvs/buf.h +++ b/usr.bin/cvs/buf.h @@ -1,4 +1,4 @@ -/* $OpenBSD: buf.h,v 1.16 2006/05/28 22:43:55 joris Exp $ */ +/* $OpenBSD: buf.h,v 1.17 2006/06/14 14:10:50 joris Exp $ */ /* * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org> * All rights reserved. @@ -34,6 +34,7 @@ typedef struct cvs_buf BUF; BUF *cvs_buf_alloc(size_t, u_int); BUF *cvs_buf_load(const char *, u_int); +BUF *cvs_buf_load_fd(int, u_int); void cvs_buf_free(BUF *); void *cvs_buf_release(BUF *); u_char cvs_buf_getc(BUF *, size_t); diff --git a/usr.bin/cvs/commit.c b/usr.bin/cvs/commit.c index bbf588fefb4..d2021d87b9f 100644 --- a/usr.bin/cvs/commit.c +++ b/usr.bin/cvs/commit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: commit.c,v 1.72 2006/06/13 06:51:32 joris Exp $ */ +/* $OpenBSD: commit.c,v 1.73 2006/06/14 14:10:50 joris Exp $ */ /* * Copyright (c) 2006 Joris Vink <joris@openbsd.org> * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org> @@ -245,7 +245,7 @@ cvs_commit_local(struct cvs_file *cf) if (b == NULL) fatal("cvs_commit_local: failed to get HEAD"); } else { - if ((b = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL) + if ((b = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL) fatal("cvs_commit_local: failed to load file"); } @@ -347,7 +347,7 @@ commit_diff_file(struct cvs_file *cf) if (cf->file_status == FILE_MODIFIED || cf->file_status == FILE_ADDED) { - if ((b1 = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL) + if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL) fatal("commit_diff_file: failed to load '%s'", cf->file_path); } else { diff --git a/usr.bin/cvs/diff.h b/usr.bin/cvs/diff.h index 686ececbf2e..6f938614704 100644 --- a/usr.bin/cvs/diff.h +++ b/usr.bin/cvs/diff.h @@ -1,4 +1,4 @@ -/* $OpenBSD: diff.h,v 1.12 2006/05/31 22:24:12 joris Exp $ */ +/* $OpenBSD: diff.h,v 1.13 2006/06/14 14:10:50 joris Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. * All rights reserved. @@ -91,7 +91,7 @@ #define D_SKIPPED1 8 /* path1 was a special file */ #define D_SKIPPED2 9 /* path2 was a special file */ -BUF *cvs_diff3(RCSFILE *, char *, RCSNUM *, RCSNUM *, int); +BUF *cvs_diff3(RCSFILE *, char *, int, RCSNUM *, RCSNUM *, int); void diff_output(const char *, ...); int cvs_diffreg(const char *, const char *, BUF *out); int ed_patch_lines(struct cvs_lines *, struct cvs_lines *); diff --git a/usr.bin/cvs/diff3.c b/usr.bin/cvs/diff3.c index 16a7657fd7d..8a16d4cffc3 100644 --- a/usr.bin/cvs/diff3.c +++ b/usr.bin/cvs/diff3.c @@ -1,4 +1,4 @@ -/* $OpenBSD: diff3.c,v 1.24 2006/05/27 03:30:30 joris Exp $ */ +/* $OpenBSD: diff3.c,v 1.25 2006/06/14 14:10:50 joris Exp $ */ /* * Copyright (C) Caldera International Inc. 2001-2002. @@ -72,7 +72,7 @@ static const char copyright[] = #ifndef lint static const char rcsid[] = - "$OpenBSD: diff3.c,v 1.24 2006/05/27 03:30:30 joris Exp $"; + "$OpenBSD: diff3.c,v 1.25 2006/06/14 14:10:50 joris Exp $"; #endif /* not lint */ #include "includes.h" @@ -155,7 +155,8 @@ static int diff3_internal(int, char **, const char *, const char *); int diff3_conflicts = 0; BUF * -cvs_diff3(RCSFILE *rf, char *workfile, RCSNUM *rev1, RCSNUM *rev2, int verbose) +cvs_diff3(RCSFILE *rf, char *workfile, int workfd, RCSNUM *rev1, + RCSNUM *rev2, int verbose) { int argc; char *data, *patch; @@ -169,7 +170,7 @@ cvs_diff3(RCSFILE *rf, char *workfile, RCSNUM *rev1, RCSNUM *rev2, int verbose) rcsnum_tostr(rev1, r1, sizeof(r1)); rcsnum_tostr(rev2, r2, sizeof(r2)); - if ((b1 = cvs_buf_load(workfile, BUF_AUTOEXT)) == NULL) + if ((b1 = cvs_buf_load_fd(workfd, BUF_AUTOEXT)) == NULL) goto out; if (verbose == 1) diff --git a/usr.bin/cvs/file.c b/usr.bin/cvs/file.c index b679e3466b0..6fb4a065093 100644 --- a/usr.bin/cvs/file.c +++ b/usr.bin/cvs/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.158 2006/06/03 19:07:13 joris Exp $ */ +/* $OpenBSD: file.c,v 1.159 2006/06/14 14:10:50 joris Exp $ */ /* * Copyright (c) 2006 Joris Vink <joris@openbsd.org> * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org> @@ -674,8 +674,7 @@ cvs_file_classify(struct cvs_file *cf, const char *tag, int loud) b1 = rcs_kwexp_buf(b1, cf->file_rcs, cf->file_rcsrev); - /* XXX */ - b2 = cvs_buf_load(cf->file_path, BUF_AUTOEXT); + b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT); if (b2 == NULL) fatal("failed to get file content for comparison"); diff --git a/usr.bin/cvs/import.c b/usr.bin/cvs/import.c index c965fd235cd..c266a8eaf3f 100644 --- a/usr.bin/cvs/import.c +++ b/usr.bin/cvs/import.c @@ -1,4 +1,4 @@ -/* $OpenBSD: import.c,v 1.49 2006/06/11 17:50:11 joris Exp $ */ +/* $OpenBSD: import.c,v 1.50 2006/06/14 14:10:50 joris Exp $ */ /* * Copyright (c) 2006 Joris Vink <joris@openbsd.org> * @@ -179,7 +179,7 @@ import_new(struct cvs_file *cf) if ((branch = rcsnum_parse(import_branch)) == NULL) fatal("import_new: failed to parse branch"); - if ((bp = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL) + if ((bp = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL) fatal("import_new: failed to load %s", cf->file_path); cvs_buf_putc(bp, '\0'); @@ -249,8 +249,7 @@ import_update(struct cvs_file *cf) if ((b1 = rcs_getrev(cf->file_rcs, rev)) == NULL) fatal("import_update: failed to grab revision"); - /* XXX */ - if ((b2 = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL) + if ((b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL) fatal("import_update: failed to load %s", cf->file_path); @@ -321,8 +320,7 @@ import_get_rcsdiff(struct cvs_file *cf, RCSNUM *rev) char *delta, *p1, *p2; BUF *b1, *b2, *b3; - /* XXX */ - if ((b1 = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL) + if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL) fatal("import_get_rcsdiff: failed loading %s", cf->file_path); if ((b2 = rcs_getrev(cf->file_rcs, rev)) == NULL) diff --git a/usr.bin/cvs/update.c b/usr.bin/cvs/update.c index e2794ec3cc4..44826460bd3 100644 --- a/usr.bin/cvs/update.c +++ b/usr.bin/cvs/update.c @@ -1,4 +1,4 @@ -/* $OpenBSD: update.c,v 1.71 2006/06/06 05:13:39 joris Exp $ */ +/* $OpenBSD: update.c,v 1.72 2006/06/14 14:10:50 joris Exp $ */ /* * Copyright (c) 2006 Joris Vink <joris@openbsd.org> * @@ -310,7 +310,7 @@ cvs_update_local(struct cvs_file *cf) cvs_printf("U %s\n", cf->file_path); break; case FILE_MERGE: - bp = cvs_diff3(cf->file_rcs, cf->file_path, + bp = cvs_diff3(cf->file_rcs, cf->file_path, cf->fd, cf->file_ent->ce_rev, cf->file_rcsrev, 1); if (bp == NULL) fatal("cvs_update_local: failed to merge"); @@ -381,7 +381,7 @@ update_has_conflict_markers(struct cvs_file *cf) cvs_log(LP_TRACE, "update_has_conflict_markers(%s)", cf->file_path); - if ((bp = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL) + if ((bp = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL) fatal("update_has_conflict_markers: failed to load %s", cf->file_path); |