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>
37 #include <sys/param.h>
39 # include <bsd/string.h>
42 #include "metastore.h"
43 #include "metaentry.h"
47 # define PATH_MAX 4096
50 /* Free's a metaentry and all its parameters */
52 mentry_free(struct metaentry
*m
)
63 for (i
= 0; i
< m
->xattrs
; i
++) {
64 free(m
->xattr_names
[i
]);
65 free(m
->xattr_values
[i
]);
69 free(m
->xattr_values
);
70 free(m
->xattr_lvalues
);
75 /* Allocates an empty metahash table */
76 static struct metahash
*
79 struct metahash
*mhash
;
80 mhash
= xmalloc(sizeof(struct metahash
));
81 memset(mhash
, 0, sizeof(struct metahash
));
85 /* Generates a hash key (using djb2) */
93 hash
= ((hash
<< 5) + hash
) + c
;
95 return hash
% HASH_INDEXES
;
98 /* Allocates an empty metaentry */
99 static struct metaentry
*
102 struct metaentry
*mentry
;
103 mentry
= xmalloc(sizeof(struct metaentry
));
104 memset(mentry
, 0, sizeof(struct metaentry
));
108 /* Does a bisect search for the closest match in a metaentry list */
110 mentry_find(const char *path
, struct metahash
*mhash
)
112 struct metaentry
*base
;
116 msg(MSG_ERROR
, "%s called with empty hash table\n", __func__
);
121 for (base
= mhash
->bucket
[key
]; base
; base
= base
->next
) {
122 if (!strcmp(base
->path
, path
))
129 /* Inserts a metaentry into a metaentry list */
131 mentry_insert(struct metaentry
*mentry
, struct metahash
*mhash
)
135 key
= hash(mentry
->path
);
136 mentry
->next
= mhash
->bucket
[key
];
137 mhash
->bucket
[key
] = mentry
;
141 /* Prints a metaentry */
143 mentry_print(const struct metaentry
*mentry
)
147 if (!mentry
|| !mentry
->path
) {
149 "Incorrect meta entry passed to printmetaentry\n");
153 msg(MSG_DEBUG
, "===========================\n");
154 msg(MSG_DEBUG
, "Dump of metaentry %p\n", mentry
);
155 msg(MSG_DEBUG
, "===========================\n");
157 msg(MSG_DEBUG
, "path\t\t: %s\n", mentry
->path
);
158 msg(MSG_DEBUG
, "owner\t\t: %s\n", mentry
->owner
);
159 msg(MSG_DEBUG
, "group\t\t: %s\n", mentry
->group
);
160 msg(MSG_DEBUG
, "mtime\t\t: %ld\n", (unsigned long)mentry
->mtime
);
161 msg(MSG_DEBUG
, "mtimensec\t: %ld\n", (unsigned long)mentry
->mtimensec
);
162 msg(MSG_DEBUG
, "mode\t\t: %ld\n", (unsigned long)mentry
->mode
);
163 for (i
= 0; i
< mentry
->xattrs
; i
++) {
164 msg(MSG_DEBUG
, "xattr[%i]\t: %s=\"", i
, mentry
->xattr_names
[i
]);
165 binary_print(mentry
->xattr_values
[i
], mentry
->xattr_lvalues
[i
]);
166 msg(MSG_DEBUG
, "\"\n");
169 msg(MSG_DEBUG
, "===========================\n\n");
172 /* Prints all metaentries in a metaentry list */
174 mentries_print(const struct metahash
*mhash
)
176 const struct metaentry
*mentry
;
179 for (index
= 0; index
< HASH_INDEXES
; index
++)
180 for (mentry
= mhash
->bucket
[index
]; mentry
; mentry
= mentry
->next
)
181 mentry_print(mentry
);
183 msg(MSG_DEBUG
, "%i entries in total\n", mhash
->count
);
187 /* Creates a metaentry for the file/dir/etc at path */
189 mentry_create(const char *path
)
191 ssize_t lsize
, vsize
;
197 struct metaentry
*mentry
;
199 if (lstat(path
, &sbuf
)) {
200 msg(MSG_ERROR
, "lstat failed for %s: %s\n",
201 path
, strerror(errno
));
205 pbuf
= xgetpwuid(sbuf
.st_uid
);
207 msg(MSG_ERROR
, "getpwuid failed for %s: uid %i not found\n",
208 path
, (int)sbuf
.st_uid
);
212 gbuf
= xgetgrgid(sbuf
.st_gid
);
214 msg(MSG_ERROR
, "getgrgid failed for %s: gid %i not found\n",
215 path
, (int)sbuf
.st_gid
);
219 mentry
= mentry_alloc();
220 mentry
->path
= xstrdup(path
);
221 mentry
->pathlen
= strlen(mentry
->path
);
222 mentry
->owner
= xstrdup(pbuf
->pw_name
);
223 mentry
->group
= xstrdup(gbuf
->gr_name
);
224 mentry
->mode
= sbuf
.st_mode
& 0177777;
225 mentry
->mtime
= sbuf
.st_mtim
.tv_sec
;
226 mentry
->mtimensec
= sbuf
.st_mtim
.tv_nsec
;
228 /* symlinks have no xattrs */
229 if (S_ISLNK(mentry
->mode
))
232 lsize
= listxattr(path
, NULL
, 0);
234 /* Perhaps the FS doesn't support xattrs? */
235 if (errno
== ENOTSUP
)
238 msg(MSG_ERROR
, "listxattr failed for %s: %s\n",
239 path
, strerror(errno
));
243 list
= xmalloc(lsize
);
244 lsize
= listxattr(path
, list
, lsize
);
246 msg(MSG_ERROR
, "listxattr failed for %s: %s\n",
247 path
, strerror(errno
));
253 for (attr
= list
; attr
< list
+ lsize
; attr
= strchr(attr
, '\0') + 1) {
263 mentry
->xattr_names
= xmalloc(i
* sizeof(char *));
264 mentry
->xattr_values
= xmalloc(i
* sizeof(char *));
265 mentry
->xattr_lvalues
= xmalloc(i
* sizeof(ssize_t
));
268 for (attr
= list
; attr
< list
+ lsize
; attr
= strchr(attr
, '\0') + 1) {
272 mentry
->xattr_names
[i
] = xstrdup(attr
);
273 mentry
->xattr_values
[i
] = NULL
;
275 vsize
= getxattr(path
, attr
, NULL
, 0);
277 msg(MSG_ERROR
, "getxattr failed for %s: %s\n",
278 path
, strerror(errno
));
280 mentry
->xattrs
= i
+ 1;
285 mentry
->xattr_lvalues
[i
] = vsize
;
286 mentry
->xattr_values
[i
] = xmalloc(vsize
);
288 vsize
= getxattr(path
, attr
, mentry
->xattr_values
[i
], vsize
);
290 msg(MSG_ERROR
, "getxattr failed for %s: %s\n",
291 path
, strerror(errno
));
293 mentry
->xattrs
= i
+ 1;
304 /* Cleans up a path and makes it relative to cwd unless it is absolute */
306 normalize_path(const char *orig
)
308 char *real
= realpath(orig
, NULL
);
312 getcwd(cwd
, PATH_MAX
);
316 if (!strncmp(real
, cwd
, strlen(cwd
))) {
317 result
= xmalloc(strlen(real
) - strlen(cwd
) + 1 + 1);
320 strcat(result
, real
+ strlen(cwd
));
322 result
= xstrdup(real
);
329 /* Internal function for the recursive path walk */
331 mentries_recurse(const char *path
, struct metahash
*mhash
, msettings
*st
)
334 struct metaentry
*mentry
;
335 char tpath
[PATH_MAX
];
342 if (lstat(path
, &sbuf
)) {
343 msg(MSG_ERROR
, "lstat failed for %s: %s\n",
344 path
, strerror(errno
));
348 mentry
= mentry_create(path
);
352 mentry_insert(mentry
, mhash
);
354 if (S_ISDIR(sbuf
.st_mode
)) {
357 msg(MSG_ERROR
, "opendir failed for %s: %s\n",
358 path
, strerror(errno
));
362 while ((dent
= readdir(dir
))) {
363 if (!strcmp(dent
->d_name
, ".") ||
364 !strcmp(dent
->d_name
, "..") ||
365 (!st
->do_git
&& !strcmp(dent
->d_name
, ".git"))
368 snprintf(tpath
, PATH_MAX
, "%s/%s", path
, dent
->d_name
);
369 tpath
[PATH_MAX
- 1] = '\0';
370 mentries_recurse(tpath
, mhash
, st
);
377 /* Recurses opath and adds metadata entries to the metaentry list */
379 mentries_recurse_path(const char *opath
, struct metahash
**mhash
, msettings
*st
)
381 char *path
= normalize_path(opath
);
384 *mhash
= mhash_alloc();
385 mentries_recurse(path
, *mhash
, st
);
389 /* Stores metaentries to a file */
391 mentries_tofile(const struct metahash
*mhash
, const char *path
)
394 const struct metaentry
*mentry
;
398 to
= fopen(path
, "w");
400 msg(MSG_CRITICAL
, "Failed to open %s: %s\n",
401 path
, strerror(errno
));
405 write_binary_string(SIGNATURE
, SIGNATURELEN
, to
);
406 write_binary_string(VERSION
, VERSIONLEN
, to
);
408 for (key
= 0; key
< HASH_INDEXES
; key
++) {
409 for (mentry
= mhash
->bucket
[key
]; mentry
; mentry
= mentry
->next
) {
410 write_string(mentry
->path
, to
);
411 write_string(mentry
->owner
, to
);
412 write_string(mentry
->group
, to
);
413 write_int((uint64_t)mentry
->mtime
, 8, to
);
414 write_int((uint64_t)mentry
->mtimensec
, 8, to
);
415 write_int((uint64_t)mentry
->mode
, 2, to
);
416 write_int(mentry
->xattrs
, 4, to
);
417 for (i
= 0; i
< mentry
->xattrs
; i
++) {
418 write_string(mentry
->xattr_names
[i
], to
);
419 write_int(mentry
->xattr_lvalues
[i
], 4, to
);
420 write_binary_string(mentry
->xattr_values
[i
],
421 mentry
->xattr_lvalues
[i
], to
);
429 /* Creates a metaentry list from a file */
431 mentries_fromfile(struct metahash
**mhash
, const char *path
)
433 struct metaentry
*mentry
;
442 *mhash
= mhash_alloc();
444 fd
= open(path
, O_RDONLY
);
446 msg(MSG_CRITICAL
, "Failed to open %s: %s\n",
447 path
, strerror(errno
));
451 if (fstat(fd
, &sbuf
)) {
452 msg(MSG_CRITICAL
, "Failed to stat %s: %s\n",
453 path
, strerror(errno
));
457 if (sbuf
.st_size
< (SIGNATURELEN
+ VERSIONLEN
)) {
458 msg(MSG_CRITICAL
, "File %s has an invalid size\n", path
);
462 mmapstart
= mmap(NULL
, (size_t)sbuf
.st_size
, PROT_READ
,
464 if (mmapstart
== MAP_FAILED
) {
465 msg(MSG_CRITICAL
, "Unable to mmap %s: %s\n",
466 path
, strerror(errno
));
470 max
= mmapstart
+ sbuf
.st_size
;
472 if (strncmp(ptr
, SIGNATURE
, SIGNATURELEN
)) {
473 msg(MSG_CRITICAL
, "Invalid signature for file %s\n", path
);
478 if (strncmp(ptr
, VERSION
, VERSIONLEN
)) {
479 msg(MSG_CRITICAL
, "Invalid version of file %s\n", path
);
484 while (ptr
< mmapstart
+ sbuf
.st_size
) {
486 msg(MSG_CRITICAL
, "Invalid characters in file %s\n",
491 mentry
= mentry_alloc();
492 mentry
->path
= read_string(&ptr
, max
);
493 mentry
->pathlen
= strlen(mentry
->path
);
494 mentry
->owner
= read_string(&ptr
, max
);
495 mentry
->group
= read_string(&ptr
, max
);
496 mentry
->mtime
= (time_t)read_int(&ptr
, 8, max
);
497 mentry
->mtimensec
= (time_t)read_int(&ptr
, 8, max
);
498 mentry
->mode
= (mode_t
)read_int(&ptr
, 2, max
);
499 mentry
->xattrs
= (unsigned)read_int(&ptr
, 4, max
);
501 if (!mentry
->xattrs
) {
502 mentry_insert(mentry
, *mhash
);
506 mentry
->xattr_names
= xmalloc(mentry
->xattrs
* sizeof(char *));
507 mentry
->xattr_lvalues
= xmalloc(mentry
->xattrs
* sizeof(ssize_t
));
508 mentry
->xattr_values
= xmalloc(mentry
->xattrs
* sizeof(char *));
510 for (i
= 0; i
< mentry
->xattrs
; i
++) {
511 mentry
->xattr_names
[i
] = read_string(&ptr
, max
);
512 mentry
->xattr_lvalues
[i
] = (int)read_int(&ptr
, 4, max
);
513 mentry
->xattr_values
[i
] = read_binary_string(
515 mentry
->xattr_lvalues
[i
],
519 mentry_insert(mentry
, *mhash
);
523 munmap(mmapstart
, sbuf
.st_size
);
527 /* Searches haystack for an xattr matching xattr number n in needle */
529 mentry_find_xattr(struct metaentry
*haystack
, struct metaentry
*needle
,
534 for (i
= 0; i
< haystack
->xattrs
; i
++) {
535 if (strcmp(haystack
->xattr_names
[i
], needle
->xattr_names
[n
]))
537 if (haystack
->xattr_lvalues
[i
] != needle
->xattr_lvalues
[n
])
539 if (bcmp(haystack
->xattr_values
[i
], needle
->xattr_values
[n
],
540 needle
->xattr_lvalues
[n
])
548 /* Returns zero if all xattrs in left and right match */
550 mentry_compare_xattr(struct metaentry
*left
, struct metaentry
*right
)
554 if (left
->xattrs
!= right
->xattrs
)
557 /* Make sure all xattrs in left are found in right and vice versa */
558 for (i
= 0; i
< left
->xattrs
; i
++) {
559 if ( mentry_find_xattr(right
, left
, i
) < 0
560 || mentry_find_xattr(left
, right
, i
) < 0
569 /* Compares two metaentries and returns an int with a bitmask of differences */
571 mentry_compare(struct metaentry
*left
, struct metaentry
*right
, msettings
*st
)
573 int retval
= DIFF_NONE
;
575 if (!left
|| !right
) {
576 msg(MSG_ERROR
, "%s called with empty list\n", __func__
);
580 if (strcmp(left
->path
, right
->path
))
583 if (strcmp(left
->owner
, right
->owner
))
584 retval
|= DIFF_OWNER
;
586 if (strcmp(left
->group
, right
->group
))
587 retval
|= DIFF_GROUP
;
589 if ((left
->mode
& 07777) != (right
->mode
& 07777))
592 if ((left
->mode
& S_IFMT
) != (right
->mode
& S_IFMT
))
595 if (st
->do_mtime
&& strcmp(left
->path
, st
->metafile
) &&
596 ( left
->mtime
!= right
->mtime
597 || left
->mtimensec
!= right
->mtimensec
)
599 retval
|= DIFF_MTIME
;
601 if (mentry_compare_xattr(left
, right
)) {
602 retval
|= DIFF_XATTR
;
609 /* Compares lists of real and stored metadata and calls pfunc for each */
611 mentries_compare(struct metahash
*mhashreal
,
612 struct metahash
*mhashstored
,
614 (struct metaentry
*real
, struct metaentry
*stored
, int cmp
),
617 struct metaentry
*real
, *stored
;
620 if (!mhashreal
|| !mhashstored
) {
621 msg(MSG_ERROR
, "%s called with empty list\n", __func__
);
625 for (key
= 0; key
< HASH_INDEXES
; key
++) {
626 for (real
= mhashreal
->bucket
[key
]; real
; real
= real
->next
) {
627 stored
= mentry_find(real
->path
, mhashstored
);
630 pfunc(real
, NULL
, DIFF_ADDED
);
632 pfunc(real
, stored
, mentry_compare(real
, stored
, st
));
635 for (stored
= mhashstored
->bucket
[key
]; stored
; stored
= stored
->next
) {
636 real
= mentry_find(stored
->path
, mhashreal
);
639 pfunc(NULL
, stored
, DIFF_DELE
);
644 /* Dumps given metadata */
646 mentries_dump(struct metahash
*mhash
)
648 const struct metaentry
*mentry
;
649 char mode
[11 + 1] = "";
650 char date
[12 + 2 + 2 + 2*1 + 1 + 2 + 2 + 2 + 2*1 + 1] = "";
651 char zone
[5 + 1] = "";
654 for (int key
= 0; key
< HASH_INDEXES
; key
++) {
655 for (mentry
= mhash
->bucket
[key
]; mentry
; mentry
= mentry
->next
) {
656 strmode(mentry
->mode
, mode
);
657 localtime_r(&mentry
->mtime
, &cal
);
658 strftime(date
, sizeof(date
), "%F %T", &cal
);
659 strftime(zone
, sizeof(zone
), "%z", &cal
);
660 printf("%s\t%s\t%s\t%s.%09ld %s\t%s%s\n",
662 mentry
->owner
, mentry
->group
,
663 date
, mentry
->mtimensec
, zone
,
664 mentry
->path
, S_ISDIR(mentry
->mode
) ? "/" : "");
665 for (unsigned i
= 0; i
< mentry
->xattrs
; i
++) {
666 printf("\t\t\t\t%s%s\t%s=",
667 mentry
->path
, S_ISDIR(mentry
->mode
) ? "/" : "",
668 mentry
->xattr_names
[i
]);
670 for (; p
< mentry
->xattr_lvalues
[i
]; p
++) {
671 const char ch
= mentry
->xattr_values
[i
][p
];
672 if ((unsigned)(ch
- 32) > 126 - 32) {
679 (int)mentry
->xattr_lvalues
[i
],
680 mentry
->xattr_values
[i
]);
683 for (p
= 0; p
< mentry
->xattr_lvalues
[i
]; p
++)
684 printf("%02hhx", (char)mentry
->xattr_values
[i
][p
]);