1 // $Id: lsxid.c,v 1.9 2005/03/24 12:44:17 ensc Exp $ --*- c -*--
3 // Copyright (C) 2004 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.
26 #include <ensc_vector/vector.h>
27 #include <lib/vserver.h>
28 #include <lib/vserver-internal.h>
33 #include <sys/param.h>
34 #include <sys/types.h>
39 #define ENSC_WRAPPERS_STDLIB 1
44 { "help", no_argument, 0, CMD_HELP },
45 { "version", no_argument, 0, CMD_VERSION },
49 char const CMDLINE_OPTIONS_SHORT[] = "Radnx";
51 struct XidNameMapping {
56 static struct Vector xid_name_mapping;
59 showHelp(int fd, char const *cmd, int res)
61 WRITE_MSG(fd, "Usage: ");
64 " [-Radnx] [--] <file>*\n\n"
66 " -R ... recurse through directories\n"
67 " -a ... display files starting with '.' also\n"
68 " -d ... list directories like other files instead of\n"
69 " listing their content\n"
70 " -n ... do not try to do xid -> vserver-name mapping\n"
71 " -x ... do not cross filesystems\n\n"
72 "Please report bugs to " PACKAGE_BUGREPORT "\n");
80 "lsxid " VERSION " -- shows the context which is associated to a file\n"
81 "This program is part of " PACKAGE_STRING "\n\n"
82 "Copyright (C) 2004 Enrico Scholz\n"
83 VERSION_COPYRIGHT_DISCLAIMER);
88 fixupParams(struct Arguments UNUSED * args, int UNUSED argc)
90 Vector_init(&xid_name_mapping, sizeof (struct XidNameMapping));
94 cmpMap(void const *xid_v, void const *map_v)
96 xid_t const * const xid = xid_v;
97 struct XidNameMapping const * const map = map_v;
99 return *xid - map->xid;
103 lookupContext(xid_t xid)
105 struct XidNameMapping *res;
107 res = Vector_search(&xid_name_mapping, &xid, cmpMap);
110 vcCfgStyle style = vcCFG_AUTO;
111 char const * vcfg = vc_getVserverByCtx(xid, &style, 0);
113 res = Vector_insert(&xid_name_mapping, &xid, cmpMap);
114 if (vcfg) res->name = vc_getVserverName(vcfg, style);
118 free(const_cast(char *)(vcfg));
127 getFileContext(char const *name)
130 uint32_t mask = VC_IATTR_XID;
132 if (vc_get_iattr(name, &res, 0, &mask)==-1)
133 perror("vc_get_iattr()");
135 return (mask&VC_IATTR_XID) ? res : VC_NOCTX;
139 handleFile(char const *name, char const *display_name)
142 char buf[MAX(sizeof(ctx)*3+1, 20)];
143 bool need_write = true;
145 memset(buf, ' ', sizeof buf);
148 ctx = getFileContext(name);
150 # warning Compiling in debug-code
151 ctx = random() % 10 + 49213;
155 memcpy(buf, "!!ERR!!", 7);
156 Vwrite(1, buf, sizeof buf);
159 else if (global_args->do_mapping) {
160 char const * vname = lookupContext(ctx);
161 if (!vname) buf[0] = '\0';
163 size_t l = strlen(vname);
164 if (l<sizeof(buf)) Vwrite(1, buf, sizeof(buf)-l);
172 size_t l = utilvserver_fmt_ulong(buf, ctx);
173 if (l<sizeof(buf)) Vwrite(1, buf+l, sizeof(buf)-l);
178 Vwrite(1, display_name, strlen(display_name));
181 return ctx!=VC_NOCTX;