1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
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>
24 EC_LOG_TYPE_REGISTER(node_file);
29 /* below functions pointers are only useful for test */
30 int (*lstat)(const char *pathname, struct stat *buf);
31 DIR *(*opendir)(const char *name);
32 struct dirent *(*readdir)(DIR *dirp);
33 int (*closedir)(DIR *dirp);
34 int (*dirfd)(DIR *dirp);
35 int (*fstatat)(int dirfd, const char *pathname, struct stat *buf,
40 ec_node_file_parse(const struct ec_node *gen_node,
41 struct ec_parse *state,
42 const struct ec_strvec *strvec)
47 if (ec_strvec_len(strvec) == 0)
48 return EC_PARSE_NOMATCH;
54 * Almost the same than dirname (3) and basename (3) except that:
55 * - it always returns a substring of the given path, which can
57 * - the behavior is different when the path finishes with a '/'
58 * - the path argument is not modified
59 * - the outputs are allocated and must be freed with ec_free().
61 * path dirname basename split_path
62 * /usr/lib /usr lib /usr/ lib
69 static int split_path(const char *path, char **dname_p, char **bname_p)
78 last_slash = strrchr(path, '/');
79 if (last_slash == NULL)
82 dirlen = last_slash - path + 1;
84 dname = ec_strdup(path);
89 bname = ec_strdup(path + dirlen);
102 ec_node_file_complete(const struct ec_node *gen_node,
103 struct ec_comp *comp,
104 const struct ec_strvec *strvec)
106 struct ec_node_file *node = (struct ec_node_file *)gen_node;
107 char *dname = NULL, *bname = NULL, *effective_dir;
108 struct ec_comp_item *item = NULL;
109 enum ec_comp_type type;
113 struct dirent *de = NULL;
115 char *comp_str = NULL;
116 char *disp_str = NULL;
120 * Example with this file tree:
131 * Input Output completions
132 * / [dir1/, dir2/, file5]
135 * /dir1/ [file1, file2, subdir/]
141 if (ec_strvec_len(strvec) != 1)
144 input = ec_strvec_val(strvec, 0);
145 if (split_path(input, &dname, &bname) < 0)
148 if (strcmp(dname, "") == 0)
151 effective_dir = dname;
153 if (node->lstat(effective_dir, &st) < 0)
155 if (!S_ISDIR(st.st_mode))
158 dir = node->opendir(effective_dir);
162 bname_len = strlen(bname);
164 int save_errno = errno;
167 de = node->readdir(dir);
177 if (!ec_str_startswith(de->d_name, bname))
179 if (bname[0] != '.' && de->d_name[0] == '.')
182 /* add '/' if it's a dir */
183 if (de->d_type == DT_DIR) {
185 } else if (de->d_type == DT_UNKNOWN) {
186 int dir_fd = node->dirfd(dir);
190 if (node->fstatat(dir_fd, de->d_name, &st2, 0) < 0)
192 if (S_ISDIR(st2.st_mode))
201 type = EC_COMP_PARTIAL;
202 if (ec_asprintf(&comp_str, "%s%s/", input,
203 &de->d_name[bname_len]) < 0)
205 if (ec_asprintf(&disp_str, "%s/", de->d_name) < 0)
209 if (ec_asprintf(&comp_str, "%s%s", input,
210 &de->d_name[bname_len]) < 0)
212 if (ec_asprintf(&disp_str, "%s", de->d_name) < 0)
215 if (ec_comp_add_item(comp, gen_node, &item,
216 type, input, comp_str) < 0)
219 /* fix the display string: we don't want to display the full
221 if (ec_comp_item_set_display(item, disp_str) < 0)
252 ec_node_file_init_priv(struct ec_node *gen_node)
254 struct ec_node_file *node = (struct ec_node_file *)gen_node;
257 node->opendir = opendir;
258 node->readdir = readdir;
260 node->fstatat = fstatat;
265 static struct ec_node_type ec_node_file_type = {
267 .parse = ec_node_file_parse,
268 .complete = ec_node_file_complete,
269 .size = sizeof(struct ec_node_file),
270 .init_priv = ec_node_file_init_priv,
273 EC_NODE_TYPE_REGISTER(ec_node_file_type);
275 /* LCOV_EXCL_START */
277 test_lstat(const char *pathname, struct stat *buf)
279 if (!strcmp(pathname, "/tmp/toto/")) {
280 struct stat st = { .st_mode = S_IFDIR };
281 memcpy(buf, &st, sizeof(*buf));
290 test_opendir(const char *name)
294 if (strcmp(name, "/tmp/toto/")) {
299 p = malloc(sizeof(int));
306 static struct dirent *
307 test_readdir(DIR *dirp)
309 static struct dirent de[] = {
310 { .d_type = DT_DIR, .d_name = ".." },
311 { .d_type = DT_DIR, .d_name = "." },
312 { .d_type = DT_REG, .d_name = "bar" },
313 { .d_type = DT_UNKNOWN, .d_name = "bar2" },
314 { .d_type = DT_REG, .d_name = "foo" },
315 { .d_type = DT_DIR, .d_name = "titi" },
316 { .d_type = DT_UNKNOWN, .d_name = "tutu" },
319 int *p = (int *)dirp;
320 struct dirent *ret = &de[*p];
322 if (!strcmp(ret->d_name, ""))
331 test_closedir(DIR *dirp)
338 test_dirfd(DIR *dirp)
340 int *p = (int *)dirp;
345 test_fstatat(int dirfd, const char *pathname, struct stat *buf,
351 if (!strcmp(pathname, "bar2")) {
352 struct stat st = { .st_mode = S_IFREG };
353 memcpy(buf, &st, sizeof(*buf));
355 } else if (!strcmp(pathname, "tutu")) {
356 struct stat st = { .st_mode = S_IFDIR };
357 memcpy(buf, &st, sizeof(*buf));
366 ec_node_file_override_functions(struct ec_node *gen_node)
368 struct ec_node_file *node = (struct ec_node_file *)gen_node;
370 node->lstat = test_lstat;
371 node->opendir = test_opendir;
372 node->readdir = test_readdir;
373 node->closedir = test_closedir;
374 node->dirfd = test_dirfd;
375 node->fstatat = test_fstatat;
380 static int ec_node_file_testcase(void)
382 struct ec_node *node;
385 node = ec_node("file", EC_NO_ID);
387 EC_LOG(EC_LOG_ERR, "cannot create node\n");
390 ec_node_file_override_functions(node);
392 /* any string matches */
393 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
394 testres |= EC_TEST_CHECK_PARSE(node, 1, "/tmp/bar");
395 testres |= EC_TEST_CHECK_PARSE(node, -1);
397 /* test completion */
398 testres |= EC_TEST_CHECK_COMPLETE(node,
401 testres |= EC_TEST_CHECK_COMPLETE(node,
402 "/tmp/toto/t", EC_NODE_ENDLIST,
404 testres |= EC_TEST_CHECK_COMPLETE_PARTIAL(node,
405 "/tmp/toto/t", EC_NODE_ENDLIST,
406 "/tmp/toto/titi/", "/tmp/toto/tutu/", EC_NODE_ENDLIST);
407 testres |= EC_TEST_CHECK_COMPLETE(node,
408 "/tmp/toto/f", EC_NODE_ENDLIST,
409 "/tmp/toto/foo", EC_NODE_ENDLIST);
410 testres |= EC_TEST_CHECK_COMPLETE(node,
411 "/tmp/toto/b", EC_NODE_ENDLIST,
412 "/tmp/toto/bar", "/tmp/toto/bar2", EC_NODE_ENDLIST);
420 static struct ec_test ec_node_file_test = {
422 .test = ec_node_file_testcase,
425 EC_TEST_REGISTER(ec_node_file_test);