blob: dd53ff22c3a8ef48e24764a3eb9a9b0dd99202ff [file] [log] [blame]
Daniel Lezcanoc193b602011-06-08 23:30:00 +02001/*******************************************************************************
2 * Copyright (C) 2010, Linaro Limited.
3 *
4 * This file is part of PowerDebug.
5 *
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 * Author:
12 * Daniel Lezcano <daniel.lezcano@linaro.org>
13 *
14 *******************************************************************************/
15
16#define _GNU_SOURCE
17#include <stdio.h>
18#undef _GNU_SOURCE
19#include <stdlib.h>
20#include <string.h>
21#include <dirent.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25
26#include "tree.h"
27
28/*
29 * Allocate a tree structure and initialize the different fields.
30 *
31 * @path : the absolute path to the directory
32 * @depth : the depth in the tree
33 * Returns a tree structure on success, NULL otherwise
34 */
35static inline struct tree *tree_alloc(const char *path, int depth)
36{
37 struct tree *t;
38
39 t = malloc(sizeof(*t));
40 if (!t)
41 return NULL;
42
43 /* Full pathname */
44 t->path = strdup(path);
45 if (!t->path) {
46 free(t);
47 return NULL;
48 }
49
50 /* Basename pointer on the full path name */
51 t->name = strrchr(t->path, '/') + 1;
52
53 t->depth = depth;
54 t->tail = t;
55 t->child = NULL;
56 t->parent = NULL;
57 t->next = NULL;
58 t->prev = NULL;
59 t->private = NULL;
Daniel Lezcanocb86e1d2011-06-08 23:30:01 +020060 t->nrchild = 0;
Daniel Lezcanoc193b602011-06-08 23:30:00 +020061
62 return t;
63}
64
65/*
66 * Free a tree structure and the fields we allocated in the
67 * tree_alloc function.
68 *
69 * @t : the tree structure to be freed
70 */
71static inline void tree_free(struct tree *t)
72{
73 free(t->path);
74 free(t);
75}
76
77/*
78 * Add at the end of the list the new list element.
79 *
80 * @head : the list to be appened
81 * @new : the new element to be added at the end of the list
82 */
83static inline void tree_add_tail(struct tree *head, struct tree *new)
84{
85 new->prev = head->tail;
86 head->tail->next = new;
87 head->tail = new;
88}
89
90/*
91 * Add a child in to a parent list, at the end of this list.
92 *
93 * @parent : the parent list to add the child
94 * @child : the child to be added
95 */
96static inline void tree_add_child(struct tree *parent, struct tree *child)
97{
98 child->parent = parent;
99
100 if (parent->child)
101 return tree_add_tail(parent->child, child);
102
103 parent->child = child;
104}
105
106/*
107 * This function will browse the directory structure and build a
108 * tree reflecting the content of the directory tree.
109 *
110 * @tree : the root node of the tree
111 * @filter : a callback to filter out the directories
112 * Returns 0 on success, -1 otherwise
113 */
114static int tree_scan(struct tree *tree, tree_filter_t filter)
115{
116 DIR *dir;
117 char *basedir, *newpath;
118 struct dirent dirent, *direntp;
119 struct stat s;
120 int ret = 0;
121
122 dir = opendir(tree->path);
123 if (!dir)
124 return -1;
125
126 while (!readdir_r(dir, &dirent, &direntp)) {
127
128 struct tree *child;
129
130 if (!direntp)
131 break;
132
133 if (direntp->d_name[0] == '.')
134 continue;
135
136 if (filter && filter(direntp->d_name))
137 continue;
138
139 ret = asprintf(&basedir, "%s", tree->path);
140 if (ret < 0)
141 return -1;
142
143 ret = basename(basedir) ? 0 : -1;
144 if (ret < 0)
145 goto out_free_basedir;
146
147 ret = asprintf(&newpath, "%s/%s", basedir, direntp->d_name);
148 if (ret < 0)
149 goto out_free_basedir;
150
151 ret = stat(newpath, &s);
152 if (ret)
153 goto out_free_newpath;
154
155 if (S_ISDIR(s.st_mode)) {
156
157 ret = -1;
158
159 child = tree_alloc(newpath, tree->depth + 1);
160 if (!child)
161 goto out_free_newpath;
162
163 tree_add_child(tree, child);
164
Daniel Lezcanocb86e1d2011-06-08 23:30:01 +0200165 tree->nrchild++;
166
Daniel Lezcanoc193b602011-06-08 23:30:00 +0200167 ret = tree_scan(child, filter);
168 }
169
170 out_free_newpath:
171 free(newpath);
172
173 out_free_basedir:
174 free(basedir);
175
176 if (ret)
177 break;
178 }
179
180 closedir(dir);
181
182 return ret;
183}
184
185/*
186 * This function takes the topmost directory path and populate the
187 * directory tree structures.
188 *
189 * @tree : a path to the topmost directory path
190 * Returns a tree structure corresponding to the root node of the
191 * directory tree representation on success, NULL otherwise
192 */
193struct tree *tree_load(const char *path, tree_filter_t filter)
194{
195 struct tree *tree;
196
197 tree = tree_alloc(path, 0);
198 if (!tree)
199 return NULL;
200
201 if (tree_scan(tree, filter)) {
202 tree_free(tree);
203 return NULL;
204 }
205
206 return tree;
207}
208
209/*
210 * This function will go over the tree passed as parameter and
211 * will call the callback passed as parameter for each node.
212 *
213 * @tree : the topmost node where we begin to browse the tree
214 * Returns 0 on success, < 0 otherwise
215 */
216int tree_for_each(struct tree *tree, tree_cb_t cb, void *data)
217{
218 if (!tree)
219 return 0;
220
221 if (cb(tree, data))
222 return -1;
223
224 if (tree_for_each(tree->child, cb, data))
225 return -1;
226
227 return tree_for_each(tree->next, cb, data);
228}
Daniel Lezcano357dd8a2011-06-08 23:30:00 +0200229
Daniel Lezcano6d42e812011-06-08 23:30:01 +0200230int tree_for_each_reverse(struct tree *tree, tree_cb_t cb, void *data)
231{
232 if (!tree)
233 return 0;
234
235 if (cb(tree, data))
236 return -1;
237
238 if (tree_for_each_reverse(tree->prev, cb, data))
239 return -1;
240
241 return tree_for_each_reverse(tree->parent, cb, data);
242}
243
Daniel Lezcanoafe62252011-06-08 23:30:00 +0200244int tree_for_each_parent(struct tree *tree, tree_cb_t cb, void *data)
245{
246 if (!tree)
247 return 0;
248
249 if (tree_for_each_parent(tree->parent, cb, data))
250 return -1;
251
252 return cb(tree, data);
253}
254
Daniel Lezcano357dd8a2011-06-08 23:30:00 +0200255struct tree *tree_find(struct tree *tree, const char *name)
256{
257 struct tree *t;
258
259 if (!tree)
260 return NULL;
261
262 if (!strcmp(tree->name, name))
263 return tree;
264
265 t = tree_find(tree->child, name);
266 if (t)
267 return t;
268
269 return tree_find(tree->next, name);
270}
Daniel Lezcanofabe20a2011-06-08 23:30:01 +0200271
272struct struct_find {
273 int nr;
274 const char *name;
275 struct tree ***ptree;
276};
277
278static int tree_finds_cb(struct tree *tree, void *data)
279{
280 struct struct_find *sf = data;
281
282 if (strncmp(sf->name, tree->name, strlen(sf->name)))
283 return 0;
284
285 if (sf->ptree)
286 (*(sf->ptree))[sf->nr] = tree;
287
288 sf->nr++;
289
290 return 0;
291}
292
293int tree_finds(struct tree *tree, const char *name, struct tree ***ptr)
294{
295 struct struct_find sf = { .nr = 0, .ptree = NULL, .name = name };
296 int nmatch;
297
298 /* first pass : count # of matching nodes */
299 tree_for_each(tree, tree_finds_cb, &sf);
300
301 /* no match */
302 if (!sf.nr)
303 return 0;
304
305 *ptr = malloc(sizeof(struct tree *) * sf.nr);
306 if (!*ptr)
307 return -1;
308
309 /* store the result as it will be overwritten by the next call */
310 nmatch = sf.nr;
311 sf.nr = 0;
312 sf.ptree = ptr;
313
314 /* second pass : fill with the matching nodes */
315 tree_for_each(tree, tree_finds_cb, &sf);
316
317 return nmatch;
318}