Daniel Lezcano | c193b60 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 1 | /******************************************************************************* |
| 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> |
Daniel Lezcano | 25fc4a3 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 20 | #include <stdbool.h> |
Daniel Lezcano | c193b60 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <dirent.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include "tree.h" |
| 28 | |
| 29 | /* |
| 30 | * Allocate a tree structure and initialize the different fields. |
| 31 | * |
| 32 | * @path : the absolute path to the directory |
| 33 | * @depth : the depth in the tree |
| 34 | * Returns a tree structure on success, NULL otherwise |
| 35 | */ |
| 36 | static inline struct tree *tree_alloc(const char *path, int depth) |
| 37 | { |
| 38 | struct tree *t; |
| 39 | |
| 40 | t = malloc(sizeof(*t)); |
| 41 | if (!t) |
| 42 | return NULL; |
| 43 | |
| 44 | /* Full pathname */ |
| 45 | t->path = strdup(path); |
| 46 | if (!t->path) { |
| 47 | free(t); |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | /* Basename pointer on the full path name */ |
| 52 | t->name = strrchr(t->path, '/') + 1; |
| 53 | |
| 54 | t->depth = depth; |
| 55 | t->tail = t; |
| 56 | t->child = NULL; |
| 57 | t->parent = NULL; |
| 58 | t->next = NULL; |
| 59 | t->prev = NULL; |
| 60 | t->private = NULL; |
Daniel Lezcano | cb86e1d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 61 | t->nrchild = 0; |
Daniel Lezcano | c193b60 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 62 | |
| 63 | return t; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Free a tree structure and the fields we allocated in the |
| 68 | * tree_alloc function. |
| 69 | * |
| 70 | * @t : the tree structure to be freed |
| 71 | */ |
| 72 | static inline void tree_free(struct tree *t) |
| 73 | { |
| 74 | free(t->path); |
| 75 | free(t); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Add at the end of the list the new list element. |
| 80 | * |
| 81 | * @head : the list to be appened |
| 82 | * @new : the new element to be added at the end of the list |
| 83 | */ |
| 84 | static inline void tree_add_tail(struct tree *head, struct tree *new) |
| 85 | { |
| 86 | new->prev = head->tail; |
| 87 | head->tail->next = new; |
| 88 | head->tail = new; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Add a child in to a parent list, at the end of this list. |
| 93 | * |
| 94 | * @parent : the parent list to add the child |
| 95 | * @child : the child to be added |
| 96 | */ |
| 97 | static inline void tree_add_child(struct tree *parent, struct tree *child) |
| 98 | { |
| 99 | child->parent = parent; |
| 100 | |
| 101 | if (parent->child) |
| 102 | return tree_add_tail(parent->child, child); |
| 103 | |
| 104 | parent->child = child; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * This function will browse the directory structure and build a |
| 109 | * tree reflecting the content of the directory tree. |
| 110 | * |
| 111 | * @tree : the root node of the tree |
| 112 | * @filter : a callback to filter out the directories |
| 113 | * Returns 0 on success, -1 otherwise |
| 114 | */ |
Daniel Lezcano | 25fc4a3 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 115 | static int tree_scan(struct tree *tree, tree_filter_t filter, bool follow) |
Daniel Lezcano | c193b60 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 116 | { |
| 117 | DIR *dir; |
| 118 | char *basedir, *newpath; |
| 119 | struct dirent dirent, *direntp; |
| 120 | struct stat s; |
| 121 | int ret = 0; |
| 122 | |
| 123 | dir = opendir(tree->path); |
| 124 | if (!dir) |
| 125 | return -1; |
| 126 | |
| 127 | while (!readdir_r(dir, &dirent, &direntp)) { |
| 128 | |
| 129 | struct tree *child; |
| 130 | |
| 131 | if (!direntp) |
| 132 | break; |
| 133 | |
| 134 | if (direntp->d_name[0] == '.') |
| 135 | continue; |
| 136 | |
| 137 | if (filter && filter(direntp->d_name)) |
| 138 | continue; |
| 139 | |
| 140 | ret = asprintf(&basedir, "%s", tree->path); |
| 141 | if (ret < 0) |
| 142 | return -1; |
| 143 | |
| 144 | ret = basename(basedir) ? 0 : -1; |
| 145 | if (ret < 0) |
| 146 | goto out_free_basedir; |
| 147 | |
| 148 | ret = asprintf(&newpath, "%s/%s", basedir, direntp->d_name); |
| 149 | if (ret < 0) |
| 150 | goto out_free_basedir; |
| 151 | |
| 152 | ret = stat(newpath, &s); |
| 153 | if (ret) |
| 154 | goto out_free_newpath; |
| 155 | |
Daniel Lezcano | 25fc4a3 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 156 | if (S_ISDIR(s.st_mode) || (S_ISLNK(s.st_mode) && follow)) { |
Daniel Lezcano | c193b60 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 157 | |
| 158 | ret = -1; |
| 159 | |
| 160 | child = tree_alloc(newpath, tree->depth + 1); |
| 161 | if (!child) |
| 162 | goto out_free_newpath; |
| 163 | |
| 164 | tree_add_child(tree, child); |
| 165 | |
Daniel Lezcano | cb86e1d | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 166 | tree->nrchild++; |
| 167 | |
Daniel Lezcano | 25fc4a3 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 168 | ret = tree_scan(child, filter, follow); |
Daniel Lezcano | c193b60 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | out_free_newpath: |
| 172 | free(newpath); |
| 173 | |
| 174 | out_free_basedir: |
| 175 | free(basedir); |
| 176 | |
| 177 | if (ret) |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | closedir(dir); |
| 182 | |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * This function takes the topmost directory path and populate the |
| 188 | * directory tree structures. |
| 189 | * |
| 190 | * @tree : a path to the topmost directory path |
| 191 | * Returns a tree structure corresponding to the root node of the |
| 192 | * directory tree representation on success, NULL otherwise |
| 193 | */ |
Daniel Lezcano | 25fc4a3 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 194 | struct tree *tree_load(const char *path, tree_filter_t filter, bool follow) |
Daniel Lezcano | c193b60 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 195 | { |
| 196 | struct tree *tree; |
| 197 | |
| 198 | tree = tree_alloc(path, 0); |
| 199 | if (!tree) |
| 200 | return NULL; |
| 201 | |
Daniel Lezcano | 25fc4a3 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 202 | if (tree_scan(tree, filter, follow)) { |
Daniel Lezcano | c193b60 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 203 | tree_free(tree); |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | return tree; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * This function will go over the tree passed as parameter and |
| 212 | * will call the callback passed as parameter for each node. |
| 213 | * |
| 214 | * @tree : the topmost node where we begin to browse the tree |
| 215 | * Returns 0 on success, < 0 otherwise |
| 216 | */ |
| 217 | int tree_for_each(struct tree *tree, tree_cb_t cb, void *data) |
| 218 | { |
| 219 | if (!tree) |
| 220 | return 0; |
| 221 | |
| 222 | if (cb(tree, data)) |
| 223 | return -1; |
| 224 | |
| 225 | if (tree_for_each(tree->child, cb, data)) |
| 226 | return -1; |
| 227 | |
| 228 | return tree_for_each(tree->next, cb, data); |
| 229 | } |
Daniel Lezcano | 357dd8a | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 230 | |
Daniel Lezcano | 02f8f40 | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 231 | /* |
| 232 | * This function will go over the tree passed as parameter at the reverse |
| 233 | * order and will call the callback passed as parameter for each. |
| 234 | * @tree : the lower node where we begin to browse the tree at the reverse |
| 235 | * order |
| 236 | * cb : a callback for each node the function will go over |
| 237 | * data : some private data to be passed across the callbacks |
| 238 | * Returns 0 on success, < 0 otherwise |
| 239 | */ |
Daniel Lezcano | 6d42e81 | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 240 | int tree_for_each_reverse(struct tree *tree, tree_cb_t cb, void *data) |
| 241 | { |
| 242 | if (!tree) |
| 243 | return 0; |
| 244 | |
| 245 | if (cb(tree, data)) |
| 246 | return -1; |
| 247 | |
| 248 | if (tree_for_each_reverse(tree->prev, cb, data)) |
| 249 | return -1; |
| 250 | |
| 251 | return tree_for_each_reverse(tree->parent, cb, data); |
| 252 | } |
| 253 | |
Daniel Lezcano | 02f8f40 | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 254 | |
| 255 | /* |
| 256 | * The function will go over all the parent of the specified node passed |
| 257 | * as parameter. |
| 258 | * @tree : the child node from where we back path to the parent |
| 259 | * cb : a callback for each node the function will go over |
| 260 | * data : some private data to be passed across the callbacks |
| 261 | * Returns 0 on success, < 0 otherwise |
| 262 | */ |
Daniel Lezcano | afe6225 | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 263 | int tree_for_each_parent(struct tree *tree, tree_cb_t cb, void *data) |
| 264 | { |
| 265 | if (!tree) |
| 266 | return 0; |
| 267 | |
| 268 | if (tree_for_each_parent(tree->parent, cb, data)) |
| 269 | return -1; |
| 270 | |
| 271 | return cb(tree, data); |
| 272 | } |
| 273 | |
Daniel Lezcano | 02f8f40 | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 274 | /* |
| 275 | * The function will return the first node which match with the name as |
| 276 | * parameter. |
| 277 | * @tree : the tree where we begin to find |
| 278 | * @name : the name of the node the function must look for. |
| 279 | * Returns a pointer to the tree structure if found, NULL otherwise. |
| 280 | */ |
Daniel Lezcano | 357dd8a | 2011-06-08 23:30:00 +0200 | [diff] [blame] | 281 | struct tree *tree_find(struct tree *tree, const char *name) |
| 282 | { |
| 283 | struct tree *t; |
| 284 | |
| 285 | if (!tree) |
| 286 | return NULL; |
| 287 | |
| 288 | if (!strcmp(tree->name, name)) |
| 289 | return tree; |
| 290 | |
| 291 | t = tree_find(tree->child, name); |
| 292 | if (t) |
| 293 | return t; |
| 294 | |
| 295 | return tree_find(tree->next, name); |
| 296 | } |
Daniel Lezcano | fabe20a | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 297 | |
| 298 | struct struct_find { |
| 299 | int nr; |
| 300 | const char *name; |
| 301 | struct tree ***ptree; |
| 302 | }; |
| 303 | |
| 304 | static int tree_finds_cb(struct tree *tree, void *data) |
| 305 | { |
| 306 | struct struct_find *sf = data; |
| 307 | |
Daniel Lezcano | 528bb3f | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 308 | if (!strlen(sf->name)) |
| 309 | return 0; |
| 310 | |
Daniel Lezcano | fabe20a | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 311 | if (strncmp(sf->name, tree->name, strlen(sf->name))) |
| 312 | return 0; |
| 313 | |
| 314 | if (sf->ptree) |
| 315 | (*(sf->ptree))[sf->nr] = tree; |
| 316 | |
| 317 | sf->nr++; |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Daniel Lezcano | 02f8f40 | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 322 | /* |
| 323 | * This function will search for all the nodes where the name begin |
| 324 | * with the name passed as parameter. *Note* the function allocates |
| 325 | * the array, it is up to the caller to free this array. |
| 326 | * @tree : the topmost node of the tree where we being to search |
| 327 | * @name : the name to find in the tree |
| 328 | * @ptr : a pointer to a pointer of pointer of tree structure :) |
| 329 | * Returns the number of elements found in the tree, < 0 if something |
| 330 | * went wrong. |
| 331 | */ |
Daniel Lezcano | fabe20a | 2011-06-08 23:30:01 +0200 | [diff] [blame] | 332 | int tree_finds(struct tree *tree, const char *name, struct tree ***ptr) |
| 333 | { |
| 334 | struct struct_find sf = { .nr = 0, .ptree = NULL, .name = name }; |
| 335 | int nmatch; |
| 336 | |
| 337 | /* first pass : count # of matching nodes */ |
| 338 | tree_for_each(tree, tree_finds_cb, &sf); |
| 339 | |
| 340 | /* no match */ |
| 341 | if (!sf.nr) |
| 342 | return 0; |
| 343 | |
| 344 | *ptr = malloc(sizeof(struct tree *) * sf.nr); |
| 345 | if (!*ptr) |
| 346 | return -1; |
| 347 | |
| 348 | /* store the result as it will be overwritten by the next call */ |
| 349 | nmatch = sf.nr; |
| 350 | sf.nr = 0; |
| 351 | sf.ptree = ptr; |
| 352 | |
| 353 | /* second pass : fill with the matching nodes */ |
| 354 | tree_for_each(tree, tree_finds_cb, &sf); |
| 355 | |
| 356 | return nmatch; |
| 357 | } |