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"
46 /* Free's a metaentry and all its parameters */
48 mentry_free(struct metaentry
*m
)
59 for (i
= 0; i
< m
->xattrs
; i
++) {
60 free(m
->xattr_names
[i
]);
61 free(m
->xattr_values
[i
]);
65 free(m
->xattr_values
);
66 free(m
->xattr_lvalues
);
71 /* Allocates an empty metahash table */
72 static struct metahash
*
75 struct metahash
*mhash
;
76 mhash
= xmalloc(sizeof(struct metahash
));
77 memset(mhash
, 0, sizeof(struct metahash
));
81 /* Generates a hash key (using djb2) */
89 hash
= ((hash
<< 5) + hash
) + c
;
91 return hash
% HASH_INDEXES
;
94 /* Allocates an empty metaentry */
95 static struct metaentry
*
98 struct metaentry
*mentry
;
99 mentry
= xmalloc(sizeof(struct metaentry
));
100 memset(mentry
, 0, sizeof(struct metaentry
));
104 /* Does a bisect search for the closest match in a metaentry list */
106 mentry_find(const char *path
, struct metahash
*mhash
)
108 struct metaentry
*base
;
112 msg(MSG_ERROR
, "%s called with empty hash table\n", __func__
);
117 for (base
= mhash
->bucket
[key
]; base
; base
= base
->next
) {
118 if (!strcmp(base
->path
, path
))
125 /* Inserts a metaentry into a metaentry list */
127 mentry_insert(struct metaentry
*mentry
, struct metahash
*mhash
)
131 key
= hash(mentry
->path
);
132 mentry
->next
= mhash
->bucket
[key
];
133 mhash
->bucket
[key
] = mentry
;
137 /* Prints a metaentry */
139 mentry_print(const struct metaentry
*mentry
)
143 if (!mentry
|| !mentry
->path
) {
145 "Incorrect meta entry passed to printmetaentry\n");
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 */
170 mentries_print(const struct metahash
*mhash
)
172 const struct metaentry
*mentry
;
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
);
183 /* Creates a metaentry for the file/dir/etc at path */
185 mentry_create(const char *path
)
187 ssize_t lsize
, vsize
;
193 struct metaentry
*mentry
;
195 if (lstat(path
, &sbuf
)) {
196 msg(MSG_ERROR
, "lstat failed for %s: %s\n",
197 path
, strerror(errno
));
201 pbuf
= xgetpwuid(sbuf
.st_uid
);
203 msg(MSG_ERROR
, "getpwuid failed for %s: uid %i not found\n",
204 path
, (int)sbuf
.st_uid
);
208 gbuf
= xgetgrgid(sbuf
.st_gid
);
210 msg(MSG_ERROR
, "getgrgid failed for %s: gid %i not found\n",
211 path
, (int)sbuf
.st_gid
);
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
))
228 lsize
= listxattr(path
, NULL
, 0);
230 /* Perhaps the FS doesn't support xattrs? */
231 if (errno
== ENOTSUP
)
234 msg(MSG_ERROR
, "listxattr failed for %s: %s\n",
235 path
, strerror(errno
));
239 list
= xmalloc(lsize
);
240 lsize
= listxattr(path
, list
, lsize
);
242 msg(MSG_ERROR
, "listxattr failed for %s: %s\n",
243 path
, strerror(errno
));
249 for (attr
= list
; attr
< list
+ lsize
; attr
= strchr(attr
, '\0') + 1) {
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
));
264 for (attr
= list
; attr
< list
+ lsize
; attr
= strchr(attr
, '\0') + 1) {
268 mentry
->xattr_names
[i
] = xstrdup(attr
);
269 mentry
->xattr_values
[i
] = NULL
;
271 vsize
= getxattr(path
, attr
, NULL
, 0);
273 msg(MSG_ERROR
, "getxattr failed for %s: %s\n",
274 path
, strerror(errno
));
276 mentry
->xattrs
= i
+ 1;
281 mentry
->xattr_lvalues
[i
] = vsize
;
282 mentry
->xattr_values
[i
] = xmalloc(vsize
);
284 vsize
= getxattr(path
, attr
, mentry
->xattr_values
[i
], vsize
);
286 msg(MSG_ERROR
, "getxattr failed for %s: %s\n",
287 path
, strerror(errno
));
289 mentry
->xattrs
= i
+ 1;
300 /* Cleans up a path and makes it relative to cwd unless it is absolute */
302 normalize_path(const char *orig
)
304 char *real
= realpath(orig
, NULL
);
308 getcwd(cwd
, PATH_MAX
);
312 if (!strncmp(real
, cwd
, strlen(cwd
))) {
313 result
= xmalloc(strlen(real
) - strlen(cwd
) + 1 + 1);
316 strcat(result
, real
+ strlen(cwd
));
318 result
= xstrdup(real
);
325 /* Internal function for the recursive path walk */
327 mentries_recurse(const char *path
, struct metahash
*mhash
, msettings
*st
)
330 struct metaentry
*mentry
;
331 char tpath
[PATH_MAX
];
338 if (lstat(path
, &sbuf
)) {
339 msg(MSG_ERROR
, "lstat failed for %s: %s\n",
340 path
, strerror(errno
));
344 mentry
= mentry_create(path
);
348 mentry_insert(mentry
, mhash
);
350 if (S_ISDIR(sbuf
.st_mode
)) {
353 msg(MSG_ERROR
, "opendir failed for %s: %s\n",
354 path
, strerror(errno
));
358 while ((dent
= readdir(dir
))) {
359 if (!strcmp(dent
->d_name
, ".") ||
360 !strcmp(dent
->d_name
, "..") ||
361 (!st
->do_git
&& !strcmp(dent
->d_name
, ".git"))
364 snprintf(tpath
, PATH_MAX
, "%s/%s", path
, dent
->d_name
);
365 tpath
[PATH_MAX
- 1] = '\0';
366 mentries_recurse(tpath
, mhash
, st
);
373 /* Recurses opath and adds metadata entries to the metaentry list */
375 mentries_recurse_path(const char *opath
, struct metahash
**mhash
, msettings
*st
)
377 char *path
= normalize_path(opath
);
380 *mhash
= mhash_alloc();
381 mentries_recurse(path
, *mhash
, st
);
385 /* Stores metaentries to a file */
387 mentries_tofile(const struct metahash
*mhash
, const char *path
)
390 const struct metaentry
*mentry
;
394 to
= fopen(path
, "w");
396 msg(MSG_CRITICAL
, "Failed to open %s: %s\n",
397 path
, strerror(errno
));
401 write_binary_string(SIGNATURE
, SIGNATURELEN
, to
);
402 write_binary_string(VERSION
, VERSIONLEN
, to
);
404 for (key
= 0; key
< HASH_INDEXES
; key
++) {
405 for (mentry
= mhash
->bucket
[key
]; mentry
; mentry
= mentry
->next
) {
406 write_string(mentry
->path
, to
);
407 write_string(mentry
->owner
, to
);
408 write_string(mentry
->group
, to
);
409 write_int((uint64_t)mentry
->mtime
, 8, to
);
410 write_int((uint64_t)mentry
->mtimensec
, 8, to
);
411 write_int((uint64_t)mentry
->mode
, 2, to
);
412 write_int(mentry
->xattrs
, 4, to
);
413 for (i
= 0; i
< mentry
->xattrs
; i
++) {
414 write_string(mentry
->xattr_names
[i
], to
);
415 write_int(mentry
->xattr_lvalues
[i
], 4, to
);
416 write_binary_string(mentry
->xattr_values
[i
],
417 mentry
->xattr_lvalues
[i
], to
);
425 /* Creates a metaentry list from a file */
427 mentries_fromfile(struct metahash
**mhash
, const char *path
)
429 struct metaentry
*mentry
;
438 *mhash
= mhash_alloc();
440 fd
= open(path
, O_RDONLY
);
442 msg(MSG_CRITICAL
, "Failed to open %s: %s\n",
443 path
, strerror(errno
));
447 if (fstat(fd
, &sbuf
)) {
448 msg(MSG_CRITICAL
, "Failed to stat %s: %s\n",
449 path
, strerror(errno
));
453 if (sbuf
.st_size
< (SIGNATURELEN
+ VERSIONLEN
)) {
454 msg(MSG_CRITICAL
, "File %s has an invalid size\n", path
);
458 mmapstart
= mmap(NULL
, (size_t)sbuf
.st_size
, PROT_READ
,
460 if (mmapstart
== MAP_FAILED
) {
461 msg(MSG_CRITICAL
, "Unable to mmap %s: %s\n",
462 path
, strerror(errno
));
466 max
= mmapstart
+ sbuf
.st_size
;
468 if (strncmp(ptr
, SIGNATURE
, SIGNATURELEN
)) {
469 msg(MSG_CRITICAL
, "Invalid signature for file %s\n", path
);
474 if (strncmp(ptr
, VERSION
, VERSIONLEN
)) {
475 msg(MSG_CRITICAL
, "Invalid version of file %s\n", path
);
480 while (ptr
< mmapstart
+ sbuf
.st_size
) {
482 msg(MSG_CRITICAL
, "Invalid characters in file %s\n",
487 mentry
= mentry_alloc();
488 mentry
->path
= read_string(&ptr
, max
);
489 mentry
->pathlen
= strlen(mentry
->path
);
490 mentry
->owner
= read_string(&ptr
, max
);
491 mentry
->group
= read_string(&ptr
, max
);
492 mentry
->mtime
= (time_t)read_int(&ptr
, 8, max
);
493 mentry
->mtimensec
= (time_t)read_int(&ptr
, 8, max
);
494 mentry
->mode
= (mode_t
)read_int(&ptr
, 2, max
);
495 mentry
->xattrs
= (unsigned)read_int(&ptr
, 4, max
);
497 if (!mentry
->xattrs
) {
498 mentry_insert(mentry
, *mhash
);
502 mentry
->xattr_names
= xmalloc(mentry
->xattrs
* sizeof(char *));
503 mentry
->xattr_lvalues
= xmalloc(mentry
->xattrs
* sizeof(ssize_t
));
504 mentry
->xattr_values
= xmalloc(mentry
->xattrs
* sizeof(char *));
506 for (i
= 0; i
< mentry
->xattrs
; i
++) {
507 mentry
->xattr_names
[i
] = read_string(&ptr
, max
);
508 mentry
->xattr_lvalues
[i
] = (int)read_int(&ptr
, 4, max
);
509 mentry
->xattr_values
[i
] = read_binary_string(
511 mentry
->xattr_lvalues
[i
],
515 mentry_insert(mentry
, *mhash
);
519 munmap(mmapstart
, sbuf
.st_size
);
523 /* Searches haystack for an xattr matching xattr number n in needle */
525 mentry_find_xattr(struct metaentry
*haystack
, struct metaentry
*needle
,
530 for (i
= 0; i
< haystack
->xattrs
; i
++) {
531 if (strcmp(haystack
->xattr_names
[i
], needle
->xattr_names
[n
]))
533 if (haystack
->xattr_lvalues
[i
] != needle
->xattr_lvalues
[n
])
535 if (bcmp(haystack
->xattr_values
[i
], needle
->xattr_values
[n
],
536 needle
->xattr_lvalues
[n
])
544 /* Returns zero if all xattrs in left and right match */
546 mentry_compare_xattr(struct metaentry
*left
, struct metaentry
*right
)
550 if (left
->xattrs
!= right
->xattrs
)
553 /* Make sure all xattrs in left are found in right and vice versa */
554 for (i
= 0; i
< left
->xattrs
; i
++) {
555 if ( mentry_find_xattr(right
, left
, i
) < 0
556 || mentry_find_xattr(left
, right
, i
) < 0
565 /* Compares two metaentries and returns an int with a bitmask of differences */
567 mentry_compare(struct metaentry
*left
, struct metaentry
*right
, msettings
*st
)
569 int retval
= DIFF_NONE
;
571 if (!left
|| !right
) {
572 msg(MSG_ERROR
, "%s called with empty list\n", __func__
);
576 if (strcmp(left
->path
, right
->path
))
579 if (strcmp(left
->owner
, right
->owner
))
580 retval
|= DIFF_OWNER
;
582 if (strcmp(left
->group
, right
->group
))
583 retval
|= DIFF_GROUP
;
585 if ((left
->mode
& 07777) != (right
->mode
& 07777))
588 if ((left
->mode
& S_IFMT
) != (right
->mode
& S_IFMT
))
591 if (st
->do_mtime
&& strcmp(left
->path
, st
->metafile
) &&
592 ( left
->mtime
!= right
->mtime
593 || left
->mtimensec
!= right
->mtimensec
)
595 retval
|= DIFF_MTIME
;
597 if (mentry_compare_xattr(left
, right
)) {
598 retval
|= DIFF_XATTR
;
605 /* Compares lists of real and stored metadata and calls pfunc for each */
607 mentries_compare(struct metahash
*mhashreal
,
608 struct metahash
*mhashstored
,
610 (struct metaentry
*real
, struct metaentry
*stored
, int cmp
),
613 struct metaentry
*real
, *stored
;
616 if (!mhashreal
|| !mhashstored
) {
617 msg(MSG_ERROR
, "%s called with empty list\n", __func__
);
621 for (key
= 0; key
< HASH_INDEXES
; key
++) {
622 for (real
= mhashreal
->bucket
[key
]; real
; real
= real
->next
) {
623 stored
= mentry_find(real
->path
, mhashstored
);
626 pfunc(real
, NULL
, DIFF_ADDED
);
628 pfunc(real
, stored
, mentry_compare(real
, stored
, st
));
631 for (stored
= mhashstored
->bucket
[key
]; stored
; stored
= stored
->next
) {
632 real
= mentry_find(stored
->path
, mhashreal
);
635 pfunc(NULL
, stored
, DIFF_DELE
);
640 /* Dumps given metadata */
642 mentries_dump(struct metahash
*mhash
)
644 const struct metaentry
*mentry
;
645 char mode
[11 + 1] = "";
646 char date
[12 + 2 + 2 + 2*1 + 1 + 2 + 2 + 2 + 2*1 + 1] = "";
647 char zone
[5 + 1] = "";
650 for (int key
= 0; key
< HASH_INDEXES
; key
++) {
651 for (mentry
= mhash
->bucket
[key
]; mentry
; mentry
= mentry
->next
) {
652 strmode(mentry
->mode
, mode
);
653 localtime_r(&mentry
->mtime
, &cal
);
654 strftime(date
, sizeof(date
), "%F %T", &cal
);
655 strftime(zone
, sizeof(zone
), "%z", &cal
);
656 printf("%s\t%s\t%s\t%s.%09ld %s\t%s%s\n",
658 mentry
->owner
, mentry
->group
,
659 date
, mentry
->mtimensec
, zone
,
660 mentry
->path
, S_ISDIR(mentry
->mode
) ? "/" : "");
661 for (unsigned i
= 0; i
< mentry
->xattrs
; i
++) {
662 printf("\t\t\t\t%s%s\t%s=",
663 mentry
->path
, S_ISDIR(mentry
->mode
) ? "/" : "",
664 mentry
->xattr_names
[i
]);
666 for (; p
< mentry
->xattr_lvalues
[i
]; p
++) {
667 const char ch
= mentry
->xattr_values
[i
][p
];
668 if ((unsigned)(ch
- 32) > 126 - 32) {
675 (int)mentry
->xattr_lvalues
[i
],
676 mentry
->xattr_values
[i
]);
679 for (p
= 0; p
< mentry
->xattr_lvalues
[i
]; p
++)
680 printf("%02hhx", (char)mentry
->xattr_values
[i
][p
]);