Add optimization flags to gcc
[metastore.git] / metaentry.c
blobe9d2566afa7340028211630722936e889b910b4a
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 version 2 of the License.
9 *
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.
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>
28 #include <attr/xattr.h>
29 #include <limits.h>
30 #include <dirent.h>
31 #include <sys/mman.h>
32 #include <utime.h>
33 #include <fcntl.h>
34 #include <stdint.h>
36 #include "metastore.h"
37 #include "metaentry.h"
38 #include "utils.h"
40 /* Free's a metaentry and all its parameters */
41 static void
42 mentry_free(struct metaentry *m)
44 int i;
46 if (!m)
47 return;
49 free(m->path);
50 free(m->owner);
51 free(m->group);
53 for (i = 0; i < m->xattrs; i++) {
54 free(m->xattr_names[i]);
55 free(m->xattr_values[i]);
58 free(m->xattr_names);
59 free(m->xattr_values);
60 free(m->xattr_lvalues);
62 free(m);
65 /* Allocates an empty metahash table */
66 static struct metahash *
67 mhash_alloc()
69 struct metahash *mhash;
70 mhash = xmalloc(sizeof(struct metahash));
71 memset(mhash, 0, sizeof(struct metahash));
72 return mhash;
75 /* Generates a hash key (using djb2) */
76 static unsigned int
77 hash(const char *str)
79 unsigned int hash = 5381;
80 int c;
82 while ((c = *str++))
83 hash = ((hash << 5) + hash) + c;
85 return hash % HASH_INDEXES;
88 /* Allocates an empty metaentry */
89 static struct metaentry *
90 mentry_alloc()
92 struct metaentry *mentry;
93 mentry = xmalloc(sizeof(struct metaentry));
94 memset(mentry, 0, sizeof(struct metaentry));
95 return mentry;
98 /* Does a bisect search for the closest match in a metaentry list */
99 struct metaentry *
100 mentry_find(const char *path, struct metahash *mhash)
102 struct metaentry *base;
103 unsigned int key;
105 if (!mhash) {
106 msg(MSG_ERROR, "%s called with empty hash table\n", __FUNCTION__);
107 return NULL;
110 key = hash(path);
111 for (base = mhash->bucket[key]; base; base = base->next) {
112 if (!strcmp(base->path, path))
113 return base;
116 return NULL;
119 /* Inserts a metaentry into a metaentry list */
120 static void
121 mentry_insert(struct metaentry *mentry, struct metahash *mhash)
123 struct metaentry *base;
124 unsigned int key;
126 key = hash(mentry->path);
127 if (!mhash->bucket[key]) {
128 mhash->bucket[key] = mentry;
129 return;
132 for (base = mhash->bucket[key]; base->next; base = base->next)
133 /* Do nothing */;
134 base->next = mentry;
137 #ifdef DEBUG
138 /* Prints a metaentry */
139 static void
140 mentry_print(const struct metaentry *mentry)
142 int i;
144 if (!mentry || !mentry->path) {
145 msg(MSG_DEBUG,
146 "Incorrect meta entry passed to printmetaentry\n");
147 return;
150 msg(MSG_DEBUG, "===========================\n");
151 msg(MSG_DEBUG, "Dump of metaentry %p\n", mentry);
152 msg(MSG_DEBUG, "===========================\n");
154 msg(MSG_DEBUG, "path\t\t: %s\n", mentry->path);
155 msg(MSG_DEBUG, "owner\t\t: %s\n", mentry->owner);
156 msg(MSG_DEBUG, "group\t\t: %s\n", mentry->group);
157 msg(MSG_DEBUG, "mtime\t\t: %ld\n", (unsigned long)mentry->mtime);
158 msg(MSG_DEBUG, "mtimensec\t: %ld\n", (unsigned long)mentry->mtimensec);
159 msg(MSG_DEBUG, "mode\t\t: %ld\n", (unsigned long)mentry->mode);
160 for (i = 0; i < mentry->xattrs; i++) {
161 msg(MSG_DEBUG, "xattr[%i]\t: %s=\"", i, mentry->xattr_names[i]);
162 binary_print(mentry->xattr_values[i], mentry->xattr_lvalues[i]);
163 msg(MSG_DEBUG, "\"\n");
166 msg(MSG_DEBUG, "===========================\n\n");
169 /* Prints all metaentries in a metaentry list */
170 static void
171 mentries_print(const struct metahash *mhash)
173 const struct metaentry *mentry;
174 int index;
176 for (index = 0; index < HASH_INDEXES; index++)
177 for (mentry = mhash->bucket[i]; mentry; mentry = mentry->next)
178 mentry_print(mentry);
180 msg(MSG_DEBUG, "%i entries in total\n", mhash->count);
182 #endif
184 /* Creates a metaentry for the file/dir/etc at path */
185 static struct metaentry *
186 mentry_create(const char *path)
188 ssize_t lsize, vsize;
189 char *list, *attr;
190 struct stat sbuf;
191 struct passwd *pbuf;
192 struct group *gbuf;
193 int i;
194 struct metaentry *mentry;
196 if (lstat(path, &sbuf)) {
197 msg(MSG_ERROR, "lstat failed for %s: %s\n",
198 path, strerror(errno));
199 return NULL;
202 pbuf = xgetpwuid(sbuf.st_uid);
203 if (!pbuf) {
204 msg(MSG_ERROR, "getpwuid failed for %i: %s\n",
205 (int)sbuf.st_uid, strerror(errno));
206 return NULL;
209 gbuf = xgetgrgid(sbuf.st_gid);
210 if (!gbuf) {
211 msg(MSG_ERROR, "getgrgid failed for %i: %s\n",
212 (int)sbuf.st_gid, strerror(errno));
213 return NULL;
216 mentry = mentry_alloc();
217 mentry->path = xstrdup(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 msg(MSG_ERROR, "listxattr failed for %s: %s\n",
231 path, strerror(errno));
232 return NULL;
235 list = xmalloc(lsize);
236 lsize = listxattr(path, list, lsize);
237 if (lsize < 0) {
238 msg(MSG_ERROR, "listxattr failed for %s: %s\n",
239 path, strerror(errno));
240 free(list);
241 return NULL;
244 i = 0;
245 for (attr = list; attr < list + lsize; attr = strchr(attr, '\0') + 1) {
246 if (*attr == '\0')
247 continue;
248 i++;
251 if (i == 0)
252 return mentry;
254 mentry->xattrs = i;
255 mentry->xattr_names = xmalloc(i * sizeof(char *));
256 mentry->xattr_values = xmalloc(i * sizeof(char *));
257 mentry->xattr_lvalues = xmalloc(i * sizeof(ssize_t));
259 i = 0;
260 for (attr = list; attr < list + lsize; attr = strchr(attr, '\0') + 1) {
261 if (*attr == '\0')
262 continue;
264 mentry->xattr_names[i] = xstrdup(attr);
265 vsize = getxattr(path, attr, NULL, 0);
266 if (vsize < 0) {
267 msg(MSG_ERROR, "getxattr failed for %s: %s\n",
268 path, strerror(errno));
269 free(list);
270 mentry_free(mentry);
271 return NULL;
274 mentry->xattr_lvalues[i] = vsize;
275 mentry->xattr_values[i] = xmalloc(vsize);
277 vsize = getxattr(path, attr, mentry->xattr_values[i], vsize);
278 if (vsize < 0) {
279 msg(MSG_ERROR, "getxattr failed for %s: %s\n",
280 path, strerror(errno));
281 free(list);
282 mentry_free(mentry);
283 return NULL;
285 i++;
288 free(list);
289 return mentry;
292 /* Cleans up a path and makes it relative to current working dir unless it is absolute */
293 static char *
294 normalize_path(const char *orig)
296 char *real = canonicalize_file_name(orig);
297 char cwd[PATH_MAX];
298 char *result;
300 getcwd(cwd, PATH_MAX);
301 if (!real)
302 return NULL;
304 if (!strncmp(real, cwd, strlen(cwd))) {
305 result = xmalloc(strlen(real) - strlen(cwd) + 1 + 1);
306 result[0] = '\0';
307 strcat(result, ".");
308 strcat(result, real + strlen(cwd));
309 } else {
310 result = xstrdup(real);
313 free(real);
314 return result;
317 /* Internal function for the recursive path walk */
318 static void
319 mentries_recurse(const char *path, struct metahash *mhash)
321 struct stat sbuf;
322 struct metaentry *mentry;
323 char tpath[PATH_MAX];
324 DIR *dir;
325 struct dirent *dent;
327 if (!path)
328 return;
330 if (lstat(path, &sbuf)) {
331 msg(MSG_ERROR, "lstat failed for %s: %s\n",
332 path, strerror(errno));
333 return;
336 mentry = mentry_create(path);
337 if (!mentry)
338 return;
340 mentry_insert(mentry, mhash);
342 if (S_ISDIR(sbuf.st_mode)) {
343 dir = opendir(path);
344 if (!dir) {
345 msg(MSG_ERROR, "opendir failed for %s: %s\n",
346 path, strerror(errno));
347 return;
350 while ((dent = readdir(dir))) {
351 if (!strcmp(dent->d_name, ".") ||
352 !strcmp(dent->d_name, "..") ||
353 !strcmp(dent->d_name, ".git"))
354 continue;
355 snprintf(tpath, PATH_MAX, "%s/%s", path, dent->d_name);
356 tpath[PATH_MAX - 1] = '\0';
357 mentries_recurse(tpath, mhash);
360 closedir(dir);
364 /* Recurses opath and adds metadata entries to the metaentry list */
365 void
366 mentries_recurse_path(const char *opath, struct metahash **mhash)
368 char *path = normalize_path(opath);
370 if (!(*mhash))
371 *mhash = mhash_alloc();
372 mentries_recurse(path, *mhash);
373 free(path);
376 /* Stores metaentries to a file */
377 void
378 mentries_tofile(const struct metahash *mhash, const char *path)
380 FILE *to;
381 const struct metaentry *mentry;
382 int key, i;
384 to = fopen(path, "w");
385 if (!to) {
386 msg(MSG_CRITICAL, "Failed to open %s: %s\n",
387 path, strerror(errno));
388 exit(EXIT_FAILURE);
391 write_binary_string(SIGNATURE, SIGNATURELEN, to);
392 write_binary_string(VERSION, VERSIONLEN, to);
394 for (key = 0; key < HASH_INDEXES; key++) {
395 for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) {
396 write_string(mentry->path, to);
397 write_string(mentry->owner, to);
398 write_string(mentry->group, to);
399 write_int((uint64_t)mentry->mtime, 8, to);
400 write_int((uint64_t)mentry->mtimensec, 8, to);
401 write_int((uint64_t)mentry->mode, 2, to);
402 write_int(mentry->xattrs, 4, to);
403 for (i = 0; i < mentry->xattrs; i++) {
404 write_string(mentry->xattr_names[i], to);
405 write_int(mentry->xattr_lvalues[i], 4, to);
406 write_binary_string(mentry->xattr_values[i],
407 mentry->xattr_lvalues[i], to);
412 fclose(to);
415 /* Creates a metaentry list from a file */
416 void
417 mentries_fromfile(struct metahash **mhash, const char *path)
419 struct metaentry *mentry;
420 char *mmapstart;
421 char *ptr;
422 char *max;
423 int fd;
424 struct stat sbuf;
425 int i;
427 if (!(*mhash))
428 *mhash = mhash_alloc();
430 fd = open(path, O_RDONLY);
431 if (fd < 0) {
432 msg(MSG_CRITICAL, "Failed to open %s: %s\n",
433 path, strerror(errno));
434 exit(EXIT_FAILURE);
437 if (fstat(fd, &sbuf)) {
438 msg(MSG_CRITICAL, "Failed to stat %s: %s\n",
439 path, strerror(errno));
440 exit(EXIT_FAILURE);
443 if (sbuf.st_size < (SIGNATURELEN + VERSIONLEN)) {
444 msg(MSG_CRITICAL, "File %s has an invalid size\n", path);
445 exit(EXIT_FAILURE);
448 mmapstart = mmap(NULL, (size_t)sbuf.st_size, PROT_READ,
449 MAP_SHARED, fd, 0);
450 if (mmapstart == MAP_FAILED) {
451 msg(MSG_CRITICAL, "Unable to mmap %s: %s\n",
452 path, strerror(errno));
453 exit(EXIT_FAILURE);
455 ptr = mmapstart;
456 max = mmapstart + sbuf.st_size;
458 if (strncmp(ptr, SIGNATURE, SIGNATURELEN)) {
459 msg(MSG_CRITICAL, "Invalid signature for file %s\n", path);
460 goto out;
462 ptr += SIGNATURELEN;
464 if (strncmp(ptr, VERSION, VERSIONLEN)) {
465 msg(MSG_CRITICAL, "Invalid version of file %s\n", path);
466 goto out;
468 ptr += VERSIONLEN;
470 while (ptr < mmapstart + sbuf.st_size) {
471 if (*ptr == '\0') {
472 msg(MSG_CRITICAL, "Invalid characters in file %s\n",
473 path);
474 goto out;
477 mentry = mentry_alloc();
478 mentry->path = read_string(&ptr, max);
479 mentry->owner = read_string(&ptr, max);
480 mentry->group = read_string(&ptr, max);
481 mentry->mtime = (time_t)read_int(&ptr, 8, max);
482 mentry->mtimensec = (time_t)read_int(&ptr, 8, max);
483 mentry->mode = (mode_t)read_int(&ptr, 2, max);
484 mentry->xattrs = (unsigned int)read_int(&ptr, 4, max);
486 if (!mentry->xattrs) {
487 mentry_insert(mentry, *mhash);
488 continue;
491 mentry->xattr_names = xmalloc(mentry->xattrs *
492 sizeof(char *));
493 mentry->xattr_lvalues = xmalloc(mentry->xattrs *
494 sizeof(int));
495 mentry->xattr_values = xmalloc(mentry->xattrs *
496 sizeof(char *));
498 for (i = 0; i < mentry->xattrs; i++) {
499 mentry->xattr_names[i] = read_string(&ptr, max);
500 mentry->xattr_lvalues[i] =
501 (int)read_int(&ptr, 4, max);
502 mentry->xattr_values[i] =
503 read_binary_string(&ptr,
504 mentry->xattr_lvalues[i],
505 max);
507 mentry_insert(mentry, *mhash);
510 out:
511 munmap(mmapstart, sbuf.st_size);
512 close(fd);
515 /* Searches haystack for an xattr matching xattr number n in needle */
517 mentry_find_xattr(struct metaentry *haystack, struct metaentry *needle, int n)
519 int i;
521 for (i = 0; i < haystack->xattrs; i++) {
522 if (strcmp(haystack->xattr_names[i], needle->xattr_names[n]))
523 continue;
524 if (haystack->xattr_lvalues[i] != needle->xattr_lvalues[n])
525 return -1;
526 if (bcmp(haystack->xattr_values[i], needle->xattr_values[n],
527 needle->xattr_lvalues[n]))
528 return -1;
529 return i;
531 return -1;
534 /* Returns zero if all xattrs in left and right match */
535 static int
536 mentry_compare_xattr(struct metaentry *left, struct metaentry *right)
538 int i;
540 if (left->xattrs != right->xattrs)
541 return 1;
543 /* Make sure all xattrs in left are found in right and vice versa */
544 for (i = 0; i < left->xattrs; i++) {
545 if (mentry_find_xattr(right, left, i) < 0 ||
546 mentry_find_xattr(left, right, i) < 0) {
547 return 1;
551 return 0;
554 /* Compares two metaentries and returns an int with a bitmask of differences */
555 static int
556 mentry_compare(struct metaentry *left, struct metaentry *right, int do_mtime)
558 int retval = DIFF_NONE;
560 if (!left || !right) {
561 msg(MSG_ERROR, "%s called with empty list\n", __FUNCTION__);
562 return -1;
565 if (strcmp(left->path, right->path))
566 return -1;
568 if (strcmp(left->owner, right->owner))
569 retval |= DIFF_OWNER;
571 if (strcmp(left->group, right->group))
572 retval |= DIFF_GROUP;
574 if ((left->mode & 07777) != (right->mode & 07777))
575 retval |= DIFF_MODE;
577 if ((left->mode & S_IFMT) != (right->mode & S_IFMT))
578 retval |= DIFF_TYPE;
580 if (do_mtime && strcmp(left->path, METAFILE) &&
581 (left->mtime != right->mtime ||
582 left->mtimensec != right->mtimensec))
583 retval |= DIFF_MTIME;
585 if (mentry_compare_xattr(left, right)) {
586 retval |= DIFF_XATTR;
587 return retval;
590 return retval;
593 /* Compares lists of real and stored metadata and calls pfunc for each */
594 void
595 mentries_compare(struct metahash *mhashreal,
596 struct metahash *mhashstored,
597 void (*pfunc)
598 (struct metaentry *real, struct metaentry *stored, int do_mtime),
599 int do_mtime)
601 struct metaentry *real, *stored;
602 int key;
604 if (!mhashreal || !mhashstored) {
605 msg(MSG_ERROR, "%s called with empty list\n", __FUNCTION__);
606 return;
609 for (key = 0; key < HASH_INDEXES; key++) {
610 for (real = mhashreal->bucket[key]; real; real = real->next) {
611 stored = mentry_find(real->path, mhashstored);
613 if (!stored)
614 pfunc(real, NULL, DIFF_ADDED);
615 else
616 pfunc(real, stored, mentry_compare(real, stored, do_mtime));
619 for (stored = mhashstored->bucket[key]; stored; stored = stored->next) {
620 real = mentry_find(stored->path, mhashreal);
622 if (!real)
623 pfunc(NULL, stored, DIFF_DELE);