zdir (3) Linux Manual Page
zdir – work with file-system directories
Synopsis
// Create a new directory item that loads in the full tree of the specified
// path, optionally located under some parent path. If parent is "-", then
// loads only the top-level directory, and does not use parent as a path.
CZMQ_EXPORT zdir_t *
zdir_new(const char *path, const char *parent);
// Destroy a directory tree and all children it contains.
CZMQ_EXPORT void
zdir_destroy(zdir_t **self_p);
// Return directory path
CZMQ_EXPORT const char *
zdir_path(zdir_t *self);
// Return last modification time for directory.
CZMQ_EXPORT time_t
zdir_modified(zdir_t *self);
// Return total hierarchy size, in bytes of data contained in all files
// in the directory tree.
CZMQ_EXPORT off_t
zdir_cursize(zdir_t *self);
// Return directory count
CZMQ_EXPORT size_t
zdir_count(zdir_t *self);
// Returns a sorted list of zfile objects; Each entry in the list is a pointer
// to a zfile_t item already allocated in the zdir tree. Do not destroy the
// original zdir tree until you are done with this list.
// The caller is responsible for destroying the return value when finished with it.
CZMQ_EXPORT zlist_t *
zdir_list(zdir_t *self);
// Remove directory, optionally including all files that it contains, at
// all levels. If force is false, will only remove the directory if empty.
// If force is true, will remove all files and all subdirectories.
CZMQ_EXPORT void
zdir_remove(zdir_t *self, bool force);
// Calculate differences between two versions of a directory tree.
// Returns a list of zdir_patch_t patches. Either older or newer may
// be null, indicating the directory is empty/absent. If alias is set,
// generates virtual filename (minus path, plus alias).
// The caller is responsible for destroying the return value when finished with it.
CZMQ_EXPORT zlist_t *
zdir_diff(zdir_t *older, zdir_t *newer, const char *alias);
// Return full contents of directory as a zdir_patch list.
// The caller is responsible for destroying the return value when finished with it.
CZMQ_EXPORT zlist_t *
zdir_resync(zdir_t *self, const char *alias);
// Load directory cache; returns a hash table containing the SHA-1 digests
// of every file in the tree. The cache is saved between runs in .cache.
// The caller is responsible for destroying the return value when finished with it.
CZMQ_EXPORT zhash_t *
zdir_cache(zdir_t *self);
// Print contents of directory to open stream
CZMQ_EXPORT void
zdir_fprint(zdir_t *self, FILE *file, int indent);
// Print contents of directory to stdout
CZMQ_EXPORT void
zdir_print(zdir_t *self, int indent);
// Create a new zdir_watch actor instance:
//
// zactor_t *watch = zactor_new (zdir_watch, NULL);
//
// Destroy zdir_watch instance:
//
// zactor_destroy (&watch);
//
// Enable verbose logging of commands and activity:
//
// zstr_send (watch, "VERBOSE");
//
// Subscribe to changes to a directory path:
//
// zsock_send (watch, "ss", "SUBSCRIBE", "directory_path");
//
// Unsubscribe from changes to a directory path:
//
// zsock_send (watch, "ss", "UNSUBSCRIBE", "directory_path");
//
// Receive directory changes:
// zsock_recv (watch, "sp", &path, &patches);
//
// // Delete the received data.
// free (path);
// zlist_destroy (&patches);
CZMQ_EXPORT void
zdir_watch(zsock_t *pipe, void *unused);
// Self test of this class.
CZMQ_EXPORT void
zdir_test(bool verbose);
Description
The zdir class gives access to the file system index. It will load a directory tree (a directory plus all child directories) into a zdir structure and then let you navigate that structure. It exists mainly to wrap non-portable OS functions to do this.
Please add @discuss section in ../src/zdir.c.
Example
From zdir_test method.
zdir_t *older = zdir_new(".", NULL);
assert(older);
if (verbose) {
printf("
");
zdir_dump(older, 0);
}
zdir_t *newer = zdir_new("..", NULL);
assert(newer);
zlist_t *patches = zdir_diff(older, newer, "/");
assert(patches);
while (zlist_size(patches)) {
zdir_patch_t *patch = (zdir_patch_t *)zlist_pop(patches);
zdir_patch_destroy(&patch);
}
zlist_destroy(&patches);
zdir_destroy(&older);
zdir_destroy(&newer);
zdir_t *nosuch = zdir_new("does-not-exist", NULL);
assert(nosuch == NULL);
// zdir_watch test:
zactor_t *watch = zactor_new(zdir_watch, NULL);
assert(watch);
if (verbose) {
zsock_send(watch, "s", "VERBOSE");
assert(zsock_wait(watch) == 0);
}
// need to create a file in the test directory we're watching
// in order to ensure the directory exists
zfile_t *initfile = zfile_new("./zdir-test-dir", "initial_file");
assert(initfile);
zfile_output(initfile);
fprintf(zfile_handle(initfile), "initial file
");
zfile_close(initfile);
zfile_destroy(&initfile);
zclock_sleep(1001); // wait for initial file to become 'stable'
zsock_send(watch, "si", "TIMEOUT", 100);
assert(zsock_wait(watch) == 0);
zsock_send(watch, "ss", "SUBSCRIBE", "zdir-test-dir");
assert(zsock_wait(watch) == 0);
zsock_send(watch, "ss", "UNSUBSCRIBE", "zdir-test-dir");
assert(zsock_wait(watch) == 0);
zsock_send(watch, "ss", "SUBSCRIBE", "zdir-test-dir");
assert(zsock_wait(watch) == 0);
zfile_t *newfile = zfile_new("zdir-test-dir", "test_abc");
zfile_output(newfile);
fprintf(zfile_handle(newfile), "test file
");
zfile_close(newfile);
zpoller_t *watch_poll = zpoller_new(watch, NULL);
// poll for a certain timeout before giving up and failing the test.
assert(zpoller_wait(watch_poll, 1001) == watch);
// wait for notification of the file being added
char *path;
int rc = zsock_recv(watch, "sp", &path, &patches);
assert(rc == 0);
assert(streq(path, "zdir-test-dir"));
free(path);
assert(zlist_size(patches) == 1);
zdir_patch_t *patch = (zdir_patch_t *)zlist_pop(patches);
assert(streq(zdir_patch_path(patch), "zdir-test-dir"));
zfile_t *patch_file = zdir_patch_file(patch);
assert(streq(zfile_filename(patch_file, ""), "zdir-test-dir/test_abc"));
zdir_patch_destroy(&patch);
zlist_destroy(&patches);
// remove the file
zfile_remove(newfile);
zfile_destroy(&newfile);
// poll for a certain timeout before giving up and failing the test.
assert(zpoller_wait(watch_poll, 1001) == watch);
// wait for notification of the file being removed
rc = zsock_recv(watch, "sp", &path, &patches);
assert(rc == 0);
assert(streq(path, "zdir-test-dir"));
free(path);
assert(zlist_size(patches) == 1);
patch = (zdir_patch_t *)zlist_pop(patches);
assert(streq(zdir_patch_path(patch), "zdir-test-dir"));
patch_file = zdir_patch_file(patch);
assert(streq(zfile_filename(patch_file, ""), "zdir-test-dir/test_abc"));
zdir_patch_destroy(&patch);
zlist_destroy(&patches);
zpoller_destroy(&watch_poll);
zactor_destroy(&watch);
// clean up by removing the test directory.
zdir_t *testdir = zdir_new("zdir-test-dir", NULL);
zdir_remove(testdir, true);
zdir_destroy(&testdir);
Authors
The czmq manual was written by the authors in the AUTHORS file.
Resources
Main web site: m[blue]m[]
Report bugs to the email <m[blue]zeromq-dev [at] lists.zeromq.orgm[][1]>
Copyright
Copyright (c) 1991-2012 iMatix Corporation — http://www.imatix.com Copyright other contributors as noted in the AUTHORS file. This file is part of CZMQ, the high-level C binding for 0MQ: http://czmq.zeromq.org This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. LICENSE included with the czmq distribution.
Notes
- 1.
- zeromq-dev [at] lists.zeromq.org
- mailto:zeromq-dev [at] lists.zeromq.org
