version.sh: Fix sed error on BSDs: bad flag in substitute command: '}'.
[metastore.git] / src / metaentry.c
blobfdb7f7155eefe4e29e94cdeddc8d043fd8212bda
1 /*
2 * Various functions to work with meta entries.
4 * Copyright (C) 2007 David Härdeman <david@hardeman.nu>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #define _GNU_SOURCE
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <sys/xattr.h>
28 #include <limits.h>
29 #include <dirent.h>
30 #include <sys/mman.h>
31 #include <utime.h>
32 #include <fcntl.h>
33 #include <stdint.h>
34 #include <errno.h>
35 #include <time.h>
37 #include <sys/param.h>
38 #ifndef BSD
39 # include <bsd/string.h>
40 #endif
42 #include "metastore.h"
43 #include "metaentry.h"
44 #include "utils.h"
46 /* Free's a metaentry and all its parameters */
47 static void
48 mentry_free(struct metaentry *m)
50 unsigned i;
52 if (!m)
53 return;
55 free(m->path);
56 free(m->owner);
57 free(m->group);
59 for (i = 0; i < m->xattrs; i++) {
60 free(m->xattr_names[i]);
61 free(m->xattr_values[i]);
64 free(m->xattr_names);
65 free(m->xattr_values);
66 free(m->xattr_lvalues);
68 free(m);
71 /* Allocates an empty metahash table */
72 static struct metahash *
73 mhash_alloc()
75 struct metahash *mhash;
76 mhash = xmalloc(sizeof(struct metahash));
77 memset(mhash, 0, sizeof(struct metahash));
78 return mhash;
81 /* Generates a hash key (using djb2) */
82 static unsigned int
83 hash(const char *str)
85 unsigned int hash = 5381;
86 int c;
88 while ((c = *str++))
89 hash = ((hash << 5) + hash) + c;
91 return hash % HASH_INDEXES;
94 /* Allocates an empty metaentry */
95 static struct metaentry *
96 mentry_alloc()
98 struct metaentry *mentry;
99 mentry = xmalloc(sizeof(struct metaentry));
100 memset(mentry, 0, sizeof(struct metaentry));
101 return mentry;
104 /* Does a bisect search for the closest match in a metaentry list */
105 struct metaentry *
106 mentry_find(const char *path, struct metahash *mhash)
108 struct metaentry *base;
109 unsigned int key;
111 if (!mhash) {
112 msg(MSG_ERROR, "%s called with empty hash table\n", __FUNCTION__);
113 return NULL;
116 key = hash(path);
117 for (base = mhash->bucket[key]; base; base = base->next) {
118 if (!strcmp(base->path, path))
119 return base;
122 return NULL;
125 /* Inserts a metaentry into a metaentry list */
126 static void
127 mentry_insert(struct metaentry *mentry, struct metahash *mhash)
129 unsigned int key;
131 key = hash(mentry->path);
132 mentry->next = mhash->bucket[key];
133 mhash->bucket[key] = mentry;
136 #ifdef DEBUG
137 /* Prints a metaentry */
138 static void
139 mentry_print(const struct metaentry *mentry)
141 int i;
143 if (!mentry || !mentry->path) {
144 msg(MSG_DEBUG,
145 "Incorrect meta entry passed to printmetaentry\n");
146 return;
149 msg(MSG_DEBUG, "===========================\n");
150 msg(MSG_DEBUG, "Dump of metaentry %p\n", mentry);
151 msg(MSG_DEBUG, "===========================\n");
153 msg(MSG_DEBUG, "path\t\t: %s\n", mentry->path);
154 msg(MSG_DEBUG, "owner\t\t: %s\n", mentry->owner);
155 msg(MSG_DEBUG, "group\t\t: %s\n", mentry->group);
156 msg(MSG_DEBUG, "mtime\t\t: %ld\n", (unsigned long)mentry->mtime);
157 msg(MSG_DEBUG, "mtimensec\t: %ld\n", (unsigned long)mentry->mtimensec);
158 msg(MSG_DEBUG, "mode\t\t: %ld\n", (unsigned long)mentry->mode);
159 for (i = 0; i < mentry->xattrs; i++) {
160 msg(MSG_DEBUG, "xattr[%i]\t: %s=\"", i, mentry->xattr_names[i]);
161 binary_print(mentry->xattr_values[i], mentry->xattr_lvalues[i]);
162 msg(MSG_DEBUG, "\"\n");
165 msg(MSG_DEBUG, "===========================\n\n");
168 /* Prints all metaentries in a metaentry list */
169 static void
170 mentries_print(const struct metahash *mhash)
172 const struct metaentry *mentry;
173 int index;
175 for (index = 0; index < HASH_INDEXES; index++)
176 for (mentry = mhash->bucket[index]; mentry; mentry = mentry->next)
177 mentry_print(mentry);
179 msg(MSG_DEBUG, "%i entries in total\n", mhash->count);
181 #endif
183 /* Creates a metaentry for the file/dir/etc at path */
184 struct metaentry *
185 mentry_create(const char *path)
187 ssize_t lsize, vsize;
188 char *list, *attr;
189 struct stat sbuf;
190 struct passwd *pbuf;
191 struct group *gbuf;
192 int i;
193 struct metaentry *mentry;
195 if (lstat(path, &sbuf)) {
196 msg(MSG_ERROR, "lstat failed for %s: %s\n",
197 path, strerror(errno));
198 return NULL;
201 pbuf = xgetpwuid(sbuf.st_uid);
202 if (!pbuf) {
203 msg(MSG_ERROR, "getpwuid failed for %s: uid %i not found\n",
204 path, (int)sbuf.st_uid);
205 return NULL;
208 gbuf = xgetgrgid(sbuf.st_gid);
209 if (!gbuf) {
210 msg(MSG_ERROR, "getgrgid failed for %s: gid %i not found\n",
211 path, (int)sbuf.st_gid);
212 return NULL;
215 mentry = mentry_alloc();
216 mentry->path = xstrdup(path);
217 mentry->pathlen = strlen(mentry->path);
218 mentry->owner = xstrdup(pbuf->pw_name);
219 mentry->group = xstrdup(gbuf->gr_name);
220 mentry->mode = sbuf.st_mode & 0177777;
221 mentry->mtime = sbuf.st_mtim.tv_sec;
222 mentry->mtimensec = sbuf.st_mtim.tv_nsec;
224 /* symlinks have no xattrs */
225 if (S_ISLNK(mentry->mode))
226 return mentry;
228 lsize = listxattr(path, NULL, 0);
229 if (lsize < 0) {
230 /* Perhaps the FS doesn't support xattrs? */
231 if (errno == ENOTSUP)
232 return mentry;
234 msg(MSG_ERROR, "listxattr failed for %s: %s\n",
235 path, strerror(errno));
236 return NULL;
239 list = xmalloc(lsize);
240 lsize = listxattr(path, list, lsize);
241 if (lsize < 0) {
242 msg(MSG_ERROR, "listxattr failed for %s: %s\n",
243 path, strerror(errno));
244 free(list);
245 return NULL;
248 i = 0;
249 for (attr = list; attr < list + lsize; attr = strchr(attr, '\0') + 1) {
250 if (*attr == '\0')
251 continue;
252 i++;
255 if (i == 0)
256 return mentry;
258 mentry->xattrs = i;
259 mentry->xattr_names = xmalloc(i * sizeof(char *));
260 mentry->xattr_values = xmalloc(i * sizeof(char *));
261 mentry->xattr_lvalues = xmalloc(i * sizeof(ssize_t));
263 i = 0;
264 for (attr = list; attr < list + lsize; attr = strchr(attr, '\0') + 1) {
265 if (*attr == '\0')
266 continue;
268 mentry->xattr_names[i] = xstrdup(attr);
269 vsize = getxattr(path, attr, NULL, 0);
270 if (vsize < 0) {
271 msg(MSG_ERROR, "getxattr failed for %s: %s\n",
272 path, strerror(errno));
273 free(list);
274 mentry_free(mentry);
275 return NULL;
278 mentry->xattr_lvalues[i] = vsize;
279 mentry->xattr_values[i] = xmalloc(vsize);
281 vsize = getxattr(path, attr, mentry->xattr_values[i], vsize);
282 if (vsize < 0) {
283 msg(MSG_ERROR, "getxattr failed for %s: %s\n",
284 path, strerror(errno));
285 free(list);
286 mentry_free(mentry);
287 return NULL;
289 i++;
292 free(list);
293 return mentry;
296 /* Cleans up a path and makes it relative to current working dir unless it is absolute */
297 static char *
298 normalize_path(const char *orig)
300 char *real = realpath(orig, NULL);
301 char cwd[PATH_MAX];
302 char *result;
304 getcwd(cwd, PATH_MAX);
305 if (!real)
306 return NULL;
308 if (!strncmp(real, cwd, strlen(cwd))) {
309 result = xmalloc(strlen(real) - strlen(cwd) + 1 + 1);
310 result[0] = '\0';
311 strcat(result, ".");
312 strcat(result, real + strlen(cwd));
313 } else {
314 result = xstrdup(real);
317 free(real);
318 return result;
321 /* Internal function for the recursive path walk */
322 static void
323 mentries_recurse(const char *path, struct metahash *mhash, msettings *st)
325 struct stat sbuf;
326 struct metaentry *mentry;
327 char tpath[PATH_MAX];
328 DIR *dir;
329 struct dirent *dent;
331 if (!path)
332 return;
334 if (lstat(path, &sbuf)) {
335 msg(MSG_ERROR, "lstat failed for %s: %s\n",
336 path, strerror(errno));
337 return;
340 mentry = mentry_create(path);
341 if (!mentry)
342 return;
344 mentry_insert(mentry, mhash);
346 if (S_ISDIR(sbuf.st_mode)) {
347 dir = opendir(path);
348 if (!dir) {
349 msg(MSG_ERROR, "opendir failed for %s: %s\n",
350 path, strerror(errno));
351 return;
354 while ((dent = readdir(dir))) {
355 if (!strcmp(dent->d_name, ".") ||
356 !strcmp(dent->d_name, "..") ||
357 (!st->do_git && !strcmp(dent->d_name, ".git")))
358 continue;
359 snprintf(tpath, PATH_MAX, "%s/%s", path, dent->d_name);
360 tpath[PATH_MAX - 1] = '\0';
361 mentries_recurse(tpath, mhash, st);
364 closedir(dir);
368 /* Recurses opath and adds metadata entries to the metaentry list */
369 void
370 mentries_recurse_path(const char *opath, struct metahash **mhash, msettings *st)
372 char *path = normalize_path(opath);
374 if (!(*mhash))
375 *mhash = mhash_alloc();
376 mentries_recurse(path, *mhash, st);
377 free(path);
380 /* Stores metaentries to a file */
381 void
382 mentries_tofile(const struct metahash *mhash, const char *path)
384 FILE *to;
385 const struct metaentry *mentry;
386 int key;
387 unsigned i;
389 to = fopen(path, "w");
390 if (!to) {
391 msg(MSG_CRITICAL, "Failed to open %s: %s\n",
392 path, strerror(errno));
393 exit(EXIT_FAILURE);
396 write_binary_string(SIGNATURE, SIGNATURELEN, to);
397 write_binary_string(VERSION, VERSIONLEN, to);
399 for (key = 0; key < HASH_INDEXES; key++) {
400 for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) {
401 write_string(mentry->path, to);
402 write_string(mentry->owner, to);
403 write_string(mentry->group, to);
404 write_int((uint64_t)mentry->mtime, 8, to);
405 write_int((uint64_t)mentry->mtimensec, 8, to);
406 write_int((uint64_t)mentry->mode, 2, to);
407 write_int(mentry->xattrs, 4, to);
408 for (i = 0; i < mentry->xattrs; i++) {
409 write_string(mentry->xattr_names[i], to);
410 write_int(mentry->xattr_lvalues[i], 4, to);
411 write_binary_string(mentry->xattr_values[i],
412 mentry->xattr_lvalues[i], to);
417 fclose(to);
420 /* Creates a metaentry list from a file */
421 void
422 mentries_fromfile(struct metahash **mhash, const char *path)
424 struct metaentry *mentry;
425 char *mmapstart;
426 char *ptr;
427 char *max;
428 int fd;
429 struct stat sbuf;
430 unsigned i;
432 if (!(*mhash))
433 *mhash = mhash_alloc();
435 fd = open(path, O_RDONLY);
436 if (fd < 0) {
437 msg(MSG_CRITICAL, "Failed to open %s: %s\n",
438 path, strerror(errno));
439 exit(EXIT_FAILURE);
442 if (fstat(fd, &sbuf)) {
443 msg(MSG_CRITICAL, "Failed to stat %s: %s\n",
444 path, strerror(errno));
445 exit(EXIT_FAILURE);
448 if (sbuf.st_size < (SIGNATURELEN + VERSIONLEN)) {
449 msg(MSG_CRITICAL, "File %s has an invalid size\n", path);
450 exit(EXIT_FAILURE);
453 mmapstart = mmap(NULL, (size_t)sbuf.st_size, PROT_READ,
454 MAP_SHARED, fd, 0);
455 if (mmapstart == MAP_FAILED) {
456 msg(MSG_CRITICAL, "Unable to mmap %s: %s\n",
457 path, strerror(errno));
458 exit(EXIT_FAILURE);
460 ptr = mmapstart;
461 max = mmapstart + sbuf.st_size;
463 if (strncmp(ptr, SIGNATURE, SIGNATURELEN)) {
464 msg(MSG_CRITICAL, "Invalid signature for file %s\n", path);
465 goto out;
467 ptr += SIGNATURELEN;
469 if (strncmp(ptr, VERSION, VERSIONLEN)) {
470 msg(MSG_CRITICAL, "Invalid version of file %s\n", path);
471 goto out;
473 ptr += VERSIONLEN;
475 while (ptr < mmapstart + sbuf.st_size) {
476 if (*ptr == '\0') {
477 msg(MSG_CRITICAL, "Invalid characters in file %s\n",
478 path);
479 goto out;
482 mentry = mentry_alloc();
483 mentry->path = read_string(&ptr, max);
484 mentry->pathlen = strlen(mentry->path);
485 mentry->owner = read_string(&ptr, max);
486 mentry->group = read_string(&ptr, max);
487 mentry->mtime = (time_t)read_int(&ptr, 8, max);
488 mentry->mtimensec = (time_t)read_int(&ptr, 8, max);
489 mentry->mode = (mode_t)read_int(&ptr, 2, max);
490 mentry->xattrs = (unsigned int)read_int(&ptr, 4, max);
492 if (!mentry->xattrs) {
493 mentry_insert(mentry, *mhash);
494 continue;
497 mentry->xattr_names = xmalloc(mentry->xattrs *
498 sizeof(char *));
499 mentry->xattr_lvalues = xmalloc(mentry->xattrs *
500 sizeof(int));
501 mentry->xattr_values = xmalloc(mentry->xattrs *
502 sizeof(char *));
504 for (i = 0; i < mentry->xattrs; i++) {
505 mentry->xattr_names[i] = read_string(&ptr, max);
506 mentry->xattr_lvalues[i] =
507 (int)read_int(&ptr, 4, max);
508 mentry->xattr_values[i] =
509 read_binary_string(&ptr,
510 mentry->xattr_lvalues[i],
511 max);
513 mentry_insert(mentry, *mhash);
516 out:
517 munmap(mmapstart, sbuf.st_size);
518 close(fd);
521 /* Searches haystack for an xattr matching xattr number n in needle */
523 mentry_find_xattr(struct metaentry *haystack, struct metaentry *needle,
524 unsigned n)
526 unsigned i;
528 for (i = 0; i < haystack->xattrs; i++) {
529 if (strcmp(haystack->xattr_names[i], needle->xattr_names[n]))
530 continue;
531 if (haystack->xattr_lvalues[i] != needle->xattr_lvalues[n])
532 return -1;
533 if (bcmp(haystack->xattr_values[i], needle->xattr_values[n],
534 needle->xattr_lvalues[n]))
535 return -1;
536 return i;
538 return -1;
541 /* Returns zero if all xattrs in left and right match */
542 static int
543 mentry_compare_xattr(struct metaentry *left, struct metaentry *right)
545 unsigned i;
547 if (left->xattrs != right->xattrs)
548 return 1;
550 /* Make sure all xattrs in left are found in right and vice versa */
551 for (i = 0; i < left->xattrs; i++) {
552 if (mentry_find_xattr(right, left, i) < 0 ||
553 mentry_find_xattr(left, right, i) < 0) {
554 return 1;
558 return 0;
561 /* Compares two metaentries and returns an int with a bitmask of differences */
563 mentry_compare(struct metaentry *left, struct metaentry *right, msettings *st)
565 int retval = DIFF_NONE;
567 if (!left || !right) {
568 msg(MSG_ERROR, "%s called with empty list\n", __FUNCTION__);
569 return -1;
572 if (strcmp(left->path, right->path))
573 return -1;
575 if (strcmp(left->owner, right->owner))
576 retval |= DIFF_OWNER;
578 if (strcmp(left->group, right->group))
579 retval |= DIFF_GROUP;
581 if ((left->mode & 07777) != (right->mode & 07777))
582 retval |= DIFF_MODE;
584 if ((left->mode & S_IFMT) != (right->mode & S_IFMT))
585 retval |= DIFF_TYPE;
587 if (st->do_mtime && strcmp(left->path, st->metafile) &&
588 (left->mtime != right->mtime ||
589 left->mtimensec != right->mtimensec))
590 retval |= DIFF_MTIME;
592 if (mentry_compare_xattr(left, right)) {
593 retval |= DIFF_XATTR;
594 return retval;
597 return retval;
600 /* Compares lists of real and stored metadata and calls pfunc for each */
601 void
602 mentries_compare(struct metahash *mhashreal,
603 struct metahash *mhashstored,
604 void (*pfunc)
605 (struct metaentry *real, struct metaentry *stored, int cmp),
606 msettings *st)
608 struct metaentry *real, *stored;
609 int key;
611 if (!mhashreal || !mhashstored) {
612 msg(MSG_ERROR, "%s called with empty list\n", __FUNCTION__);
613 return;
616 for (key = 0; key < HASH_INDEXES; key++) {
617 for (real = mhashreal->bucket[key]; real; real = real->next) {
618 stored = mentry_find(real->path, mhashstored);
620 if (!stored)
621 pfunc(real, NULL, DIFF_ADDED);
622 else
623 pfunc(real, stored, mentry_compare(real, stored, st));
626 for (stored = mhashstored->bucket[key]; stored; stored = stored->next) {
627 real = mentry_find(stored->path, mhashreal);
629 if (!real)
630 pfunc(NULL, stored, DIFF_DELE);
635 /* Dumps given metadata */
636 void
637 mentries_dump(struct metahash *mhash)
639 const struct metaentry *mentry;
640 char mode[11 + 1] = "";
641 char date[12 + 2 + 2 + 2*1 + 1 + 2 + 2 + 2 + 2*1 + 1] = "";
642 char zone[5 + 1] = "";
643 struct tm cal;
645 for (int key = 0; key < HASH_INDEXES; key++) {
646 for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) {
647 strmode(mentry->mode, mode);
648 localtime_r(&mentry->mtime, &cal);
649 strftime(date, sizeof(date), "%F %T", &cal);
650 strftime(zone, sizeof(zone), "%z", &cal);
651 printf("%s\t%s\t%s\t%s.%09ld %s\t%s%s\n",
652 mode,
653 mentry->owner, mentry->group,
654 date, mentry->mtimensec, zone,
655 mentry->path, S_ISDIR(mentry->mode) ? "/" : "");
656 for (int i = 0; i < mentry->xattrs; i++) {
657 printf("\t\t\t\t%s%s\t%s=",
658 mentry->path, S_ISDIR(mentry->mode) ? "/" : "",
659 mentry->xattr_names[i]);
660 ssize_t p = 0;
661 for (; p < mentry->xattr_lvalues[i]; p++) {
662 const char ch = mentry->xattr_values[i][p];
663 if ((unsigned)(ch - 32) > 126 - 32) {
664 p = -1;
665 break;
668 if (p >= 0)
669 printf("\"%.*s\"\n",
670 (int)mentry->xattr_lvalues[i],
671 mentry->xattr_values[i]);
672 else {
673 printf("0x");
674 for (p = 0; p < mentry->xattr_lvalues[i]; p++)
675 printf("%02hhx", (char)mentry->xattr_values[i][p]);
676 printf("\n");