Annotation of src/usr.bin/cvs/repository.c, Revision 1.26
1.26 ! deraadt 1: /* $OpenBSD: repository.c,v 1.25 2017/05/31 16:18:20 joris Exp $ */
1.1 joris 2: /*
3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17:
1.12 otto 18: #include <sys/stat.h>
19:
20: #include <errno.h>
21: #include <fcntl.h>
22: #include <pwd.h>
23: #include <string.h>
24: #include <unistd.h>
1.1 joris 25:
26: #include "cvs.h"
27:
1.23 ray 28: struct wklhead repo_locks;
1.1 joris 29:
30: void
31: cvs_repository_unlock(const char *repo)
32: {
1.24 deraadt 33: char fpath[PATH_MAX];
1.1 joris 34:
35: cvs_log(LP_TRACE, "cvs_repository_unlock(%s)", repo);
36:
1.11 xsa 37: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s", repo, CVS_LOCK);
1.1 joris 38:
1.25 joris 39: if (unlink(fpath) == -1 && errno != ENOENT)
40: cvs_log(LP_ERR, "warning: failed to unlink %s", fpath);
1.1 joris 41: }
42:
43: void
1.20 joris 44: cvs_repository_lock(const char *repo, int wantlock)
1.1 joris 45: {
1.6 xsa 46: int i;
1.20 joris 47: uid_t myuid;
1.1 joris 48: struct stat st;
1.24 deraadt 49: char fpath[PATH_MAX];
1.1 joris 50: struct passwd *pw;
1.7 xsa 51:
52: if (cvs_noexec == 1 || cvs_readonlyfs == 1)
53: return;
1.1 joris 54:
1.20 joris 55: cvs_log(LP_TRACE, "cvs_repository_lock(%s, %d)", repo, wantlock);
1.1 joris 56:
1.11 xsa 57: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s", repo, CVS_LOCK);
1.1 joris 58:
1.20 joris 59: myuid = getuid();
60:
1.1 joris 61: for (i = 0; i < CVS_LOCK_TRIES; i++) {
62: if (cvs_quit)
63: fatal("received signal %d", sig_received);
64:
65: if (stat(fpath, &st) == -1)
66: break;
67:
1.20 joris 68: if (st.st_uid == myuid)
69: return;
70:
1.1 joris 71: if ((pw = getpwuid(st.st_uid)) == NULL)
72: fatal("cvs_repository_lock: %s", strerror(errno));
73:
74: cvs_log(LP_NOTICE, "waiting for %s's lock in '%s'",
75: pw->pw_name, repo);
76: sleep(CVS_LOCK_SLEEP);
77: }
78:
79: if (i == CVS_LOCK_TRIES)
80: fatal("maximum wait time for lock inside '%s' reached", repo);
1.20 joris 81:
82: if (wantlock == 0)
83: return;
1.1 joris 84:
1.26 ! deraadt 85: if ((i = open(fpath, O_WRONLY|O_CREAT|O_TRUNC, 0755)) == -1) {
1.1 joris 86: if (errno == EEXIST)
87: fatal("cvs_repository_lock: somebody beat us");
88: else
1.3 david 89: fatal("cvs_repository_lock: %s: %s",
1.1 joris 90: fpath, strerror(errno));
91: }
92:
93: (void)close(i);
1.23 ray 94: worklist_add(fpath, &repo_locks);
1.1 joris 95: }
96:
97: void
98: cvs_repository_getdir(const char *dir, const char *wdir,
1.21 joris 99: struct cvs_flisthead *fl, struct cvs_flisthead *dl, int flags)
1.1 joris 100: {
1.8 joris 101: int type;
1.1 joris 102: DIR *dirp;
1.8 joris 103: struct stat st;
1.1 joris 104: struct dirent *dp;
1.24 deraadt 105: char *s, fpath[PATH_MAX], rpath[PATH_MAX];
1.1 joris 106:
107: if ((dirp = opendir(dir)) == NULL)
108: fatal("cvs_repository_getdir: failed to open '%s'", dir);
109:
110: while ((dp = readdir(dirp)) != NULL) {
111: if (!strcmp(dp->d_name, ".") ||
112: !strcmp(dp->d_name, "..") ||
113: !strcmp(dp->d_name, CVS_LOCK))
1.2 joris 114: continue;
115:
1.24 deraadt 116: (void)xsnprintf(fpath, PATH_MAX, "%s/%s", wdir, dp->d_name);
117: (void)xsnprintf(rpath, PATH_MAX, "%s/%s", dir, dp->d_name);
1.18 joris 118:
119: if (!TAILQ_EMPTY(&checkout_ign_pats)) {
1.19 joris 120: if ((s = strrchr(fpath, ',')) != NULL)
121: *s = '\0';
1.18 joris 122: if (cvs_file_chkign(fpath))
123: continue;
1.19 joris 124: if (s != NULL)
125: *s = ',';
1.18 joris 126: }
1.1 joris 127:
128: /*
1.8 joris 129: * nfs and afs will show d_type as DT_UNKNOWN
130: * for files and/or directories so when we encounter
1.10 todd 131: * this we call lstat() on the path to be sure.
1.1 joris 132: */
1.8 joris 133: if (dp->d_type == DT_UNKNOWN) {
1.10 todd 134: if (lstat(rpath, &st) == -1)
1.8 joris 135: fatal("'%s': %s", rpath, strerror(errno));
136:
137: switch (st.st_mode & S_IFMT) {
138: case S_IFDIR:
139: type = CVS_DIR;
140: break;
141: case S_IFREG:
142: type = CVS_FILE;
143: break;
144: default:
145: fatal("Unknown file type in repository");
146: }
147: } else {
148: switch (dp->d_type) {
149: case DT_DIR:
150: type = CVS_DIR;
151: break;
152: case DT_REG:
153: type = CVS_FILE;
154: break;
155: default:
156: fatal("Unknown file type in repository");
157: }
158: }
159:
1.22 joris 160: if (!(flags & REPOSITORY_DODIRS) && type == CVS_DIR) {
161: if (strcmp(dp->d_name, CVS_PATH_ATTIC))
162: continue;
163: }
1.8 joris 164:
165: switch (type) {
166: case CVS_DIR:
1.21 joris 167: if (!strcmp(dp->d_name, CVS_PATH_ATTIC)) {
168: cvs_repository_getdir(rpath, wdir, fl, dl,
169: REPOSITORY_IS_ATTIC);
170: } else {
1.22 joris 171: cvs_file_get(fpath, 0, dl, CVS_DIR);
1.21 joris 172: }
1.8 joris 173: break;
174: case CVS_FILE:
1.17 tobias 175: if ((s = strrchr(fpath, ',')) != NULL &&
176: s != fpath && !strcmp(s, RCS_FILE_EXT)) {
1.1 joris 177: *s = '\0';
1.21 joris 178: cvs_file_get(fpath,
179: (flags & REPOSITORY_IS_ATTIC) ?
1.22 joris 180: FILE_INSIDE_ATTIC : 0, fl, CVS_FILE);
1.17 tobias 181: }
1.8 joris 182: break;
183: default:
184: fatal("type %d unknown, shouldn't happen", type);
1.1 joris 185: }
186: }
187:
188: (void)closedir(dirp);
189: }