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

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