Support building with no extended attributes support.
[metastore.git] / src / metaentry.c
blob6c5bc84a257154a06431ab7f86dd2b230fdf5989
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Various functions to work with meta entries.
5 * Copyright (C) 2007-2008 David Härdeman <david@hardeman.nu>
6 * Copyright (C) 2012-2018 Przemyslaw Pawelczyk <przemoc@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; only version 2 of the License is applicable.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #define _GNU_SOURCE
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
29 #if !defined(NO_XATTR) || !NO_XATTR
30 # include <sys/xattr.h>
31 #endif /* !NO_XATTR */
33 #include <limits.h>
34 #include <dirent.h>
35 #include <sys/mman.h>
36 #include <utime.h>
37 #include <fcntl.h>
38 #include <stdint.h>
39 #include <errno.h>
40 #include <time.h>
42 #include <sys/param.h>
43 #ifndef BSD
44 # include <bsd/string.h>
45 #endif
47 #include "metastore.h"
48 #include "metaentry.h"
49 #include "utils.h"
51 #ifndef PATH_MAX
52 # define PATH_MAX 4096
53 #endif
55 #if !defined(NO_XATTR) || !NO_XATTR
56 /* Free's a metaentry and all its parameters */
57 static void
58 mentry_free(struct metaentry *m)
60 unsigned i;
62 if (!m)
63 return;
65 free(m->path);
66 free(m->owner);
67 free(m->group);
69 for (i = 0; i < m->xattrs; i++) {
70 free(m->xattr_names[i]);
71 free(m->xattr_values[i]);
74 free(m->xattr_names);
75 free(m->xattr_values);
76 free(m->xattr_lvalues);
78 free(m);
80 #endif /* !NO_XATTR */
82 /* Allocates an empty metahash table */
83 static struct metahash *
84 mhash_alloc(void)
86 struct metahash *mhash;
87 mhash = xmalloc(sizeof(struct metahash));
88 memset(mhash, 0, sizeof(struct metahash));
89 return mhash;
92 /* Generates a hash key (using djb2) */
93 static unsigned
94 hash(const char *str)
96 unsigned hash = 5381;
97 int c;
99 while ((c = *str++))
100 hash = ((hash << 5) + hash) + c;
102 return hash % HASH_INDEXES;
105 /* Allocates an empty metaentry */
106 static struct metaentry *
107 mentry_alloc(void)
109 struct metaentry *mentry;
110 mentry = xmalloc(sizeof(struct metaentry));
111 memset(mentry, 0, sizeof(struct metaentry));
112 return mentry;
115 /* Does a bisect search for the closest match in a metaentry list */
116 static struct metaentry *
117 mentry_find(const char *path, struct metahash *mhash)
119 struct metaentry *base;
120 unsigned key;
122 if (!mhash) {
123 msg(MSG_ERROR, "%s called with empty hash table\n", __func__);
124 return NULL;
127 key = hash(path);
128 for (base = mhash->bucket[key]; base; base = base->next) {
129 if (!strcmp(base->path, path))
130 return base;
133 return NULL;
136 /* Inserts a metaentry into a metaentry list */
137 static void
138 mentry_insert(struct metaentry *mentry, struct metahash *mhash)
140 unsigned key;
142 key = hash(mentry->path);
143 mentry->next = mhash->bucket[key];
144 mhash->bucket[key] = mentry;
147 #ifdef DEBUG
148 /* Prints a metaentry */
149 static void
150 mentry_print(const struct metaentry *mentry)
152 int i;
154 if (!mentry || !mentry->path) {
155 msg(MSG_DEBUG,
156 "Incorrect meta entry passed to printmetaentry\n");
157 return;
160 msg(MSG_DEBUG, "===========================\n");
161 msg(MSG_DEBUG, "Dump of metaentry %p\n", mentry);
162 msg(MSG_DEBUG, "===========================\n");
164 msg(MSG_DEBUG, "path\t\t: %s\n", mentry->path);
165 msg(MSG_DEBUG, "owner\t\t: %s\n", mentry->owner);
166 msg(MSG_DEBUG, "group\t\t: %s\n", mentry->group);
167 msg(MSG_DEBUG, "mtime\t\t: %ld\n", (unsigned long)mentry->mtime);
168 msg(MSG_DEBUG, "mtimensec\t: %ld\n", (unsigned long)mentry->mtimensec);
169 msg(MSG_DEBUG, "mode\t\t: %ld\n", (unsigned long)mentry->mode);
170 for (i = 0; i < mentry->xattrs; i++) {
171 msg(MSG_DEBUG, "xattr[%i]\t: %s=\"", i, mentry->xattr_names[i]);
172 binary_print(mentry->xattr_values[i], mentry->xattr_lvalues[i]);
173 msg(MSG_DEBUG, "\"\n");
176 msg(MSG_DEBUG, "===========================\n\n");
179 /* Prints all metaentries in a metaentry list */
180 static void
181 mentries_print(const struct metahash *mhash)
183 const struct metaentry *mentry;
184 int index;
186 for (index = 0; index < HASH_INDEXES; index++)
187 for (mentry = mhash->bucket[index]; mentry; mentry = mentry->next)
188 mentry_print(mentry);
190 msg(MSG_DEBUG, "%i entries in total\n", mhash->count);
192 #endif
194 /* Creates a metaentry for the file/dir/etc at path */
195 struct metaentry *
196 mentry_create(const char *path)
198 #if !defined(NO_XATTR) || !NO_XATTR
199 ssize_t lsize, vsize;
200 char *list, *attr;
201 #endif /* !NO_XATTR */
202 struct stat sbuf;
203 struct passwd *pbuf;
204 struct group *gbuf;
205 #if !defined(NO_XATTR) || !NO_XATTR
206 int i;
207 #endif /* !NO_XATTR */
208 struct metaentry *mentry;
210 if (lstat(path, &sbuf)) {
211 msg(MSG_ERROR, "lstat failed for %s: %s\n",
212 path, strerror(errno));
213 return NULL;
216 pbuf = xgetpwuid(sbuf.st_uid);
217 if (!pbuf) {
218 msg(MSG_ERROR, "getpwuid failed for %s: uid %i not found\n",
219 path, (int)sbuf.st_uid);
220 return NULL;
223 gbuf = xgetgrgid(sbuf.st_gid);
224 if (!gbuf) {
225 msg(MSG_ERROR, "getgrgid failed for %s: gid %i not found\n",
226 path, (int)sbuf.st_gid);
227 return NULL;
230 mentry = mentry_alloc();
231 mentry->path = xstrdup(path);
232 mentry->pathlen = strlen(mentry->path);
233 mentry->owner = xstrdup(pbuf->pw_name);
234 mentry->group = xstrdup(gbuf->gr_name);
235 mentry->mode = sbuf.st_mode & 0177777;
236 mentry->mtime = sbuf.st_mtim.tv_sec;
237 mentry->mtimensec = sbuf.st_mtim.tv_nsec;
239 /* symlinks have no xattrs */
240 if (S_ISLNK(mentry->mode))
241 return mentry;
243 #if !defined(NO_XATTR) || !NO_XATTR
244 lsize = listxattr(path, NULL, 0);
245 if (lsize < 0) {
246 /* Perhaps the FS doesn't support xattrs? */
247 if (errno == ENOTSUP)
248 return mentry;
250 msg(MSG_ERROR, "listxattr failed for %s: %s\n",
251 path, strerror(errno));
252 return NULL;
255 list = xmalloc(lsize);
256 lsize = listxattr(path, list, lsize);
257 if (lsize < 0) {
258 msg(MSG_ERROR, "listxattr failed for %s: %s\n",
259 path, strerror(errno));
260 free(list);
261 return NULL;
264 i = 0;
265 for (attr = list; attr < list + lsize; attr = strchr(attr, '\0') + 1) {
266 if (*attr == '\0')
267 continue;
268 i++;
271 if (i == 0)
272 return mentry;
274 mentry->xattrs = i;
275 mentry->xattr_names = xmalloc(i * sizeof(char *));
276 mentry->xattr_values = xmalloc(i * sizeof(char *));
277 mentry->xattr_lvalues = xmalloc(i * sizeof(ssize_t));
279 i = 0;
280 for (attr = list; attr < list + lsize; attr = strchr(attr, '\0') + 1) {
281 if (*attr == '\0')
282 continue;
284 mentry->xattr_names[i] = xstrdup(attr);
285 mentry->xattr_values[i] = NULL;
287 vsize = getxattr(path, attr, NULL, 0);
288 if (vsize < 0) {
289 msg(MSG_ERROR, "getxattr failed for %s: %s\n",
290 path, strerror(errno));
291 free(list);
292 mentry->xattrs = i + 1;
293 mentry_free(mentry);
294 return NULL;
297 mentry->xattr_lvalues[i] = vsize;
298 mentry->xattr_values[i] = xmalloc(vsize);
300 vsize = getxattr(path, attr, mentry->xattr_values[i], vsize);
301 if (vsize < 0) {
302 msg(MSG_ERROR, "getxattr failed for %s: %s\n",
303 path, strerror(errno));
304 free(list);
305 mentry->xattrs = i + 1;
306 mentry_free(mentry);
307 return NULL;
309 i++;
312 free(list);
313 #endif /* !NO_XATTR */
315 return mentry;
318 /* Cleans up a path and makes it relative to cwd unless it is absolute */
319 static char *
320 normalize_path(const char *orig)
322 char *real = realpath(orig, NULL);
323 char cwd[PATH_MAX];
324 char *result;
326 getcwd(cwd, PATH_MAX);
327 if (!real)
328 return NULL;
330 if (!strncmp(real, cwd, strlen(cwd))) {
331 result = xmalloc(strlen(real) - strlen(cwd) + 1 + 1);
332 result[0] = '\0';
333 strcat(result, ".");
334 strcat(result, real + strlen(cwd));
335 } else {
336 result = xstrdup(real);
339 free(real);
340 return result;
343 /* Internal function for the recursive path walk */
344 static void
345 mentries_recurse(const char *path, struct metahash *mhash, msettings *st)
347 struct stat sbuf;
348 struct metaentry *mentry;
349 char tpath[PATH_MAX];
350 DIR *dir;
351 struct dirent *dent;
353 if (!path)
354 return;
356 if (lstat(path, &sbuf)) {
357 msg(MSG_ERROR, "lstat failed for %s: %s\n",
358 path, strerror(errno));
359 return;
362 mentry = mentry_create(path);
363 if (!mentry)
364 return;
366 mentry_insert(mentry, mhash);
368 if (S_ISDIR(sbuf.st_mode)) {
369 dir = opendir(path);
370 if (!dir) {
371 msg(MSG_ERROR, "opendir failed for %s: %s\n",
372 path, strerror(errno));
373 return;
376 while ((dent = readdir(dir))) {
377 if (!strcmp(dent->d_name, ".") ||
378 !strcmp(dent->d_name, "..") ||
379 (!st->do_git && !strcmp(dent->d_name, ".git"))
381 continue;
382 snprintf(tpath, PATH_MAX, "%s/%s", path, dent->d_name);
383 tpath[PATH_MAX - 1] = '\0';
384 mentries_recurse(tpath, mhash, st);
387 closedir(dir);
391 /* Recurses opath and adds metadata entries to the metaentry list */
392 void
393 mentries_recurse_path(const char *opath, struct metahash **mhash, msettings *st)
395 char *path = normalize_path(opath);
397 if (!(*mhash))
398 *mhash = mhash_alloc();
399 mentries_recurse(path, *mhash, st);
400 free(path);
403 /* Stores metaentries to a file */
404 void
405 mentries_tofile(const struct metahash *mhash, const char *path)
407 FILE *to;
408 const struct metaentry *mentry;
409 int key;
410 unsigned i;
412 to = fopen(path, "w");
413 if (!to) {
414 msg(MSG_CRITICAL, "Failed to open %s: %s\n",
415 path, strerror(errno));
416 exit(EXIT_FAILURE);
419 write_binary_string(SIGNATURE, SIGNATURELEN, to);
420 write_binary_string(VERSION, VERSIONLEN, to);
422 for (key = 0; key < HASH_INDEXES; key++) {
423 for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) {
424 write_string(mentry->path, to);
425 write_string(mentry->owner, to);
426 write_string(mentry->group, to);
427 write_int((uint64_t)mentry->mtime, 8, to);
428 write_int((uint64_t)mentry->mtimensec, 8, to);
429 write_int((uint64_t)mentry->mode, 2, to);
430 write_int(mentry->xattrs, 4, to);
431 for (i = 0; i < mentry->xattrs; i++) {
432 write_string(mentry->xattr_names[i], to);
433 write_int(mentry->xattr_lvalues[i], 4, to);
434 write_binary_string(mentry->xattr_values[i],
435 mentry->xattr_lvalues[i], to);
440 fclose(to);
443 /* Creates a metaentry list from a file */
444 void
445 mentries_fromfile(struct metahash **mhash, const char *path)
447 struct metaentry *mentry;
448 char *mmapstart;
449 char *ptr;
450 char *max;
451 int fd;
452 struct stat sbuf;
453 unsigned i;
455 if (!(*mhash))
456 *mhash = mhash_alloc();
458 fd = open(path, O_RDONLY);
459 if (fd < 0) {
460 msg(MSG_CRITICAL, "Failed to open %s: %s\n",
461 path, strerror(errno));
462 exit(EXIT_FAILURE);
465 if (fstat(fd, &sbuf)) {
466 msg(MSG_CRITICAL, "Failed to stat %s: %s\n",
467 path, strerror(errno));
468 exit(EXIT_FAILURE);
471 if (sbuf.st_size < (SIGNATURELEN + VERSIONLEN)) {
472 msg(MSG_CRITICAL, "File %s has an invalid size\n", path);
473 exit(EXIT_FAILURE);
476 mmapstart = mmap(NULL, (size_t)sbuf.st_size, PROT_READ,
477 MAP_SHARED, fd, 0);
478 if (mmapstart == MAP_FAILED) {
479 msg(MSG_CRITICAL, "Unable to mmap %s: %s\n",
480 path, strerror(errno));
481 exit(EXIT_FAILURE);
483 ptr = mmapstart;
484 max = mmapstart + sbuf.st_size;
486 if (strncmp(ptr, SIGNATURE, SIGNATURELEN)) {
487 msg(MSG_CRITICAL, "Invalid signature for file %s\n", path);
488 goto out;
490 ptr += SIGNATURELEN;
492 if (strncmp(ptr, VERSION, VERSIONLEN)) {
493 msg(MSG_CRITICAL, "Invalid version of file %s\n", path);
494 goto out;
496 ptr += VERSIONLEN;
498 while (ptr < mmapstart + sbuf.st_size) {
499 if (*ptr == '\0') {
500 msg(MSG_CRITICAL, "Invalid characters in file %s\n",
501 path);
502 goto out;
505 mentry = mentry_alloc();
506 mentry->path = read_string(&ptr, max);
507 mentry->pathlen = strlen(mentry->path);
508 mentry->owner = read_string(&ptr, max);
509 mentry->group = read_string(&ptr, max);
510 mentry->mtime = (time_t)read_int(&ptr, 8, max);
511 mentry->mtimensec = (time_t)read_int(&ptr, 8, max);
512 mentry->mode = (mode_t)read_int(&ptr, 2, max);
513 mentry->xattrs = (unsigned)read_int(&ptr, 4, max);
515 if (!mentry->xattrs) {
516 mentry_insert(mentry, *mhash);
517 continue;
520 mentry->xattr_names = xmalloc(mentry->xattrs * sizeof(char *));
521 mentry->xattr_lvalues = xmalloc(mentry->xattrs * sizeof(ssize_t));
522 mentry->xattr_values = xmalloc(mentry->xattrs * sizeof(char *));
524 for (i = 0; i < mentry->xattrs; i++) {
525 mentry->xattr_names[i] = read_string(&ptr, max);
526 mentry->xattr_lvalues[i] = (int)read_int(&ptr, 4, max);
527 mentry->xattr_values[i] = read_binary_string(
528 &ptr,
529 mentry->xattr_lvalues[i],
533 mentry_insert(mentry, *mhash);
536 out:
537 munmap(mmapstart, sbuf.st_size);
538 close(fd);
541 /* Searches haystack for an xattr matching xattr number n in needle */
543 mentry_find_xattr(struct metaentry *haystack, struct metaentry *needle,
544 unsigned n)
546 unsigned i;
548 for (i = 0; i < haystack->xattrs; i++) {
549 if (strcmp(haystack->xattr_names[i], needle->xattr_names[n]))
550 continue;
551 if (haystack->xattr_lvalues[i] != needle->xattr_lvalues[n])
552 return -1;
553 if (bcmp(haystack->xattr_values[i], needle->xattr_values[n],
554 needle->xattr_lvalues[n])
556 return -1;
557 return i;
559 return -1;
562 /* Returns zero if all xattrs in left and right match */
563 static int
564 mentry_compare_xattr(struct metaentry *left, struct metaentry *right)
566 unsigned i;
568 if (left->xattrs != right->xattrs)
569 return 1;
571 /* Make sure all xattrs in left are found in right and vice versa */
572 for (i = 0; i < left->xattrs; i++) {
573 if ( mentry_find_xattr(right, left, i) < 0
574 || mentry_find_xattr(left, right, i) < 0
576 return 1;
580 return 0;
583 /* Compares two metaentries and returns an int with a bitmask of differences */
585 mentry_compare(struct metaentry *left, struct metaentry *right, msettings *st)
587 int retval = DIFF_NONE;
589 if (!left || !right) {
590 msg(MSG_ERROR, "%s called with empty list\n", __func__);
591 return -1;
594 if (strcmp(left->path, right->path))
595 return -1;
597 if (strcmp(left->owner, right->owner))
598 retval |= DIFF_OWNER;
600 if (strcmp(left->group, right->group))
601 retval |= DIFF_GROUP;
603 if ((left->mode & 07777) != (right->mode & 07777))
604 retval |= DIFF_MODE;
606 if ((left->mode & S_IFMT) != (right->mode & S_IFMT))
607 retval |= DIFF_TYPE;
609 if (st->do_mtime && strcmp(left->path, st->metafile) &&
610 ( left->mtime != right->mtime
611 || left->mtimensec != right->mtimensec)
613 retval |= DIFF_MTIME;
615 if (mentry_compare_xattr(left, right)) {
616 retval |= DIFF_XATTR;
617 return retval;
620 return retval;
623 /* Compares lists of real and stored metadata and calls pfunc for each */
624 void
625 mentries_compare(struct metahash *mhashreal,
626 struct metahash *mhashstored,
627 void (*pfunc)
628 (struct metaentry *real, struct metaentry *stored, int cmp),
629 msettings *st)
631 struct metaentry *real, *stored;
632 int key;
634 if (!mhashreal || !mhashstored) {
635 msg(MSG_ERROR, "%s called with empty list\n", __func__);
636 return;
639 for (key = 0; key < HASH_INDEXES; key++) {
640 for (real = mhashreal->bucket[key]; real; real = real->next) {
641 stored = mentry_find(real->path, mhashstored);
643 if (!stored)
644 pfunc(real, NULL, DIFF_ADDED);
645 else
646 pfunc(real, stored, mentry_compare(real, stored, st));
649 for (stored = mhashstored->bucket[key]; stored; stored = stored->next) {
650 real = mentry_find(stored->path, mhashreal);
652 if (!real)
653 pfunc(NULL, stored, DIFF_DELE);
658 /* Dumps given metadata */
659 void
660 mentries_dump(struct metahash *mhash)
662 const struct metaentry *mentry;
663 char mode[11 + 1] = "";
664 char date[12 + 2 + 2 + 2*1 + 1 + 2 + 2 + 2 + 2*1 + 1] = "";
665 char zone[5 + 1] = "";
666 struct tm cal;
668 for (int key = 0; key < HASH_INDEXES; key++) {
669 for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) {
670 strmode(mentry->mode, mode);
671 localtime_r(&mentry->mtime, &cal);
672 strftime(date, sizeof(date), "%F %T", &cal);
673 strftime(zone, sizeof(zone), "%z", &cal);
674 printf("%s\t%s\t%s\t%s.%09ld %s\t%s%s\n",
675 mode,
676 mentry->owner, mentry->group,
677 date, mentry->mtimensec, zone,
678 mentry->path, S_ISDIR(mentry->mode) ? "/" : "");
679 for (unsigned i = 0; i < mentry->xattrs; i++) {
680 printf("\t\t\t\t%s%s\t%s=",
681 mentry->path, S_ISDIR(mentry->mode) ? "/" : "",
682 mentry->xattr_names[i]);
683 ssize_t p = 0;
684 for (; p < mentry->xattr_lvalues[i]; p++) {
685 const char ch = mentry->xattr_values[i][p];
686 if ((unsigned)(ch - 32) > 126 - 32) {
687 p = -1;
688 break;
691 if (p >= 0)
692 printf("\"%.*s\"\n",
693 (int)mentry->xattr_lvalues[i],
694 mentry->xattr_values[i]);
695 else {
696 printf("0x");
697 for (p = 0; p < mentry->xattr_lvalues[i]; p++)
698 printf("%02hhx", (char)mentry->xattr_values[i][p]);
699 printf("\n");