sq
[protos/libecoli.git] / lib / ecoli_node_file.c
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #define _GNU_SOURCE /* for asprintf */
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <dirent.h>
37
38 #include <ecoli_log.h>
39 #include <ecoli_malloc.h>
40 #include <ecoli_test.h>
41 #include <ecoli_strvec.h>
42 #include <ecoli_node.h>
43 #include <ecoli_parsed.h>
44 #include <ecoli_completed.h>
45 #include <ecoli_node_file.h>
46
47 struct ec_node_file {
48         struct ec_node gen;
49 };
50
51 static int
52 ec_node_file_parse(const struct ec_node *gen_node,
53                 struct ec_parsed *state,
54                 const struct ec_strvec *strvec)
55 {
56         (void)gen_node;
57         (void)state;
58
59         if (ec_strvec_len(strvec) == 0)
60                 return EC_PARSED_NOMATCH;
61
62         return 1;
63 }
64
65 /*
66  * Almost the same than dirname (3) and basename (3) except that
67  * it always returns a substring of the given path, which can
68  * be empty.
69  * - the behavior is different when the path finishes with a '/'
70  * - the argument is not modified
71  * - the output is allocated and must be freed with ec_free().
72  *
73  *   path       dirname   basename       split_path
74  *   /usr/lib   /usr      lib          /usr/     lib
75  *   /usr/      /         usr          /usr/
76  *   usr        .         usr                    usr
77  *   /          /         /            /
78  *   .          .         .                      .
79  *   ..         .         ..                     ..
80  */
81 static int split_path(const char *path, char **dname_p, char **bname_p)
82 {
83         char *last_slash;
84         size_t dirlen;
85         char *dname, *bname;
86
87         *dname_p = NULL;
88         *bname_p = NULL;
89
90         last_slash = strrchr(path, '/');
91         if (last_slash == NULL)
92                 dirlen = 0;
93         else
94                 dirlen = last_slash - path + 1;
95
96         dname = ec_strdup(path);
97         if (dname == NULL)
98                 return -ENOMEM;
99         dname[dirlen] = '\0';
100
101         bname = ec_strdup(path + dirlen);
102         if (bname == NULL) {
103                 ec_free(dname);
104                 return -ENOMEM;
105         }
106
107         *dname_p = dname;
108         *bname_p = bname;
109
110         return 0;
111 }
112
113 static int
114 ec_node_file_complete(const struct ec_node *gen_node,
115                 struct ec_completed *completed,
116                 struct ec_parsed *state,
117                 const struct ec_strvec *strvec)
118 {
119         struct stat st;
120         const char *path;
121         size_t bname_len;
122         struct dirent *de = NULL;
123         DIR *dir = NULL;
124         char *dname = NULL, *bname = NULL, *effective_dir;
125         char *add = NULL;
126         int ret;
127         int is_dir = 0;
128
129         /*
130          * Example with this file tree:
131          * /
132          * ├── dir1
133          * │   ├── file1
134          * │   ├── file2
135          * │   └── subdir
136          * │       └── file3
137          * ├── dir2
138          * │   └── file4
139          * └── file5
140          *
141          * Input     Output completions
142          *   /       [dir1/, dir2/, file5]
143          *   /d      [dir1/, dir2/]
144          *   /f      [file5]
145          *   /dir1/  [file1, file2, subdir/]
146          *
147          *
148          *
149          */
150
151         if (ec_strvec_len(strvec) != 1)
152                 goto out;
153
154         path = ec_strvec_val(strvec, 0);
155         ret = split_path(path, &dname, &bname);
156         if (ret < 0) {
157                 ec_completed_free(completed);
158                 completed = NULL;
159                 goto out;
160         }
161
162         if (strcmp(dname, "") == 0)
163                 effective_dir = ".";
164         else
165                 effective_dir = dname;
166
167         ret = lstat(effective_dir, &st);
168         if (ret != 0) {
169                 ret = -errno;
170                 goto out;
171         }
172         if (!S_ISDIR(st.st_mode))
173                 goto out;
174
175         dir = opendir(effective_dir);
176         if (dir == NULL)
177                 goto out;
178
179         bname_len = strlen(bname);
180         while (1) {
181                 de = readdir(dir);
182                 if (de == NULL)
183                         goto out;
184
185                 if (strncmp(bname, de->d_name, bname_len))
186                         continue;
187                 if (bname[0] != '.' && de->d_name[0] == '.')
188                         continue;
189
190                 /* add '/' if it's a dir */
191                 if (de->d_type == DT_DIR) {
192                         is_dir = 1;
193                 } else if (de->d_type == DT_UNKNOWN) { // XXX todo
194                 } else {
195                         is_dir = 0;
196                 }
197
198                 if (is_dir) {
199                         if (asprintf(&add, "%s/", &de->d_name[bname_len]) < 0) {
200                                 ret = -errno;
201                                 goto out;
202                         }
203                         if (ec_completed_add_partial_match(
204                                         completed, state, gen_node, add) < 0) {
205                                 ec_completed_free(completed);
206                                 completed = NULL;
207                                 goto out;
208                         }
209                 } else {
210                         if (asprintf(&add, "%s", &de->d_name[bname_len]) < 0) {
211                                 ret = -errno;
212                                 goto out;
213                         }
214                         if (ec_completed_add_match(completed, state, gen_node,
215                                                         add) < 0) {
216                                 ec_completed_free(completed);
217                                 completed = NULL;
218                                 goto out;
219                         }
220                 }
221         }
222         ret = 0;
223
224 out:
225         free(add);
226         ec_free(dname);
227         ec_free(bname);
228         if (dir != NULL)
229                 closedir(dir);
230
231         return ret;
232 }
233
234 static struct ec_node_type ec_node_file_type = {
235         .name = "file",
236         .parse = ec_node_file_parse,
237         .complete = ec_node_file_complete,
238         .size = sizeof(struct ec_node_file),
239 };
240
241 EC_NODE_TYPE_REGISTER(ec_node_file_type);
242
243 /* LCOV_EXCL_START */
244 static int ec_node_file_testcase(void)
245 {
246         struct ec_node *node;
247         int ret = 0;
248
249         node = ec_node("file", NULL);
250         if (node == NULL) {
251                 ec_log(EC_LOG_ERR, "cannot create node\n");
252                 return -1;
253         }
254         /* any string matches */
255         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo");
256         ret |= EC_TEST_CHECK_PARSE(node, 1, "/tmp/bar");
257         ret |= EC_TEST_CHECK_PARSE(node, -1);
258
259         /* test completion */
260 #if 0 // XXX how to properly test file completion?
261         ret |= EC_TEST_CHECK_COMPLETE(node,
262                 EC_NODE_ENDLIST,
263                 EC_NODE_ENDLIST,
264                 "");
265         ret |= EC_TEST_CHECK_COMPLETE(node,
266                 "", EC_NODE_ENDLIST,
267                 EC_NODE_ENDLIST,
268                 "");
269         ret |= EC_TEST_CHECK_COMPLETE(node,
270                 "/", EC_NODE_ENDLIST,
271                 EC_NODE_ENDLIST,
272                 "");
273         ret |= EC_TEST_CHECK_COMPLETE(node,
274                 "/tmp", EC_NODE_ENDLIST,
275                 EC_NODE_ENDLIST,
276                 "");
277         ret |= EC_TEST_CHECK_COMPLETE(node,
278                 "/tmp/", EC_NODE_ENDLIST,
279                 EC_NODE_ENDLIST,
280                 "");
281         ret |= EC_TEST_CHECK_COMPLETE(node,
282                 "/tmp/.", EC_NODE_ENDLIST,
283                 EC_NODE_ENDLIST,
284                 "");
285 #endif
286         ec_node_free(node);
287
288         return ret;
289 }
290 /* LCOV_EXCL_STOP */
291
292 static struct ec_test ec_node_file_test = {
293         .name = "node_file",
294         .test = ec_node_file_testcase,
295 };
296
297 EC_TEST_REGISTER(ec_node_file_test);