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
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 },
73 showHelp(int fd, char const *cmd, int res)
75 WRITE_MSG(fd, "Usage:\n ");
78 " --xid <xid> (--space|--inodes) [--blocksize <blocksize>] [--script] <directory>*\n"
80 "Please report bugs to " PACKAGE_BUGREPORT "\n");
89 "vdu " VERSION " -- calculates the size of a directory\n"
90 "This program is part of " PACKAGE_STRING "\n\n"
91 "Copyright (C) 2006 Enrico Scholz\n"
92 VERSION_COPYRIGHT_DISCLAIMER);
96 /* basic hash table implementation for inode tracking */
98 typedef struct hash_entry {
99 struct hash_entry *next;
103 typedef struct hash_table {
104 hash_entry *entries[HASH_SIZE];
107 static hash_table ht;
112 memset(&ht, 0, sizeof(hash_table));
120 for (i = 0; i < HASH_SIZE; i++) {
121 for (e = ht.entries[i], p = NULL; e; e = e->next) {
130 hash_insert(ino_t inode)
133 unsigned int hashval = inode % HASH_SIZE;
135 /* no one else here */
136 if (ht.entries[hashval] == NULL) {
137 ht.entries[hashval] = malloc(sizeof(hash_entry));
138 ht.entries[hashval]->next = NULL;
139 ht.entries[hashval]->inode = inode;
143 for (e = ht.entries[hashval], p = NULL; e; e = e->next) {
144 /* already in the hash table */
145 if (e->inode == inode)
147 else if (e->inode > inode) {
150 ht.entries[hashval] = malloc(sizeof(hash_entry));
151 ht.entries[hashval]->next = e;
152 ht.entries[hashval]->inode = inode;
154 /* we're in the middle */
156 p->next = malloc(sizeof(hash_entry));
158 p->next->inode = inode;
165 p->next = malloc(sizeof(hash_entry));
166 p->next->next = NULL;
167 p->next->inode = inode;
173 vdu_onedir(struct Arguments const *args, char const *path, uint64_t *size)
177 struct stat dirst, st;
178 char entpath[PATH_MAX];
180 if (lstat(path, &dirst) == -1) {
181 WRITE_MSG(2, "lstat(");
188 dir = Eopendir(path);
189 while ((ent = Ereaddir(dir)) != NULL) {
190 if (ent->d_name[0] == '.' && (ent->d_name[1] == '\0' || (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
193 strcpy(entpath, path);
194 strcat(entpath, "/");
195 strcat(entpath, ent->d_name);
197 if (lstat(entpath, &st) == -1) {
198 WRITE_MSG(2, "lstat(");
199 WRITE_STR(2, entpath);
205 if (vc_getfilecontext(entpath) != args->xid)
208 if (st.st_nlink > 1 && hash_insert(st.st_ino) == -1)
212 *size += st.st_blocks << 9; /* * 512 */
216 if (S_ISDIR(st.st_mode) && dirst.st_dev == st.st_dev)
217 vdu_onedir(args, entpath, size);
222 int main(int argc, char *argv[])
224 struct Arguments args = {
233 int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
237 case CMD_HELP : showHelp(1, argv[0], 0);
238 case CMD_VERSION : showVersion();
239 case CMD_XID : args.xid = Evc_xidopt2xid(optarg,true); break;
240 case CMD_SPACE : args.space = true; break;
241 case CMD_INODES : args.inodes = true; break;
242 case CMD_SCRIPT : args.script = true; break;
246 args.blocksize = strtol(optarg, &endptr, 0);
247 if ((args.blocksize == 0 && errno != 0) || *endptr != '\0') {
248 WRITE_MSG(2, "Invalid block size argument: '");
249 WRITE_STR(2, optarg);
250 WRITE_MSG(2, "'; try '--help' for more information\n");
256 WRITE_MSG(2, "Try '");
257 WRITE_STR(2, argv[0]);
258 WRITE_MSG(2, " --help' for more information.\n");
264 if (args.xid==VC_NOCTX)
265 WRITE_MSG(2, "No xid specified; try '--help' for more information\n");
266 else if (!args.space && !args.inodes)
267 WRITE_MSG(2, "Must specify --space or --inodes; try '--help' for more information\n");
268 else if (args.space && args.inodes)
269 WRITE_MSG(2, "Can only do one thing at a time; try '--help' for more information\n");
270 else if (optind==argc)
271 WRITE_MSG(2, "No directory specified; try '--help' for more information\n");
275 char buf[sizeof(size)*3 + 2];
278 for (i = optind; i < argc; i++) {
282 vdu_onedir(&args, argv[i], &size);
286 WRITE_STR(1, argv[i]);
290 size /= args.blocksize;
291 len = utilvserver_fmt_uint64(buf, size);