1ZDIR(3)                           CZMQ Manual                          ZDIR(3)
2
3
4

NAME

6       zdir - Class for work with file-system directories
7

SYNOPSIS

9       //  This is a stable class, and may not change except for emergencies. It
10       //  is provided in stable builds.
11       //  Create a new directory item that loads in the full tree of the specified
12       //  path, optionally located under some parent path. If parent is "-", then
13       //  loads only the top-level directory, and does not use parent as a path.
14       CZMQ_EXPORT zdir_t *
15           zdir_new (const char *path, const char *parent);
16
17       //  Destroy a directory tree and all children it contains.
18       CZMQ_EXPORT void
19           zdir_destroy (zdir_t **self_p);
20
21       //  Return directory path
22       CZMQ_EXPORT const char *
23           zdir_path (zdir_t *self);
24
25       //  Return last modification time for directory.
26       CZMQ_EXPORT time_t
27           zdir_modified (zdir_t *self);
28
29       //  Return total hierarchy size, in bytes of data contained in all files
30       //  in the directory tree.
31       CZMQ_EXPORT off_t
32           zdir_cursize (zdir_t *self);
33
34       //  Return directory count
35       CZMQ_EXPORT size_t
36           zdir_count (zdir_t *self);
37
38       //  Returns a sorted list of zfile objects; Each entry in the list is a pointer
39       //  to a zfile_t item already allocated in the zdir tree. Do not destroy the
40       //  original zdir tree until you are done with this list.
41       //  Caller owns return value and must destroy it when done.
42       CZMQ_EXPORT zlist_t *
43           zdir_list (zdir_t *self);
44
45       //  Remove directory, optionally including all files that it contains, at
46       //  all levels. If force is false, will only remove the directory if empty.
47       //  If force is true, will remove all files and all subdirectories.
48       CZMQ_EXPORT void
49           zdir_remove (zdir_t *self, bool force);
50
51       //  Calculate differences between two versions of a directory tree.
52       //  Returns a list of zdir_patch_t patches. Either older or newer may
53       //  be null, indicating the directory is empty/absent. If alias is set,
54       //  generates virtual filename (minus path, plus alias).
55       //  Caller owns return value and must destroy it when done.
56       CZMQ_EXPORT zlist_t *
57           zdir_diff (zdir_t *older, zdir_t *newer, const char *alias);
58
59       //  Return full contents of directory as a zdir_patch list.
60       //  Caller owns return value and must destroy it when done.
61       CZMQ_EXPORT zlist_t *
62           zdir_resync (zdir_t *self, const char *alias);
63
64       //  Load directory cache; returns a hash table containing the SHA-1 digests
65       //  of every file in the tree. The cache is saved between runs in .cache.
66       //  Caller owns return value and must destroy it when done.
67       CZMQ_EXPORT zhash_t *
68           zdir_cache (zdir_t *self);
69
70       //  Print contents of directory to open stream
71       CZMQ_EXPORT void
72           zdir_fprint (zdir_t *self, FILE *file, int indent);
73
74       //  Print contents of directory to stdout
75       CZMQ_EXPORT void
76           zdir_print (zdir_t *self, int indent);
77
78       //  Create a new zdir_watch actor instance:
79       //
80       //      zactor_t *watch = zactor_new (zdir_watch, NULL);
81       //
82       //  Destroy zdir_watch instance:
83       //
84       //      zactor_destroy (&watch);
85       //
86       //  Enable verbose logging of commands and activity:
87       //
88       //      zstr_send (watch, "VERBOSE");
89       //
90       //  Subscribe to changes to a directory path:
91       //
92       //      zsock_send (watch, "ss", "SUBSCRIBE", "directory_path");
93       //
94       //  Unsubscribe from changes to a directory path:
95       //
96       //      zsock_send (watch, "ss", "UNSUBSCRIBE", "directory_path");
97       //
98       //  Receive directory changes:
99       //      zsock_recv (watch, "sp", &path, &patches);
100       //
101       //      // Delete the received data.
102       //      free (path);
103       //      zlist_destroy (&patches);
104       CZMQ_EXPORT void
105           zdir_watch (zsock_t *pipe, void *unused);
106
107       //  Self test of this class.
108       CZMQ_EXPORT void
109           zdir_test (bool verbose);
110
111       Please add '@interface' section in './../src/zdir.c'.
112

DESCRIPTION

