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>
55 struct backoff_timer {
63 static void bt_update(struct backoff_timer *);
64 static void bt_addjitter(struct backoff_timer *);
67 asciitoint(const char *s, int *val, int base)
73 longval = strtol(s, &end, base);
74 if (errno || *end != '\0')
76 if (longval > INT_MAX || longval < INT_MIN) {
85 lprintf(int level, const char *fmt, ...)
98 ret = vfprintf(to, fmt, ap);
105 * Compute the MD5 checksum of a file. The md parameter must
106 * point to a buffer containing at least MD5_DIGEST_SIZE bytes.
108 * Do not confuse OpenSSL's MD5_DIGEST_LENGTH with our own
109 * MD5_DIGEST_SIZE macro.
112 MD5_File(char *path, char *md)
119 fd = open(path, O_RDONLY);
123 while ((n = read(fd, buf, sizeof(buf))) > 0)
124 MD5_Update(&ctx, buf, n);
133 * Wrapper around MD5_Final() that converts the 128 bits MD5 hash
134 * to an ASCII string representing this value in hexadecimal.
137 MD5_End(char *md, MD5_CTX *c)
139 unsigned char md5[MD5_DIGEST_LENGTH];
140 const char hex[] = "0123456789abcdef";
145 for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
146 md[j++] = hex[md5[i] >> 4];
147 md[j++] = hex[md5[i] & 0xf];
153 pathcmp(const char *s1, const char *s2)
164 } while (c1 == c2 && c1 != '\0');
170 commonpathlength(const char *a, size_t alen, const char *b, size_t blen)
172 size_t i, minlen, lastslash;
174 minlen = min(alen, blen);
176 for (i = 0; i < minlen; i++) {
180 if (i == 0) /* Include the leading slash. */
187 /* One path is a prefix of the other/ */
188 if (alen > minlen) { /* Path "b" is a prefix of "a". */
189 if (a[minlen] == '/')
193 } else if (blen > minlen) { /* Path "a" is a prefix of "b". */
194 if (b[minlen] == '/')
200 /* The paths are identical. */
205 pathlast(const char *path)
209 s = strrchr(path, '/');
216 rcsdatetotm(const char *revdate, struct tm *tm)
221 cp = strchr(revdate, '.');
226 cp = strptime(revdate, "%Y.%m.%d.%H.%M.%S", tm);
228 cp = strptime(revdate, "%y.%m.%d.%H.%M.%S", tm);
231 if (cp == NULL || *cp != '\0')
237 rcsdatetotime(const char *revdate)
243 error = rcsdatetotm(revdate, &tm);
251 * Checks if a file is an RCS file.
254 isrcs(const char *file, size_t *len)
261 while ((cp = strstr(cp, "..")) != NULL) {
262 if (cp == file || cp[2] == '\0' ||
263 (cp[-1] == '/' && cp[2] == '/'))
268 if (*len < 2 || file[*len - 1] != 'v' || file[*len - 2] != ',') {
276 * Returns a buffer allocated with malloc() containing the absolute
277 * pathname to the checkout file made from the prefix and the path
278 * of the corresponding RCS file relatively to the prefix. If the
279 * filename is not an RCS filename, NULL will be returned.
282 checkoutpath(const char *prefix, const char *file)
287 if (!isrcs(file, &len))
289 xasprintf(&path, "%s/%.*s", prefix, (int)len - 2, file);
294 * Returns a cvs path allocated with malloc() containing absolute pathname to a
295 * file in cvs mode which can reside in the attic. XXX: filename has really no
299 cvspath(const char *prefix, const char *file, int attic)
304 last = pathlast(file);
306 xasprintf(&path, "%s/%.*sAttic/%s", prefix, (int)(last - file),
309 xasprintf(&path, "%s/%s", prefix, file);
315 * Regular or attic path if regular fails.
316 * XXX: This should perhaps also check if the Attic file exists too, and return
320 atticpath(const char *prefix, const char *file)
324 path = cvspath(prefix, file, 0);
325 if (access(path, F_OK) != 0) {
327 path = cvspath(prefix, file, 1);
333 mkdirhier(char *path, mode_t mask)
337 int error, finish, rv;
342 for (i = len - 1; i > 0; i--) {
343 if (path[i] == '/') {
345 if (access(path, F_OK) == 0) {
349 if (errno != ENOENT) {
363 fa = fattr_new(FT_DIRECTORY, -1);
364 fattr_mergedefault(fa);
365 fattr_umask(fa, mask);
369 error = fattr_makenode(fa, path);
371 rv = fattr_install(fa, path, NULL);
372 if (error || rv == -1)
376 i += strlen(path + i);
385 * Compute temporary pathnames.
386 * This can look a bit like overkill but we mimic CVSup's behaviour.
388 #define TEMPNAME_PREFIX "#cvs.csup"
390 static pthread_mutex_t tempname_mtx = PTHREAD_MUTEX_INITIALIZER;
391 static pid_t tempname_pid = -1;
392 static int tempname_count;
395 tempname(const char *path)
400 error = pthread_mutex_lock(&tempname_mtx);
402 if (tempname_pid == -1) {
403 tempname_pid = getpid();
406 count = tempname_count++;
407 error = pthread_mutex_unlock(&tempname_mtx);
409 cp = strrchr(path, '/');
411 xasprintf(&temp, "%s-%ld.%d", TEMPNAME_PREFIX,
412 (long)tempname_pid, count);
414 xasprintf(&temp, "%.*s%s-%ld.%d", (int)(cp - path + 1), path,
415 TEMPNAME_PREFIX, (long)tempname_pid, count);
431 xrealloc(void *buf, size_t size)
434 buf = realloc(buf, size);
441 xstrdup(const char *str)
452 xasprintf(char **ret, const char *format, ...)
457 va_start(ap, format);
458 rv = vasprintf(ret, format, ap);
470 p = xmalloc(sizeof(struct pattlist));
471 p->size = 4; /* Initial size. */
472 p->patterns = xmalloc(p->size * sizeof(char *));
478 pattlist_add(struct pattlist *p, const char *pattern)
481 if (p->in == p->size) {
483 p->patterns = xrealloc(p->patterns, p->size * sizeof(char *));
485 assert(p->in < p->size);
486 p->patterns[p->in++] = xstrdup(pattern);
490 pattlist_get(struct pattlist *p, size_t i)
494 return (p->patterns[i]);
498 pattlist_size(struct pattlist *p)
505 pattlist_free(struct pattlist *p)
509 for (i = 0; i < p->in; i++)
510 free(p->patterns[i]);
515 /* Creates a backoff timer. */
516 struct backoff_timer *
517 bt_new(time_t min, time_t max, float backoff, float jitter)
519 struct backoff_timer *bt;
521 bt = xmalloc(sizeof(struct backoff_timer));
524 bt->backoff = backoff;
532 /* Updates the backoff timer. */
534 bt_update(struct backoff_timer *bt)
537 bt->interval = (time_t)min(bt->interval * bt->backoff, bt->max);
541 /* Adds some jitter. */
543 bt_addjitter(struct backoff_timer *bt)
547 mag = (long)(bt->jitter * bt->interval);
548 /* We want a random number between -mag and mag. */
549 bt->interval += (time_t)(random() % (2 * mag) - mag);
552 /* Returns the current timer value. */
554 bt_get(struct backoff_timer *bt)
557 return (bt->interval);
560 /* Times out for bt->interval seconds. */
562 bt_pause(struct backoff_timer *bt)
570 bt_free(struct backoff_timer *bt)
576 /* Compare two revisions. */
578 rcsnum_cmp(char *revision1, char *revision2)
580 char *ptr1, *ptr2, *dot1, *dot2;
581 int num1len, num2len, ret;
585 while (*ptr1 != '\0' && *ptr2 != '\0') {
586 dot1 = strchr(ptr1, '.');
587 dot2 = strchr(ptr2, '.');
589 dot1 = strchr(ptr1, '\0');
591 dot2 = strchr(ptr2, '\0');
593 num1len = dot1 - ptr1;
594 num2len = dot2 - ptr2;
595 /* Check the distance between each, showing how many digits */
596 if (num1len > num2len)
598 else if (num1len < num2len)
601 /* Equal distance means we must check each character. */
602 ret = strncmp(ptr1, ptr2, num1len);
605 ptr1 = (*dot1 == '.') ? (dot1 + 1) : dot1;
606 ptr2 = (*dot2 == '.') ? (dot2 + 1) : dot2;
609 if (*ptr1 != '\0' && *ptr2 == '\0')
611 if (*ptr1 == '\0' && *ptr2 != '\0')
617 /* Returns 0 if a rcsrev is not a trunk revision number. */
619 rcsrev_istrunk(char *revnum)
623 tmp = strchr(revnum, '.');
625 if (strchr(tmp, '.') != NULL)
630 /* Return prefix of rcsfile. */
632 rcsrev_prefix(char *revnum)
636 modrev = xstrdup(revnum);
637 pos = strrchr(modrev, '.');