enhance dumps
[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 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <dirent.h>
36
37 #include <ecoli_log.h>
38 #include <ecoli_malloc.h>
39 #include <ecoli_test.h>
40 #include <ecoli_strvec.h>
41 #include <ecoli_string.h>
42 #include <ecoli_node.h>
43 #include <ecoli_parsed.h>
44 #include <ecoli_completed.h>
45 #include <ecoli_node_file.h>
46
47 EC_LOG_TYPE_REGISTER(node_file);
48
49 struct ec_node_file {
50         struct ec_node gen;
51 };
52
53 static int
54 ec_node_file_parse(const struct ec_node *gen_node,
55                 struct ec_parsed *state,
56                 const struct ec_strvec *strvec)
57 {
58         (void)gen_node;
59         (void)state;
60
61         if (ec_strvec_len(strvec) == 0)
62                 return EC_PARSED_NOMATCH;
63
64         return 1;
65 }
66
67 /*
68  * Almost the same than dirname (3) and basename (3) except that:
69  * - it always returns a substring of the given path, which can
70  *   be empty.
71  * - the behavior is different when the path finishes with a '/'
72  * - the path argument is not modified
73  * - the outputs are allocated and must be freed with ec_free().
74  *
75  *   path       dirname   basename       split_path
76  *   /usr/lib   /usr      lib          /usr/     lib
77  *   /usr/      /         usr          /usr/
78  *   usr        .         usr                    usr
79  *   /          /         /            /
80  *   .          .         .                      .
81  *   ..         .         ..                     ..
82  */
83 static int split_path(const char *path, char **dname_p, char **bname_p)
84 {
85         char *last_slash;
86         size_t dirlen;
87         char *dname, *bname;
88
89         *dname_p = NULL;
90         *bname_p = NULL;
91
92         last_slash = strrchr(path, '/');
93         if (last_slash == NULL)
94                 dirlen = 0;
95         else
96                 dirlen = last_slash - path + 1;
97
98         dname = ec_strdup(path);
99         if (dname == NULL)
100                 return -ENOMEM;
101         dname[dirlen] = '\0';
102
103         bname = ec_strdup(path + dirlen);
104         if (bname == NULL) {
105                 ec_free(dname);
106                 return -ENOMEM;
107         }
108
109         *dname_p = dname;
110         *bname_p = bname;
111
112         return 0;
113 }
114
115 static int
116 ec_node_file_complete(const struct ec_node *gen_node,
117                 struct ec_completed *completed,
118                 const struct ec_strvec *strvec)
119 {
120         char *dname = NULL, *bname = NULL, *effective_dir;
121         struct ec_completed_item *item = NULL;
122         enum ec_completed_type type;
123         struct stat st, st2;
124         const char *input;
125         size_t bname_len;
126         struct dirent *de = NULL;
127         DIR *dir = NULL;
128         char *comp_str = NULL;
129         char *disp_str = NULL;
130         int ret;
131         int is_dir = 0;
132
133         /*
134          * Example with this file tree:
135          * /
136          * ├── dir1
137          * │   ├── file1
138          * │   ├── file2
139          * │   └── subdir
140          * │       └── file3
141          * ├── dir2
142          * │   └── file4
143          * └── file5
144          *
145          * Input     Output completions
146          *   /       [dir1/, dir2/, file5]
147          *   /d      [dir1/, dir2/]
148          *   /f      [file5]
149          *   /dir1/  [file1, file2, subdir/]
150          *
151          *
152          *
153          */
154
155         if (ec_strvec_len(strvec) != 1)
156                 goto out;
157
158         input = ec_strvec_val(strvec, 0);
159         ret = split_path(input, &dname, &bname);
160         if (ret < 0) {
161                 ec_completed_free(completed);
162                 completed = NULL;
163                 goto out;
164         }
165
166         if (strcmp(dname, "") == 0)
167                 effective_dir = ".";
168         else
169                 effective_dir = dname;
170
171         ret = lstat(effective_dir, &st);
172         if (ret != 0) {
173                 ret = -errno;
174                 goto out;
175         }
176         if (!S_ISDIR(st.st_mode))
177                 goto out;
178
179         dir = opendir(effective_dir);
180         if (dir == NULL)
181                 goto out;
182
183         bname_len = strlen(bname);
184         while (1) {
185                 de = readdir(dir);
186                 if (de == NULL)
187                         goto out;
188
189                 if (!ec_str_startswith(de->d_name, bname))
190                         continue;
191                 if (bname[0] != '.' && de->d_name[0] == '.')
192                         continue;
193
194                 /* add '/' if it's a dir */
195                 if (de->d_type == DT_DIR) {
196                         is_dir = 1;
197                 } else if (de->d_type == DT_UNKNOWN) {
198                         int dir_fd = dirfd(dir);
199
200                         if (dir_fd < 0)
201                                 goto out;
202                         ret = fstatat(dir_fd, de->d_name, &st2, 0);
203                         if (ret != 0) {
204                                 ret = -errno;
205                                 goto out;
206                         }
207                         if (!S_ISDIR(st2.st_mode))
208                                 goto out;
209                         is_dir = 1;
210                 } else {
211                         is_dir = 0;
212                 }
213
214                 if (is_dir) {
215                         type = EC_COMP_PARTIAL;
216                         if (ec_asprintf(&comp_str, "%s%s/", input,
217                                         &de->d_name[bname_len]) < 0) {
218                                 ret = -errno;
219                                 goto out;
220                         }
221                         if (ec_asprintf(&disp_str, "%s/", de->d_name) < 0) {
222                                 ret = -errno;
223                                 goto out;
224                         }
225                 } else {
226                         type = EC_COMP_FULL;
227                         if (ec_asprintf(&comp_str, "%s%s", input,
228                                         &de->d_name[bname_len]) < 0) {
229                                 ret = -errno;
230                                 goto out;
231                         }
232                         if (ec_asprintf(&disp_str, "%s", de->d_name) < 0) {
233                                 ret = -errno;
234                                 goto out;
235                         }
236                 }
237                 ret = ec_completed_add_item(completed, gen_node, &item,
238                                         type, input, comp_str);
239                 if (ret < 0)
240                         goto out;
241
242                 /* fix the display string: we don't want to display the full
243                  * path. */
244                 ret = ec_completed_item_set_display(item, disp_str);
245                 if (ret < 0)
246                         goto out;
247
248                 item = NULL;
249                 ec_free(comp_str);
250                 comp_str = NULL;
251                 ec_free(disp_str);
252                 disp_str = NULL;
253         }
254         ret = 0;
255
256 out:
257         ec_free(comp_str);
258         ec_free(disp_str);
259         ec_free(dname);
260         ec_free(bname);
261         if (dir != NULL)
262                 closedir(dir);
263
264         return ret;
265 }
266
267 static struct ec_node_type ec_node_file_type = {
268         .name = "file",
269         .parse = ec_node_file_parse,
270         .complete = ec_node_file_complete,
271         .size = sizeof(struct ec_node_file),
272 };
273
274 EC_NODE_TYPE_REGISTER(ec_node_file_type);
275
276 /* LCOV_EXCL_START */
277 static int ec_node_file_testcase(void)
278 {
279         struct ec_node *node;
280         int ret = 0;
281
282         node = ec_node("file", EC_NO_ID);
283         if (node == NULL) {
284                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
285                 return -1;
286         }
287         /* any string matches */
288         ret |= EC_TEST_CHECK_PARSE(node, 1, "foo");
289         ret |= EC_TEST_CHECK_PARSE(node, 1, "/tmp/bar");
290         ret |= EC_TEST_CHECK_PARSE(node, -1);
291
292         /* test completion */
293         ret |= EC_TEST_CHECK_COMPLETE(node,
294                 EC_NODE_ENDLIST,
295                 EC_NODE_ENDLIST);
296         ret |= EC_TEST_CHECK_COMPLETE(node,
297                 "/tmp/toto/t", EC_NODE_ENDLIST,
298                 EC_NODE_ENDLIST);
299         ret |= EC_TEST_CHECK_COMPLETE_PARTIAL(node,
300                 "/tmp/toto/t", EC_NODE_ENDLIST,
301                 "/tmp/toto/titi/", EC_NODE_ENDLIST);
302         ret |= EC_TEST_CHECK_COMPLETE(node,
303                 "/tmp/toto/f", EC_NODE_ENDLIST,
304                 "/tmp/toto/foo", EC_NODE_ENDLIST);
305         ret |= EC_TEST_CHECK_COMPLETE(node,
306                 "/tmp/toto/b", EC_NODE_ENDLIST,
307                 "/tmp/toto/bar", "/tmp/toto/bar2", EC_NODE_ENDLIST);
308
309         ec_node_free(node);
310
311         return ret;
312 }
313 /* LCOV_EXCL_STOP */
314
315 static struct ec_test ec_node_file_test = {
316         .name = "node_file",
317         .test = ec_node_file_testcase,
318 };
319
320 EC_TEST_REGISTER(ec_node_file_test);