summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2003-08-26 21:43:31 +0000
committerLennart Poettering <lennart@poettering.net>2003-08-26 21:43:31 +0000
commit3bc3ad24a2c089b4ee80fc1765fab3d2af378d00 (patch)
tree632a002b1c9a507ab49c11875eb373a6642902fb
parentd4a8a10792c7f9b777487b2d15ab56c737e7e35c (diff)
Initial commit
git-svn-id: file:///home/lennart/svn/public/syrep/trunk@2 07ea20a6-d2c5-0310-9e02-9ef735347d72
-rw-r--r--doc/usage-intro.txt19
-rw-r--r--src/Makefile18
-rw-r--r--src/cache.c229
-rw-r--r--src/cache.h13
-rw-r--r--src/context.c199
-rw-r--r--src/context.h28
-rw-r--r--src/dbstruct.h60
-rw-r--r--src/dbutil.c167
-rw-r--r--src/dbutil.h13
-rw-r--r--src/diff.c345
-rw-r--r--src/diff.h18
-rw-r--r--src/dump.c32
-rw-r--r--src/dump.h8
-rw-r--r--src/history.c48
-rw-r--r--src/history.h8
-rw-r--r--src/info.c25
-rw-r--r--src/info.h8
-rw-r--r--src/list.c162
-rw-r--r--src/list.h8
-rw-r--r--src/md5.c381
-rw-r--r--src/md5.h91
-rw-r--r--src/md5util.c85
-rw-r--r--src/md5util.h30
-rw-r--r--src/merge.c132
-rw-r--r--src/merge.h8
-rw-r--r--src/package.c310
-rw-r--r--src/package.h14
-rw-r--r--src/syrep.c301
-rw-r--r--src/syrep.ggo27
-rw-r--r--src/syrep.h13
-rw-r--r--src/update.c207
-rw-r--r--src/update.h9
-rw-r--r--src/util.c106
-rw-r--r--src/util.h16
-rw-r--r--test/Makefile20
35 files changed, 3158 insertions, 0 deletions
diff --git a/doc/usage-intro.txt b/doc/usage-intro.txt
new file mode 100644
index 0000000..dcb9ee3
--- /dev/null
+++ b/doc/usage-intro.txt
@@ -0,0 +1,19 @@
+diff [DIR|SNAP] [DIR|SNAP]
+
+update [-S SNAP] DIR
+
+list [DIR|SNAP]
+
+info [DIR|SNAP]
+
+merge [-S SNAP] SNAP DIR
+merge [-S SNAP] PATCH DIR
+
+makepatch [-S SNAP] -O PATCH SNAP DIR
+makepatch [-S SNAP] SNAP DIR > PATCH
+
+extract [-o DIR] PATCH
+
+history [DIR|SNAP]
+
+
diff --git a/src/Makefile b/src/Makefile
new file mode 100644
index 0000000..45f00b2
--- /dev/null
+++ b/src/Makefile
@@ -0,0 +1,18 @@
+CFLAGS=-Wall -pipe -O0 -g
+CC=gcc
+
+
+all: syrep
+
+syrep: cache.o update.o util.o syrep.o md5.o md5util.o context.o package.o dbutil.o cmdline.o info.o history.o dump.o list.o diff.o merge.o
+ $(CC) -g $^ -o $@ -ldb
+
+cmdline.c cmdline.h: syrep.ggo Makefile
+ gengetopt --unamed-opts < $<
+
+update.o: cmdline.h
+
+clean:
+ rm -f *.o syrep cmdline.[ch]
+
+.PHONY: all clean
diff --git a/src/cache.c b/src/cache.c
new file mode 100644
index 0000000..860b4fb
--- /dev/null
+++ b/src/cache.c
@@ -0,0 +1,229 @@
+#include <db.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <time.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "cache.h"
+#include "md5util.h"
+
+struct syrep_cache_key {
+ uint64_t dev;
+ uint64_t inode;
+ uint32_t date;
+ uint64_t size;
+};
+
+struct syrep_cache_data {
+ uint8_t digest[16];
+ uint32_t timestamp;
+};
+
+struct syrep_md_cache {
+ DB* db;
+ uint32_t timestamp;
+ int ro;
+};
+
+struct syrep_md_cache* md_cache_open(const char* fn, int ro) {
+ struct syrep_md_cache *c = NULL;
+ int ret;
+
+ assert(fn);
+
+ if (!(c = malloc(sizeof(struct syrep_md_cache))))
+ goto fail;
+
+ c->timestamp = time(NULL);
+ c->ro = ro;
+
+ if ((ret = db_create(&c->db, NULL, 0))) {
+ fprintf(stderr, "db_create: %s\n", db_strerror(ret));
+ goto fail;
+ }
+
+ if ((ret = c->db->open(c->db, NULL, fn, NULL, DB_BTREE, ro ? DB_RDONLY : DB_CREATE, 0664))) {
+ c->db->err(c->db, ret, "open(%s)", fn);
+ goto fail;
+ }
+
+ return c;
+
+fail:
+
+ if (c) {
+ if (c->db)
+ c->db->close(c->db, 0);
+
+ free(c);
+ }
+
+ return NULL;
+}
+
+void md_cache_close(struct syrep_md_cache *c) {
+ assert(c);
+
+ if (c->db)
+ c->db->close(c->db, 0);
+
+ free(c);
+}
+
+static int get(struct syrep_md_cache *c, const struct syrep_cache_key *k, uint8_t digest[16]) {
+ int ret;
+ DBT key, data;
+
+ assert(c && c->db && k);
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ key.data = (void*) k;
+ key.size = sizeof(struct syrep_cache_key);
+
+ if ((ret = c->db->get(c->db, NULL, &key, &data, 0)) != 0) {
+ if (ret == DB_NOTFOUND) {
+ //fprintf(stderr, "Cache MISS!\n");
+ return 0;
+ }
+
+ c->db->err(c->db, ret, "cache::get");
+ return -1;
+ }
+
+ //fprintf(stderr, "Cache HIT\n");
+
+ assert(data.data);
+ memcpy(digest, &((struct syrep_cache_data*) data.data)->digest, 16);
+
+ return 1;
+}
+
+static int put(struct syrep_md_cache *c, const struct syrep_cache_key *k, const uint8_t digest[16], uint32_t timestamp) {
+ int ret;
+ DBT key, data;
+ struct syrep_cache_data d;
+
+ assert(c && c->db && k);
+
+ memset(&key, 0, sizeof(key));
+ key.data = (void*) k;
+ key.size = sizeof(struct syrep_cache_key);
+
+ memcpy(d.digest, digest, 16);
+ d.timestamp = timestamp;
+
+ memset(&data, 0, sizeof(data));
+ data.data = &d;
+ data.size = sizeof(struct syrep_cache_data);
+
+ if ((ret = c->db->put(c->db, NULL, &key, &data, 0)) != 0) {
+ c->db->err(c->db, ret, "cache::put");
+ return -1;
+ }
+
+ return 0;
+}
+
+int md_cache_get(struct syrep_md_cache *c, const char *path, uint8_t digest[16]) {
+ struct syrep_cache_key k;
+ int r = -1, fd = -1, j;
+ struct stat st;
+
+ if ((fd = open(path, O_RDONLY)) < 0) {
+ fprintf(stderr, "open(%s): %s\n", path, strerror(errno));
+ goto finish;
+ }
+
+ if (fstat(fd, &st) < 0) {
+ fprintf(stderr, "fstat(%s): %s\n", path, strerror(errno));
+ goto finish;
+ }
+
+ if (!S_ISREG(st.st_mode)) {
+ fprintf(stderr, "<%s> not a regular file: %s\n", path, strerror(errno));
+ goto finish;
+ }
+
+ memset(&k, 0, sizeof(k));
+ k.dev = (uint64_t) st.st_dev;
+ k.inode = (uint64_t) st.st_ino;
+ k.date = (uint32_t) st.st_mtime;
+ k.size = (uint64_t) st.st_size;
+
+ if (!c)
+ j = 0;
+ else
+ if ((j = get(c, &k, digest)) < 0)
+ goto finish;
+
+
+ if (!j)
+ if (fdmd5(fd, st.st_size, digest) < 0)
+ goto finish;
+
+ if (c && !c->ro)
+ put(c, &k, digest, c->timestamp);
+
+ r = 0;
+
+finish:
+
+ if (fd >= 0)
+ close(fd);
+
+ return r;
+}
+
+int md_cache_vacuum(struct syrep_md_cache*c) {
+ int r = -1, ret;
+ DBC *cursor;
+ DBT key, data;
+ int ndel = 0, ntotal = 0;
+
+ if (c->ro)
+ return 0;
+
+ assert(c && c->db);
+
+ if ((ret = c->db->cursor(c->db, NULL, &cursor, 0)) != 0) {
+ c->db->err(c->db, ret, "cache::vacuum");
+ return -1;
+ }
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ while ((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
+ struct syrep_cache_data *d;
+
+ ntotal++;
+ d = (struct syrep_cache_data*) data.data;
+ assert(d);
+ if (d->timestamp < c->timestamp) {
+ cursor->c_del(cursor, 0);
+ ndel++;
+ }
+ }
+
+ if (ret != DB_NOTFOUND) {
+ c->db->err(c->db, ret, "cache::vacuum()");
+ goto finish;
+ }
+
+ /*fprintf(stderr, "Cache vacuum successfully completed, %i of %i entries deleted.\n", ndel, ntotal);*/
+
+ r = 0;
+
+finish:
+
+ cursor->c_close(cursor);
+
+ return r;
+}
diff --git a/src/cache.h b/src/cache.h
new file mode 100644
index 0000000..7087b03
--- /dev/null
+++ b/src/cache.h
@@ -0,0 +1,13 @@
+#ifndef foomdcachehfoo
+#define foomdcachehfoo
+
+#include <stdint.h>
+
+struct syrep_md_cache;
+
+struct syrep_md_cache* md_cache_open(const char* fn, int ro);
+void md_cache_close(struct syrep_md_cache *c);
+int md_cache_get(struct syrep_md_cache *c, const char *path, uint8_t digest[16]);
+int md_cache_vacuum(struct syrep_md_cache *c);
+
+#endif
diff --git a/src/context.c b/src/context.c
new file mode 100644
index 0000000..00455ea
--- /dev/null
+++ b/src/context.c
@@ -0,0 +1,199 @@
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+#include <limits.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "context.h"
+#include "package.h"
+
+int db_context_free(struct syrep_db_context* c) {
+ if (c) {
+
+ if (c->db_id_meta)
+ c->db_id_meta->close(c->db_id_meta, 0);
+
+ if (c->db_md_name)
+ c->db_md_name->close(c->db_md_name, 0);
+
+ if (c->db_name_md)
+ c->db_name_md->close(c->db_name_md, 0);
+
+ if (c->db_md_lastname)
+ c->db_md_lastname->close(c->db_md_lastname, 0);
+
+ if (c->db_name_lastmd)
+ c->db_name_lastmd->close(c->db_name_lastmd, 0);
+
+ if (c->db_version_timestamp)
+ c->db_version_timestamp->close(c->db_version_timestamp, 0);
+
+ if (c->package)
+ package_remove(c->package);
+
+ if (c->origin)
+ free(c->origin);
+
+ free(c);
+ }
+
+ return 0;
+}
+
+
+static DB* open_db(const char*path, int dup) {
+ int ret;
+ DB* db;
+
+ if ((ret = db_create(&db, NULL, 0))) {
+ fprintf(stderr, "db_create: %s\n", db_strerror(ret));
+ return NULL;
+ }
+
+ if (dup)
+ db->set_flags(db, DB_DUPSORT);
+
+ //db->set_pagesize(db, 4096*8);
+
+ if ((ret = db->open(db, NULL, path, NULL, DB_BTREE, DB_CREATE, 0664))) {
+ db->err(db, ret, "open(%s)", path);
+ db->close(db, 0);
+ return NULL;
+ }
+
+ return db;
+}
+
+struct syrep_db_context* db_context_open(const char *filename) {
+ struct syrep_db_context *c = NULL;
+ const char* path;
+ FILE *f;
+
+ if (!(c = malloc(sizeof(struct syrep_db_context))))
+ goto fail;
+
+ memset(c, 0, sizeof(struct syrep_db_context));
+
+ if (!(c->package = package_open(filename)))
+ goto fail;
+
+ path = package_get_item(c->package, "timestamp");
+ if ((f = fopen(path, "r"))) {
+ if (fscanf(f, "%i", &c->timestamp) != 1)
+ c->timestamp = 0;
+ fclose(f);
+ }
+
+ if (!c->timestamp)
+ c->timestamp = time(NULL);
+
+ path = package_get_item(c->package, "version");
+ if ((f = fopen(path, "r"))) {
+ if (fscanf(f, "%u", &c->version) != 1)
+ c->version = 0;
+ fclose(f);
+ }
+
+ path = package_get_item(c->package, "origin");
+ if ((f = fopen(path, "r"))) {
+ char hn[256];
+ if (fgets(hn, sizeof(hn), f)) {
+ char *nl;
+
+ if ((nl = strchr(hn, '\n')))
+ *nl = 0;
+
+ if (hn[0])
+ c->origin = strdup(hn);
+ }
+
+ fclose(f);
+ } else
+ c->version = 0;
+
+ if (!c->origin) {
+ char hn[256];
+ if (gethostname(hn, sizeof(hn)) < 0)
+ goto fail;
+
+ c->origin = strdup(hn);
+ }
+
+ /* Creating database id_meta */
+ if (!(c->db_id_meta = open_db(package_get_item(c->package, "id_meta"), 0)))
+ goto fail;
+
+ /* Creating database md_name */
+ if (!(c->db_md_name = open_db(package_get_item(c->package, "md_name"), 1)))
+ goto fail;
+
+ /* Creating database name_md */
+ if (!(c->db_name_md = open_db(package_get_item(c->package, "name_md"), 1)))
+ goto fail;
+
+ /* Creating database name_lastmd */
+ if (!(c->db_name_lastmd = open_db(package_get_item(c->package, "name_lastmd"), 0)))
+ goto fail;
+
+ /* Creating database md_lastname */
+ if (!(c->db_md_lastname = open_db(package_get_item(c->package, "md_lastname"), 0)))
+ goto fail;
+
+ /* Creating database version_timestamp */
+ if (!(c->db_version_timestamp = open_db(package_get_item(c->package, "version_timestamp"), 0)))
+ goto fail;
+
+ return c;
+
+
+fail:
+ db_context_free(c);
+
+ return NULL;
+}
+
+int db_context_save(struct syrep_db_context *c, const char *filename) {
+ FILE *f;
+ assert(c && c->package && filename);
+
+ if (c->db_id_meta)
+ c->db_id_meta->sync(c->db_id_meta, 0);
+
+ if (c->db_md_name)
+ c->db_md_name->sync(c->db_md_name, 0);
+
+ if (c->db_name_md)
+ c->db_name_md->sync(c->db_name_md, 0);
+
+ if (c->db_md_lastname)
+ c->db_md_lastname->sync(c->db_md_lastname, 0);
+
+ if (c->db_name_lastmd)
+ c->db_name_lastmd->sync(c->db_name_lastmd, 0);
+
+ if (c->db_version_timestamp)
+ c->db_version_timestamp->sync(c->db_version_timestamp, 0);
+
+ if (!(f = fopen(package_get_item(c->package, "timestamp"), "w+")))
+ return -1;
+
+ fprintf(f, "%i\n", c->timestamp);
+ fclose(f);
+
+ if (!(f = fopen(package_get_item(c->package, "version"), "w+")))
+ return -1;
+
+ fprintf(f, "%u\n", c->version);
+ fclose(f);
+
+ if (!(f = fopen(package_get_item(c->package, "origin"), "w+")))
+ return -1;
+
+ fprintf(f, "%s\n", c->origin);
+ fclose(f);
+
+ return package_save(c->package, filename);
+}
diff --git a/src/context.h b/src/context.h
new file mode 100644
index 0000000..6e51b1c
--- /dev/null
+++ b/src/context.h
@@ -0,0 +1,28 @@
+#ifndef foocontexthfoo
+#define foocontexthfoo
+
+#include <db.h>
+#include <stdint.h>
+
+struct syrep_db_context {
+ struct package *package;
+
+ DB *db_id_meta,
+ *db_md_name,
+ *db_name_md,
+ *db_name_lastmd,
+ *db_md_lastname,
+ *db_version_timestamp;
+
+ uint32_t timestamp;
+ uint32_t version;
+ int modified;
+
+ char* origin;
+};
+
+struct syrep_db_context* db_context_open(const char *path);
+int db_context_save(struct syrep_db_context *c, const char *path);
+int db_context_free(struct syrep_db_context* c);
+
+#endif
diff --git a/src/dbstruct.h b/src/dbstruct.h
new file mode 100644
index 0000000..2555cc1
--- /dev/null
+++ b/src/dbstruct.h
@@ -0,0 +1,60 @@
+#ifndef foodbstructhfoo
+#define foodbstructhfoo
+
+#include <limits.h>
+#include <stdint.h>
+
+#include "syrep.h"
+
+struct syrep_md {
+ uint8_t digest[SYREP_DIGESTLENGTH];
+};
+
+struct syrep_name {
+ char path[PATH_MAX+1];
+};
+
+struct syrep_id {
+ struct syrep_md md;
+ struct syrep_name name;
+};
+
+struct syrep_meta {
+ uint32_t first_seen;
+ uint32_t last_seen;
+};
+
+/* struct syrep_namels { */
+/* uint32_t last_seen; */
+/* struct syrep_name name; */
+/* }; */
+
+/* struct syrep_mdls { */
+/* uint32_t last_seen; */
+/* struct syrep_md md; */
+/* }; */
+
+struct syrep_timestamp {
+ uint32_t t;
+};
+
+struct syrep_version {
+ uint32_t v;
+};
+
+/* Table layout:
+ *
+ * syrep_id :: syrep_meta => id_meta
+ * syrep_md :: syrep_name => md_name (DUP)
+ * syrep_name :: syrep_md => name_md (DUP)
+ * syrep_version :: syrep_timestamp => version_timestamp
+ * syrep_name :: syrep_md => name_lastmd
+ * syrep_md :: last_md => md_lastmd
+ *
+ */
+
+/*
+ * syrep_namels :: syrep_md => namels_md
+ * syrep_mdls :: syrep_name => mdls_name
+ */
+#endif
diff --git a/src/dbutil.c b/src/dbutil.c
new file mode 100644
index 0000000..b7b44b9
--- /dev/null
+++ b/src/dbutil.c
@@ -0,0 +1,167 @@
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+
+#include "dbutil.h"
+#include "util.h"
+
+int get_meta_by_name_md(struct syrep_db_context *c, const struct syrep_name*name, const struct syrep_md *md, struct syrep_meta *meta) {
+ int ret;
+ struct syrep_id id;
+ DBT key, data;
+
+ assert(c && c->db_id_meta && name);
+
+ memset(&id, 0, sizeof(id));
+ memcpy(&id.name, name, sizeof(struct syrep_name));
+ memcpy(&id.md, md, sizeof(struct syrep_md));
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ key.data = &id;
+ key.size = sizeof(struct syrep_id);
+
+ if ((ret = c->db_id_meta->get(c->db_id_meta, NULL, &key, &data, 0))) {
+ if (ret == DB_NOTFOUND)
+ return 0;
+
+ c->db_id_meta->err(c->db_id_meta, ret, "id_meta::get");
+ return -1;
+ }
+
+ assert(data.data);
+
+ if (meta)
+ memcpy(meta, data.data, sizeof(struct syrep_meta));
+
+ return 1;
+}
+
+
+int get_current_name_by_md(struct syrep_db_context *c, const struct syrep_md *md, struct syrep_name *name) {
+ int ret, f;
+ struct syrep_meta meta;
+ DBT key, data;
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+ key.data = (void*) md;
+ key.size = sizeof(struct syrep_md);
+
+ if ((ret = c->db_md_lastname->get(c->db_md_lastname, NULL, &key, &data, 0))) {
+ if (ret == DB_NOTFOUND)
+ return 0;
+
+ c->db_md_lastname->err(c->db_md_lastname, ret, "md_lastname::get()");
+ return -1;
+ }
+
+ assert(data.data);
+
+ if ((f = get_meta_by_name_md(c, (struct syrep_name*) data.data, md, &meta)) < 0)
+ return -1;
+
+ if (!f) {
+ fprintf(stderr, "Database inconsistency\n");
+ return -1;
+ }
+
+ if (meta.last_seen != c->version)
+ return 0;
+
+ if (name)
+ memcpy(name, data.data, sizeof(struct syrep_name));
+
+ return 1;
+}
+
+int get_last_md_by_name(struct syrep_db_context *c, const struct syrep_name *name, struct syrep_md *md) {
+ int ret;
+ DBT key, data;
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ key.data = (void*) name;
+ key.size = sizeof(struct syrep_name);
+
+ if ((ret = c->db_name_lastmd->get(c->db_name_lastmd, NULL, &key, &data, 0))) {
+ if (ret == DB_NOTFOUND) {
+ return 0;
+ }
+
+ c->db_name_lastmd->err(c->db_name_lastmd, ret, "name_lastmd::get()");
+ return -1;
+ }
+
+
+ assert(data.data);
+ if (md)
+ memcpy(md, data.data, sizeof(struct syrep_md));
+
+ return 1;
+}
+
+
+int get_current_md_by_name(struct syrep_db_context *c, const struct syrep_name *name, struct syrep_md *md) {
+ struct syrep_md lmd;
+ struct syrep_meta meta;
+ int f;
+
+ if ((f = get_last_md_by_name(c, name, &lmd)) < 0)
+ return -1;
+
+ if (!f)
+ return 0;
+
+ if ((f = get_meta_by_name_md(c, name, &lmd, &meta)) < 0)
+ return -1;
+
+ if (!f) {
+ fprintf(stderr, "Database inconsistency\n");
+ return -1;
+ }
+
+ if (meta.last_seen != c->version)
+ return 0;
+
+ memcpy(md, &lmd, sizeof(struct syrep_md));
+ return 1;
+}
+
+
+uint32_t get_version_timestamp(struct syrep_db_context *c, uint32_t v) {
+ DBT key, data;
+ struct syrep_version version;
+ struct syrep_timestamp *timestamp;
+ int ret;
+
+ assert(c && c->db_version_timestamp);
+
+ if (v > c->version)
+ return time(NULL);
+
+ if (v <= 0)
+ v = 1;
+
+ memset(&version, 0, sizeof(version));
+ version.v = v;
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ key.data = &version;
+ key.size = sizeof(version);
+
+ if ((ret = c->db_version_timestamp->get(c->db_version_timestamp, NULL, &key, &data, 0))) {
+ c->db_version_timestamp->err(c->db_version_timestamp, ret, "version_timestamp::get");
+ return (uint32_t) -1;
+ }
+
+ timestamp = (struct syrep_timestamp*) data.data;
+ assert(timestamp);
+
+ return timestamp->t;
+}
+
diff --git a/src/dbutil.h b/src/dbutil.h
new file mode 100644
index 0000000..8f6a95a
--- /dev/null
+++ b/src/dbutil.h
@@ -0,0 +1,13 @@
+#ifndef foodbutilhfoo
+#define foodbutilhfoo
+
+#include "dbstruct.h"
+#include "context.h"
+
+int get_meta_by_name_md(struct syrep_db_context *c, const struct syrep_name*name, const struct syrep_md *md, struct syrep_meta *meta);
+int get_last_md_by_name(struct syrep_db_context *c, const struct syrep_name *name, struct syrep_md *md);
+int get_current_md_by_name(struct syrep_db_context *c, const struct syrep_name *name, struct syrep_md *md);
+int get_current_name_by_md(struct syrep_db_context *c, const struct syrep_md *md, struct syrep_name *name);
+uint32_t get_version_timestamp(struct syrep_db_context *c, uint32_t v);
+
+#endif
diff --git a/src/diff.c b/src/diff.c
new file mode 100644
index 0000000..100d4f9
--- /dev/null
+++ b/src/diff.c
@@ -0,0 +1,345 @@
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "diff.h"
+#include "dbstruct.h"
+#include "util.h"
+#include "md5util.h"
+#include "dbutil.h"
+
+static int add_diff_entry(DB *ddb, struct syrep_name *name, int action, struct syrep_db_context *repository) {
+ DBT key, data;
+ int ret;
+ struct diff_entry de;
+
+ memset(&de, 0, sizeof(de));
+ de.action = action;
+ de.repository = repository;
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ key.data = name;
+ key.size = sizeof(struct syrep_name);
+
+ data.data = &de;
+ data.size = sizeof(struct diff_entry);
+
+ if ((ret = ddb->put(ddb, NULL, &key, &data, DB_NOOVERWRITE)) != 0) {
+ DBT data2;
+
+ if (ret != DB_KEYEXIST) {
+ ddb->err(ddb, ret, "ddb::put()");
+ return -1;
+ }
+
+ memset(&data2, 0, sizeof(data2));
+
+ if ((ret = ddb->get(ddb, NULL, &key, &data2, 0)) != 0) {
+ ddb->err(ddb, ret, "ddb::get()");
+ return -1;
+ }
+
+ if (data.size != data2.size || memcmp(data.data, data2.data, data.size)) {
+ fprintf(stderr, "Snapshot inconsistency\n");
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static int foreach(DB *ddb, struct syrep_db_context *c1, struct syrep_db_context *c2, struct syrep_name *name) {
+ struct syrep_md md1, md2;
+ int md1_valid, md2_valid;
+
+
+ if ((md1_valid = get_current_md_by_name(c1, name, &md1)) < 0)
+ return -1;
+
+ if ((md2_valid = get_current_md_by_name(c2, name, &md2)) < 0)
+ return -1;
+
+
+ //fprintf(stderr, "FOREACH %i %i %s\n", md1_valid, md2_valid, name->path);
+
+ if (md1_valid && md2_valid) {
+ int f1, f2;
+
+ /* Same file? */
+ if (!memcmp(&md1, &md2, sizeof(struct syrep_md)))
+ return 0;
+
+ if ((f1 = get_meta_by_name_md(c1, name, &md2, NULL)) < 0)
+ return -1;
+
+ if ((f2 = get_meta_by_name_md(c2, name, &md1, NULL)) < 0)
+ return -1;
+
+ /* The version in c1 is a newer version of that in c2 */
+ if (f1 && !f2)
+ return add_diff_entry(ddb, name, DIFF_COPY, c1);
+
+ /* Vice versa */
+ if (!f1 && f2)
+ return add_diff_entry(ddb, name, DIFF_COPY, c2);
+
+ /* Completely different file */
+ return add_diff_entry(ddb, name, DIFF_CONFLICT, NULL);
+
+ } else if (md1_valid) {
+ struct syrep_meta meta1, meta2;
+ int f1, f2;
+ uint32_t t1, t2;
+
+ if ((md2_valid = get_last_md_by_name(c2, name, &md2)) < 0)
+ return -1;
+
+ if (!md2_valid)
+ return add_diff_entry(ddb, name, DIFF_COPY, c1);
+
+ if (memcmp(&md1, &md2, sizeof(struct syrep_md)))
+ return add_diff_entry(ddb, name, DIFF_COPY, c1);
+
+ if ((f1 = get_meta_by_name_md(c1, name, &md1, &meta1)) < 0)
+ return -1;
+
+ if ((f2 = get_meta_by_name_md(c2, name, &md2, &meta2)) < 0)
+ return -1;
+
+ if (!f1 || !f2) {
+ fprintf(stderr, "Database inconsistency\n");
+ return -1;
+ }
+
+ /* Check whether file reappeared in c1 */
+ if ((t1 = get_version_timestamp(c1, meta1.first_seen-1)) == (uint32_t) -1)
+ return -1;
+
+ if ((t2 = get_version_timestamp(c2, meta2.last_seen+1)) == (uint32_t) -1)
+ return -1;
+
+ if (t1 >= t2)
+ return add_diff_entry(ddb, name, DIFF_COPY, c1);
+
+
+ /* Check whether file was deleted in c2 */
+ if ((t1 = get_version_timestamp(c1, meta1.first_seen)) == (uint32_t) -1)
+ return -1;
+
+ if ((t2 = get_version_timestamp(c2, meta2.last_seen)) == (uint32_t) -1)
+ return -1;
+
+ if (t1 < t2)
+ return add_diff_entry(ddb, name, DIFF_DELETE, c1);
+
+ return add_diff_entry(ddb, name, DIFF_CONFLICT, NULL);
+
+ } else if (md2_valid) {
+ fprintf(stderr, "This should be impossible!\n");
+ abort();
+ }
+
+ return 0;
+}
+
+
+static int enumerate(DB *ddb, struct syrep_db_context *c1, struct syrep_db_context *c2) {
+ int r = -1, ret;
+ DBC *cursor = NULL;
+ DBT key, data;
+
+ if ((ret = c1->db_id_meta->cursor(c1->db_id_meta, NULL, &cursor, 0)) != 0) {
+ c1->db_id_meta->err(c1->db_id_meta, ret, "id_meta::cursor()");
+ goto finish;
+ }
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ while ((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
+ struct syrep_id *id = (struct syrep_id*) key.data;
+ struct syrep_meta *meta = (struct syrep_meta*) data.data;
+
+
+
+ assert(id && meta);
+
+ if (meta->last_seen != c1->version)
+ continue;
+
+ if (foreach(ddb, c1, c2, &id->name) < 0) {
+ fprintf(stderr, "foreach() failed\n");
+ goto finish;
+ }
+ }
+
+
+ if (ret != DB_NOTFOUND) {
+ c1->db_id_meta->err(c1->db_id_meta, ret, "id_meta::c_get()");
+ goto finish;
+ }
+
+ r = 0;
+
+finish:
+
+ if (cursor)
+ cursor->c_close(cursor);
+
+ return r;
+}
+
+DB* make_diff(struct syrep_db_context *c1, struct syrep_db_context *c2) {
+ int ret;
+ DB *ddb = NULL;
+
+ if ((ret = db_create(&ddb, NULL, 0))) {
+ fprintf(stderr, "ddb::create(): %s\n", db_strerror(ret));
+ goto finish;
+ }
+
+ if ((ret = ddb->open(ddb, NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0664))) {
+ ddb->err(ddb, ret, "ddb::open()");
+ goto finish;
+ }
+
+ if (enumerate(ddb, c1, c2) < 0)
+ goto finish;
+
+ if (enumerate(ddb, c2, c1) < 0)
+ goto finish;
+
+ return ddb;
+
+finish:
+
+ if (ddb)
+ ddb->close(ddb, 0);
+
+ return NULL;
+}
+
+
+struct cb_info {
+ struct syrep_db_context *c1, *c2;
+};
+
+static int list_cb(DB *ddb, struct syrep_name *name, struct diff_entry *de, void *p) {
+ struct syrep_md md1, md2;
+ int f1, f2;
+ struct cb_info *cb_info = p;
+
+ assert(name && de);
+
+ if ((f1 = get_last_md_by_name(cb_info->c1, name, &md1)) < 0)
+ return -1;
+
+ if ((f2 = get_last_md_by_name(cb_info->c2, name, &md2)) < 0)
+ return -1;
+
+ if (!(f1 || f2)) {
+ fprintf(stderr, "Diff inconsicteny\n");
+ return -1;
+ }
+
+ switch (de->action) {
+ case DIFF_COPY: {
+ char d[33];
+ char src, dst;
+
+ if (de->repository == cb_info->c1) {
+ src = 'A'; dst = 'B';
+ fhex_md5(md1.digest, d);
+ } else {
+ src = 'B'; dst = 'A';
+ fhex_md5(md2.digest, d);
+ }
+
+ d[32] = 0;
+
+ fprintf(stderr, "COPY <%s|%s> FROM %c TO %c\n", d, name->path, src, dst);
+ break;
+ }
+
+ case DIFF_DELETE: {
+ char d[33];
+ int rep;
+
+ if (de->repository == cb_info->c1) {
+ rep = 'A';
+ fhex_md5(md1.digest, d);
+ } else {
+ rep = 'B';
+ fhex_md5(md2.digest, d);
+ }
+
+ d[32] = 0;
+
+ fprintf(stderr, "DELETE <%s|%s> FROM %c\n", d, name->path, rep);
+ break;
+
+ }
+
+ case DIFF_CONFLICT: {
+ char d1[33], d2[33];
+
+ fhex_md5(md1.digest, d1);
+ fhex_md5(md2.digest, d2);
+
+ d1[32] = d2[32] = 0;
+
+ fprintf(stderr, "CONFLICT <%s> BETWEEN <%s> IN A AND <%s> IN B\n", name->path, d1, d2);
+ break;
+ }
+ }
+
+ return 0;
+}
+
+int list_diff(struct syrep_db_context *c1, struct syrep_db_context *c2, DB *ddb) {
+ struct cb_info cb_info;
+ cb_info.c1 = c1;
+ cb_info.c2 = c2;
+
+ return diff_foreach(ddb, list_cb, &cb_info);
+}
+
+int diff_foreach(DB *ddb, int (*cb)(DB *ddb, struct syrep_name *name, struct diff_entry *de, void *p), void *p) {
+ DBC *cursor = NULL;
+ int r = -1, ret;
+ DBT key, data;
+
+ if ((ret = ddb->cursor(ddb, NULL, &cursor, 0)) != 0) {
+ ddb->err(ddb, ret, "ddb::cursor()");
+ goto finish;
+ }
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ while ((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
+ int t;
+ struct syrep_name *name = (struct syrep_name*) key.data;
+ struct diff_entry *de = (struct diff_entry*) data.data;
+
+ if ((t = cb(ddb, name, de, p)) < 0) {
+ r = t;
+ goto finish;
+ }
+ }
+
+ if (ret != DB_NOTFOUND) {
+ ddb->err(ddb, ret, "ddb::c_get() failed");
+ goto finish;
+ }
+
+ r = 0;
+
+finish:
+ if (cursor)
+ cursor->c_close(cursor);
+
+ return r;
+}
diff --git a/src/diff.h b/src/diff.h
new file mode 100644
index 0000000..b4a8295
--- /dev/null
+++ b/src/diff.h
@@ -0,0 +1,18 @@
+#ifndef foodiffhfoo
+#define foodiffhfoo
+
+#include "context.h"
+#include "dbstruct.h"
+
+enum { DIFF_COPY, DIFF_DELETE, DIFF_CONFLICT, DIFF_IGNORE };
+
+struct diff_entry {
+ int action;
+ struct syrep_db_context *repository;
+};
+
+DB* make_diff(struct syrep_db_context *c1, struct syrep_db_context *c2);
+int diff_foreach(DB *ddb, int (*cb)(DB *db, struct syrep_name *name, struct diff_entry *de, void *p), void *p);
+int list_diff(struct syrep_db_context *c1, struct syrep_db_context *c2, DB *ddb);
+
+#endif
diff --git a/src/dump.c b/src/dump.c
new file mode 100644
index 0000000..c09da8d
--- /dev/null
+++ b/src/dump.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+#include "dump.h"
+#include "package.h"
+
+static int foreach(struct package *p, char *name, char *path, void *u) {
+ struct stat st;
+ uint32_t size;
+
+ if (stat(path, &st) < 0) {
+ if (errno == ENOENT)
+ size = 0;
+ else {
+ fprintf(stderr, "stat(%s) failed: %s\n", path, strerror(errno));
+ return -1;
+ }
+ } else
+ size = (uint32_t) st.st_size;
+
+ printf("%s (%u bytes)\n", name, size);
+ return 0;
+}
+
+int dump(struct syrep_db_context *c) {
+ assert(c);
+
+ return package_foreach(c->package, foreach, NULL);
+}
diff --git a/src/dump.h b/src/dump.h
new file mode 100644
index 0000000..b0690a5
--- /dev/null
+++ b/src/dump.h
@@ -0,0 +1,8 @@
+#ifndef foodumphfoo
+#define foodumphfoo
+
+#include "context.h"
+
+int dump(struct syrep_db_context *c);
+
+#endif
diff --git a/src/history.c b/src/history.c
new file mode 100644
index 0000000..f8fbe63
--- /dev/null
+++ b/src/history.c
@@ -0,0 +1,48 @@
+#include <assert.h>
+#include <string.h>
+#include <time.h>
+
+#include "history.h"
+#include "dbstruct.h"
+
+int history(struct syrep_db_context *c) {
+ int r = -1, ret;
+ DBC *cursor = NULL;
+ DBT key, data;
+
+ assert(c && c->db_version_timestamp);
+
+ if ((ret = c->db_version_timestamp->cursor(c->db_version_timestamp, NULL, &cursor, 0)) != 0) {
+ c->db_version_timestamp->err(c->db_version_timestamp, ret, "version_timestamp::cursor()");
+ goto finish;
+ }
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ while ((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
+ struct syrep_version *version;
+ struct syrep_timestamp *timestamp;
+
+ version = key.data;
+ timestamp = data.data;
+
+ assert(version && timestamp);
+
+ fprintf(stderr, "%4u %10u %s", version->v, timestamp->t, ctime((time_t*) (&timestamp->t)));
+ }
+
+ if (ret != DB_NOTFOUND) {
+ c->db_version_timestamp->err(c->db_version_timestamp, ret, "version_timestamp::c_get()");
+ goto finish;
+ }
+
+ r = 0;
+
+finish:
+
+ if (cursor)
+ cursor->c_close(cursor);
+
+ return r;
+}
diff --git a/src/history.h b/src/history.h
new file mode 100644
index 0000000..2082a0c
--- /dev/null
+++ b/src/history.h
@@ -0,0 +1,8 @@
+#ifndef foohistoryfoo
+#define foohistoryfoo
+
+#include "context.h"
+
+int history(struct syrep_db_context *c);
+
+#endif
diff --git a/src/info.c b/src/info.c
new file mode 100644
index 0000000..1475f37
--- /dev/null
+++ b/src/info.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <assert.h>
+
+#include "info.h"
+#include "util.h"
+
+int info(struct syrep_db_context *c) {
+ assert(c);
+ fprintf(stderr, "Origin: %s\n", c->origin);
+ fprintf(stderr, "Timestamp: %u\n", c->timestamp);
+ fprintf(stderr, "Version: %u\n", c->version);
+ fprintf(stderr, "Database name_meta: ");
+ statistics(c->db_id_meta);
+ fprintf(stderr, "Database md_name: ");
+ statistics(c->db_md_name);
+ fprintf(stderr, "Database name_md: ");
+ statistics(c->db_name_md);
+ fprintf(stderr, "Database name_lastmd: ");
+ statistics(c->db_name_lastmd);
+ fprintf(stderr, "Database md_lastname: ");
+ statistics(c->db_md_lastname);
+ fprintf(stderr, "Database version_timestamp: ");
+ statistics(c->db_version_timestamp);
+ return 0;
+}
diff --git a/src/info.h b/src/info.h
new file mode 100644
index 0000000..93a38b8
--- /dev/null
+++ b/src/info.h
@@ -0,0 +1,8 @@
+#ifndef fooinfohfoo
+#define fooinfohfoo
+
+#include "context.h"
+
+int info(struct syrep_db_context *c);
+
+#endif
diff --git a/src/list.c b/src/list.c
new file mode 100644
index 0000000..c5c8b90
--- /dev/null
+++ b/src/list.c
@@ -0,0 +1,162 @@
+#include <string.h>
+#include <assert.h>
+#include <stdlib.h>
+
+#include "list.h"
+#include "context.h"
+#include "md5util.h"
+#include "dbstruct.h"
+#include "util.h"
+#include "dbutil.h"
+#include "syrep.h"
+
+static int handle_file(struct syrep_db_context *c, const struct syrep_name *name, const struct syrep_md *md, const struct syrep_meta *meta) {
+ struct syrep_meta local_meta;
+ assert(c && name && md);
+
+ if (!meta) {
+ int f;
+ if ((f = get_meta_by_name_md(c, name, md, &local_meta)) < 0)
+ return -1;
+
+ if (f)
+ meta = &local_meta;
+ }
+
+ if (!args.show_deleted_flag && meta->last_seen != c->version)
+ return 0;
+
+ if (meta) {
+ if (!args.show_by_md_flag) {
+ char d[SYREP_DIGESTLENGTH*2+1];
+ fhex(md->digest, SYREP_DIGESTLENGTH, d);
+ d[SYREP_DIGESTLENGTH*2] = 0;
+
+ fprintf(stderr, "%s %s%s", d, name->path, meta->last_seen == c->version ? "\t\t" : "\t(deleted)");
+ } else
+ fprintf(stderr, "\t%s%s", name->path, meta->last_seen == c->version ? "\t\t" : "\t(deleted)");
+
+ if (args.show_times_flag)
+ fprintf(stderr, "\t(first-seen: %u; last-seen: %u)\n", meta->first_seen, meta->last_seen);
+ else
+ fputc('\n', stderr);
+ } else {
+ char d[SYREP_DIGESTLENGTH*2+1];
+ fhex(md->digest, SYREP_DIGESTLENGTH, d);
+ d[SYREP_DIGESTLENGTH*2] = 0;
+
+ fprintf(stderr, "\t%s", name->path);
+ }
+
+ return 0;
+}
+
+int list(struct syrep_db_context *c) {
+ int r = -1, ret;
+ DBC *cursor = NULL;
+ DBT key, data;
+
+
+#if 1
+
+ {
+ if ((ret = c->db_name_lastmd->cursor(c->db_name_lastmd, NULL, &cursor, 0)) != 0) {
+ c->db_name_lastmd->err(c->db_name_lastmd, ret, "db_name_lastmd");
+ goto finish;
+ }
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ while ((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
+ struct syrep_name *name = (struct syrep_name*) key.data;
+ struct syrep_md *md = (struct syrep_md*) data.data;
+
+ if (handle_file(c, name, md, NULL) < 0)
+ fprintf(stderr, "handle_file() failed\n");
+
+ }
+
+ if (ret != DB_NOTFOUND) {
+ c->db_name_lastmd->err(c->db_name_lastmd, ret, "name_lastmd::c_get");
+ goto finish;
+ }
+
+ r = 0;
+ }
+
+ goto finish;
+
+#endif
+
+ if (args.show_by_md_flag) {
+ struct syrep_md previous_md;
+ memset(&previous_md, 0, sizeof(previous_md));
+
+ if ((ret = c->db_md_name->cursor(c->db_md_name, NULL, &cursor, 0)) != 0) {
+ c->db_md_name->err(c->db_md_name, ret, "md_name");
+ goto finish;
+ }
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ while ((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
+ struct syrep_md *md = (struct syrep_md*) key.data;
+ struct syrep_name *name = (struct syrep_name*) data.data;
+ struct syrep_meta meta;
+
+ if (memcmp(&previous_md, md, sizeof(previous_md))) {
+ char d[SYREP_DIGESTLENGTH*2+1];
+ fhex(md->digest, SYREP_DIGESTLENGTH, d);
+ d[SYREP_DIGESTLENGTH*2] = 0;
+ fprintf(stderr, "%s:\n", d);
+ memcpy(&previous_md, md, sizeof(previous_md));
+ }
+
+ if ((ret = get_meta_by_name_md(c, name, md, &meta)) < 0)
+ goto finish;
+
+ if (handle_file(c, name, md, &meta) < 0)
+ fprintf(stderr, "handle_file() failed\n");
+ }
+
+ if (ret != DB_NOTFOUND) {
+ c->db_md_name->err(c->db_md_name, ret, "md_name::c_get");
+ goto finish;
+ }
+
+ r = 0;
+ } else {
+
+ if ((ret = c->db_id_meta->cursor(c->db_id_meta, NULL, &cursor, 0)) != 0) {
+ c->db_id_meta->err(c->db_id_meta, ret, "id_meta");
+ goto finish;
+ }
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ while ((ret = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
+ struct syrep_id *id = (struct syrep_id*) key.data;
+
+ if (handle_file(c, &id->name, &id->md, (struct syrep_meta*) data.data) < 0)
+ fprintf(stderr, "handle_file() failed\n");
+
+ }
+
+ if (ret != DB_NOTFOUND) {
+ c->db_id_meta->err(c->db_id_meta, ret, "id_meta::c_get");
+ goto finish;
+ }
+
+ r = 0;
+ }
+
+finish:
+
+ if (cursor)
+ cursor->c_close(cursor);
+
+ return r;
+}
diff --git a/src/list.h b/src/list.h
new file mode 100644
index 0000000..c66fe75
--- /dev/null
+++ b/src/list.h
@@ -0,0 +1,8 @@
+#ifndef foolisthfoo
+#define foolisthfoo
+
+#include "context.h"
+
+int list(struct syrep_db_context *c);
+
+#endif
diff --git a/src/md5.c b/src/md5.c
new file mode 100644
index 0000000..2c9c2fc
--- /dev/null
+++ b/src/md5.c
@@ -0,0 +1,381 @@
+/*
+ Copyright (C) 1999, 2000, 2002 Aladdin Enterprises. All rights reserved.
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+ L. Peter Deutsch
+ ghost@aladdin.com
+
+ */
+/* $Id$ */
+/*
+ Independent implementation of MD5 (RFC 1321).
+
+ This code implements the MD5 Algorithm defined in RFC 1321, whose
+ text is available at
+ http://www.ietf.org/rfc/rfc1321.txt
+ The code is derived from the text of the RFC, including the test suite
+ (section A.5) but excluding the rest of Appendix A. It does not include
+ any code or documentation that is identified in the RFC as being
+ copyrighted.
+
+ The original and principal author of md5.c is L. Peter Deutsch
+ <ghost@aladdin.com>. Other authors are noted in the change history
+ that follows (in reverse chronological order):
+
+ 2002-04-13 lpd Clarified derivation from RFC 1321; now handles byte order
+ either statically or dynamically; added missing #include <string.h>
+ in library.
+ 2002-03-11 lpd Corrected argument list for main(), and added int return
+ type, in test program and T value program.
+ 2002-02-21 lpd Added missing #include <stdio.h> in test program.
+ 2000-07-03 lpd Patched to eliminate warnings about "constant is
+ unsigned in ANSI C, signed in traditional"; made test program
+ self-checking.
+ 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
+ 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
+ 1999-05-03 lpd Original version.
+ */
+
+#include "md5.h"
+#include <string.h>
+
+#undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */
+#ifdef ARCH_IS_BIG_ENDIAN
+# define BYTE_ORDER (ARCH_IS_BIG_ENDIAN ? 1 : -1)
+#else
+# define BYTE_ORDER 0
+#endif
+
+#define T_MASK ((md5_word_t)~0)
+#define T1 /* 0xd76aa478 */ (T_MASK ^ 0x28955b87)
+#define T2 /* 0xe8c7b756 */ (T_MASK ^ 0x173848a9)
+#define T3 0x242070db
+#define T4 /* 0xc1bdceee */ (T_MASK ^ 0x3e423111)
+#define T5 /* 0xf57c0faf */ (T_MASK ^ 0x0a83f050)
+#define T6 0x4787c62a
+#define T7 /* 0xa8304613 */ (T_MASK ^ 0x57cfb9ec)
+#define T8 /* 0xfd469501 */ (T_MASK ^ 0x02b96afe)
+#define T9 0x698098d8
+#define T10 /* 0x8b44f7af */ (T_MASK ^ 0x74bb0850)
+#define T11 /* 0xffff5bb1 */ (T_MASK ^ 0x0000a44e)
+#define T12 /* 0x895cd7be */ (T_MASK ^ 0x76a32841)
+#define T13 0x6b901122
+#define T14 /* 0xfd987193 */ (T_MASK ^ 0x02678e6c)
+#define T15 /* 0xa679438e */ (T_MASK ^ 0x5986bc71)
+#define T16 0x49b40821
+#define T17 /* 0xf61e2562 */ (T_MASK ^ 0x09e1da9d)
+#define T18 /* 0xc040b340 */ (T_MASK ^ 0x3fbf4cbf)
+#define T19 0x265e5a51
+#define T20 /* 0xe9b6c7aa */ (T_MASK ^ 0x16493855)
+#define T21 /* 0xd62f105d */ (T_MASK ^ 0x29d0efa2)
+#define T22 0x02441453
+#define T23 /* 0xd8a1e681 */ (T_MASK ^ 0x275e197e)
+#define T24 /* 0xe7d3fbc8 */ (T_MASK ^ 0x182c0437)
+#define T25 0x21e1cde6
+#define T26 /* 0xc33707d6 */ (T_MASK ^ 0x3cc8f829)
+#define T27 /* 0xf4d50d87 */ (T_MASK ^ 0x0b2af278)
+#define T28 0x455a14ed
+#define T29 /* 0xa9e3e905 */ (T_MASK ^ 0x561c16fa)
+#define T30 /* 0xfcefa3f8 */ (T_MASK ^ 0x03105c07)
+#define T31 0x676f02d9
+#define T32 /* 0x8d2a4c8a */ (T_MASK ^ 0x72d5b375)
+#define T33 /* 0xfffa3942 */ (T_MASK ^ 0x0005c6bd)
+#define T34 /* 0x8771f681 */ (T_MASK ^ 0x788e097e)
+#define T35 0x6d9d6122
+#define T36 /* 0xfde5380c */ (T_MASK ^ 0x021ac7f3)
+#define T37 /* 0xa4beea44 */ (T_MASK ^ 0x5b4115bb)
+#define T38 0x4bdecfa9
+#define T39 /* 0xf6bb4b60 */ (T_MASK ^ 0x0944b49f)
+#define T40 /* 0xbebfbc70 */ (T_MASK ^ 0x4140438f)
+#define T41 0x289b7ec6
+#define T42 /* 0xeaa127fa */ (T_MASK ^ 0x155ed805)
+#define T43 /* 0xd4ef3085 */ (T_MASK ^ 0x2b10cf7a)
+#define T44 0x04881d05
+#define T45 /* 0xd9d4d039 */ (T_MASK ^ 0x262b2fc6)
+#define T46 /* 0xe6db99e5 */ (T_MASK ^ 0x1924661a)
+#define T47 0x1fa27cf8
+#define T48 /* 0xc4ac5665 */ (T_MASK ^ 0x3b53a99a)
+#define T49 /* 0xf4292244 */ (T_MASK ^ 0x0bd6ddbb)
+#define T50 0x432aff97
+#define T51 /* 0xab9423a7 */ (T_MASK ^ 0x546bdc58)
+#define T52 /* 0xfc93a039 */ (T_MASK ^ 0x036c5fc6)
+#define T53 0x655b59c3
+#define T54 /* 0x8f0ccc92 */ (T_MASK ^ 0x70f3336d)
+#define T55 /* 0xffeff47d */ (T_MASK ^ 0x00100b82)
+#define T56 /* 0x85845dd1 */ (T_MASK ^ 0x7a7ba22e)
+#define T57 0x6fa87e4f
+#define T58 /* 0xfe2ce6e0 */ (T_MASK ^ 0x01d3191f)
+#define T59 /* 0xa3014314 */ (T_MASK ^ 0x5cfebceb)
+#define T60 0x4e0811a1
+#define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d)
+#define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca)
+#define T63 0x2ad7d2bb
+#define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e)
+
+
+static void
+md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/)
+{
+ md5_word_t
+ a = pms->abcd[0], b = pms->abcd[1],
+ c = pms->abcd[2], d = pms->abcd[3];
+ md5_word_t t;
+#if BYTE_ORDER > 0
+ /* Define storage only for big-endian CPUs. */
+ md5_word_t X[16];
+#else
+ /* Define storage for little-endian or both types of CPUs. */
+ md5_word_t xbuf[16];
+ const md5_word_t *X;
+#endif
+
+ {
+#if BYTE_ORDER == 0
+ /*
+ * Determine dynamically whether this is a big-endian or
+ * little-endian machine, since we can use a more efficient
+ * algorithm on the latter.
+ */
+ static const int w = 1;
+
+ if (*((const md5_byte_t *)&w)) /* dynamic little-endian */
+#endif
+#if BYTE_ORDER <= 0 /* little-endian */
+ {
+ /*
+ * On little-endian machines, we can process properly aligned
+ * data without copying it.
+ */
+ if (!((data - (const md5_byte_t *)0) & 3)) {
+ /* data are properly aligned */
+ X = (const md5_word_t *)data;
+ } else {
+ /* not aligned */
+ memcpy(xbuf, data, 64);
+ X = xbuf;
+ }
+ }
+#endif
+#if BYTE_ORDER == 0
+ else /* dynamic big-endian */
+#endif
+#if BYTE_ORDER >= 0 /* big-endian */
+ {
+ /*
+ * On big-endian machines, we must arrange the bytes in the
+ * right order.
+ */
+ const md5_byte_t *xp = data;
+ int i;
+
+# if BYTE_ORDER == 0
+ X = xbuf; /* (dynamic only) */
+# else
+# define xbuf X /* (static only) */
+# endif
+ for (i = 0; i < 16; ++i, xp += 4)
+ xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
+ }
+#endif
+ }
+
+#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
+
+ /* Round 1. */
+ /* Let [abcd k s i] denote the operation
+ a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
+#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
+#define SET(a, b, c, d, k, s, Ti)\
+ t = a + F(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
+ /* Do the following 16 operations. */
+ SET(a, b, c, d, 0, 7, T1);
+ SET(d, a, b, c, 1, 12, T2);
+ SET(c, d, a, b, 2, 17, T3);
+ SET(b, c, d, a, 3, 22, T4);
+ SET(a, b, c, d, 4, 7, T5);
+ SET(d, a, b, c, 5, 12, T6);
+ SET(c, d, a, b, 6, 17, T7);
+ SET(b, c, d, a, 7, 22, T8);
+ SET(a, b, c, d, 8, 7, T9);
+ SET(d, a, b, c, 9, 12, T10);
+ SET(c, d, a, b, 10, 17, T11);
+ SET(b, c, d, a, 11, 22, T12);
+ SET(a, b, c, d, 12, 7, T13);
+ SET(d, a, b, c, 13, 12, T14);
+ SET(c, d, a, b, 14, 17, T15);
+ SET(b, c, d, a, 15, 22, T16);
+#undef SET
+
+ /* Round 2. */
+ /* Let [abcd k s i] denote the operation
+ a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
+#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
+#define SET(a, b, c, d, k, s, Ti)\
+ t = a + G(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
+ /* Do the following 16 operations. */
+ SET(a, b, c, d, 1, 5, T17);
+ SET(d, a, b, c, 6, 9, T18);
+ SET(c, d, a, b, 11, 14, T19);
+ SET(b, c, d, a, 0, 20, T20);
+ SET(a, b, c, d, 5, 5, T21);
+ SET(d, a, b, c, 10, 9, T22);
+ SET(c, d, a, b, 15, 14, T23);
+ SET(b, c, d, a, 4, 20, T24);
+ SET(a, b, c, d, 9, 5, T25);
+ SET(d, a, b, c, 14, 9, T26);
+ SET(c, d, a, b, 3, 14, T27);
+ SET(b, c, d, a, 8, 20, T28);
+ SET(a, b, c, d, 13, 5, T29);
+ SET(d, a, b, c, 2, 9, T30);
+ SET(c, d, a, b, 7, 14, T31);
+ SET(b, c, d, a, 12, 20, T32);
+#undef SET
+
+ /* Round 3. */
+ /* Let [abcd k s t] denote the operation
+ a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
+#define H(x, y, z) ((x) ^ (y) ^ (z))
+#define SET(a, b, c, d, k, s, Ti)\
+ t = a + H(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
+ /* Do the following 16 operations. */
+ SET(a, b, c, d, 5, 4, T33);
+ SET(d, a, b, c, 8, 11, T34);
+ SET(c, d, a, b, 11, 16, T35);
+ SET(b, c, d, a, 14, 23, T36);
+ SET(a, b, c, d, 1, 4, T37);
+ SET(d, a, b, c, 4, 11, T38);
+ SET(c, d, a, b, 7, 16, T39);
+ SET(b, c, d, a, 10, 23, T40);
+ SET(a, b, c, d, 13, 4, T41);
+ SET(d, a, b, c, 0, 11, T42);
+ SET(c, d, a, b, 3, 16, T43);
+ SET(b, c, d, a, 6, 23, T44);
+ SET(a, b, c, d, 9, 4, T45);
+ SET(d, a, b, c, 12, 11, T46);
+ SET(c, d, a, b, 15, 16, T47);
+ SET(b, c, d, a, 2, 23, T48);
+#undef SET
+
+ /* Round 4. */
+ /* Let [abcd k s t] denote the operation
+ a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
+#define I(x, y, z) ((y) ^ ((x) | ~(z)))
+#define SET(a, b, c, d, k, s, Ti)\
+ t = a + I(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
+ /* Do the following 16 operations. */
+ SET(a, b, c, d, 0, 6, T49);
+ SET(d, a, b, c, 7, 10, T50);
+ SET(c, d, a, b, 14, 15, T51);
+ SET(b, c, d, a, 5, 21, T52);
+ SET(a, b, c, d, 12, 6, T53);
+ SET(d, a, b, c, 3, 10, T54);
+ SET(c, d, a, b, 10, 15, T55);
+ SET(b, c, d, a, 1, 21, T56);
+ SET(a, b, c, d, 8, 6, T57);
+ SET(d, a, b, c, 15, 10, T58);
+ SET(c, d, a, b, 6, 15, T59);
+ SET(b, c, d, a, 13, 21, T60);
+ SET(a, b, c, d, 4, 6, T61);
+ SET(d, a, b, c, 11, 10, T62);
+ SET(c, d, a, b, 2, 15, T63);
+ SET(b, c, d, a, 9, 21, T64);
+#undef SET
+
+ /* Then perform the following additions. (That is increment each
+ of the four registers by the value it had before this block
+ was started.) */
+ pms->abcd[0] += a;
+ pms->abcd[1] += b;
+ pms->abcd[2] += c;
+ pms->abcd[3] += d;
+}
+
+void
+md5_init(md5_state_t *pms)
+{
+ pms->count[0] = pms->count[1] = 0;
+ pms->abcd[0] = 0x67452301;
+ pms->abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476;
+ pms->abcd[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301;
+ pms->abcd[3] = 0x10325476;
+}
+
+void
+md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes)
+{
+ const md5_byte_t *p = data;
+ int left = nbytes;
+ int offset = (pms->count[0] >> 3) & 63;
+ md5_word_t nbits = (md5_word_t)(nbytes << 3);
+
+ if (nbytes <= 0)
+ return;
+
+ /* Update the message length. */
+ pms->count[1] += nbytes >> 29;
+ pms->count[0] += nbits;
+ if (pms->count[0] < nbits)
+ pms->count[1]++;
+
+ /* Process an initial partial block. */
+ if (offset) {
+ int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
+
+ memcpy(pms->buf + offset, p, copy);
+ if (offset + copy < 64)
+ return;
+ p += copy;
+ left -= copy;
+ md5_process(pms, pms->buf);
+ }
+
+ /* Process full blocks. */
+ for (; left >= 64; p += 64, left -= 64)
+ md5_process(pms, p);
+
+ /* Process a final partial block. */
+ if (left)
+ memcpy(pms->buf, p, left);
+}
+
+void
+md5_finish(md5_state_t *pms, md5_byte_t digest[16])
+{
+ static const md5_byte_t pad[64] = {
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ };
+ md5_byte_t data[8];
+ int i;
+
+ /* Save the length before padding. */
+ for (i = 0; i < 8; ++i)
+ data[i] = (md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3));
+ /* Pad to 56 bytes mod 64. */
+ md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
+ /* Append the length. */
+ md5_append(pms, data, 8);
+ for (i = 0; i < 16; ++i)
+ digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3));
+}
diff --git a/src/md5.h b/src/md5.h
new file mode 100644
index 0000000..5eb6d6c
--- /dev/null
+++ b/src/md5.h
@@ -0,0 +1,91 @@
+/*
+ Copyright (C) 1999, 2002 Aladdin Enterprises. All rights reserved.
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+ L. Peter Deutsch
+ ghost@aladdin.com
+
+ */
+/* $Id$ */
+/*
+ Independent implementation of MD5 (RFC 1321).
+
+ This code implements the MD5 Algorithm defined in RFC 1321, whose
+ text is available at
+ http://www.ietf.org/rfc/rfc1321.txt
+ The code is derived from the text of the RFC, including the test suite
+ (section A.5) but excluding the rest of Appendix A. It does not include
+ any code or documentation that is identified in the RFC as being
+ copyrighted.
+
+ The original and principal author of md5.h is L. Peter Deutsch
+ <ghost@aladdin.com>. Other authors are noted in the change history
+ that follows (in reverse chronological order):
+
+ 2002-04-13 lpd Removed support for non-ANSI compilers; removed
+ references to Ghostscript; clarified derivation from RFC 1321;
+ now handles byte order either statically or dynamically.
+ 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
+ 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
+ added conditionalization for C++ compilation from Martin
+ Purschke <purschke@bnl.gov>.
+ 1999-05-03 lpd Original version.
+ */
+
+#ifndef md5_INCLUDED
+# define md5_INCLUDED
+
+/*
+ * This package supports both compile-time and run-time determination of CPU
+ * byte order. If ARCH_IS_BIG_ENDIAN is defined as 0, the code will be
+ * compiled to run only on little-endian CPUs; if ARCH_IS_BIG_ENDIAN is
+ * defined as non-zero, the code will be compiled to run only on big-endian
+ * CPUs; if ARCH_IS_BIG_ENDIAN is not defined, the code will be compiled to
+ * run on either big- or little-endian CPUs, but will run slightly less
+ * efficiently on either one than if ARCH_IS_BIG_ENDIAN is defined.
+ */
+
+typedef unsigned char md5_byte_t; /* 8-bit byte */
+typedef unsigned int md5_word_t; /* 32-bit word */
+
+/* Define the state of the MD5 Algorithm. */
+typedef struct md5_state_s {
+ md5_word_t count[2]; /* message length in bits, lsw first */
+ md5_word_t abcd[4]; /* digest buffer */
+ md5_byte_t buf[64]; /* accumulate block */
+} md5_state_t;
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/* Initialize the algorithm. */
+void md5_init(md5_state_t *pms);
+
+/* Append a string to the message. */
+void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes);
+
+/* Finish the message and return the digest. */
+void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
+
+#ifdef __cplusplus
+} /* end extern "C" */
+#endif
+
+#endif /* md5_INCLUDED */
diff --git a/src/md5util.c b/src/md5util.c
new file mode 100644
index 0000000..41a5a31
--- /dev/null
+++ b/src/md5util.c
@@ -0,0 +1,85 @@
+/***
+ This file is part of pam_dotfile.
+
+ pam_dotfile is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ pam_dotfile is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with pam_dotfile; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA
+***/
+
+#include <unistd.h>
+#include <sys/mman.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "md5util.h"
+#include "md5.h"
+
+void fhex(const unsigned char *bin,int len, char *txt) {
+ const static char hex[] = "0123456789abcdef";
+ int i;
+
+ for (i = 0; i < len; i++) {
+ txt[i*2] = hex[bin[i]>>4];
+ txt[i*2+1] = hex[bin[i]&0xF];
+ }
+}
+
+#define MMAPSIZE (100*1024*1024)
+#define BUFSIZE (1024*1024)
+
+int fdmd5(int fd, size_t l, char *md) {
+ void *d;
+ off_t o = 0;
+ size_t m;
+ int r = 0;
+ md5_state_t s;
+
+ md5_init(&s);
+
+ m = l < MMAPSIZE ? l : MMAPSIZE;
+
+ while (l > 0 && (d = mmap(NULL, m, PROT_READ, MAP_SHARED, fd, o))) {
+ md5_append(&s, d, m);
+ munmap(d, m);
+
+ o += m;
+ l -= m;
+ m = l < MMAPSIZE ? l : MMAPSIZE;
+
+ }
+
+ if (l > 0) {
+ void *p;
+ fprintf(stderr, "mmap() failed\n");
+
+ if (!(p = malloc(BUFSIZE)))
+ r = -1;
+ else {
+
+ for (;;) {
+ ssize_t r;
+
+ if ((r = read(fd, p, BUFSIZE)) <= 0)
+ break;
+
+ md5_append(&s, p, r);
+ }
+
+ free(p);
+ }
+ }
+
+ md5_finish(&s, md);
+ return r;
+}
diff --git a/src/md5util.h b/src/md5util.h
new file mode 100644
index 0000000..83f4bb7
--- /dev/null
+++ b/src/md5util.h
@@ -0,0 +1,30 @@
+#ifndef foomd5utilhfoo
+#define foomd5utilhfoo
+
+/***
+ This file is part of pam_dotfile.
+
+ pam_dotfile is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ pam_dotfile is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with pam_dotfile; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA
+***/
+
+#include <sys/types.h>
+
+void fhex(const unsigned char *bin, int len, char *txt);
+#define fhex_md5(bin,txt) fhex((bin),16,(txt))
+
+int fdmd5(int fd, size_t l, char *md);
+
+#endif
diff --git a/src/merge.c b/src/merge.c
new file mode 100644
index 0000000..aef3f8f
--- /dev/null
+++ b/src/merge.c
@@ -0,0 +1,132 @@
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "md5util.h"
+#include "diff.h"
+#include "dbutil.h"
+
+struct cb_info {
+ struct syrep_db_context *c1;
+ struct syrep_db_context *c2;
+ const char *root;
+};
+
+int conflict_phase(DB *ddb, struct syrep_name *name, struct diff_entry *de, void *p) {
+ struct cb_info *cb_info = p;
+ char path[PATH_MAX+1];
+
+ assert(ddb && name && de && p);
+
+ if (de->action != DIFF_CONFLICT)
+ return 0;
+
+ snprintf(path, sizeof(path), "%s/%s", cb_info->root, name->path);
+ fprintf(stderr, "QUERY %s\n", path);
+
+ return 0;
+}
+
+int copy_phase(DB *ddb, struct syrep_name *name, struct diff_entry *de, void *p) {
+ struct cb_info *cb_info = p;
+ struct syrep_name name2;
+ struct syrep_md md;
+ char path[PATH_MAX+1];
+ int f;
+ char d[SYREP_DIGESTLENGTH*2+1];
+
+ assert(ddb && name && de && p);
+
+ if (de->action != DIFF_COPY)
+ return 0;
+
+ if (de->repository == cb_info->c2)
+ return 0;
+
+ snprintf(path, sizeof(path), "%s/%s", cb_info->root, name->path);
+
+ if ((f = get_current_md_by_name(cb_info->c1, name, &md)) < 0)
+ return -1;
+
+
+ if (!f) {
+ fprintf(stderr, "Diff invalid!\n");
+ return -1;
+ }
+
+ fhex(md.digest, SYREP_DIGESTLENGTH, d);
+ d[SYREP_DIGESTLENGTH*2] = 0;
+
+ fprintf(stderr, "Searching for %s\n", d);
+
+ if ((f = get_current_name_by_md(cb_info->c2, &md, &name2)) < 0)
+ return -1;
+
+ if (f) {
+ char path2[PATH_MAX+1];
+ snprintf(path2, sizeof(path2), "%s/%s", cb_info->root, name2.path);
+ fprintf(stderr, "Linking file <%s> to <%s>\n", path2, path);
+ if (link(path2, path) < 0) {
+ fprintf(stderr, "Failed to link: %s\n", strerror(errno));
+ return -1;
+ }
+ } else
+ fprintf(stderr, "MISSING FILE %s\n", path);
+
+ return 0;
+}
+
+int delete_phase(DB *ddb, struct syrep_name *name, struct diff_entry *de, void *p) {
+ struct cb_info *cb_info = p;
+ char path[PATH_MAX+1];
+
+ assert(ddb && name && de && p);
+
+ if (de->action != DIFF_DELETE)
+ return 0;
+
+ if (de->repository == cb_info->c1)
+ return 0;
+
+ snprintf(path, sizeof(path), "%s/%s", cb_info->root, name->path);
+ fprintf(stderr, "Deleting file <%s>\n", path);
+
+ if (unlink(path) < 0) {
+ fprintf(stderr, "Failed to unlink: %s\n", strerror(errno));
+ return -1;
+ }
+
+ return 0;
+}
+
+/* Merges c1 into c2 in directory "root" */
+int merge_snapshot(struct syrep_db_context *c1, struct syrep_db_context *c2, const char* root) {
+ struct cb_info cb_info;
+ DB *ddb = NULL;
+
+ memset(&cb_info, 0, sizeof(cb_info));
+ cb_info.c1 = c1;
+ cb_info.c2 = c2;
+ cb_info.root = root;
+
+ if (!(ddb = make_diff(c1, c2)))
+ goto finish;
+
+ if (diff_foreach(ddb, conflict_phase, &cb_info) < 0)
+ goto finish;
+
+ if (diff_foreach(ddb, copy_phase, &cb_info) < 0)
+ goto finish;
+
+ if (diff_foreach(ddb, delete_phase, &cb_info) < 0)
+ goto finish;
+
+finish:
+ if (ddb)
+ ddb->close(ddb, 0);
+
+ return 0;
+}
+
diff --git a/src/merge.h b/src/merge.h
new file mode 100644
index 0000000..2b88712
--- /dev/null
+++ b/src/merge.h
@@ -0,0 +1,8 @@
+#ifndef foomergehfoo
+#define foomergehfoo
+
+#include "context.h"
+
+int merge_snapshot(struct syrep_db_context *c1, struct syrep_db_context *c2, const char* root);
+
+#endif
diff --git a/src/package.c b/src/package.c
new file mode 100644
index 0000000..168a6d8
--- /dev/null
+++ b/src/package.c
@@ -0,0 +1,310 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#include <unistd.h>
+#include <assert.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "package.h"
+
+struct package_item;
+
+struct package_item {
+ char *name;
+ char *path;
+ struct package_item *next;
+};
+
+struct package {
+ char *base;
+ int count;
+ struct package_item *items;
+ struct package_item *last;
+};
+
+static int copy(FILE *f, FILE *t, uint32_t len) {
+ static char buf[2048*4];
+
+ while (len > 0) {
+ uint32_t n;
+
+ if (feof(f))
+ return -1;
+
+ n = len > sizeof(buf) ? sizeof(buf) : len;
+
+ if (fread(buf, n, 1, f) != 1)
+ return -1;
+
+ if (fwrite(buf, n, 1, t) != 1)
+ return -1;
+
+ len -= n;
+ }
+
+ return 0;
+}
+
+static char *tmp(char *fn, int l) {
+ char *t;
+
+ if (!(t = getenv("TMPDIR")))
+ if (!(t = getenv("TEMP")))
+ if (!(t = getenv("TMP")))
+ t = "/tmp";
+
+ snprintf(fn, l, "%s/pkgXXXXXX", t);
+ return fn;
+}
+
+static struct package_item *item_new(const char *name, const char *path) {
+ struct package_item *i = NULL;
+
+ if (!(i = malloc(sizeof(struct package_item))))
+ return NULL;
+
+ memset(i, 0, sizeof(struct package_item));
+
+ if (!(i->name = strdup(name)))
+ goto fail;
+
+ if (!(i->path = strdup(path)))
+ goto fail;
+
+ return i;
+
+fail:
+ if(i) {
+ if (i->path)
+ free(i->path);
+ if (i->name)
+ free(i->name);
+ free(i);
+ }
+
+ return NULL;
+}
+
+struct package* package_open(const char *fn) {
+ struct package *p;
+ FILE *f = NULL;
+ char path[PATH_MAX];
+
+ if (!(p = malloc(sizeof(struct package)))) {
+ fprintf(stderr, "Failed to allocate package structure.\n");
+ return NULL;
+ }
+
+ memset(p, 0, sizeof(struct package));
+
+ tmp(path, sizeof(path));
+ if (!mkdtemp(path)) {
+ fprintf(stderr, "Failed to create temporary directory.\n");
+ goto finish;
+ }
+
+ if (!(p->base = strdup(path))) {
+ fprintf(stderr, "Failed to allocate memory.\n");
+ goto finish;
+ }
+
+ if (fn) {
+ if (!(f = fopen(fn, "r"))) {
+ fprintf(stderr, "Failed to open <%s>: %s\n", fn, strerror(errno));
+ goto finish;
+ }
+
+ for (;;) {
+ uint32_t len;
+ char name[PACKAGE_ITEM_NAME_LEN+1];
+ FILE *d = NULL;
+ struct package_item *pi;
+
+ if (!fread(name, sizeof(name)-1, 1, f))
+ break;
+
+ name[sizeof(name)-1] = 0;
+
+ if (fread(&len, sizeof(len), 1, f) != 1) {
+ fprintf(stderr, "Short read while reading length: %s\n", strerror(errno));
+ goto finish;
+ }
+
+ snprintf(path, sizeof(path), "%s/%i", p->base, p->count++);
+ if (!(d = fopen(path, "w+"))) {
+ fprintf(stderr, "Failed to open target file: %s\n", strerror(errno));
+ goto finish;
+ }
+
+ if (copy(f, d, len) < 0) {
+ fprintf(stderr, "Copy failed: %s\n", strerror(errno));
+ fclose(d);
+ unlink(path);
+ goto finish;
+ }
+
+ fclose(d);
+ if (!(pi = item_new(name, path))) {
+ unlink(path);
+ fprintf(stderr, "Failed to allocate memory.\n");
+ goto finish;
+ }
+
+ assert(!!p->last == !!p->items);
+
+ if (p->last) {
+ p->last->next = pi;
+ p->last = pi;
+ } else
+ p->items = p->last = pi;
+ }
+
+ fclose(f);
+ }
+
+ return p;
+
+finish:
+ if (f)
+ fclose(f);
+
+ if (p)
+ package_remove(p);
+
+ return NULL;
+}
+
+int package_save(struct package *p, const char *fn) {
+ FILE *f, *s = NULL;
+ int r = 0;
+ struct package_item *i;
+
+ assert(p && fn);
+
+ if (!(f = fopen(fn, "w+")))
+ return -1;
+
+ for (i = p->items; i; i = i->next) {
+ char name[PACKAGE_ITEM_NAME_LEN+1];
+ uint32_t len;
+ struct stat st;
+
+ if (!i->name || !i->path)
+ continue;
+
+ if (!(s = fopen(i->path, "r")))
+ continue;
+
+ //fprintf(stderr, "Saving %s (%s)...\n", i->name, i->path);
+
+ memset(name, 0, sizeof(name));
+ strncpy(name, i->name, sizeof(name)-1);
+
+ if (fwrite(name, sizeof(name)-1, 1, f) != 1) {
+ r = 1;
+ break;
+ }
+
+ if (fstat(fileno(s), &st) < 0) {
+ fprintf(stderr, "Could not get file size: %s\n", strerror(errno));
+ r = 1;
+ break;
+ }
+
+ len = st.st_size;
+
+ if (fwrite(&len, sizeof(len), 1, f) != 1) {
+ r = 1;
+ break;
+ }
+
+ if (copy(s, f, len) < 0) {
+ fprintf(stderr, "Could not copy file to package: %s\n", strerror(errno));
+ r = 1;
+ break;
+ }
+
+ fclose(s);
+ s = NULL;
+ }
+
+ if (s)
+ fclose(s);
+
+ fclose(f);
+
+ if (r != 0)
+ unlink(fn);
+
+ return r;
+}
+
+const char *package_get_item(struct package* p, const char *name) {
+ struct package_item *i;
+ char path[PATH_MAX];
+ assert(p && name);
+
+ for (i = p->items; i; i = i->next) {
+ if (strncmp(name, i->name, PACKAGE_ITEM_NAME_LEN) == 0)
+ return i->path;
+ }
+
+ snprintf(path, sizeof(path), "%s/%i", p->base, p->count++);
+
+ if (!(i = item_new(name, path))) {
+ unlink(path);
+ return NULL;
+ }
+
+ assert(!!p->last == !!p->items);
+
+ if (p->last) {
+ p->last->next = i;
+ p->last = i;
+ } else
+ p->items = p->last = i;
+
+ return i->path;
+}
+
+void package_remove(struct package *p) {
+ struct package_item *i, *n;
+ assert(p);
+
+ for (i = p->items; i; i = n) {
+ n = i->next;
+
+ if (i->path)
+ if (unlink(i->path))
+ if (errno != ENOENT)
+ fprintf(stderr, "Failed to remove <%s>: %s\n", i->path, strerror(errno));
+
+ free(i->path);
+ free(i->name);
+ free(i);
+ }
+
+ if (p->base) {
+ if (rmdir(p->base))
+ fprintf(stderr, "Failed to remove <%s>: %s\n", p->base, strerror(errno));
+
+ free(p->base);
+ }
+ free(p);
+}
+
+
+int package_foreach(struct package *p, int (*cb) (struct package *p, char *name, char *path, void *u), void *u) {
+ struct package_item *i;
+ assert(p);
+
+ for (i = p->items; i; i = i->next)
+ if (cb(p, i->name, i->path, u) < 0)
+ return -1;
+
+ return 0;
+}
diff --git a/src/package.h b/src/package.h
new file mode 100644
index 0000000..62aca3b
--- /dev/null
+++ b/src/package.h
@@ -0,0 +1,14 @@
+#ifndef foopackagehfoo
+#define foopackagehfoo
+
+#define PACKAGE_ITEM_NAME_LEN 32
+
+struct package;
+
+struct package* package_open(const char *fn);
+void package_remove(struct package *p);
+int package_save(struct package *p, const char *fn);
+const char *package_get_item(struct package* p, const char *name);
+int package_foreach(struct package *p, int (*cb) (struct package *p, char *name, char *path, void *u), void *u);
+
+#endif
diff --git a/src/syrep.c b/src/syrep.c
new file mode 100644
index 0000000..07bbe37
--- /dev/null
+++ b/src/syrep.c
@@ -0,0 +1,301 @@
+#include <stdint.h>
+#include <limits.h>
+#include <db.h>
+#include <assert.h>
+#include <malloc.h>
+#include <string.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include "md5util.h"
+#include "md5.h"
+#include "context.h"
+#include "cache.h"
+#include "update.h"
+#include "list.h"
+#include "diff.h"
+#include "util.h"
+#include "cmdline.h"
+#include "syrep.h"
+#include "merge.h"
+#include "info.h"
+#include "history.h"
+#include "dump.h"
+
+struct gengetopt_args_info args;
+
+int do_diff(void) {
+ struct syrep_db_context *c1 = NULL, *c2 = NULL;
+ char *path1 = NULL, *path2 = NULL;
+ DB *ddb = NULL;
+ int r = 1;
+
+ if (args.inputs_num != 2) {
+ fprintf(stderr, "ERROR: Need exactly two repository snapshots for diff command\n");
+ goto finish;
+ }
+
+ if (!(path1 = strdup(get_attached_filename(args.inputs[0], SYREP_SNAPSHOTFILENAME))))
+ goto finish;
+
+ if (!(path2 = strdup(get_attached_filename(args.inputs[1], SYREP_SNAPSHOTFILENAME))))
+ goto finish;
+
+ if (!strcmp(path1, path2)) {
+ fprintf(stderr, "ERROR: diff command requires two distinct snapshots as arguments\n");
+ goto finish;
+ }
+
+ if (!(c1 = db_context_open(path1)))
+ goto finish;
+
+ if (!(c2 = db_context_open(path2)))
+ goto finish;
+
+ if (!(ddb = make_diff(c1, c2)))
+ goto finish;
+
+ if (list_diff(c1, c2, ddb) < 0)
+ goto finish;
+
+ r = 0;
+
+finish:
+ if (c1)
+ db_context_free(c1);
+
+ if (c2)
+ db_context_free(c2);
+
+ if (ddb)
+ ddb->close(ddb, 0);
+
+ if (path1)
+ free(path1);
+
+ if (path2)
+ free(path2);
+
+ return r;
+}
+
+int do_merge(void) {
+ struct syrep_db_context *c1 = NULL, *c2 = NULL;
+ char *path1 = NULL, *path2 = NULL;
+ int r = 1;
+
+ if (args.inputs_num != 2) {
+ fprintf(stderr, "ERROR: Need exactly one repository snapshot and one repository for merge command\n");
+ goto finish;
+ }
+
+ if (isdirectory(args.inputs[1]) <= 0) {
+ fprintf(stderr, "ERROR: %s is not a directory\n", args.inputs[1]);
+ goto finish;
+ }
+
+ if (!(path1 = strdup(get_attached_filename(args.inputs[0], SYREP_SNAPSHOTFILENAME))))
+ goto finish;
+
+ if (!(path2 = strdup(get_attached_filename(args.inputs[1], SYREP_SNAPSHOTFILENAME))))
+ goto finish;
+
+ if (!strcmp(path1, path2)) {
+ fprintf(stderr, "ERROR: diff command requires two distinct snapshots as arguments\n");
+ goto finish;
+ }
+
+ if (!(c1 = db_context_open(path1)))
+ goto finish;
+
+ if (!(c2 = db_context_open(path2)))
+ goto finish;
+
+ if (merge_snapshot(c1, c2, args.inputs[1]) < 0)
+ goto finish;
+
+ r = 0;
+
+finish:
+ if (c1)
+ db_context_free(c1);
+
+ if (c2)
+ db_context_free(c2);
+
+ if (path1)
+ free(path1);
+
+ if (path2)
+ free(path2);
+
+ return r;
+}
+
+int do_foreach(int (*func) (struct syrep_db_context *c)) {
+ char *path = NULL;
+ int r = 1, i;
+ struct syrep_db_context *c = NULL;
+
+ if (args.inputs_num < 1)
+ fprintf(stderr, "WARNING: No repository or snapshot to specified!\n");
+
+ for (i = 0; i < args.inputs_num; i++) {
+
+ if (!(path = strdup(get_attached_filename(args.inputs[i], SYREP_SNAPSHOTFILENAME))))
+ goto finish;
+
+ if (!(c = db_context_open(path)))
+ goto finish;
+
+ if (args.inputs_num > 1)
+ fprintf(stderr, "*** %s ***\n", path);
+
+ if (func(c) < 0)
+ goto finish;
+
+ if (args.inputs_num > 1 && i < args.inputs_num-1)
+ fprintf(stderr, "\n");
+
+ db_context_free(c);
+ c = NULL;
+
+ free(path);
+ path = NULL;
+ }
+
+ r = 0;
+
+finish:
+ if (c)
+ db_context_free(c);
+
+ if (path)
+ free(path);
+
+ return r;
+}
+
+int do_update(void) {
+ char *path = NULL;
+ int r = 1, i;
+ struct syrep_db_context *c = NULL;
+ struct syrep_md_cache *cache = NULL;
+
+ if (args.inputs_num < 1)
+ fprintf(stderr, "WARNING: No repository to update specified!\n");
+
+ if (args.snapshot_given && args.inputs_num != 1) {
+ fprintf(stderr, "ERROR: If a snapshot file is specified only a single directory name is accepted!\n");
+ goto finish;
+ }
+
+ for (i = 0; i < args.inputs_num; i++) {
+ static char saved_cwd[PATH_MAX];
+
+ if (isdirectory(args.inputs[i]) <= 0) {
+ fprintf(stderr, "%s is not a directory\n", args.inputs[i]);
+ return 1;
+ }
+
+ if (!(path = strdup(get_attached_filename(args.inputs[i], SYREP_SNAPSHOTFILENAME))))
+ goto finish;
+
+ if (!(c = db_context_open(path))) {
+ fprintf(stderr, "Initializing repository.\n");
+ if (!(c = db_context_open(NULL)))
+ goto finish;
+ }
+
+ if (!args.no_cache_flag) {
+ const char *p;
+
+ if (args.cache_given)
+ cache = md_cache_open(args.cache_arg, args.ro_cache_flag);
+ else if ((p = get_attached_filename(args.inputs[i], SYREP_MDCACHEFILENAME)))
+ cache = md_cache_open(p, args.ro_cache_flag);
+ }
+
+ if (getcwd(saved_cwd, sizeof(saved_cwd)) < 0) {
+ fprintf(stderr, "getcwd(): %s\n", strerror(errno));
+ goto finish;
+ }
+
+ if (chdir(args.inputs[i]) < 0) {
+ fprintf(stderr, "Failed to chdir() to directory %s: %s\n", args.inputs[i], strerror(errno));
+ goto finish;
+ }
+
+ if (update(c, cache) < 0)
+ goto finish;
+
+ if (chdir(saved_cwd) < 0) {
+ fprintf(stderr, "Failed to chdir() back to directory %s: %s\n", saved_cwd, strerror(errno));
+ goto finish;
+ }
+
+ if (db_context_save(c, path) < 0)
+ goto finish;
+
+ if (!args.no_purge_flag && cache)
+ md_cache_vacuum(cache);
+
+ db_context_free(c);
+ c = NULL;
+
+ free(path);
+ path = NULL;
+
+ if (cache) {
+ md_cache_close(cache);
+ cache = NULL;
+ }
+ }
+
+ r = 0;
+
+finish:
+ if (c)
+ db_context_free(c);
+
+ if (path)
+ free(path);
+
+ if (cache)
+ md_cache_close(cache);
+
+ return r;
+}
+
+int main(int argc, char *argv[]) {
+ char *b;
+
+ if ((b = strrchr(argv[0], '/')))
+ argv[0] = b+1;
+
+ cmdline_parser(argc, argv, &args);
+
+ if (args.list_flag)
+ return do_foreach(list);
+ else if (args.info_flag)
+ return do_foreach(info);
+ else if (args.history_flag)
+ return do_foreach(history);
+ else if (args.diff_flag)
+ return do_diff();
+ else if (args.update_flag)
+ return do_update();
+ else if (args.merge_flag)
+ return do_merge();
+ else if (args.dump_flag)
+ return do_foreach(dump);
+
+ cmdline_parser_print_help();
+
+ return 1;
+}
diff --git a/src/syrep.ggo b/src/syrep.ggo
new file mode 100644
index 0000000..20aa0bf
--- /dev/null
+++ b/src/syrep.ggo
@@ -0,0 +1,27 @@
+purpose "Synchronize Repositories"
+package "syrep"
+version "0.1"
+
+option "verbose" - "Enable verbose operation" flag off
+
+option "list" - "List a repository snapshot" flag off
+ option "show-deleted" - "list: Show deleted entries of repository snapshot" flag off
+ option "show-by-md" - "list: Show files by message digests" flag off
+ option "show-times" - "list: Show first and last seen times" flag off
+
+option "info" - "Show information about a repository or snapshot" flag off
+option "history" - "Show history of a repository or snapshot" flag off
+option "dump" - "Show a structure dump of a repository or snapshot" flag off
+
+option "update" - "Update a repository snapshot" flag off
+ option "snapshot" S "update: Use the specified snapshot file instead of the one contained in the repository" string no
+ option "cache" C "update: Use the specified cache file instead of the one contained in the repository" string no
+ option "no-cache" - "update: Don't use a message digest cache" flag off
+ option "no-purge" - "update: Don't pruge obsolete entries from cache after update run" flag off
+ option "ro-cache" - "update: Use read only cache" flag off
+ option "progress" - "update: Show progress" flag off
+
+option "diff" - "Show difference between two repositories or snapshots" flag off
+
+option "merge" - "Merge a snapshot or a repository into a repository" flag off
+ option "question" - "merge: Ask a question before each action" flag off
diff --git a/src/syrep.h b/src/syrep.h
new file mode 100644
index 0000000..66d0a4d
--- /dev/null
+++ b/src/syrep.h
@@ -0,0 +1,13 @@
+#ifndef foosyrephfoo
+#define foosyrephfoo
+
+#include "cmdline.h"
+
+#define SYREP_SNAPSHOTFILENAME "current.syrep"
+#define SYREP_MDCACHEFILENAME "md-cache"
+
+#define SYREP_DIGESTLENGTH 16
+
+extern struct gengetopt_args_info args;
+
+#endif
diff --git a/src/update.c b/src/update.c
new file mode 100644
index 0000000..2104217
--- /dev/null
+++ b/src/update.c
@@ -0,0 +1,207 @@
+#include <dirent.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+#include <time.h>
+
+#include "update.h"
+#include "dbstruct.h"
+#include "util.h"
+#include "dbutil.h"
+#include "md5util.h"
+
+static int dbput(DB* db, const void *k, int klen, const void*d, int dlen, int f) {
+ DBT key, data;
+ int ret;
+
+ memset(&key, 0, sizeof(key));
+ key.data = (void*) k;
+ key.size = klen;
+
+ memset(&data, 0, sizeof(data));
+ data.data = (void*) d;
+ data.size = dlen;
+
+ if ((ret = db->put(db, NULL, &key, &data, f)) != 0) {
+ if (ret != DB_KEYEXIST) {
+ db->err(db, ret, "db::put()");
+ return -1;
+ }
+
+ return 0;
+ }
+
+ return 1;
+}
+
+static int write_entry(struct syrep_db_context *c, const struct syrep_name *name, const struct syrep_md *md, const struct syrep_meta *meta) {
+ struct syrep_id id;
+ int f;
+
+ assert(c && c->db_id_meta && c->db_md_name && c->db_name_md && c->db_md_lastname && c->db_name_lastmd && name && md && meta);
+
+ /*** Update id_meta ***/
+ memset(&id, 0, sizeof(id));
+ memcpy(&id.name, name, sizeof(struct syrep_name));
+ memcpy(&id.md, md, sizeof(struct syrep_md));
+
+ if (dbput(c->db_id_meta, &id, sizeof(struct syrep_id), meta, sizeof(struct syrep_meta), 0) < 0)
+ return -1;
+
+ /*** Update md_name ***/
+ if (dbput(c->db_md_name, md, sizeof(struct syrep_md), name, sizeof(struct syrep_name), DB_NODUPDATA) < 0)
+ return -1;
+
+ /*** Update name_md ***/
+ if (dbput(c->db_name_md, name, sizeof(struct syrep_name), md, sizeof(struct syrep_md), DB_NODUPDATA) < 0)
+ return -1;
+
+ /*** Update md_lastname ***/
+ if (dbput(c->db_md_lastname, md, sizeof(struct syrep_md), name, sizeof(struct syrep_name), 0) < 0)
+ return -1;
+
+ /*** Update name_lastmd ***/
+ if ((f = dbput(c->db_name_lastmd, name, sizeof(struct syrep_name), md, sizeof(struct syrep_md), 0)) < 0)
+ return -1;
+
+ //fprintf(stderr, "Insert: %s %i\n", name->path, f);
+
+ c->modified = 1;
+ return 0;
+}
+
+static int handle_file(struct syrep_db_context *c, uint32_t version, const char *path, const struct syrep_md *md) {
+ int r;
+ struct syrep_meta meta;
+ struct syrep_name name;
+
+ memset(&name, 0, sizeof(name));
+ strncpy(name.path, path, PATH_MAX);
+
+ memset(&meta, 0, sizeof(meta));
+
+ if ((r = get_meta_by_name_md(c, &name, md, &meta)) < 0)
+ return -1;
+
+ if (r) { /* File is alread known */
+
+ if (meta.last_seen != c->version) { /* File was deleted preiously */
+ if (args.verbose_flag)
+ fprintf(stderr, "%s: File reappeared\n", path);
+ meta.first_seen = meta.last_seen = version;
+ } else { /* File is not new */
+ if (args.verbose_flag)
+ fprintf(stderr, "%s: File still available\n", path);
+ meta.last_seen = version;
+ }
+
+ } else { /* File is new */
+ if (args.verbose_flag)
+ fprintf(stderr, "%s: File is new\n", path);
+ meta.first_seen = meta.last_seen = version;
+ }
+
+ return write_entry(c, &name, md, &meta);
+}
+
+static int iterate_dir(struct syrep_db_context *c, struct syrep_md_cache *cache, uint32_t version, const char *root) {
+ DIR *dir;
+ struct dirent *de;
+ char p[PATH_MAX];
+
+ if (!(dir = opendir(root)))
+ return -1;
+
+ while ((de = readdir(dir))) {
+ struct syrep_md md;
+ struct stat st;
+
+ if (de->d_name[0] == '.')
+ continue;
+
+ if (!strncmp(de->d_name, ".syrep", 6))
+ continue;
+
+ if (args.progress_flag)
+ rotdash();
+
+ snprintf(p, sizeof(p), "%s/%s", root, de->d_name);
+
+ normalize_path(p);
+
+ if (stat(p, &st) < 0) {
+ fprintf(stderr, "stat(%s) failed: %s\n", p, strerror(errno));
+ continue;
+ }
+
+ if (S_ISDIR(st.st_mode)) {
+ if (iterate_dir(c, cache, version, p) < 0)
+ fprintf(stderr, "iterate_dir(%s) failed: %s\n", p, strerror(errno));
+ } else if (S_ISREG(st.st_mode)) {
+ if (md_cache_get(cache, p, md.digest) < 0)
+ continue;
+
+ if ((handle_file(c, version, p, &md)) < 0) {
+ fprintf(stderr, "handle_file(%s) failed.\n", p);
+ return -1;
+ }
+ }
+ }
+
+ closedir(dir);
+
+ return 0;
+}
+
+static int new_version(struct syrep_db_context *c, uint32_t v, uint32_t t) {
+ DBT key, data;
+ struct syrep_version version;
+ struct syrep_timestamp timestamp;
+ int ret;
+
+ assert(c && c->db_version_timestamp);
+
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+ memset(&version, 0, sizeof(version));
+ memset(&timestamp, 0, sizeof(timestamp));
+
+ version.v = v;
+ key.data = &version;
+ key.size = sizeof(version);
+
+ timestamp.t = t;
+ data.data = &timestamp;
+ data.size = sizeof(timestamp);
+
+ if ((ret = c->db_version_timestamp->put(c->db_version_timestamp, NULL, &key, &data, 0))) {
+ c->db_version_timestamp->err(c->db_version_timestamp, ret, "version_timestamp::get");
+ return -1;
+ }
+
+ c->timestamp = t;
+ c->version = v;
+
+ return 0;
+}
+
+int update(struct syrep_db_context *c, struct syrep_md_cache *cache) {
+ uint32_t now, version;
+
+ assert(c);
+
+ now = (uint32_t) time(NULL);
+ version = c->version+1;
+
+ if (iterate_dir(c, cache, version, ".") < 0)
+ return -1;
+
+ if (new_version(c, version, now) < 0)
+ return -1;
+
+ if (args.verbose_flag)
+ fprintf(stderr, "Wrote version %u.\n", c->version);
+
+ return 0;
+}
diff --git a/src/update.h b/src/update.h
new file mode 100644
index 0000000..f176e05
--- /dev/null
+++ b/src/update.h
@@ -0,0 +1,9 @@
+#ifndef fooupdatehfoo
+#define fooupdatehfoo
+
+#include "context.h"
+#include "cache.h"
+
+int update(struct syrep_db_context *c, struct syrep_md_cache *cache);
+
+#endif
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..91954dd
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,106 @@
+#include <stdio.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#include <unistd.h>
+#include "util.h"
+
+void statistics(DB *db) {
+ DB_BTREE_STAT *statp;
+ int ret;
+
+ assert(db);
+
+ if ((ret = db->stat(db, &statp, 0)) != 0) {
+ db->err(db, ret, "DB->stat");
+ return;
+ }
+
+ printf("Database contains %lu records\n", (long unsigned) statp->bt_ndata);
+ free(statp);
+}
+
+char* normalize_path(char *s) {
+ char *l, *p, *d;
+
+ // deletes /./ and //
+
+ if (*s == '/')
+ l = p = d = s+1;
+ else
+ l = p = d = s;
+
+ for (; *p; p++) {
+ if (*p == '/') {
+
+ if (l-p == 0) {
+ l++;
+ continue;
+ }
+
+ if (p-l == 1 && *l == '.') {
+ l += 2;
+ continue;
+ }
+
+ while (l <= p)
+ *(d++) = *(l++);
+ }
+ }
+
+ while (l <= p)
+ *(d++) = *(l++);
+
+ return s;
+}
+
+void rotdash(void) {
+ static const char dashes[] = /* ".oOo"; */ "|/-\\";
+ const static char *d = dashes;
+
+ if (isatty(fileno(stderr))) {
+ fprintf(stderr, "%c\b", *d);
+
+ d++;
+ if (!*d)
+ d = dashes;
+ }
+}
+
+const char* get_attached_filename(const char *path, const char *fn) {
+ static char npath[PATH_MAX];
+ struct stat st;
+
+ if (stat(path, &st) < 0) {
+ if (errno == ENOENT)
+ return path;
+
+ fprintf(stderr, "stat(%s) failed: %s\n", path, strerror(errno));
+ return NULL;
+ }
+
+ if (S_ISREG(st.st_mode))
+ return path;
+
+ if (S_ISDIR(st.st_mode)) {
+ snprintf(npath, sizeof(npath), "%s/.syrep", path);
+ mkdir(npath, 0777);
+ snprintf(npath, sizeof(npath), "%s/.syrep/%s", path, fn);
+ return npath;
+ }
+
+ fprintf(stderr, "<%s> is not a valid syrep snapshot\n", path);
+ return NULL;
+}
+
+int isdirectory(const char *path) {
+ struct stat st;
+
+ if (stat(path, &st) < 0)
+ return -1;
+
+ return !!S_ISDIR(st.st_mode);
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..dbdb165
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,16 @@
+#ifndef fooutilhfoo
+#define fooutilhfoo
+
+#include <db.h>
+
+void statistics(DB *db);
+char* normalize_path(char *s);
+void rotdash(void);
+const char* get_attached_filename(const char *path, const char *fn);
+int isdirectory(const char *path);
+
+#ifndef MIN
+#define MIN(a,b) ((a)<(b)?(a):(b))
+#endif
+
+#endif
diff --git a/test/Makefile b/test/Makefile
new file mode 100644
index 0000000..f5d2ae0
--- /dev/null
+++ b/test/Makefile
@@ -0,0 +1,20 @@
+SYREP=../syrep
+
+%:
+ mkdir -p rep1 rep2
+ $(SYREP) --show-deleted --show-times --$@ rep1 rep2
+
+all: update
+
+merge:
+ @echo "Merging rep1 into rep2"
+ $(SYREP) --merge rep1 rep2
+ @echo "Merging rep2 into rep1"
+ $(SYREP) --merge rep2 rep1
+
+clean:
+
+mrproper:
+ rm -rf rep1/.syrep rep2/.syrep
+
+.PHONY: all merge clean mrproper