3 // Copyright (C) 2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <lib/vserver.h>
35 #define ENSC_WRAPPERS_PREFIX "vdu: "
36 #define ENSC_WRAPPERS_VSERVER 1
37 #define ENSC_WRAPPERS_UNISTD 1
38 #define ENSC_WRAPPERS_DIRENT 1
39 #define ENSC_WRAPPERS_FCNTL 1
40 #define ENSC_WRAPPERS_STAT 1
43 #define CMD_HELP 0x1000
44 #define CMD_VERSION 0x1001
45 #define CMD_XID 0x2000
46 #define CMD_SPACE 0x2001
47 #define CMD_INODES 0x2002
48 #define CMD_SCRIPT 0x2003
49 #define CMD_BLOCKSIZE 0x2005
51 int wrapper_exit_code = 1;
55 { "help", no_argument, 0, CMD_HELP },
56 { "version", no_argument, 0, CMD_VERSION },
57 { "xid", required_argument, 0, CMD_XID },
58 { "space", no_argument, 0, CMD_SPACE },
59 { "inodes", no_argument, 0, CMD_INODES },
60 { "script", no_argument, 0, CMD_SCRIPT },
61 { "blocksize", required_argument, 0, CMD_BLOCKSIZE },
70 unsigned long blocksize;
74 uint_least64_t blocks;
75 uint_least64_t inodes;
78 struct TraversalParams {
79 struct Arguments const * const args;
80 struct Result * const result;
84 showHelp(int fd, char const *cmd, int res)
86 WRITE_MSG(fd, "Usage:\n ");
89 " --xid <xid> (--space|--inodes) [--blocksize <blocksize>] [--script] <directory>*\n"
91 "Please report bugs to " PACKAGE_BUGREPORT "\n");
100 "vdu " VERSION " -- calculates the size of a directory\n"
101 "This program is part of " PACKAGE_STRING "\n\n"
102 "Copyright (C) 2006 Enrico Scholz\n"
103 VERSION_COPYRIGHT_DISCLAIMER);
107 /* basic hash table implementation for inode tracking */
108 #define HASH_SIZE 103
109 typedef struct hash_entry {
110 struct hash_entry *next;
114 typedef struct hash_table {
115 hash_entry *entries[HASH_SIZE];
118 static hash_table ht;
123 memset(&ht, 0, sizeof(hash_table));
131 for (i = 0; i < HASH_SIZE; i++) {
132 for (e = ht.entries[i], p = NULL; e; e = e->next) {
141 hash_insert(ino_t inode)
144 unsigned int hashval = inode % HASH_SIZE;
146 /* no one else here */
147 if (ht.entries[hashval] == NULL) {
148 ht.entries[hashval] = malloc(sizeof(hash_entry));
149 ht.entries[hashval]->next = NULL;
150 ht.entries[hashval]->inode = inode;
154 for (e = ht.entries[hashval], p = NULL; e; e = e->next) {
155 /* already in the hash table */
156 if (e->inode == inode)
158 else if (e->inode > inode) {
161 ht.entries[hashval] = malloc(sizeof(hash_entry));
162 ht.entries[hashval]->next = e;
163 ht.entries[hashval]->inode = inode;
165 /* we're in the middle */
167 p->next = malloc(sizeof(hash_entry));
169 p->next->inode = inode;
176 p->next = malloc(sizeof(hash_entry));
177 p->next->next = NULL;
178 p->next->inode = inode;
184 visitDirEntry(char const *name, dev_t const dir_dev,
185 struct TraversalParams *params);
188 visitDir(char const *name, struct stat const *expected_stat, struct TraversalParams *params)
190 int fd = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
193 EsafeChdir(name, expected_stat);
198 struct dirent *ent = Ereaddir(dir);
201 if (isDotfile(ent->d_name)) continue;
202 visitDirEntry(ent->d_name, expected_stat->st_dev, params);
212 visitDirEntry(char const *name, dev_t const dir_dev,
213 struct TraversalParams *params)
220 xid = vc_getfilecontext(name);
221 if (xid == params->args->xid &&
222 (st.st_nlink == 1 || hash_insert(st.st_ino) != -1)) {
223 params->result->blocks += st.st_blocks;
224 params->result->inodes += 1;
227 if (S_ISDIR(st.st_mode) && dir_dev == st.st_dev)
228 visitDir(name, &st, params);
232 visitDirStart(char const *name, struct TraversalParams *params)
235 int fd = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
240 visitDirEntry(".", st.st_dev, params);
246 int main(int argc, char *argv[])
248 struct Arguments args = {
257 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
261 case CMD_HELP : showHelp(1, argv[0], 0);
262 case CMD_VERSION : showVersion();
263 case CMD_XID : args.xid = Evc_xidopt2xid(optarg,true); break;
264 case CMD_SPACE : args.space = true; break;
265 case CMD_INODES : args.inodes = true; break;
266 case CMD_SCRIPT : args.script = true; break;
268 if (!isNumberUnsigned(optarg, &args.blocksize, false)) {
269 WRITE_MSG(2, "Invalid block size argument: '");
270 WRITE_STR(2, optarg);
271 WRITE_MSG(2, "'; try '--help' for more information\n");
276 WRITE_MSG(2, "Try '");
277 WRITE_STR(2, argv[0]);
278 WRITE_MSG(2, " --help' for more information.\n");
284 if (args.xid==VC_NOCTX)
285 WRITE_MSG(2, "No xid specified; try '--help' for more information\n");
286 else if (!args.space && !args.inodes)
287 WRITE_MSG(2, "Must specify --space or --inodes; try '--help' for more information\n");
288 else if (optind==argc)
289 WRITE_MSG(2, "No directory specified; try '--help' for more information\n");
293 struct Result result;
294 struct TraversalParams params = {
299 for (i = optind; i < argc; i++) {
301 char buf[sizeof(size)*3 + 3];
302 char const * delim = "";
308 visitDirStart(argv[i], ¶ms);
312 WRITE_STR(1, argv[i]);
317 len = utilvserver_fmt_uint64(buf, result.blocks*512 / args.blocksize);
318 if (*delim) WRITE_STR(1, delim);
323 len = utilvserver_fmt_uint64(buf, result.inodes);
324 if (*delim) WRITE_STR(1, delim);