[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.19

1.19    ! joris       1: /*     $OpenBSD: repository.c,v 1.18 2008/02/03 22:50:28 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:
                     28: struct cvs_wklhead repo_locks;
                     29:
                     30: void
                     31: cvs_repository_unlock(const char *repo)
                     32: {
                     33:        char fpath[MAXPATHLEN];
                     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:
                     39:        /* XXX - this ok? */
                     40:        cvs_worklist_run(&repo_locks, cvs_worklist_unlink);
                     41: }
                     42:
                     43: void
                     44: cvs_repository_lock(const char *repo)
                     45: {
1.6       xsa        46:        int i;
1.1       joris      47:        struct stat st;
                     48:        char fpath[MAXPATHLEN];
                     49:        struct passwd *pw;
1.7       xsa        50:
                     51:        if (cvs_noexec == 1 || cvs_readonlyfs == 1)
                     52:                return;
1.1       joris      53:
                     54:        cvs_log(LP_TRACE, "cvs_repository_lock(%s)", repo);
                     55:
1.11      xsa        56:        (void)xsnprintf(fpath, sizeof(fpath), "%s/%s", repo, CVS_LOCK);
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: {
1.8       joris      92:        int type;
1.1       joris      93:        DIR *dirp;
1.8       joris      94:        struct stat st;
1.1       joris      95:        struct dirent *dp;
1.9       otto       96:        char *s, fpath[MAXPATHLEN], rpath[MAXPATHLEN];
1.1       joris      97:
                     98:        if ((dirp = opendir(dir)) == NULL)
                     99:                fatal("cvs_repository_getdir: failed to open '%s'", dir);
                    100:
                    101:        while ((dp = readdir(dirp)) != NULL) {
                    102:                if (!strcmp(dp->d_name, ".") ||
                    103:                    !strcmp(dp->d_name, "..") ||
                    104:                    !strcmp(dp->d_name, CVS_LOCK))
1.2       joris     105:                        continue;
                    106:
1.11      xsa       107:                (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", wdir, dp->d_name);
                    108:                (void)xsnprintf(rpath, MAXPATHLEN, "%s/%s", dir, dp->d_name);
1.18      joris     109:
                    110:                if (!TAILQ_EMPTY(&checkout_ign_pats)) {
1.19    ! joris     111:                        if ((s = strrchr(fpath, ',')) != NULL)
        !           112:                                *s = '\0';
1.18      joris     113:                        if (cvs_file_chkign(fpath))
                    114:                                continue;
1.19    ! joris     115:                        if (s != NULL)
        !           116:                                *s = ',';
1.18      joris     117:                }
1.1       joris     118:
                    119:                /*
1.8       joris     120:                 * nfs and afs will show d_type as DT_UNKNOWN
                    121:                 * for files and/or directories so when we encounter
1.10      todd      122:                 * this we call lstat() on the path to be sure.
1.1       joris     123:                 */
1.8       joris     124:                if (dp->d_type == DT_UNKNOWN) {
1.10      todd      125:                        if (lstat(rpath, &st) == -1)
1.8       joris     126:                                fatal("'%s': %s", rpath, strerror(errno));
                    127:
                    128:                        switch (st.st_mode & S_IFMT) {
                    129:                        case S_IFDIR:
                    130:                                type = CVS_DIR;
                    131:                                break;
                    132:                        case S_IFREG:
                    133:                                type = CVS_FILE;
                    134:                                break;
                    135:                        default:
                    136:                                fatal("Unknown file type in repository");
                    137:                        }
                    138:                } else {
                    139:                        switch (dp->d_type) {
                    140:                        case DT_DIR:
                    141:                                type = CVS_DIR;
                    142:                                break;
                    143:                        case DT_REG:
                    144:                                type = CVS_FILE;
                    145:                                break;
                    146:                        default:
                    147:                                fatal("Unknown file type in repository");
                    148:                        }
                    149:                }
                    150:
                    151:                if (dodirs == 0 && type == CVS_DIR)
                    152:                        continue;
                    153:
                    154:                switch (type) {
                    155:                case CVS_DIR:
1.14      joris     156:                        if (!strcmp(dp->d_name, CVS_PATH_ATTIC))
                    157:                                cvs_repository_getdir(rpath, wdir, fl, dl, 0);
                    158:                        else
1.15      joris     159:                                cvs_file_get(fpath, 0, dl);
1.8       joris     160:                        break;
                    161:                case CVS_FILE:
1.17      tobias    162:                        if ((s = strrchr(fpath, ',')) != NULL &&
                    163:                            s != fpath && !strcmp(s, RCS_FILE_EXT)) {
1.1       joris     164:                                *s = '\0';
1.17      tobias    165:                                cvs_file_get(fpath, 0, fl);
                    166:                        }
1.8       joris     167:                        break;
                    168:                default:
                    169:                        fatal("type %d unknown, shouldn't happen", type);
1.1       joris     170:                }
                    171:        }
                    172:
                    173:        (void)closedir(dirp);
                    174: }