114       The zdir class gives access to the file system index. It will load a
115       directory tree (a directory plus all child directories) into a zdir
116       structure and then let you navigate that structure. It exists mainly to
117       wrap non-portable OS functions to do this.
118
119       Please add @discuss section in ./../src/zdir.c.
120

EXAMPLE

122       From zdir_test method.
123
124           const char *SELFTEST_DIR_RW = "src/selftest-rw";
125
126           const char *testbasedir  = "zdir-test-dir";
127           const char *testfile1 = "initial_file";
128           const char *testfile2 = "test_abc";
129           char *basedirpath = NULL;   // subdir in a test, under SELFTEST_DIR_RW
130           char *filepath1 = NULL;      // pathname to testfile in a test, in dirpath
131           char *filepath2 = NULL;      // pathname to testfile in a test, in dirpath
132
133           basedirpath = zsys_sprintf ("%s/%s", SELFTEST_DIR_RW, testbasedir);
134           assert (basedirpath);
135           filepath1 = zsys_sprintf ("%s/%s", basedirpath, testfile1);
136           assert (filepath1);
137           filepath2 = zsys_sprintf ("%s/%s", basedirpath, testfile2);
138           assert (filepath2);
139
140           /*
141           char *relfilepath2 = NULL;      // pathname to testfile in a test, in dirpath
142           relfilepath2 = zsys_sprintf ("%s/%s", testbasedir, testfile2);
143           assert (relfilepath2);
144           */
145
146           // Make sure old aborted tests do not hinder us
147           zdir_t *dir = zdir_new (basedirpath, NULL);
148           if (dir) {
149               zdir_remove (dir, true);
150               zdir_destroy (&dir);
151           }
152           zsys_file_delete (filepath1);
153           zsys_file_delete (filepath2);
154           zsys_dir_delete  (basedirpath);
155
156           dir = zdir_new ("does-not-exist", NULL);
157           if (dir) {
158               zdir_remove (dir, true);
159               zdir_destroy (&dir);
160           }
161
162           // need to create a file in the test directory we're watching
163           // in order to ensure the directory exists
164           zfile_t *initfile = zfile_new (basedirpath, testfile1);
165           assert (initfile);
166           zfile_output (initfile);
167           fprintf (zfile_handle (initfile), "initial file\n");
168           zfile_close (initfile);
169           zfile_destroy (&initfile);
170
171           zdir_t *older = zdir_new (basedirpath, NULL);
172           assert (older);
173           if (verbose) {
174               printf ("\n");
175               zdir_dump (older, 0);
176           }
177           zdir_t *newer = zdir_new (SELFTEST_DIR_RW, NULL);
178           assert (newer);
179           zlist_t *patches = zdir_diff (older, newer, "/");
180           assert (patches);
181           while (zlist_size (patches)) {
182               zdir_patch_t *patch = (zdir_patch_t *) zlist_pop (patches);
183               zdir_patch_destroy (&patch);
184           }
185           zlist_destroy (&patches);
186           zdir_destroy (&older);
187           zdir_destroy (&newer);
188
189           zdir_t *nosuch = zdir_new ("does-not-exist", NULL);
190           assert (nosuch == NULL);
191
192           // zdir_watch test:
193           zactor_t *watch = zactor_new (zdir_watch, NULL);
194           assert (watch);
195
196           int synced;
197           if (verbose) {
198               zsock_send (watch, "s", "VERBOSE");
199               synced = zsock_wait(watch);
200               assert ( synced == 0);
201           }
202
203           // wait for initial file to become 'stable'
204           #ifdef CZMQ_BUILD_DRAFT_API
205           zclock_sleep ((int)zsys_file_stable_age_msec() + 50);
206           #else
207           zclock_sleep (5050);
208           #endif
209
210           zsock_send (watch, "si", "TIMEOUT", 100);
211           synced = zsock_wait(watch);
212           assert (synced == 0);
213
214           zsock_send (watch, "ss", "SUBSCRIBE", basedirpath);
215           synced = zsock_wait(watch);
216           assert(synced == 0);
217
218           zsock_send (watch, "ss", "UNSUBSCRIBE", basedirpath);
219           synced = zsock_wait(watch);
220           assert(synced == 0);
221
222           zsock_send (watch, "ss", "SUBSCRIBE", basedirpath);
223           synced = zsock_wait(watch);
224           assert(synced == 0);
225
226           zfile_t *newfile = zfile_new (basedirpath, testfile2);
227           zfile_output (newfile);
228           fprintf (zfile_handle (newfile), "test file\n");
229           zfile_close (newfile);
230
231           zpoller_t *watch_poll = zpoller_new (watch, NULL);
232
233           // poll for a certain timeout before giving up and failing the test
234           void* polled = NULL;
235           #ifdef CZMQ_BUILD_DRAFT_API
236           polled = zpoller_wait(watch_poll, (int)zsys_file_stable_age_msec() + 150);
237           #else
238           polled = zpoller_wait(watch_poll, 5150);
239           #endif
240           assert (polled == watch);
241
242           // wait for notification of the file being added
243           char *path;
244           int rc = zsock_recv (watch, "sp", &path, &patches);
245           assert (rc == 0);
246
247           assert (streq (path, basedirpath));
248           freen (path);
249
250           if (verbose)
251               zsys_debug("zdir_test() : added : zlist_size (patches)=%d",
252                   zlist_size (patches) );
253           assert (zlist_size (patches) == 1);
254
255           zdir_patch_t *patch = (zdir_patch_t *) zlist_pop (patches);
256           if (verbose)
257               zsys_debug("zdir_test() : added : zdir_patch_path (patch)='%s'",
258                   zdir_patch_path (patch) );
259           assert (streq (zdir_patch_path (patch), basedirpath));
260
261           zfile_t *patch_file = zdir_patch_file (patch);
262           if (verbose)
263               zsys_debug("zdir_test() : added : zfile_filename (patch_file, \"\")='%s'",
264                   zfile_filename (patch_file, "") );
265           assert (streq (zfile_filename (patch_file, ""), filepath2));
266
267           zdir_patch_destroy (&patch);
268           zlist_destroy (&patches);
269
270           // remove the file
271           zfile_remove (newfile);
272           zfile_destroy (&newfile);
273
274           // poll for a certain timeout before giving up and failing the test.
275           #ifdef CZMQ_BUILD_DRAFT_API
276           polled = zpoller_wait(watch_poll, (int)zsys_file_stable_age_msec() + 150);
277           #else
278           polled = zpoller_wait(watch_poll, 5150);
279           #endif
280           assert (polled == watch);
281
282           // wait for notification of the file being removed
283           rc = zsock_recv (watch, "sp", &path, &patches);
284           assert (rc == 0);
285
286           assert (streq (path, basedirpath));
287           freen (path);
288
289           if (verbose)
290               zsys_debug("zdir_test() : removed : zlist_size (patches)=%d",
291                   zlist_size (patches) );
292           assert (zlist_size (patches) == 1);
293
294           patch = (zdir_patch_t *) zlist_pop (patches);
295           if (verbose)
296               zsys_debug("zdir_test() : removed : zdir_patch_path (patch)='%s'",
297                   zdir_patch_path (patch) );
298           assert (streq (zdir_patch_path (patch), basedirpath));
299
300           patch_file = zdir_patch_file (patch);
301           if (verbose)
302               zsys_debug("zdir_test() : removed : zfile_filename (patch_file, \"\")='%s'",
303                   zfile_filename (patch_file, "") );
304           assert (streq (zfile_filename (patch_file, ""), filepath2));
305
306           zdir_patch_destroy (&patch);
307           zlist_destroy (&patches);
308
309           zpoller_destroy (&watch_poll);
310           zactor_destroy (&watch);
311
312           // clean up by removing the test directory.
313           dir = zdir_new (basedirpath, NULL);
314           assert (dir);
315           zdir_remove (dir, true);
316           zdir_destroy (&dir);
317
318           zstr_free (&basedirpath);
319           zstr_free (&filepath1);
320           zstr_free (&filepath2);
321
322           #if defined (__WINDOWS__)
323           zsys_shutdown();
324           #endif
325
326

AUTHORS

328       The czmq manual was written by the authors in the AUTHORS file.
329

RESOURCES

331       Main web site:
332
333       Report bugs to the email <zeromq-dev@lists.zeromq.org[1]>
334
336       Copyright (c) the Contributors as noted in the AUTHORS file. This file
337       is part of CZMQ, the high-level C binding for 0MQ:
338       http://czmq.zeromq.org. This Source Code Form is subject to the terms
339       of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
340       distributed with this file, You can obtain one at
341       http://mozilla.org/MPL/2.0/. LICENSE included with the czmq
342       distribution.
343

NOTES

345        1. zeromq-dev@lists.zeromq.org
346           mailto:zeromq-dev@lists.zeromq.org
347
348
349
350CZMQ 4.2.0                        01/28/2020                           ZDIR(3)
Impressum