1 // $Id: vdu.c 2260 2006-01-22 11:56:28Z ensc $ --*- c -*--
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.
23 #include <lib/vserver.h>
34 #define ENSC_WRAPPERS_PREFIX "vdu: "
35 #define ENSC_WRAPPERS_VSERVER 1
36 #define ENSC_WRAPPERS_UNISTD 1
37 #define ENSC_WRAPPERS_DIRENT 1
38 #define ENSC_WRAPPERS_FCNTL 1
39 #define ENSC_WRAPPERS_STAT 1
42 #define CMD_HELP 0x1000
43 #define CMD_VERSION 0x1001
44 #define CMD_XID 0x2000
45 #define CMD_SPACE 0x2001
46 #define CMD_INODES 0x2002
47 #define CMD_SCRIPT 0x2003
48 #define CMD_BLOCKSIZE 0x2005
50 int wrapper_exit_code = 1;
54 { "help", no_argument, 0, CMD_HELP },
55 { "version", no_argument, 0, CMD_VERSION },
56 { "xid", required_argument, 0, CMD_XID },
57 { "space", no_argument, 0, CMD_SPACE },
58 { "inodes", no_argument, 0, CMD_INODES },
59 { "script", no_argument, 0, CMD_SCRIPT },
60 { "blocksize", required_argument, 0, CMD_BLOCKSIZE },
69 unsigned long blocksize;
73 uint_least64_t blocks;
74 uint_least64_t inodes;
77 struct TraversalParams {
78 struct Arguments const * const args;
79 struct Result * const result;
83 showHelp(int fd, char const *cmd, int res)
85 WRITE_MSG(fd, "Usage:\n ");
88 " --xid <xid> (--space|--inodes) [--blocksize <blocksize>] [--script] <directory>*\n"
90 "Please report bugs to " PACKAGE_BUGREPORT "\n");
99 "vdu " VERSION " -- calculates the size of a directory\n"
100 "This program is part of " PACKAGE_STRING "\n\n"
101 "Copyright (C) 2006 Enrico Scholz\n"
102 VERSION_COPYRIGHT_DISCLAIMER);
106 /* basic hash table implementation for inode tracking */
107 #define HASH_SIZE 103
108 typedef struct hash_entry {
109 struct hash_entry *next;
113 typedef struct hash_table {
114 hash_entry *entries[HASH_SIZE];
117 static hash_table ht;
122 memset(&ht, 0, sizeof(hash_table));
130 for (i = 0; i < HASH_SIZE; i++) {
131 for (e = ht.entries[i], p = NULL; e; e = e->next) {
140 hash_insert(ino_t inode)
143 unsigned int hashval = inode % HASH_SIZE;
145 /* no one else here */
146 if (ht.entries[hashval] == NULL) {
147 ht.entries[hashval] = malloc(sizeof(hash_entry));
148 ht.entries[hashval]->next = NULL;
149 ht.entries[hashval]->inode = inode;
153 for (e = ht.entries[hashval], p = NULL; e; e = e->next) {
154 /* already in the hash table */
155 if (e->inode == inode)
157 else if (e->inode > inode) {
160 ht.entries[hashval] = malloc(sizeof(hash_entry));
161 ht.entries[hashval]->next = e;
162 ht.entries[hashval]->inode = inode;
164 /* we're in the middle */
166 p->next = malloc(sizeof(hash_entry));
168 p->next->inode = inode;
175 p->next = malloc(sizeof(hash_entry));
176 p->next->next = NULL;
177 p->next->inode = inode;
183 visitDirEntry(char const *name, dev_t const dir_dev,
184 struct TraversalParams *params);
187 visitDir(char const *name, struct stat const *expected_stat, struct TraversalParams *params)
189 int fd = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
192 EsafeChdir(name, expected_stat);
197 struct dirent *ent = Ereaddir(dir);
200 if (isDotfile(ent->d_name)) continue;
201 visitDirEntry(ent->d_name, expected_stat->st_dev, params);
211 visitDirEntry(char const *name, dev_t const dir_dev,
212 struct TraversalParams *params)
219 xid = vc_getfilecontext(name);
220 if (xid == params->args->xid &&
221 (st.st_nlink == 1 || hash_insert(st.st_ino) != -1)) {
222 params->result->blocks += st.st_blocks;
223 params->result->inodes += 1;
226 if (S_ISDIR(st.st_mode) && dir_dev == st.st_dev)
227 visitDir(name, &st, params);
231 visitDirStart(char const *name, struct TraversalParams *params)
234 int fd = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
239 visitDirEntry(".", st.st_dev, params);
245 int main(int argc, char *argv[])
247 struct Arguments args = {
256 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
260 case CMD_HELP : showHelp(1, argv[0], 0);
261 case CMD_VERSION : showVersion();
262 case CMD_XID : args.xid = Evc_xidopt2xid(optarg,true); break;
263 case CMD_SPACE : args.space = true; break;
264 case CMD_INODES : args.inodes = true; break;
265 case CMD_SCRIPT : args.script = true; break;
267 if (!isNumberUnsigned(optarg, &args.blocksize, false)) {
268 WRITE_MSG(2, "Invalid block size argument: '");
269 WRITE_STR(2, optarg);
270 WRITE_MSG(2, "'; try '--help' for more information\n");
275 WRITE_MSG(2, "Try '");
276 WRITE_STR(2, argv[0]);
277 WRITE_MSG(2, " --help' for more information.\n");
283 if (args.xid==VC_NOCTX)
284 WRITE_MSG(2, "No xid specified; try '--help' for more information\n");
285 else if (!args.space && !args.inodes)
286 WRITE_MSG(2, "Must specify --space or --inodes; try '--help' for more information\n");
287 else if (optind==argc)
288 WRITE_MSG(2, "No directory specified; try '--help' for more information\n");
292 struct Result result;
293 struct TraversalParams params = {
298 for (i = optind; i < argc; i++) {
300 char buf[sizeof(size)*3 + 3];
301 char const * delim = "";
307 visitDirStart(argv[i], ¶ms);
311 WRITE_STR(1, argv[i]);
316 len = utilvserver_fmt_uint64(buf, result.blocks*512 / args.blocksize);
317 if (*delim) WRITE_STR(1, delim);
322 len = utilvserver_fmt_uint64(buf, result.inodes);
323 if (*delim) WRITE_STR(1, delim);