OSDN Git Service

Implement DIRTREE_SYMFOLLOW and ls -cSHL.
[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 // Create a dirtree node from a path, with stat and symlink info.
10 // (This doesn't open directory filehandles yet so as not to exhaust the
11 // filehandle space on large trees. handle_callback() does that instead.)
12
13 struct dirtree *dirtree_add_node(int dirfd, char *name, int symfollow)
14 {
15         struct dirtree *dt = NULL;
16         struct stat st;
17         char buf[4096];
18         int len = 0, linklen = 0;
19
20         if (name) {
21                 if (fstatat(dirfd, name, &st, symfollow ? 0 : AT_SYMLINK_NOFOLLOW))
22                         goto error;
23                 if (S_ISLNK(st.st_mode)) {
24                         if (0>(linklen = readlinkat(dirfd, name, buf, 4095))) goto error;
25                         buf[linklen++]=0;
26                 }
27                 len = strlen(name);
28         }
29         dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
30         if (name) {
31                 memcpy(&(dt->st), &st, sizeof(struct stat));
32                 strcpy(dt->name, name);
33
34                 if (linklen) {
35                         dt->symlink = memcpy(len+(char *)dt, buf, linklen);
36                         dt->data = --linklen;
37                 }
38         }
39
40         return dt;
41
42 error:
43         perror_msg("%s",name);
44         free(dt);
45         return 0;
46 }
47
48 // Return path to this node, assembled recursively.
49
50 char *dirtree_path(struct dirtree *node, int *plen)
51 {
52         char *path;
53         int len;
54
55         if (!node || !node->name) {
56                 path = xmalloc(*plen);
57                 *plen = 0;
58                 return path;
59         }
60
61         len = (plen ? *plen : 0)+strlen(node->name)+1;
62         path = dirtree_path(node->parent, &len);
63         if (len) path[len++]='/';
64         len = (stpcpy(path+len, node->name) - path);
65         if (plen) *plen = len;
66
67         return path;
68 }
69
70 // Default callback, filters out "." and "..".
71
72 int dirtree_notdotdot(struct dirtree *catch)
73 {
74         // Should we skip "." and ".."?
75         if (catch->name[0]=='.' && (!catch->name[1] ||
76                         (catch->name[1]=='.' && !catch->name[2])))
77                                 return 0;
78
79         return DIRTREE_SAVE|DIRTREE_RECURSE;
80 }
81
82 // get open filehandle for node in extra, giving caller the option of
83 // using DIRTREE_COMEAGAIN or not.
84 int dirtree_opennode(struct dirtree *try)
85 {
86         if (!dirtree_notdotdot(try)) return 0;
87         if (S_ISDIR(try->st.st_mode)) {
88                 if (!try->extra) {
89                         try->extra = xdup(try->data);
90                         return DIRTREE_COMEAGAIN;
91                 }
92         } else try->extra = openat(try->parent ? try->parent->data : AT_FDCWD,
93                 try->name, 0);
94
95         return DIRTREE_SAVE|DIRTREE_RECURSE;
96 }
97
98 // Handle callback for a node in the tree. Returns saved node(s) or NULL.
99 //
100 // By default, allocates a tree of struct dirtree, not following symlinks
101 // If callback==NULL, or callback always returns 0, allocate tree of struct
102 // dirtree and return root of tree.  Otherwise call callback(node) on each
103 // hit, free structures after use, and return NULL.
104 //
105
106 struct dirtree *handle_callback(struct dirtree *new,
107                                         int (*callback)(struct dirtree *node))
108 {
109         int flags, dir = S_ISDIR(new->st.st_mode);
110
111         if (!callback) callback = dirtree_notdotdot;
112
113         // Directory always has filehandle for examining contents. Whether or
114         // not we'll recurse into it gets decided later.
115
116         if (dir)
117                 new->data = openat(new->parent ? new->parent->data : AT_FDCWD,
118                         new->name, 0);
119
120         flags = callback(new);
121
122         if (dir) {
123                 if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) {
124                         dirtree_recurse(new, callback, flags & DIRTREE_SYMFOLLOW);
125                         if (flags & DIRTREE_COMEAGAIN) flags = callback(new);
126                 } else close(new->data);
127         }
128
129         // If this had children, it was callback's job to free them already.
130         if (!(flags & DIRTREE_SAVE)) {
131                 free(new);
132                 new = NULL;
133         }
134
135         return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
136 }
137
138 // Recursively read/process children of directory node (with dirfd in data),
139 // filtering through callback().
140
141 void dirtree_recurse(struct dirtree *node,
142                                         int (*callback)(struct dirtree *node), int symfollow)
143 {
144         struct dirtree *new, **ddt = &(node->child);
145         struct dirent *entry;
146         DIR *dir;
147
148         if (!(dir = fdopendir(node->data))) {
149                 char *path = dirtree_path(node, 0);
150                 perror_msg("No %s", path);
151                 free(path);
152                 close(node->data);
153
154                 return;
155         }
156
157         // according to the fddir() man page, the filehandle in the DIR * can still
158         // be externally used by things that don't lseek() it.
159
160         // The extra parentheses are to shut the stupid compiler up.
161         while ((entry = readdir(dir))) {
162                 if (!(new = dirtree_add_node(node->data, entry->d_name, symfollow)))
163                         continue;
164                 new->parent = node;
165                 new = handle_callback(new, callback);
166                 if (new == DIRTREE_ABORTVAL) break;
167                 if (new) {
168                         *ddt = new;
169                         ddt = &((*ddt)->next);
170                 }
171         }
172
173         // This closes filehandle as well, so note it
174         closedir(dir);
175         node->data = -1;
176 }
177
178 // Create dirtree from path, using callback to filter nodes.
179 // If callback == NULL allocate a tree of struct dirtree nodes and return
180 // pointer to root node.
181
182 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
183 {
184         struct dirtree *root = dirtree_add_node(AT_FDCWD, path, 0);
185
186         return root ? handle_callback(root, callback) : DIRTREE_ABORTVAL;
187 }