void zdir_patch_test (bool verbose) { printf (" * zdir_patch: "); // @selftest zfile_t *file = zfile_new (".", "bilbo"); assert (file); zdir_patch_t *patch = zdir_patch_new (".", file, patch_create, "/"); assert (patch); zfile_destroy (&file); file = zdir_patch_file (patch); assert (file); assert (streq (zfile_filename (file, "."), "bilbo")); assert (streq (zdir_patch_vpath (patch), "/bilbo")); zdir_patch_destroy (&patch); #if defined (__WINDOWS__) zsys_shutdown(); #endif // @end printf ("OK\n"); }
// -------------------------------------------------------------------------- // Self test of this class int zdir_patch_test (bool verbose) { printf (" * zdir_patch: "); // @selftest zfile_t *file = zfile_new (".", "bilbo"); zdir_patch_t *patch = zdir_patch_new (".", file, patch_create, "/"); zfile_destroy (&file); file = zdir_patch_file (patch); assert (streq (zfile_filename (file, "."), "bilbo")); assert (streq (zdir_patch_vpath (patch), "/bilbo")); zdir_patch_destroy (&patch); // @end printf ("OK\n"); return 0; }
zlist_t * zdir_resync (zdir_t *self, const char *alias) { zlist_t *patches = zlist_new (); if (!patches) return NULL; zfile_t **files = zdir_flatten (self); uint index; for (index = 0;; index++) { zfile_t *file = files [index]; if (!file) break; if (zlist_append (patches, zdir_patch_new ( self->path, file, patch_create, alias))) { zlist_destroy (&patches); break; } } freen (files); return patches; }
/// // Create new patch QZdirPatch::QZdirPatch (const QString &path, QZfile *file, int op, const QString &alias, QObject *qObjParent) : QObject (qObjParent) { this->self = zdir_patch_new (path.toUtf8().data(), file->self, op, alias.toUtf8().data()); }
zlist_t * zdir_diff (zdir_t *older, zdir_t *newer, const char *alias) { zlist_t *patches = zlist_new (); if (!patches) return NULL; zfile_t **old_files = zdir_flatten (older); zfile_t **new_files = zdir_flatten (newer); int old_index = 0; int new_index = 0; // Note that both lists are sorted, so detecting differences // is rather trivial while (old_files [old_index] || new_files [new_index]) { zfile_t *old_file = old_files [old_index]; zfile_t *new_file = new_files [new_index]; int cmp; if (!old_file) cmp = 1; // Old file was deleted at end of list else if (!new_file) cmp = -1; // New file was added at end of list else cmp = strcmp (zfile_filename (old_file, NULL), zfile_filename (new_file, NULL)); if (cmp > 0) { // New file was created if (zfile_is_stable (new_file)) { int rc = zlist_append (patches, zdir_patch_new (newer->path, new_file, patch_create, alias)); if (rc != 0) { zlist_destroy (&patches); break; } } old_index--; } else if (cmp < 0) { // Old file was deleted if (zfile_is_stable (old_file)) { int rc = zlist_append (patches, zdir_patch_new (older->path, old_file, patch_delete, alias)); if (rc != 0) { zlist_destroy (&patches); break; } } new_index--; } else if (cmp == 0 && zfile_is_stable (new_file)) { if (zfile_is_stable (old_file)) { // Old file was modified or replaced // Since we don't check file contents, treat as created // Could better do SHA check on file here if (zfile_modified (new_file) != zfile_modified (old_file) || zfile_cursize (new_file) != zfile_cursize (old_file)) { int rc = zlist_append (patches, zdir_patch_new (newer->path, new_file, patch_create, alias)); if (rc != 0) { zlist_destroy (&patches); break; } } } else { // File was created over some period of time int rc = zlist_append (patches, zdir_patch_new (newer->path, new_file, patch_create, alias)); if (rc != 0) { zlist_destroy (&patches); break; } } } old_index++; new_index++; } freen (old_files); freen (new_files); return patches; }