OSDN Git Service

Update mdev to work around the newest sysfs api breakage in the 2.6.25 kernel.
[android-x86/external-toybox.git] / lib / dirtree.c
1 /* vi: set sw=4 ts=4 :*/
2 /* dirtree.c - Functions for dealing with directory trees.
3  *
4  * Copyright 2007 Rob Landley <rob@landley.net>
5  */
6
7 #include "toys.h"
8
9 // NOTE: This uses toybuf.  Possibly it shouldn't do that.
10
11 // Create a dirtree node from a path.
12
13 struct dirtree *dirtree_add_node(char *path)
14 {
15         struct dirtree *dt;
16         char *name;
17
18         // Find last chunk of name.
19
20         for (;;) {
21                 name = strrchr(path, '/');
22
23                 if (!name) name = path;
24                 else {
25                         if (*(name+1)) name++;
26                         else {
27                                 *name=0;
28                                 continue;
29                         }
30                 }
31                 break;
32         }
33
34         dt = xzalloc(sizeof(struct dirtree)+strlen(name)+1);
35         if (lstat(path, &(dt->st))) {
36                 error_msg("Skipped '%s'",name);
37                 free(dt);
38                 return 0;
39         }
40         strcpy(dt->name, name);
41
42         return dt;
43 }
44
45 // Given a directory (in a writeable PATH_MAX buffer), recursively read in a
46 // directory tree.
47 //
48 // If callback==NULL, allocate tree of struct dirtree and
49 // return root of tree.  Otherwise call callback(node) on each hit, free
50 // structures after use, and return NULL.
51
52 struct dirtree *dirtree_read(char *path, struct dirtree *parent,
53                                         int (*callback)(char *path, struct dirtree *node))
54 {
55         struct dirtree *dt = NULL, **ddt = &dt;
56         DIR *dir;
57         int len = strlen(path);
58
59         if (!(dir = opendir(path))) perror_msg("No %s", path);
60         else for (;;) {
61                 int norecurse = 0;
62                 struct dirent *entry = readdir(dir);
63                 if (!entry) {
64                         closedir(dir);
65                         break;
66                 }
67
68                 // Skip "." and ".."
69                 if (entry->d_name[0]=='.') {
70                         if (!entry->d_name[1]) continue;
71                         if (entry->d_name[1]=='.' && !entry->d_name[2]) continue;
72                 }
73
74                 snprintf(path+len, sizeof(toybuf)-len, "/%s", entry->d_name);
75                 *ddt = dirtree_add_node(path);
76                 if (!*ddt) continue;
77                 (*ddt)->parent = parent;
78                 (*ddt)->depth = parent ? parent->depth + 1 : 1;
79                 if (callback) norecurse = callback(path, *ddt);
80                 if (!norecurse && entry->d_type == DT_DIR)
81                         (*ddt)->child = dirtree_read(path, *ddt, callback);
82                 if (callback) free(*ddt);
83                 else ddt = &((*ddt)->next);
84                 path[len]=0;
85         }
86
87         return dt;
88 }
89
90