[BACK]Return to repository.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/repository.c, Revision 1.7

1.7     ! xsa         1: /*     $OpenBSD: repository.c,v 1.6 2006/11/28 14:49:58 xsa 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:
                     18: #include "includes.h"
                     19:
                     20: #include "cvs.h"
                     21: #include "file.h"
                     22: #include "log.h"
                     23: #include "repository.h"
                     24: #include "worklist.h"
                     25:
                     26: struct cvs_wklhead repo_locks;
                     27:
                     28: void
                     29: cvs_repository_unlock(const char *repo)
                     30: {
                     31:        char fpath[MAXPATHLEN];
                     32:
                     33:        cvs_log(LP_TRACE, "cvs_repository_unlock(%s)", repo);
                     34:
1.6       xsa        35:        if (cvs_path_cat(repo, CVS_LOCK, fpath, sizeof(fpath)) >= sizeof(fpath))
                     36:                fatal("cvs_repository_unlock: truncation");
1.1       joris      37:
                     38:        /* XXX - this ok? */
                     39:        cvs_worklist_run(&repo_locks, cvs_worklist_unlink);
                     40: }
                     41:
                     42: void
                     43: cvs_repository_lock(const char *repo)
                     44: {
1.6       xsa        45:        int i;
1.1       joris      46:        struct stat st;
                     47:        char fpath[MAXPATHLEN];
                     48:        struct passwd *pw;
1.7     ! xsa        49:
        !            50:        if (cvs_noexec == 1 || cvs_readonlyfs == 1)
        !            51:                return;
1.1       joris      52:
                     53:        cvs_log(LP_TRACE, "cvs_repository_lock(%s)", repo);
                     54:
1.6       xsa        55:        if (cvs_path_cat(repo, CVS_LOCK, fpath, sizeof(fpath)) >= sizeof(fpath))
                     56:                fatal("cvs_repository_unlock: truncation");
1.1       joris      57:
                     58:        for (i = 0; i < CVS_LOCK_TRIES; i++) {
                     59:                if (cvs_quit)
                     60:                        fatal("received signal %d", sig_received);
                     61:
                     62:                if (stat(fpath, &st) == -1)
                     63:                        break;
                     64:
                     65:                if ((pw = getpwuid(st.st_uid)) == NULL)
                     66:                        fatal("cvs_repository_lock: %s", strerror(errno));
                     67:
                     68:                cvs_log(LP_NOTICE, "waiting for %s's lock in '%s'",
                     69:                    pw->pw_name, repo);
                     70:                sleep(CVS_LOCK_SLEEP);
                     71:        }
                     72:
                     73:        if (i == CVS_LOCK_TRIES)
                     74:                fatal("maximum wait time for lock inside '%s' reached", repo);
                     75:
                     76:        if ((i = open(fpath, O_WRONLY|O_CREAT|O_TRUNC, 0755)) < 0) {
                     77:                if (errno == EEXIST)
                     78:                        fatal("cvs_repository_lock: somebody beat us");
                     79:                else
1.3       david      80:                        fatal("cvs_repository_lock: %s: %s",
1.1       joris      81:                            fpath, strerror(errno));
                     82:        }
                     83:
                     84:        (void)close(i);
                     85:        cvs_worklist_add(fpath, &repo_locks);
                     86: }
                     87:
                     88: void
                     89: cvs_repository_getdir(const char *dir, const char *wdir,
1.2       joris      90:        struct cvs_flisthead *fl, struct cvs_flisthead *dl, int dodirs)
1.1       joris      91: {
                     92:        DIR *dirp;
                     93:        struct dirent *dp;
                     94:        char *s, fpath[MAXPATHLEN];
                     95:
                     96:        if ((dirp = opendir(dir)) == NULL)
                     97:                fatal("cvs_repository_getdir: failed to open '%s'", dir);
                     98:
                     99:        while ((dp = readdir(dirp)) != NULL) {
                    100:                if (!strcmp(dp->d_name, ".") ||
                    101:                    !strcmp(dp->d_name, "..") ||
1.5       xsa       102:                    !strcmp(dp->d_name, CVS_PATH_ATTIC) ||
1.1       joris     103:                    !strcmp(dp->d_name, CVS_LOCK))
                    104:                        continue;
                    105:
                    106:                if (cvs_file_chkign(dp->d_name))
1.2       joris     107:                        continue;
                    108:
                    109:                if (dodirs == 0 && dp->d_type == DT_DIR)
1.1       joris     110:                        continue;
                    111:
1.6       xsa       112:                if (cvs_path_cat(wdir, dp->d_name,
                    113:                    fpath, sizeof(fpath)) >= sizeof(fpath))
                    114:                        fatal("cvs_repository_getdir: truncation");
1.1       joris     115:
                    116:                /*
                    117:                 * Anticipate the file type for sorting, we do not determine
                    118:                 * the final file type until we have the fd floating around.
                    119:                 */
                    120:                if (dp->d_type == DT_DIR) {
                    121:                        cvs_file_get(fpath, dl);
                    122:                } else if (dp->d_type == DT_REG) {
                    123:                        if ((s = strrchr(fpath, ',')) != NULL)
                    124:                                *s = '\0';
                    125:                        cvs_file_get(fpath, fl);
                    126:                }
                    127:        }
                    128:
                    129:        (void)closedir(dirp);
                    130: }