api documentation for ec_parse
[protos/libecoli.git] / src / ecoli_node_file.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <unistd.h>
12 #include <dirent.h>
13
14 #include <ecoli_log.h>
15 #include <ecoli_malloc.h>
16 #include <ecoli_test.h>
17 #include <ecoli_strvec.h>
18 #include <ecoli_string.h>
19 #include <ecoli_node.h>
20 #include <ecoli_parse.h>
21 #include <ecoli_complete.h>
22 #include <ecoli_node_file.h>
23
24 EC_LOG_TYPE_REGISTER(node_file);
25
26 struct ec_node_file {
27         /* below functions pointers are only useful for test */
28         int (*lstat)(const char *pathname, struct stat *buf);
29         DIR *(*opendir)(const char *name);
30         struct dirent *(*readdir)(DIR *dirp);
31         int (*closedir)(DIR *dirp);
32         int (*dirfd)(DIR *dirp);
33         int (*fstatat)(int dirfd, const char *pathname, struct stat *buf,
34                 int flags);
35 };
36
37 static int
38 ec_node_file_parse(const struct ec_node *node,
39                 struct ec_pnode *pstate,
40                 const struct ec_strvec *strvec)
41 {
42         (void)node;
43         (void)pstate;
44
45         if (ec_strvec_len(strvec) == 0)
46                 return EC_PARSE_NOMATCH;
47
48         return 1;
49 }
50
51 /*
52  * Almost the same than dirname (3) and basename (3) except that:
53  * - it always returns a substring of the given path, which can
54  *   be empty.
55  * - the behavior is different when the path finishes with a '/'
56  * - the path argument is not modified
57  * - the outputs are allocated and must be freed with ec_free().
58  *
59  *   path       dirname   basename       split_path
60  *   /usr/lib   /usr      lib          /usr/     lib
61  *   /usr/      /         usr          /usr/
62  *   usr        .         usr                    usr
63  *   /          /         /            /
64  *   .          .         .                      .
65  *   ..         .         ..                     ..
66  */
67 static int split_path(const char *path, char **dname_p, char **bname_p)
68 {
69         char *last_slash;
70         size_t dirlen;
71         char *dname, *bname;
72
73         *dname_p = NULL;
74         *bname_p = NULL;
75
76         last_slash = strrchr(path, '/');
77         if (last_slash == NULL)
78                 dirlen = 0;
79         else
80                 dirlen = last_slash - path + 1;
81
82         dname = ec_strdup(path);
83         if (dname == NULL)
84                 return -1;
85         dname[dirlen] = '\0';
86
87         bname = ec_strdup(path + dirlen);
88         if (bname == NULL) {
89                 ec_free(dname);
90                 return -1;
91         }
92
93         *dname_p = dname;
94         *bname_p = bname;
95
96         return 0;
97 }
98
99 static int
100 ec_node_file_complete(const struct ec_node *node,
101                 struct ec_comp *comp,
102                 const struct ec_strvec *strvec)
103 {
104         struct ec_node_file *priv = ec_node_priv(node);
105         char *dname = NULL, *bname = NULL, *effective_dir;
106         struct ec_comp_item *item = NULL;
107         enum ec_comp_type type;
108         struct stat st, st2;
109         const char *input;
110         size_t bname_len;
111         struct dirent *de = NULL;
112         DIR *dir = NULL;
113         char *comp_str = NULL;
114         char *disp_str = NULL;
115         int is_dir = 0;
116
117         /*
118          * Example with this file tree:
119          * /
120          * ├── dir1
121          * │   ├── file1
122          * │   ├── file2
123          * │   └── subdir
124          * │       └── file3
125          * ├── dir2
126          * │   └── file4
127          * └── file5
128          *
129          * Input     Output completions
130          *   /       [dir1/, dir2/, file5]
131          *   /d      [dir1/, dir2/]
132          *   /f      [file5]
133          *   /dir1/  [file1, file2, subdir/]
134          *
135          *
136          *
137          */
138
139         if (ec_strvec_len(strvec) != 1)
140                 return 0;
141
142         input = ec_strvec_val(strvec, 0);
143         if (split_path(input, &dname, &bname) < 0)
144                 return -1;
145
146         if (strcmp(dname, "") == 0)
147                 effective_dir = ".";
148         else
149                 effective_dir = dname;
150
151         if (priv->lstat(effective_dir, &st) < 0)
152                 goto fail;
153         if (!S_ISDIR(st.st_mode))
154                 goto out;
155
156         dir = priv->opendir(effective_dir);
157         if (dir == NULL)
158                 goto fail;
159
160         bname_len = strlen(bname);
161         while (1) {
162                 int save_errno = errno;
163
164                 errno = 0;
165                 de = priv->readdir(dir);
166                 if (de == NULL) {
167                         if (errno == 0) {
168                                 errno = save_errno;
169                                 goto out;
170                         } else {
171                                 goto fail;
172                         }
173                 }
174
175                 if (!ec_str_startswith(de->d_name, bname))
176                         continue;
177                 if (bname[0] != '.' && de->d_name[0] == '.')
178                         continue;
179
180                 /* add '/' if it's a dir */
181                 if (de->d_type == DT_DIR) {
182                         is_dir = 1;
183                 } else if (de->d_type == DT_UNKNOWN) {
184                         int dir_fd = priv->dirfd(dir);
185
186                         if (dir_fd < 0)
187                                 goto fail;
188                         if (priv->fstatat(dir_fd, de->d_name, &st2, 0) < 0)
189                                 goto fail;
190                         if (S_ISDIR(st2.st_mode))
191                                 is_dir = 1;
192                         else
193                                 is_dir = 0;
194                 } else {
195                         is_dir = 0;
196                 }
197
198                 if (is_dir) {
199                         type = EC_COMP_PARTIAL;
200                         if (ec_asprintf(&comp_str, "%s%s/", input,
201                                         &de->d_name[bname_len]) < 0)
202                                 goto fail;
203                         if (ec_asprintf(&disp_str, "%s/", de->d_name) < 0)
204                                 goto fail;
205                 } else {
206                         type = EC_COMP_FULL;
207                         if (ec_asprintf(&comp_str, "%s%s", input,
208                                         &de->d_name[bname_len]) < 0)
209                                 goto fail;
210                         if (ec_asprintf(&disp_str, "%s", de->d_name) < 0)
211                                 goto fail;
212                 }
213                 item = ec_comp_add_item(comp, node, type, input, comp_str);
214                 if (item == NULL)
215                         goto out;
216
217                 /* fix the display string: we don't want to display the full
218                  * path. */
219                 if (ec_comp_item_set_display(item, disp_str) < 0)
220                         goto out;
221
222                 item = NULL;
223                 ec_free(comp_str);
224                 comp_str = NULL;
225                 ec_free(disp_str);
226                 disp_str = NULL;
227         }
228 out:
229         ec_free(comp_str);
230         ec_free(disp_str);
231         ec_free(dname);
232         ec_free(bname);
233         if (dir != NULL)
234                 priv->closedir(dir);
235
236         return 0;
237
238 fail:
239         ec_free(comp_str);
240         ec_free(disp_str);
241         ec_free(dname);
242         ec_free(bname);
243         if (dir != NULL)
244                 priv->closedir(dir);
245
246         return -1;
247 }
248
249 static int
250 ec_node_file_init_priv(struct ec_node *node)
251 {
252         struct ec_node_file *priv = ec_node_priv(node);
253
254         priv->lstat = lstat;
255         priv->opendir = opendir;
256         priv->readdir = readdir;
257         priv->dirfd = dirfd;
258         priv->fstatat = fstatat;
259
260         return 0;
261 }
262
263 static struct ec_node_type ec_node_file_type = {
264         .name = "file",
265         .parse = ec_node_file_parse,
266         .complete = ec_node_file_complete,
267         .size = sizeof(struct ec_node_file),
268         .init_priv = ec_node_file_init_priv,
269 };
270
271 EC_NODE_TYPE_REGISTER(ec_node_file_type);
272
273 /* LCOV_EXCL_START */
274 static int
275 test_lstat(const char *pathname, struct stat *buf)
276 {
277         if (!strcmp(pathname, "/tmp/toto/")) {
278                 struct stat st = { .st_mode = S_IFDIR };
279                 memcpy(buf, &st, sizeof(*buf));
280                 return 0;
281         }
282
283         errno = ENOENT;
284         return -1;
285 }
286
287 static DIR *
288 test_opendir(const char *name)
289 {
290         int *p;
291
292         if (strcmp(name, "/tmp/toto/")) {
293                 errno = ENOENT;
294                 return NULL;
295         }
296
297         p = malloc(sizeof(int));
298         if (p)
299                 *p = 0;
300
301         return (DIR *)p;
302 }
303
304 static struct dirent *
305 test_readdir(DIR *dirp)
306 {
307         static struct dirent de[] = {
308                 { .d_type = DT_DIR, .d_name = ".." },
309                 { .d_type = DT_DIR, .d_name = "." },
310                 { .d_type = DT_REG, .d_name = "bar" },
311                 { .d_type = DT_UNKNOWN, .d_name = "bar2" },
312                 { .d_type = DT_REG, .d_name = "foo" },
313                 { .d_type = DT_DIR, .d_name = "titi" },
314                 { .d_type = DT_UNKNOWN, .d_name = "tutu" },
315                 { .d_name = "" },
316         };
317         int *p = (int *)dirp;
318         struct dirent *ret = &de[*p];
319
320         if (!strcmp(ret->d_name, ""))
321                 return NULL;
322
323         *p = *p + 1;
324
325         return ret;
326 }
327
328 static int
329 test_closedir(DIR *dirp)
330 {
331         free(dirp);
332         return 0;
333 }
334
335 static int
336 test_dirfd(DIR *dirp)
337 {
338         int *p = (int *)dirp;
339         return *p;
340 }
341
342 static int
343 test_fstatat(int dirfd, const char *pathname, struct stat *buf,
344         int flags)
345 {
346         (void)dirfd;
347         (void)flags;
348
349         if (!strcmp(pathname, "bar2")) {
350                 struct stat st = { .st_mode = S_IFREG };
351                 memcpy(buf, &st, sizeof(*buf));
352                 return 0;
353         } else if (!strcmp(pathname, "tutu")) {
354                 struct stat st = { .st_mode = S_IFDIR };
355                 memcpy(buf, &st, sizeof(*buf));
356                 return 0;
357         }
358
359         errno = ENOENT;
360         return -1;
361 }
362
363 static int
364 ec_node_file_override_functions(struct ec_node *node)
365 {
366         struct ec_node_file *priv = ec_node_priv(node);
367
368         priv->lstat = test_lstat;
369         priv->opendir = test_opendir;
370         priv->readdir = test_readdir;
371         priv->closedir = test_closedir;
372         priv->dirfd = test_dirfd;
373         priv->fstatat = test_fstatat;
374
375         return 0;
376 }
377
378 static int ec_node_file_testcase(void)
379 {
380         struct ec_node *node;
381         int testres = 0;
382
383         node = ec_node("file", EC_NO_ID);
384         if (node == NULL) {
385                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
386                 return -1;
387         }
388         ec_node_file_override_functions(node);
389
390         /* any string matches */
391         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
392         testres |= EC_TEST_CHECK_PARSE(node, 1, "/tmp/bar");
393         testres |= EC_TEST_CHECK_PARSE(node, -1);
394
395         /* test completion */
396         testres |= EC_TEST_CHECK_COMPLETE(node,
397                 EC_VA_END,
398                 EC_VA_END);
399         testres |= EC_TEST_CHECK_COMPLETE(node,
400                 "/tmp/toto/t", EC_VA_END,
401                 EC_VA_END);
402         testres |= EC_TEST_CHECK_COMPLETE_PARTIAL(node,
403                 "/tmp/toto/t", EC_VA_END,
404                 "/tmp/toto/titi/", "/tmp/toto/tutu/", EC_VA_END);
405         testres |= EC_TEST_CHECK_COMPLETE(node,
406                 "/tmp/toto/f", EC_VA_END,
407                 "/tmp/toto/foo", EC_VA_END);
408         testres |= EC_TEST_CHECK_COMPLETE(node,
409                 "/tmp/toto/b", EC_VA_END,
410                 "/tmp/toto/bar", "/tmp/toto/bar2", EC_VA_END);
411
412         ec_node_free(node);
413
414         return testres;
415 }
416 /* LCOV_EXCL_STOP */
417
418 static struct ec_test ec_node_file_test = {
419         .name = "node_file",
420         .test = ec_node_file_testcase,
421 };
422
423 EC_TEST_REGISTER(ec_node_file_test);