summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/metaentry.c125
1 files changed, 73 insertions, 52 deletions
diff --git a/src/metaentry.c b/src/metaentry.c
index 978ef72..035901c 100644
--- a/src/metaentry.c
+++ b/src/metaentry.c
@@ -405,25 +405,14 @@ mentries_recurse_path(const char *opath, struct metahash **mhash, msettings *st)
free(path);
}
-/* Stores metaentries to a file */
-void
-mentries_tofile(const struct metahash *mhash, const char *path)
+/* Stores metaentries to a Format 0 file */
+static void
+mentries_tofile_v0(const struct metahash *mhash, FILE * to)
{
- FILE *to;
const struct metaentry *mentry;
int key;
unsigned i;
- to = fopen(path, "w");
- if (!to) {
- msg(MSG_CRITICAL, "Failed to open %s: %s\n",
- path, strerror(errno));
- exit(EXIT_FAILURE);
- }
-
- write_binary_string(SIGNATURE, SIGNATURELEN, to);
- write_binary_string(VERSION_0, VERSIONLEN, to);
-
for (key = 0; key < HASH_INDEXES; key++) {
for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) {
write_string(mentry->path, to);
@@ -441,21 +430,85 @@ mentries_tofile(const struct metahash *mhash, const char *path)
}
}
}
+}
+
+/* Stores metaentries to a file */
+void
+mentries_tofile(const struct metahash *mhash, const char *path)
+{
+ FILE *to;
+
+ to = fopen(path, "w");
+ if (!to) {
+ msg(MSG_CRITICAL, "Failed to open %s: %s\n",
+ path, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ write_binary_string(SIGNATURE, SIGNATURELEN, to);
+ write_binary_string(VERSION_0, VERSIONLEN, to);
+
+ mentries_tofile_v0(mhash, to);
fclose(to);
}
+/* Creates a metaentry list from a Format 0 file buffer */
+static int
+mentries_fromfile_v0(struct metahash **mhash, const char *ptr, const char *max)
+{
+ struct metaentry *mentry;
+ unsigned i;
+
+ while (ptr < max) {
+ if (*ptr == '\0') {
+ return -1;
+ }
+
+ mentry = mentry_alloc();
+ mentry->path = read_string(&ptr, max);
+ mentry->pathlen = strlen(mentry->path);
+ mentry->owner = read_string(&ptr, max);
+ mentry->group = read_string(&ptr, max);
+ mentry->mtime = (time_t)read_int(&ptr, 8, max);
+ mentry->mtimensec = (time_t)read_int(&ptr, 8, max);
+ mentry->mode = (mode_t)read_int(&ptr, 2, max);
+ mentry->xattrs = (unsigned)read_int(&ptr, 4, max);
+
+ if (!mentry->xattrs) {
+ mentry_insert(mentry, *mhash);
+ continue;
+ }
+
+ mentry->xattr_names = xmalloc(mentry->xattrs * sizeof(char *));
+ mentry->xattr_lvalues = xmalloc(mentry->xattrs * sizeof(ssize_t));
+ mentry->xattr_values = xmalloc(mentry->xattrs * sizeof(char *));
+
+ for (i = 0; i < mentry->xattrs; i++) {
+ mentry->xattr_names[i] = read_string(&ptr, max);
+ mentry->xattr_lvalues[i] = (int)read_int(&ptr, 4, max);
+ mentry->xattr_values[i] = read_binary_string(
+ &ptr,
+ mentry->xattr_lvalues[i],
+ max
+ );
+ }
+ mentry_insert(mentry, *mhash);
+ }
+
+ return 0;
+}
+
/* Creates a metaentry list from a file */
void
mentries_fromfile(struct metahash **mhash, const char *path)
{
- struct metaentry *mentry;
char *mmapstart;
char const *ptr;
char const *max;
int fd;
struct stat sbuf;
- unsigned i;
+ int i;
if (!(*mhash))
*mhash = mhash_alloc();
@@ -500,42 +553,10 @@ mentries_fromfile(struct metahash **mhash, const char *path)
}
ptr += VERSIONLEN;
- while (ptr < max) {
- if (*ptr == '\0') {
- msg(MSG_CRITICAL, "Invalid characters in file %s\n",
- path);
- goto out;
- }
-
- mentry = mentry_alloc();
- mentry->path = read_string(&ptr, max);
- mentry->pathlen = strlen(mentry->path);
- mentry->owner = read_string(&ptr, max);
- mentry->group = read_string(&ptr, max);
- mentry->mtime = (time_t)read_int(&ptr, 8, max);
- mentry->mtimensec = (time_t)read_int(&ptr, 8, max);
- mentry->mode = (mode_t)read_int(&ptr, 2, max);
- mentry->xattrs = (unsigned)read_int(&ptr, 4, max);
-
- if (!mentry->xattrs) {
- mentry_insert(mentry, *mhash);
- continue;
- }
-
- mentry->xattr_names = xmalloc(mentry->xattrs * sizeof(char *));
- mentry->xattr_lvalues = xmalloc(mentry->xattrs * sizeof(ssize_t));
- mentry->xattr_values = xmalloc(mentry->xattrs * sizeof(char *));
-
- for (i = 0; i < mentry->xattrs; i++) {
- mentry->xattr_names[i] = read_string(&ptr, max);
- mentry->xattr_lvalues[i] = (int)read_int(&ptr, 4, max);
- mentry->xattr_values[i] = read_binary_string(
- &ptr,
- mentry->xattr_lvalues[i],
- max
- );
- }
- mentry_insert(mentry, *mhash);
+ i = mentries_fromfile_v0(mhash, ptr, max);
+ if (i != 0) {
+ msg(MSG_CRITICAL, "Invalid characters in file %s\n", path);
+ goto out;
}
out: