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);
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,
38 ec_node_file_parse(const struct ec_node *node,
39 struct ec_pnode *state,
40 const struct ec_strvec *strvec)
45 if (ec_strvec_len(strvec) == 0)
46 return EC_PARSE_NOMATCH;
52 * Almost the same than dirname (3) and basename (3) except that:
53 * - it always returns a substring of the given path, which can
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().
59 * path dirname basename split_path
60 * /usr/lib /usr lib /usr/ lib
67 static int split_path(const char *path, char **dname_p, char **bname_p)
76 last_slash = strrchr(path, '/');
77 if (last_slash == NULL)
80 dirlen = last_slash - path + 1;
82 dname = ec_strdup(path);
87 bname = ec_strdup(path + dirlen);
100 ec_node_file_complete(const struct ec_node *node,
101 struct ec_comp *comp,
102 const struct ec_strvec *strvec)
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;
111 struct dirent *de = NULL;
113 char *comp_str = NULL;
114 char *disp_str = NULL;
118 * Example with this file tree:
129 * Input Output completions
130 * / [dir1/, dir2/, file5]
133 * /dir1/ [file1, file2, subdir/]
139 if (ec_strvec_len(strvec) != 1)
142 input = ec_strvec_val(strvec, 0);
143 if (split_path(input, &dname, &bname) < 0)
146 if (strcmp(dname, "") == 0)
149 effective_dir = dname;
151 if (priv->lstat(effective_dir, &st) < 0)
153 if (!S_ISDIR(st.st_mode))
156 dir = priv->opendir(effective_dir);
160 bname_len = strlen(bname);
162 int save_errno = errno;
165 de = priv->readdir(dir);
175 if (!ec_str_startswith(de->d_name, bname))
177 if (bname[0] != '.' && de->d_name[0] == '.')
180 /* add '/' if it's a dir */
181 if (de->d_type == DT_DIR) {
183 } else if (de->d_type == DT_UNKNOWN) {
184 int dir_fd = priv->dirfd(dir);
188 if (priv->fstatat(dir_fd, de->d_name, &st2, 0) < 0)
190 if (S_ISDIR(st2.st_mode))
199 type = EC_COMP_PARTIAL;
200 if (ec_asprintf(&comp_str, "%s%s/", input,
201 &de->d_name[bname_len]) < 0)
203 if (ec_asprintf(&disp_str, "%s/", de->d_name) < 0)
207 if (ec_asprintf(&comp_str, "%s%s", input,
208 &de->d_name[bname_len]) < 0)
210 if (ec_asprintf(&disp_str, "%s", de->d_name) < 0)
213 if (ec_comp_add_item(comp, node, &item,
214 type, input, comp_str) < 0)
217 /* fix the display string: we don't want to display the full
219 if (ec_comp_item_set_display(item, disp_str) < 0)
250 ec_node_file_init_priv(struct ec_node *node)
252 struct ec_node_file *priv = ec_node_priv(node);
255 priv->opendir = opendir;
256 priv->readdir = readdir;
258 priv->fstatat = fstatat;
263 static struct ec_node_type ec_node_file_type = {
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,
271 EC_NODE_TYPE_REGISTER(ec_node_file_type);
273 /* LCOV_EXCL_START */
275 test_lstat(const char *pathname, struct stat *buf)
277 if (!strcmp(pathname, "/tmp/toto/")) {
278 struct stat st = { .st_mode = S_IFDIR };
279 memcpy(buf, &st, sizeof(*buf));
288 test_opendir(const char *name)
292 if (strcmp(name, "/tmp/toto/")) {
297 p = malloc(sizeof(int));
304 static struct dirent *
305 test_readdir(DIR *dirp)
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" },
317 int *p = (int *)dirp;
318 struct dirent *ret = &de[*p];
320 if (!strcmp(ret->d_name, ""))
329 test_closedir(DIR *dirp)
336 test_dirfd(DIR *dirp)
338 int *p = (int *)dirp;
343 test_fstatat(int dirfd, const char *pathname, struct stat *buf,
349 if (!strcmp(pathname, "bar2")) {
350 struct stat st = { .st_mode = S_IFREG };
351 memcpy(buf, &st, sizeof(*buf));
353 } else if (!strcmp(pathname, "tutu")) {
354 struct stat st = { .st_mode = S_IFDIR };
355 memcpy(buf, &st, sizeof(*buf));
364 ec_node_file_override_functions(struct ec_node *node)
366 struct ec_node_file *priv = ec_node_priv(node);
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;
378 static int ec_node_file_testcase(void)
380 struct ec_node *node;
383 node = ec_node("file", EC_NO_ID);
385 EC_LOG(EC_LOG_ERR, "cannot create node\n");
388 ec_node_file_override_functions(node);
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);
395 /* test completion */
396 testres |= EC_TEST_CHECK_COMPLETE(node,
399 testres |= EC_TEST_CHECK_COMPLETE(node,
400 "/tmp/toto/t", 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);
418 static struct ec_test ec_node_file_test = {
420 .test = ec_node_file_testcase,
423 EC_TEST_REGISTER(ec_node_file_test);