blob: 76f132f0651ae887bc79744fcb2553fe9bb5ca12 [file] [log] [blame]
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00001/*
2 * Power debug tool (powerdebug)
Daniel Lezcanoc193b602011-06-08 23:30:00 +02003 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00004 * Copyright (C) 2016, Linaro Limited.
Daniel Lezcanoc193b602011-06-08 23:30:00 +02005 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
Daniel Lezcanoc193b602011-06-08 23:30:00 +020010 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +000011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Daniel Lezcanoc193b602011-06-08 23:30:00 +020015 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +000016 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 */
Daniel Lezcanoc193b602011-06-08 23:30:00 +020021
22#define _GNU_SOURCE
23#include <stdio.h>
24#undef _GNU_SOURCE
25#include <stdlib.h>
Daniel Lezcano25fc4a32011-08-25 15:46:13 +020026#include <stdbool.h>
Daniel Lezcanoc193b602011-06-08 23:30:00 +020027#include <string.h>
28#include <dirent.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <unistd.h>
32
33#include "tree.h"
34
35/*
36 * Allocate a tree structure and initialize the different fields.
37 *
38 * @path : the absolute path to the directory
39 * @depth : the depth in the tree
40 * Returns a tree structure on success, NULL otherwise
41 */
42static inline struct tree *tree_alloc(const char *path, int depth)
43{
44 struct tree *t;
45
46 t = malloc(sizeof(*t));
47 if (!t)
48 return NULL;
49
50 /* Full pathname */
51 t->path = strdup(path);
52 if (!t->path) {
53 free(t);
54 return NULL;
55 }
56
57 /* Basename pointer on the full path name */
58 t->name = strrchr(t->path, '/') + 1;
59
60 t->depth = depth;
61 t->tail = t;
62 t->child = NULL;
63 t->parent = NULL;
64 t->next = NULL;
65 t->prev = NULL;
66 t->private = NULL;
Daniel Lezcanocb86e1d2011-06-08 23:30:01 +020067 t->nrchild = 0;
Daniel Lezcanoc193b602011-06-08 23:30:00 +020068
69 return t;
70}
71
72/*
73 * Free a tree structure and the fields we allocated in the
74 * tree_alloc function.
75 *
76 * @t : the tree structure to be freed
77 */
78static inline void tree_free(struct tree *t)
79{
80 free(t->path);
81 free(t);
82}
83
84/*
85 * Add at the end of the list the new list element.
86 *
87 * @head : the list to be appened
88 * @new : the new element to be added at the end of the list
89 */
90static inline void tree_add_tail(struct tree *head, struct tree *new)
91{
92 new->prev = head->tail;
93 head->tail->next = new;
94 head->tail = new;
95}
96
97/*
98 * Add a child in to a parent list, at the end of this list.
99 *
100 * @parent : the parent list to add the child
101 * @child : the child to be added
102 */
103static inline void tree_add_child(struct tree *parent, struct tree *child)
104{
105 child->parent = parent;
106
107 if (parent->child)
108 return tree_add_tail(parent->child, child);
109
110 parent->child = child;
111}
112
113/*
114 * This function will browse the directory structure and build a
115 * tree reflecting the content of the directory tree.
116 *
117 * @tree : the root node of the tree
118 * @filter : a callback to filter out the directories
119 * Returns 0 on success, -1 otherwise
120 */
Daniel Lezcano25fc4a32011-08-25 15:46:13 +0200121static int tree_scan(struct tree *tree, tree_filter_t filter, bool follow)
Daniel Lezcanoc193b602011-06-08 23:30:00 +0200122{
123 DIR *dir;
124 char *basedir, *newpath;
125 struct dirent dirent, *direntp;
126 struct stat s;
127 int ret = 0;
128
129 dir = opendir(tree->path);
Daniel Lezcanod42d7ad2016-02-22 15:02:57 +0100130 if (!dir)
Daniel Lezcanoc193b602011-06-08 23:30:00 +0200131 return -1;
132
133 while (!readdir_r(dir, &dirent, &direntp)) {
134
135 struct tree *child;
136
137 if (!direntp)
138 break;
139
140 if (direntp->d_name[0] == '.')
141 continue;
142
143 if (filter && filter(direntp->d_name))
144 continue;
145
146 ret = asprintf(&basedir, "%s", tree->path);
147 if (ret < 0)
148 return -1;
149
150 ret = basename(basedir) ? 0 : -1;
151 if (ret < 0)
152 goto out_free_basedir;
153
154 ret = asprintf(&newpath, "%s/%s", basedir, direntp->d_name);
155 if (ret < 0)
156 goto out_free_basedir;
157
158 ret = stat(newpath, &s);
159 if (ret)
160 goto out_free_newpath;
161
Daniel Lezcano25fc4a32011-08-25 15:46:13 +0200162 if (S_ISDIR(s.st_mode) || (S_ISLNK(s.st_mode) && follow)) {
Daniel Lezcanoc193b602011-06-08 23:30:00 +0200163
164 ret = -1;
165
166 child = tree_alloc(newpath, tree->depth + 1);
167 if (!child)
168 goto out_free_newpath;
169
170 tree_add_child(tree, child);
171
Daniel Lezcanocb86e1d2011-06-08 23:30:01 +0200172 tree->nrchild++;
173
Daniel Lezcano25fc4a32011-08-25 15:46:13 +0200174 ret = tree_scan(child, filter, follow);
Daniel Lezcanoc193b602011-06-08 23:30:00 +0200175 }
176
177 out_free_newpath:
178 free(newpath);
179
180 out_free_basedir:
181 free(basedir);
182
183 if (ret)
184 break;
185 }
186
187 closedir(dir);
188
189 return ret;
190}
191
192/*
193 * This function takes the topmost directory path and populate the
194 * directory tree structures.
195 *
196 * @tree : a path to the topmost directory path
197 * Returns a tree structure corresponding to the root node of the
198 * directory tree representation on success, NULL otherwise
199 */
Daniel Lezcano25fc4a32011-08-25 15:46:13 +0200200struct tree *tree_load(const char *path, tree_filter_t filter, bool follow)
Daniel Lezcanoc193b602011-06-08 23:30:00 +0200201{
202 struct tree *tree;
203
204 tree = tree_alloc(path, 0);
205 if (!tree)
206 return NULL;
207
Daniel Lezcano25fc4a32011-08-25 15:46:13 +0200208 if (tree_scan(tree, filter, follow)) {
Daniel Lezcanoc193b602011-06-08 23:30:00 +0200209 tree_free(tree);
210 return NULL;
211 }
212
213 return tree;
214}
215
216/*
217 * This function will go over the tree passed as parameter and
218 * will call the callback passed as parameter for each node.
219 *
220 * @tree : the topmost node where we begin to browse the tree
221 * Returns 0 on success, < 0 otherwise
222 */
223int tree_for_each(struct tree *tree, tree_cb_t cb, void *data)
224{
225 if (!tree)
226 return 0;
227
228 if (cb(tree, data))
229 return -1;
230
231 if (tree_for_each(tree->child, cb, data))
232 return -1;
233
234 return tree_for_each(tree->next, cb, data);
235}
Daniel Lezcano357dd8a2011-06-08 23:30:00 +0200236
Daniel Lezcano02f8f402011-06-08 23:30:01 +0200237/*
238 * This function will go over the tree passed as parameter at the reverse
239 * order and will call the callback passed as parameter for each.
240 * @tree : the lower node where we begin to browse the tree at the reverse
241 * order
242 * cb : a callback for each node the function will go over
243 * data : some private data to be passed across the callbacks
244 * Returns 0 on success, < 0 otherwise
245 */
Daniel Lezcano6d42e812011-06-08 23:30:01 +0200246int tree_for_each_reverse(struct tree *tree, tree_cb_t cb, void *data)
247{
248 if (!tree)
249 return 0;
250
251 if (cb(tree, data))
252 return -1;
253
254 if (tree_for_each_reverse(tree->prev, cb, data))
255 return -1;
256
257 return tree_for_each_reverse(tree->parent, cb, data);
258}
259
Daniel Lezcano02f8f402011-06-08 23:30:01 +0200260
261/*
262 * The function will go over all the parent of the specified node passed
263 * as parameter.
264 * @tree : the child node from where we back path to the parent
265 * cb : a callback for each node the function will go over
266 * data : some private data to be passed across the callbacks
267 * Returns 0 on success, < 0 otherwise
268 */
Daniel Lezcanoafe62252011-06-08 23:30:00 +0200269int tree_for_each_parent(struct tree *tree, tree_cb_t cb, void *data)
270{
271 if (!tree)
272 return 0;
273
274 if (tree_for_each_parent(tree->parent, cb, data))
275 return -1;
276
277 return cb(tree, data);
278}
279
Daniel Lezcano02f8f402011-06-08 23:30:01 +0200280/*
281 * The function will return the first node which match with the name as
282 * parameter.
283 * @tree : the tree where we begin to find
284 * @name : the name of the node the function must look for.
285 * Returns a pointer to the tree structure if found, NULL otherwise.
286 */
Daniel Lezcano357dd8a2011-06-08 23:30:00 +0200287struct tree *tree_find(struct tree *tree, const char *name)
288{
289 struct tree *t;
290
291 if (!tree)
292 return NULL;
293
294 if (!strcmp(tree->name, name))
295 return tree;
296
297 t = tree_find(tree->child, name);
298 if (t)
299 return t;
300
301 return tree_find(tree->next, name);
302}
Daniel Lezcanofabe20a2011-06-08 23:30:01 +0200303
304struct struct_find {
305 int nr;
306 const char *name;
307 struct tree ***ptree;
308};
309
310static int tree_finds_cb(struct tree *tree, void *data)
311{
312 struct struct_find *sf = data;
313
Daniel Lezcano528bb3f2011-06-21 00:57:08 +0200314 if (!strlen(sf->name))
315 return 0;
316
Daniel Lezcanofabe20a2011-06-08 23:30:01 +0200317 if (strncmp(sf->name, tree->name, strlen(sf->name)))
318 return 0;
319
320 if (sf->ptree)
321 (*(sf->ptree))[sf->nr] = tree;
322
323 sf->nr++;
324
325 return 0;
326}
327
Daniel Lezcano02f8f402011-06-08 23:30:01 +0200328/*
329 * This function will search for all the nodes where the name begin
330 * with the name passed as parameter. *Note* the function allocates
331 * the array, it is up to the caller to free this array.
332 * @tree : the topmost node of the tree where we being to search
333 * @name : the name to find in the tree
334 * @ptr : a pointer to a pointer of pointer of tree structure :)
335 * Returns the number of elements found in the tree, < 0 if something
336 * went wrong.
337 */
Daniel Lezcanofabe20a2011-06-08 23:30:01 +0200338int tree_finds(struct tree *tree, const char *name, struct tree ***ptr)
339{
340 struct struct_find sf = { .nr = 0, .ptree = NULL, .name = name };
341 int nmatch;
342
343 /* first pass : count # of matching nodes */
344 tree_for_each(tree, tree_finds_cb, &sf);
345
346 /* no match */
347 if (!sf.nr)
348 return 0;
349
350 *ptr = malloc(sizeof(struct tree *) * sf.nr);
351 if (!*ptr)
352 return -1;
353
354 /* store the result as it will be overwritten by the next call */
355 nmatch = sf.nr;
356 sf.nr = 0;
357 sf.ptree = ptr;
358
359 /* second pass : fill with the matching nodes */
360 tree_for_each(tree, tree_finds_cb, &sf);
361
362 return nmatch;
363}