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.
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.
25 #include <sys/types.h>
30 #include <attr/xattr.h>
38 #include "metastore.h"
39 #include "metaentry.h"
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
);
68 static struct metaentry
*
71 struct metaentry
*mentry
;
72 mentry
= xmalloc(sizeof(struct metaentry
));
73 memset(mentry
, 0, sizeof(struct metaentry
));
78 mentry_insert(struct metaentry
*mentry
, struct metaentry
**mhead
)
80 struct metaentry
*prev
;
81 struct metaentry
*curr
;
89 if (strcmp(mentry
->path
, (*mhead
)->path
) < 0) {
90 mentry
->next
= *mhead
;
96 for (curr
= prev
->next
; curr
; curr
= curr
->next
) {
97 comp
= strcmp(mentry
->path
, curr
->path
);
99 /* Two matching paths */
113 mentry_print(const struct metaentry
*mentry
)
117 if (!mentry
|| !mentry
->path
) {
119 "Incorrect meta entry passed to printmetaentry\n");
123 msg(MSG_DEBUG
, "===========================\n");
124 msg(MSG_DEBUG
, "Dump of metaentry %p\n", mentry
);
125 msg(MSG_DEBUG
, "===========================\n");
127 msg(MSG_DEBUG
, "path\t\t: %s\n", mentry
->path
);
128 msg(MSG_DEBUG
, "owner\t\t: %s\n", mentry
->owner
);
129 msg(MSG_DEBUG
, "group\t\t: %s\n", mentry
->group
);
130 msg(MSG_DEBUG
, "mtime\t\t: %ld\n", (unsigned long)mentry
->mtime
);
131 msg(MSG_DEBUG
, "mtimensec\t: %ld\n", (unsigned long)mentry
->mtimensec
);
132 msg(MSG_DEBUG
, "mode\t\t: %ld\n", (unsigned long)mentry
->mode
);
133 for (i
= 0; i
< mentry
->xattrs
; i
++) {
134 msg(MSG_DEBUG
, "xattr[%i]\t: %s=\"", i
, mentry
->xattr_names
[i
]);
135 binary_print(mentry
->xattr_values
[i
], mentry
->xattr_lvalues
[i
]);
136 msg(MSG_DEBUG
, "\"\n");
139 msg(MSG_DEBUG
, "===========================\n\n");
143 mentries_print(const struct metaentry
*mhead
)
145 const struct metaentry
*mentry
;
148 for (mentry
= mhead
; mentry
; mentry
= mentry
->next
) {
150 mentry_print(mentry
);
153 msg(MSG_DEBUG
, "%i entries in total\n", i
);
157 static struct metaentry
*
158 mentry_create(const char *path
)
160 ssize_t lsize
, vsize
;
166 struct metaentry
*mentry
;
168 if (lstat(path
, &sbuf
)) {
169 msg(MSG_ERROR
, "lstat failed for %s: %s\n",
170 path
, strerror(errno
));
174 pbuf
= getpwuid(sbuf
.st_uid
);
176 msg(MSG_ERROR
, "getpwuid failed for %i: %s\n",
177 (int)sbuf
.st_uid
, strerror(errno
));
181 gbuf
= getgrgid(sbuf
.st_gid
);
183 msg(MSG_ERROR
, "getgrgid failed for %i: %s\n",
184 (int)sbuf
.st_gid
, strerror(errno
));
188 mentry
= mentry_alloc();
189 mentry
->path
= xstrdup(path
);
190 mentry
->owner
= xstrdup(pbuf
->pw_name
);
191 mentry
->group
= xstrdup(gbuf
->gr_name
);
192 mentry
->mode
= sbuf
.st_mode
& 0177777;
193 mentry
->mtime
= sbuf
.st_mtim
.tv_sec
;
194 mentry
->mtimensec
= sbuf
.st_mtim
.tv_nsec
;
196 /* symlinks have no xattrs */
197 if (S_ISLNK(mentry
->mode
))
200 lsize
= listxattr(path
, NULL
, 0);
202 msg(MSG_ERROR
, "listxattr failed for %s: %s\n",
203 path
, strerror(errno
));
207 list
= xmalloc(lsize
);
208 lsize
= listxattr(path
, list
, lsize
);
210 msg(MSG_ERROR
, "listxattr failed for %s: %s\n",
211 path
, strerror(errno
));
216 for (attr
= list
; attr
< list
+ lsize
; attr
= strchr(attr
, '\0') + 1) {
226 mentry
->xattr_names
= xmalloc(i
* sizeof(char *));
227 mentry
->xattr_values
= xmalloc(i
* sizeof(char *));
228 mentry
->xattr_lvalues
= xmalloc(i
* sizeof(ssize_t
));
231 for (attr
= list
; attr
< list
+ lsize
; attr
= strchr(attr
, '\0') + 1) {
235 mentry
->xattr_names
[i
] = xstrdup(attr
);
236 vsize
= getxattr(path
, attr
, NULL
, 0);
238 msg(MSG_ERROR
, "getxattr failed for %s: %s\n",
239 path
, strerror(errno
));
243 mentry
->xattr_lvalues
[i
] = vsize
;
244 mentry
->xattr_values
[i
] = xmalloc(vsize
);
246 vsize
= getxattr(path
, attr
, mentry
->xattr_values
[i
], vsize
);
248 msg(MSG_ERROR
, "getxattr failed for %s: %s\n",
249 path
, strerror(errno
));
259 normalize_path(const char *orig
)
261 char *real
= canonicalize_file_name(orig
);
265 getcwd(cwd
, PATH_MAX
);
269 if (!strncmp(real
, cwd
, strlen(cwd
))) {
270 result
= xmalloc(strlen(real
) - strlen(cwd
) + 1 + 1);
273 strcat(result
, real
+ strlen(cwd
));
275 result
= xstrdup(real
);
283 mentries_recurse(const char *path
, struct metaentry
**mhead
)
286 struct metaentry
*mentry
;
287 char tpath
[PATH_MAX
];
294 if (lstat(path
, &sbuf
)) {
295 msg(MSG_ERROR
, "lstat failed for %s: %s\n",
296 path
, strerror(errno
));
300 mentry
= mentry_create(path
);
304 mentry_insert(mentry
, mhead
);
306 if (S_ISDIR(sbuf
.st_mode
)) {
309 msg(MSG_ERROR
, "opendir failed for %s: %s\n",
310 path
, strerror(errno
));
314 while ((dent
= readdir(dir
))) {
315 if (!strcmp(dent
->d_name
, ".") ||
316 !strcmp(dent
->d_name
, "..") ||
317 !strcmp(dent
->d_name
, ".git"))
319 snprintf(tpath
, PATH_MAX
, "%s/%s", path
, dent
->d_name
);
320 tpath
[PATH_MAX
- 1] = '\0';
321 mentries_recurse(tpath
, mhead
);
329 mentries_recurse_path(const char *opath
, struct metaentry
**mhead
)
331 char *path
= normalize_path(opath
);
332 mentries_recurse(path
, mhead
);
337 mentries_tofile(const struct metaentry
*mhead
, const char *path
)
340 const struct metaentry
*mentry
;
343 to
= fopen(path
, "w");
349 write_binary_string(SIGNATURE
, SIGNATURELEN
, to
);
350 write_binary_string(VERSION
, VERSIONLEN
, to
);
352 for (mentry
= mhead
; mentry
; mentry
= mentry
->next
) {
353 write_string(mentry
->path
, to
);
354 write_string(mentry
->owner
, to
);
355 write_string(mentry
->group
, to
);
356 write_int((uint64_t)mentry
->mtime
, 8, to
);
357 write_int((uint64_t)mentry
->mtimensec
, 8, to
);
358 write_int((uint64_t)mentry
->mode
, 2, to
);
359 write_int(mentry
->xattrs
, 4, to
);
360 for (i
= 0; i
< mentry
->xattrs
; i
++) {
361 write_string(mentry
->xattr_names
[i
], to
);
362 write_int(mentry
->xattr_lvalues
[i
], 4, to
);
363 write_binary_string(mentry
->xattr_values
[i
],
364 mentry
->xattr_lvalues
[i
], to
);
372 mentries_fromfile(struct metaentry
**mhead
, const char *path
)
374 struct metaentry
*mentry
;
382 fd
= open(path
, O_RDONLY
);
388 if (fstat(fd
, &sbuf
)) {
393 if (sbuf
.st_size
< (SIGNATURELEN
+ VERSIONLEN
)) {
394 msg(MSG_CRITICAL
, "File %s has an invalid size\n", path
);
398 mmapstart
= mmap(NULL
, (size_t)sbuf
.st_size
, PROT_READ
,
400 if (mmapstart
== MAP_FAILED
) {
401 msg(MSG_CRITICAL
, "Unable to mmap %s: %s\n",
402 path
, strerror(errno
));
406 max
= mmapstart
+ sbuf
.st_size
;
408 if (strncmp(ptr
, SIGNATURE
, SIGNATURELEN
)) {
409 msg(MSG_CRITICAL
, "Invalid signature for file %s\n", path
);
414 if (strncmp(ptr
, VERSION
, VERSIONLEN
)) {
415 msg(MSG_CRITICAL
, "Invalid version of file %s\n", path
);
420 while (ptr
< mmapstart
+ sbuf
.st_size
) {
422 msg(MSG_CRITICAL
, "Invalid characters in file %s\n",
427 mentry
= mentry_alloc();
428 mentry
->path
= read_string(&ptr
, max
);
429 mentry
->owner
= read_string(&ptr
, max
);
430 mentry
->group
= read_string(&ptr
, max
);
431 mentry
->mtime
= (time_t)read_int(&ptr
, 8, max
);
432 mentry
->mtimensec
= (time_t)read_int(&ptr
, 8, max
);
433 mentry
->mode
= (mode_t
)read_int(&ptr
, 2, max
);
434 mentry
->xattrs
= (unsigned int)read_int(&ptr
, 4, max
);
436 if (!mentry
->xattrs
) {
437 mentry_insert(mentry
, mhead
);
441 mentry
->xattr_names
= xmalloc(mentry
->xattrs
*
443 mentry
->xattr_lvalues
= xmalloc(mentry
->xattrs
*
445 mentry
->xattr_values
= xmalloc(mentry
->xattrs
*
448 for (i
= 0; i
< mentry
->xattrs
; i
++) {
449 mentry
->xattr_names
[i
] = read_string(&ptr
, max
);
450 mentry
->xattr_lvalues
[i
] =
451 (int)read_int(&ptr
, 4, max
);
452 mentry
->xattr_values
[i
] =
453 read_binary_string(&ptr
,
454 mentry
->xattr_lvalues
[i
],
457 mentry_insert(mentry
, mhead
);
461 munmap(mmapstart
, sbuf
.st_size
);
465 static struct metaentry
*
466 mentry_find(const char *path
, struct metaentry
*mhead
)
470 /* FIXME - We can do a bisect search here instead */
471 for (m
= mhead
; m
; m
= m
->next
) {
472 if (!strcmp(path
, m
->path
))
478 /* Returns xattr index in haystack which corresponds to xattr n in needle */
480 mentry_find_xattr(struct metaentry
*haystack
, struct metaentry
*needle
, int n
)
484 for (i
= 0; i
< haystack
->xattrs
; i
++) {
485 if (strcmp(haystack
->xattr_names
[i
], needle
->xattr_names
[n
]))
487 if (haystack
->xattr_lvalues
[i
] != needle
->xattr_lvalues
[n
])
489 if (bcmp(haystack
->xattr_values
[i
], needle
->xattr_values
[n
],
490 needle
->xattr_lvalues
[n
]))
497 /* Returns zero if all xattrs in left and right match */
499 mentry_compare_xattr(struct metaentry
*left
, struct metaentry
*right
)
503 if (left
->xattrs
!= right
->xattrs
)
506 /* Make sure all xattrs in left are found in right and vice versa */
507 for (i
= 0; i
< left
->xattrs
; i
++) {
508 if (mentry_find_xattr(right
, left
, i
) < 0 ||
509 mentry_find_xattr(left
, right
, i
) < 0) {
518 mentry_compare(struct metaentry
*left
, struct metaentry
*right
)
520 int retval
= DIFF_NONE
;
522 if (!left
|| !right
) {
523 msg(MSG_ERROR
, "%s called with empty list\n", __FUNCTION__
);
527 if (strcmp(left
->path
, right
->path
))
530 if (strcmp(left
->owner
, right
->owner
))
531 retval
|= DIFF_OWNER
;
533 if (strcmp(left
->group
, right
->group
))
534 retval
|= DIFF_GROUP
;
536 if ((left
->mode
& 07777) != (right
->mode
& 07777))
539 if ((left
->mode
& S_IFMT
) != (right
->mode
& S_IFMT
))
542 if (do_mtime
&& strcmp(left
->path
, METAFILE
) &&
543 (left
->mtime
!= right
->mtime
||
544 left
->mtimensec
!= right
->mtimensec
))
545 retval
|= DIFF_MTIME
;
547 if (mentry_compare_xattr(left
, right
)) {
548 retval
|= DIFF_XATTR
;
556 mentries_compare(struct metaentry
*mheadleft
,
557 struct metaentry
*mheadright
,
558 void (*printfunc
)(struct metaentry
*, struct metaentry
*, int))
560 struct metaentry
*left
, *right
;
563 if (!mheadleft
|| !mheadright
) {
564 msg(MSG_ERROR
, "%s called with empty list\n", __FUNCTION__
);
568 for (left
= mheadleft
; left
; left
= left
->next
) {
569 right
= mentry_find(left
->path
, mheadright
);
573 cmp
= mentry_compare(left
, right
);
574 printfunc(left
, right
, cmp
);
577 for (right
= mheadright
; right
; right
= right
->next
) {
578 left
= mentry_find(right
->path
, mheadleft
);
580 printfunc(left
, right
, DIFF_DELE
);