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.
24 #include <sys/types.h>
27 #include <sys/xattr.h>
35 #include <bsd/string.h>
38 #include "metastore.h"
39 #include "metaentry.h"
42 /* Free's a metaentry and all its parameters */
44 mentry_free(struct metaentry
*m
)
55 for (i
= 0; i
< m
->xattrs
; i
++) {
56 free(m
->xattr_names
[i
]);
57 free(m
->xattr_values
[i
]);
61 free(m
->xattr_values
);
62 free(m
->xattr_lvalues
);
67 /* Allocates an empty metahash table */
68 static struct metahash
*
71 struct metahash
*mhash
;
72 mhash
= xmalloc(sizeof(struct metahash
));
73 memset(mhash
, 0, sizeof(struct metahash
));
77 /* Generates a hash key (using djb2) */
81 unsigned int hash
= 5381;
85 hash
= ((hash
<< 5) + hash
) + c
;
87 return hash
% HASH_INDEXES
;
90 /* Allocates an empty metaentry */
91 static struct metaentry
*
94 struct metaentry
*mentry
;
95 mentry
= xmalloc(sizeof(struct metaentry
));
96 memset(mentry
, 0, sizeof(struct metaentry
));
100 /* Does a bisect search for the closest match in a metaentry list */
102 mentry_find(const char *path
, struct metahash
*mhash
)
104 struct metaentry
*base
;
108 msg(MSG_ERROR
, "%s called with empty hash table\n", __FUNCTION__
);
113 for (base
= mhash
->bucket
[key
]; base
; base
= base
->next
) {
114 if (!strcmp(base
->path
, path
))
121 /* Inserts a metaentry into a metaentry list */
123 mentry_insert(struct metaentry
*mentry
, struct metahash
*mhash
)
127 key
= hash(mentry
->path
);
128 mentry
->next
= mhash
->bucket
[key
];
129 mhash
->bucket
[key
] = mentry
;
133 /* Prints a metaentry */
135 mentry_print(const struct metaentry
*mentry
)
139 if (!mentry
|| !mentry
->path
) {
141 "Incorrect meta entry passed to printmetaentry\n");
145 msg(MSG_DEBUG
, "===========================\n");
146 msg(MSG_DEBUG
, "Dump of metaentry %p\n", mentry
);
147 msg(MSG_DEBUG
, "===========================\n");
149 msg(MSG_DEBUG
, "path\t\t: %s\n", mentry
->path
);
150 msg(MSG_DEBUG
, "owner\t\t: %s\n", mentry
->owner
);
151 msg(MSG_DEBUG
, "group\t\t: %s\n", mentry
->group
);
152 msg(MSG_DEBUG
, "mtime\t\t: %ld\n", (unsigned long)mentry
->mtime
);
153 msg(MSG_DEBUG
, "mtimensec\t: %ld\n", (unsigned long)mentry
->mtimensec
);
154 msg(MSG_DEBUG
, "mode\t\t: %ld\n", (unsigned long)mentry
->mode
);
155 for (i
= 0; i
< mentry
->xattrs
; i
++) {
156 msg(MSG_DEBUG
, "xattr[%i]\t: %s=\"", i
, mentry
->xattr_names
[i
]);
157 binary_print(mentry
->xattr_values
[i
], mentry
->xattr_lvalues
[i
]);
158 msg(MSG_DEBUG
, "\"\n");
161 msg(MSG_DEBUG
, "===========================\n\n");
164 /* Prints all metaentries in a metaentry list */
166 mentries_print(const struct metahash
*mhash
)
168 const struct metaentry
*mentry
;
171 for (index
= 0; index
< HASH_INDEXES
; index
++)
172 for (mentry
= mhash
->bucket
[index
]; mentry
; mentry
= mentry
->next
)
173 mentry_print(mentry
);
175 msg(MSG_DEBUG
, "%i entries in total\n", mhash
->count
);
179 /* Creates a metaentry for the file/dir/etc at path */
181 mentry_create(const char *path
)
183 ssize_t lsize
, vsize
;
189 struct metaentry
*mentry
;
191 if (lstat(path
, &sbuf
)) {
192 msg(MSG_ERROR
, "lstat failed for %s: %s\n",
193 path
, strerror(errno
));
197 pbuf
= xgetpwuid(sbuf
.st_uid
);
199 msg(MSG_ERROR
, "getpwuid failed for %s: uid %i not found\n",
200 path
, (int)sbuf
.st_uid
);
204 gbuf
= xgetgrgid(sbuf
.st_gid
);
206 msg(MSG_ERROR
, "getgrgid failed for %s: gid %i not found\n",
207 path
, (int)sbuf
.st_gid
);
211 mentry
= mentry_alloc();
212 mentry
->path
= xstrdup(path
);
213 mentry
->pathlen
= strlen(mentry
->path
);
214 mentry
->owner
= xstrdup(pbuf
->pw_name
);
215 mentry
->group
= xstrdup(gbuf
->gr_name
);
216 mentry
->mode
= sbuf
.st_mode
& 0177777;
217 mentry
->mtime
= sbuf
.st_mtim
.tv_sec
;
218 mentry
->mtimensec
= sbuf
.st_mtim
.tv_nsec
;
220 /* symlinks have no xattrs */
221 if (S_ISLNK(mentry
->mode
))
224 lsize
= listxattr(path
, NULL
, 0);
226 /* Perhaps the FS doesn't support xattrs? */
227 if (errno
== ENOTSUP
)
230 msg(MSG_ERROR
, "listxattr failed for %s: %s\n",
231 path
, strerror(errno
));
235 list
= xmalloc(lsize
);
236 lsize
= listxattr(path
, list
, lsize
);
238 msg(MSG_ERROR
, "listxattr failed for %s: %s\n",
239 path
, strerror(errno
));
245 for (attr
= list
; attr
< list
+ lsize
; attr
= strchr(attr
, '\0') + 1) {
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
));
260 for (attr
= list
; attr
< list
+ lsize
; attr
= strchr(attr
, '\0') + 1) {
264 mentry
->xattr_names
[i
] = xstrdup(attr
);
265 vsize
= getxattr(path
, attr
, NULL
, 0);
267 msg(MSG_ERROR
, "getxattr failed for %s: %s\n",
268 path
, strerror(errno
));
274 mentry
->xattr_lvalues
[i
] = vsize
;
275 mentry
->xattr_values
[i
] = xmalloc(vsize
);
277 vsize
= getxattr(path
, attr
, mentry
->xattr_values
[i
], vsize
);
279 msg(MSG_ERROR
, "getxattr failed for %s: %s\n",
280 path
, strerror(errno
));
292 /* Cleans up a path and makes it relative to current working dir unless it is absolute */
294 normalize_path(const char *orig
)
296 char *real
= realpath(orig
, NULL
);
300 getcwd(cwd
, PATH_MAX
);
304 if (!strncmp(real
, cwd
, strlen(cwd
))) {
305 result
= xmalloc(strlen(real
) - strlen(cwd
) + 1 + 1);
308 strcat(result
, real
+ strlen(cwd
));
310 result
= xstrdup(real
);
317 /* Internal function for the recursive path walk */
319 mentries_recurse(const char *path
, struct metahash
*mhash
, msettings
*st
)
322 struct metaentry
*mentry
;
323 char tpath
[PATH_MAX
];
330 if (lstat(path
, &sbuf
)) {
331 msg(MSG_ERROR
, "lstat failed for %s: %s\n",
332 path
, strerror(errno
));
336 mentry
= mentry_create(path
);
340 mentry_insert(mentry
, mhash
);
342 if (S_ISDIR(sbuf
.st_mode
)) {
345 msg(MSG_ERROR
, "opendir failed for %s: %s\n",
346 path
, strerror(errno
));
350 while ((dent
= readdir(dir
))) {
351 if (!strcmp(dent
->d_name
, ".") ||
352 !strcmp(dent
->d_name
, "..") ||
353 (!st
->do_git
&& !strcmp(dent
->d_name
, ".git")))
355 snprintf(tpath
, PATH_MAX
, "%s/%s", path
, dent
->d_name
);
356 tpath
[PATH_MAX
- 1] = '\0';
357 mentries_recurse(tpath
, mhash
, st
);
364 /* Recurses opath and adds metadata entries to the metaentry list */
366 mentries_recurse_path(const char *opath
, struct metahash
**mhash
, msettings
*st
)
368 char *path
= normalize_path(opath
);
371 *mhash
= mhash_alloc();
372 mentries_recurse(path
, *mhash
, st
);
376 /* Stores metaentries to a file */
378 mentries_tofile(const struct metahash
*mhash
, const char *path
)
381 const struct metaentry
*mentry
;
385 to
= fopen(path
, "w");
387 msg(MSG_CRITICAL
, "Failed to open %s: %s\n",
388 path
, strerror(errno
));
392 write_binary_string(SIGNATURE
, SIGNATURELEN
, to
);
393 write_binary_string(VERSION
, VERSIONLEN
, to
);
395 for (key
= 0; key
< HASH_INDEXES
; key
++) {
396 for (mentry
= mhash
->bucket
[key
]; mentry
; mentry
= mentry
->next
) {
397 write_string(mentry
->path
, to
);
398 write_string(mentry
->owner
, to
);
399 write_string(mentry
->group
, to
);
400 write_int((uint64_t)mentry
->mtime
, 8, to
);
401 write_int((uint64_t)mentry
->mtimensec
, 8, to
);
402 write_int((uint64_t)mentry
->mode
, 2, to
);
403 write_int(mentry
->xattrs
, 4, to
);
404 for (i
= 0; i
< mentry
->xattrs
; i
++) {
405 write_string(mentry
->xattr_names
[i
], to
);
406 write_int(mentry
->xattr_lvalues
[i
], 4, to
);
407 write_binary_string(mentry
->xattr_values
[i
],
408 mentry
->xattr_lvalues
[i
], to
);
416 /* Creates a metaentry list from a file */
418 mentries_fromfile(struct metahash
**mhash
, const char *path
)
420 struct metaentry
*mentry
;
429 *mhash
= mhash_alloc();
431 fd
= open(path
, O_RDONLY
);
433 msg(MSG_CRITICAL
, "Failed to open %s: %s\n",
434 path
, strerror(errno
));
438 if (fstat(fd
, &sbuf
)) {
439 msg(MSG_CRITICAL
, "Failed to stat %s: %s\n",
440 path
, strerror(errno
));
444 if (sbuf
.st_size
< (SIGNATURELEN
+ VERSIONLEN
)) {
445 msg(MSG_CRITICAL
, "File %s has an invalid size\n", path
);
449 mmapstart
= mmap(NULL
, (size_t)sbuf
.st_size
, PROT_READ
,
451 if (mmapstart
== MAP_FAILED
) {
452 msg(MSG_CRITICAL
, "Unable to mmap %s: %s\n",
453 path
, strerror(errno
));
457 max
= mmapstart
+ sbuf
.st_size
;
459 if (strncmp(ptr
, SIGNATURE
, SIGNATURELEN
)) {
460 msg(MSG_CRITICAL
, "Invalid signature for file %s\n", path
);
465 if (strncmp(ptr
, VERSION
, VERSIONLEN
)) {
466 msg(MSG_CRITICAL
, "Invalid version of file %s\n", path
);
471 while (ptr
< mmapstart
+ sbuf
.st_size
) {
473 msg(MSG_CRITICAL
, "Invalid characters in file %s\n",
478 mentry
= mentry_alloc();
479 mentry
->path
= read_string(&ptr
, max
);
480 mentry
->pathlen
= strlen(mentry
->path
);
481 mentry
->owner
= read_string(&ptr
, max
);
482 mentry
->group
= read_string(&ptr
, max
);
483 mentry
->mtime
= (time_t)read_int(&ptr
, 8, max
);
484 mentry
->mtimensec
= (time_t)read_int(&ptr
, 8, max
);
485 mentry
->mode
= (mode_t
)read_int(&ptr
, 2, max
);
486 mentry
->xattrs
= (unsigned int)read_int(&ptr
, 4, max
);
488 if (!mentry
->xattrs
) {
489 mentry_insert(mentry
, *mhash
);
493 mentry
->xattr_names
= xmalloc(mentry
->xattrs
*
495 mentry
->xattr_lvalues
= xmalloc(mentry
->xattrs
*
497 mentry
->xattr_values
= xmalloc(mentry
->xattrs
*
500 for (i
= 0; i
< mentry
->xattrs
; i
++) {
501 mentry
->xattr_names
[i
] = read_string(&ptr
, max
);
502 mentry
->xattr_lvalues
[i
] =
503 (int)read_int(&ptr
, 4, max
);
504 mentry
->xattr_values
[i
] =
505 read_binary_string(&ptr
,
506 mentry
->xattr_lvalues
[i
],
509 mentry_insert(mentry
, *mhash
);
513 munmap(mmapstart
, sbuf
.st_size
);
517 /* Searches haystack for an xattr matching xattr number n in needle */
519 mentry_find_xattr(struct metaentry
*haystack
, struct metaentry
*needle
,
524 for (i
= 0; i
< haystack
->xattrs
; i
++) {
525 if (strcmp(haystack
->xattr_names
[i
], needle
->xattr_names
[n
]))
527 if (haystack
->xattr_lvalues
[i
] != needle
->xattr_lvalues
[n
])
529 if (bcmp(haystack
->xattr_values
[i
], needle
->xattr_values
[n
],
530 needle
->xattr_lvalues
[n
]))
537 /* Returns zero if all xattrs in left and right match */
539 mentry_compare_xattr(struct metaentry
*left
, struct metaentry
*right
)
543 if (left
->xattrs
!= right
->xattrs
)
546 /* Make sure all xattrs in left are found in right and vice versa */
547 for (i
= 0; i
< left
->xattrs
; i
++) {
548 if (mentry_find_xattr(right
, left
, i
) < 0 ||
549 mentry_find_xattr(left
, right
, i
) < 0) {
557 /* Compares two metaentries and returns an int with a bitmask of differences */
559 mentry_compare(struct metaentry
*left
, struct metaentry
*right
, msettings
*st
)
561 int retval
= DIFF_NONE
;
563 if (!left
|| !right
) {
564 msg(MSG_ERROR
, "%s called with empty list\n", __FUNCTION__
);
568 if (strcmp(left
->path
, right
->path
))
571 if (strcmp(left
->owner
, right
->owner
))
572 retval
|= DIFF_OWNER
;
574 if (strcmp(left
->group
, right
->group
))
575 retval
|= DIFF_GROUP
;
577 if ((left
->mode
& 07777) != (right
->mode
& 07777))
580 if ((left
->mode
& S_IFMT
) != (right
->mode
& S_IFMT
))
583 if (st
->do_mtime
&& strcmp(left
->path
, st
->metafile
) &&
584 (left
->mtime
!= right
->mtime
||
585 left
->mtimensec
!= right
->mtimensec
))
586 retval
|= DIFF_MTIME
;
588 if (mentry_compare_xattr(left
, right
)) {
589 retval
|= DIFF_XATTR
;
596 /* Compares lists of real and stored metadata and calls pfunc for each */
598 mentries_compare(struct metahash
*mhashreal
,
599 struct metahash
*mhashstored
,
601 (struct metaentry
*real
, struct metaentry
*stored
, int cmp
),
604 struct metaentry
*real
, *stored
;
607 if (!mhashreal
|| !mhashstored
) {
608 msg(MSG_ERROR
, "%s called with empty list\n", __FUNCTION__
);
612 for (key
= 0; key
< HASH_INDEXES
; key
++) {
613 for (real
= mhashreal
->bucket
[key
]; real
; real
= real
->next
) {
614 stored
= mentry_find(real
->path
, mhashstored
);
617 pfunc(real
, NULL
, DIFF_ADDED
);
619 pfunc(real
, stored
, mentry_compare(real
, stored
, st
));
622 for (stored
= mhashstored
->bucket
[key
]; stored
; stored
= stored
->next
) {
623 real
= mentry_find(stored
->path
, mhashreal
);
626 pfunc(NULL
, stored
, DIFF_DELE
);
631 /* Dumps given metadata */
633 mentries_dump(struct metahash
*mhash
)
635 const struct metaentry
*mentry
;
636 char mode
[11 + 1] = "";
637 char date
[12 + 2 + 2 + 2*1 + 1 + 2 + 2 + 2 + 2*1 + 1] = "";
638 char zone
[5 + 1] = "";
641 for (int key
= 0; key
< HASH_INDEXES
; key
++) {
642 for (mentry
= mhash
->bucket
[key
]; mentry
; mentry
= mentry
->next
) {
643 strmode(mentry
->mode
, mode
);
644 localtime_r(&mentry
->mtime
, &cal
);
645 strftime(date
, sizeof(date
), "%F %T", &cal
);
646 strftime(zone
, sizeof(zone
), "%z", &cal
);
647 printf("%s\t%s\t%s\t%s.%09ld %s\t%s%s\n",
649 mentry
->owner
, mentry
->group
,
650 date
, mentry
->mtimensec
, zone
,
651 mentry
->path
, S_ISDIR(mentry
->mode
) ? "/" : "");
652 for (int i
= 0; i
< mentry
->xattrs
; i
++) {
653 printf("\t\t\t\t%s%s\t%s=",
654 mentry
->path
, S_ISDIR(mentry
->mode
) ? "/" : "",
655 mentry
->xattr_names
[i
]);
657 for (; p
< mentry
->xattr_lvalues
[i
]; p
++) {
658 const char ch
= mentry
->xattr_values
[i
][p
];
659 if ((unsigned)(ch
- 32) > 126 - 32) {
666 (int)mentry
->xattr_lvalues
[i
],
667 mentry
->xattr_values
[i
]);
670 for (p
= 0; p
< mentry
->xattr_lvalues
[i
]; p
++)
671 printf("%02hhx", (char)mentry
->xattr_values
[i
][p
]);