Mercurial > hg > toybox
comparison lib/dirtree.c @ 601:a6a541b7fc34
Add dirtree_parentfd()
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 16 Jun 2012 15:16:08 -0500 |
parents | fb582378a36a |
children | 8bee9c27c219 |
comparison
equal
deleted
inserted
replaced
600:58d2fd7484bd | 601:a6a541b7fc34 |
---|---|
77 return 0; | 77 return 0; |
78 | 78 |
79 return DIRTREE_SAVE|DIRTREE_RECURSE; | 79 return DIRTREE_SAVE|DIRTREE_RECURSE; |
80 } | 80 } |
81 | 81 |
82 int dirtree_parentfd(struct dirtree *node) | |
83 { | |
84 return node->parent ? node->parent->data : AT_FDCWD; | |
85 } | |
86 | |
82 // get open filehandle for node in extra, giving caller the option of | 87 // get open filehandle for node in extra, giving caller the option of |
83 // using DIRTREE_COMEAGAIN or not. | 88 // using DIRTREE_COMEAGAIN or not. |
84 int dirtree_opennode(struct dirtree *try) | 89 int dirtree_opennode(struct dirtree *try) |
85 { | 90 { |
86 if (!dirtree_notdotdot(try)) return 0; | 91 if (!dirtree_notdotdot(try)) return 0; |
87 if (S_ISDIR(try->st.st_mode)) { | 92 if (S_ISDIR(try->st.st_mode)) { |
88 if (!try->extra) { | 93 if (!try->extra) { |
89 try->extra = xdup(try->data); | 94 try->extra = xdup(try->data); |
90 return DIRTREE_COMEAGAIN; | 95 return DIRTREE_COMEAGAIN; |
91 } | 96 } |
92 } else try->extra = openat(try->parent ? try->parent->data : AT_FDCWD, | 97 } else try->extra = openat(dirtree_parentfd(try), try->name, 0); |
93 try->name, 0); | |
94 | 98 |
95 return DIRTREE_SAVE|DIRTREE_RECURSE; | 99 return DIRTREE_SAVE|DIRTREE_RECURSE; |
96 } | 100 } |
97 | 101 |
98 // Handle callback for a node in the tree. Returns saved node(s) or NULL. | 102 // Handle callback for a node in the tree. Returns saved node(s) or NULL. |
111 if (!callback) callback = dirtree_notdotdot; | 115 if (!callback) callback = dirtree_notdotdot; |
112 | 116 |
113 // Directory always has filehandle for examining contents. Whether or | 117 // Directory always has filehandle for examining contents. Whether or |
114 // not we'll recurse into it gets decided later. | 118 // not we'll recurse into it gets decided later. |
115 | 119 |
116 if (dir) | 120 if (dir) new->data = openat(dirtree_parentfd(new), new->name, 0); |
117 new->data = openat(new->parent ? new->parent->data : AT_FDCWD, | |
118 new->name, 0); | |
119 | 121 |
120 flags = callback(new); | 122 flags = callback(new); |
121 | 123 |
122 if (dir) { | 124 if (dir) { |
123 if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) { | 125 if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) { |