2 * Copyright (c) 2003-2006, Maxime Henrion <mux@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
31 #include <openssl/md5.h>
56 struct backoff_timer {
64 static void bt_update(struct backoff_timer *);
65 static void bt_addjitter(struct backoff_timer *);
68 asciitoint(const char *s, int *val, int base)
74 longval = strtol(s, &end, base);
75 if (errno || *end != '\0')
77 if (longval > INT_MAX || longval < INT_MIN) {
86 lprintf(int level, const char *fmt, ...)
99 ret = vfprintf(to, fmt, ap);
106 * Compute the MD5 checksum of a file. The md parameter must
107 * point to a buffer containing at least MD5_DIGEST_SIZE bytes.
109 * Do not confuse OpenSSL's MD5_DIGEST_LENGTH with our own
110 * MD5_DIGEST_SIZE macro.
113 MD5_File(char *path, char *md)
120 fd = open(path, O_RDONLY);
124 while ((n = read(fd, buf, sizeof(buf))) > 0)
125 MD5_Update(&ctx, buf, n);
134 * Wrapper around MD5_Final() that converts the 128 bits MD5 hash
135 * to an ASCII string representing this value in hexadecimal.
138 MD5_End(char *md, MD5_CTX *c)
140 unsigned char md5[MD5_DIGEST_LENGTH];
141 const char hex[] = "0123456789abcdef";
146 for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
147 md[j++] = hex[md5[i] >> 4];
148 md[j++] = hex[md5[i] & 0xf];
154 pathcmp(const char *s1, const char *s2)
165 } while (c1 == c2 && c1 != '\0');
171 commonpathlength(const char *a, size_t alen, const char *b, size_t blen)
173 size_t i, minlen, lastslash;
175 minlen = min(alen, blen);
177 for (i = 0; i < minlen; i++) {
181 if (i == 0) /* Include the leading slash. */
188 /* One path is a prefix of the other/ */
189 if (alen > minlen) { /* Path "b" is a prefix of "a". */
190 if (a[minlen] == '/')
194 } else if (blen > minlen) { /* Path "a" is a prefix of "b". */
195 if (b[minlen] == '/')
201 /* The paths are identical. */
210 s = strrchr(path, '/');
217 rcsdatetotm(const char *revdate, struct tm *tm)
222 cp = strchr(revdate, '.');
227 cp = strptime(revdate, "%Y.%m.%d.%H.%M.%S", tm);
229 cp = strptime(revdate, "%y.%m.%d.%H.%M.%S", tm);
232 if (cp == NULL || *cp != '\0')
238 rcsdatetotime(const char *revdate)
244 error = rcsdatetotm(revdate, &tm);
252 * Returns a buffer allocated with malloc() containing the absolute
253 * pathname to the checkout file made from the prefix and the path
254 * of the corresponding RCS file relatively to the prefix. If the
255 * filename is not an RCS filename, NULL will be returned.
258 checkoutpath(const char *prefix, const char *file)
267 while ((cp = strstr(cp, "..")) != NULL) {
268 if (cp == file || cp[2] == '\0' ||
269 (cp[-1] == '/' && cp[2] == '/'))
274 if (len < 2 || file[len - 1] != 'v' || file[len - 2] != ',')
276 xasprintf(&path, "%s/%.*s", prefix, (int)len - 2, file);
281 mkdirhier(char *path, mode_t mask)
285 int error, finish, rv;
290 for (i = len - 1; i > 0; i--) {
291 if (path[i] == '/') {
293 if (access(path, F_OK) == 0) {
297 if (errno != ENOENT) {
311 fa = fattr_new(FT_DIRECTORY, -1);
312 fattr_mergedefault(fa);
313 fattr_umask(fa, mask);
317 error = fattr_makenode(fa, path);
319 rv = fattr_install(fa, path, NULL);
320 if (error || rv == -1)
324 i += strlen(path + i);
333 * Compute temporary pathnames.
334 * This can look a bit like overkill but we mimic CVSup's behaviour.
336 #define TEMPNAME_PREFIX "#cvs.csup"
338 static pthread_mutex_t tempname_mtx = PTHREAD_MUTEX_INITIALIZER;
339 static pid_t tempname_pid = -1;
340 static int tempname_count;
343 tempname(const char *path)
348 error = pthread_mutex_lock(&tempname_mtx);
350 if (tempname_pid == -1) {
351 tempname_pid = getpid();
354 count = tempname_count++;
355 error = pthread_mutex_unlock(&tempname_mtx);
357 cp = strrchr(path, '/');
359 xasprintf(&temp, "%s-%ld.%d", TEMPNAME_PREFIX,
360 (long)tempname_pid, count);
362 xasprintf(&temp, "%.*s%s-%ld.%d", (int)(cp - path + 1), path,
363 TEMPNAME_PREFIX, (long)tempname_pid, count);
379 xrealloc(void *buf, size_t size)
382 buf = realloc(buf, size);
389 xstrdup(const char *str)
400 xasprintf(char **ret, const char *format, ...)
405 va_start(ap, format);
406 rv = vasprintf(ret, format, ap);
418 p = xmalloc(sizeof(struct pattlist));
419 p->size = 4; /* Initial size. */
420 p->patterns = xmalloc(p->size * sizeof(char *));
426 pattlist_add(struct pattlist *p, const char *pattern)
429 if (p->in == p->size) {
431 p->patterns = xrealloc(p->patterns, p->size * sizeof(char *));
433 assert(p->in < p->size);
434 p->patterns[p->in++] = xstrdup(pattern);
438 pattlist_get(struct pattlist *p, size_t i)
442 return (p->patterns[i]);
446 pattlist_size(struct pattlist *p)
453 pattlist_free(struct pattlist *p)
457 for (i = 0; i < p->in; i++)
458 free(p->patterns[i]);
463 /* Creates a backoff timer. */
464 struct backoff_timer *
465 bt_new(time_t min, time_t max, float backoff, float jitter)
467 struct backoff_timer *bt;
469 bt = xmalloc(sizeof(struct backoff_timer));
472 bt->backoff = backoff;
480 /* Updates the backoff timer. */
482 bt_update(struct backoff_timer *bt)
485 bt->interval = (time_t)min(bt->interval * bt->backoff, bt->max);
489 /* Adds some jitter. */
491 bt_addjitter(struct backoff_timer *bt)
495 mag = (long)(bt->jitter * bt->interval);
496 /* We want a random number between -mag and mag. */
497 bt->interval += (time_t)(random() % (2 * mag) - mag);
500 /* Returns the current timer value. */
502 bt_get(struct backoff_timer *bt)
505 return (bt->interval);
508 /* Times out for bt->interval seconds. */
510 bt_pause(struct backoff_timer *bt)
518 bt_free(struct backoff_timer *bt)