1ZDIR(3) CZMQ Manual ZDIR(3)
2
3
4
6 zdir - work with file-system directories
7
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
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
122 From zdir_test method.
123
124 // need to create a file in the test directory we're watching
125 // in order to ensure the directory exists
126 zfile_t *initfile = zfile_new ("./zdir-test-dir", "initial_file");
127 assert (initfile);
128 zfile_output (initfile);
129 fprintf (zfile_handle (initfile), "initial file\n");
130 zfile_close (initfile);
131 zfile_destroy (&initfile);
132
133 zdir_t *older = zdir_new ("zdir-test-dir", NULL);
134 assert (older);
135 if (verbose) {
136 printf ("\n");
137 zdir_dump (older, 0);
138 }
139 zdir_t *newer = zdir_new (".", NULL);
140 assert (newer);
141 zlist_t *patches = zdir_diff (older, newer, "/");
142 assert (patches);
143 while (zlist_size (patches)) {
144 zdir_patch_t *patch = (zdir_patch_t *) zlist_pop (patches);
145 zdir_patch_destroy (&patch);
146 }
147 zlist_destroy (&patches);
148 zdir_destroy (&older);
149 zdir_destroy (&newer);
150
151 zdir_t *nosuch = zdir_new ("does-not-exist", NULL);
152 assert (nosuch == NULL);
153
154 // zdir_watch test:
155 zactor_t *watch = zactor_new (zdir_watch, NULL);
156 assert (watch);
157
158 if (verbose) {
159 zsock_send (watch, "s", "VERBOSE");
160 assert (zsock_wait (watch) == 0);
161 }
162
163 zclock_sleep (1001); // wait for initial file to become 'stable'
164
165 zsock_send (watch, "si", "TIMEOUT", 100);
166 assert (zsock_wait (watch) == 0);
167
168 zsock_send (watch, "ss", "SUBSCRIBE", "zdir-test-dir");
169 assert (zsock_wait (watch) == 0);
170
171 zsock_send (watch, "ss", "UNSUBSCRIBE", "zdir-test-dir");
172 assert (zsock_wait (watch) == 0);
173
174 zsock_send (watch, "ss", "SUBSCRIBE", "zdir-test-dir");
175 assert (zsock_wait (watch) == 0);
176
177 zfile_t *newfile = zfile_new ("zdir-test-dir", "test_abc");
178 zfile_output (newfile);
179 fprintf (zfile_handle (newfile), "test file\n");
180 zfile_close (newfile);
181
182 zpoller_t *watch_poll = zpoller_new (watch, NULL);
183
184 // poll for a certain timeout before giving up and failing the test.
185 assert (zpoller_wait (watch_poll, 1001) == watch);
186
187 // wait for notification of the file being added
188 char *path;
189 int rc = zsock_recv (watch, "sp", &path, &patches);
190 assert (rc == 0);
191
192 assert (streq (path, "zdir-test-dir"));
193 free (path);
194
195 assert (zlist_size (patches) == 1);
196
197 zdir_patch_t *patch = (zdir_patch_t *) zlist_pop (patches);
198 assert (streq (zdir_patch_path (patch), "zdir-test-dir"));
199
200 zfile_t *patch_file = zdir_patch_file (patch);
201 assert (streq (zfile_filename (patch_file, ""), "zdir-test-dir/test_abc"));
202
203 zdir_patch_destroy (&patch);
204 zlist_destroy (&patches);
205
206 // remove the file
207 zfile_remove (newfile);
208 zfile_destroy (&newfile);
209
210 // poll for a certain timeout before giving up and failing the test.
211 assert (zpoller_wait (watch_poll, 1001) == watch);
212
213 // wait for notification of the file being removed
214 rc = zsock_recv (watch, "sp", &path, &patches);
215 assert (rc == 0);
216
217 assert (streq (path, "zdir-test-dir"));
218 free (path);
219
220 assert (zlist_size (patches) == 1);
221
222 patch = (zdir_patch_t *) zlist_pop (patches);
223 assert (streq (zdir_patch_path (patch), "zdir-test-dir"));
224
225 patch_file = zdir_patch_file (patch);
226 assert (streq (zfile_filename (patch_file, ""), "zdir-test-dir/test_abc"));
227
228 zdir_patch_destroy (&patch);
229 zlist_destroy (&patches);
230
231 zpoller_destroy (&watch_poll);
232 zactor_destroy (&watch);
233
234 // clean up by removing the test directory.
235 zdir_t *testdir = zdir_new ("zdir-test-dir", NULL);
236 zdir_remove (testdir, true);
237 zdir_destroy (&testdir);
238
239
241 The czmq manual was written by the authors in the AUTHORS file.
242
244 Main web site:
245
246 Report bugs to the email <zeromq-dev@lists.zeromq.org[1]>
247
249 Copyright (c) the Contributors as noted in the AUTHORS file. This file
250 is part of CZMQ, the high-level C binding for 0MQ:
251 http://czmq.zeromq.org. This Source Code Form is subject to the terms
252 of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
253 distributed with this file, You can obtain one at
254 http://mozilla.org/MPL/2.0/. LICENSE included with the czmq
255 distribution.
256
258 1. zeromq-dev@lists.zeromq.org
259 mailto:zeromq-dev@lists.zeromq.org
260
261
262
263CZMQ 4.0.2 12/31/2016 ZDIR(3)