merge with 0.30.213
[util-vserver.git] / src / lsxid.c
1 // $Id: lsxid.c 1980 2005-03-24 12:44:17Z ensc $    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
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.
8 //  
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.
13 //  
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.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "fstool.h"
24 #include "util.h"
25
26 #include <ensc_vector/vector.h>
27 #include <lib/vserver.h>
28 #include <lib/vserver-internal.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <assert.h>
38
39 #define ENSC_WRAPPERS_STDLIB    1
40 #include <wrappers.h>
41
42 struct option const
43 CMDLINE_OPTIONS[] = {
44   { "help",     no_argument,  0, CMD_HELP },
45   { "version",  no_argument,  0, CMD_VERSION },
46   { 0,0,0,0 }
47 };
48
49 char const              CMDLINE_OPTIONS_SHORT[] = "Radnx";
50
51 struct XidNameMapping {
52     xid_t               xid;
53     char const *        name;
54 };
55
56 static struct Vector    xid_name_mapping;
57
58 void
59 showHelp(int fd, char const *cmd, int res)
60 {
61   WRITE_MSG(fd, "Usage:  ");
62   WRITE_STR(fd, cmd);
63   WRITE_MSG(fd,
64             " [-Radnx] [--] <file>*\n\n"
65             " Options:\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");
73   exit(res);
74 }
75
76 void
77 showVersion()
78 {
79   WRITE_MSG(1,
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);
84   exit(0);
85 }
86
87 void
88 fixupParams(struct Arguments UNUSED * args, int UNUSED argc)
89 {
90   Vector_init(&xid_name_mapping, sizeof (struct XidNameMapping));
91 }
92
93 static int
94 cmpMap(void const *xid_v, void const *map_v)
95 {
96   xid_t const * const                   xid = xid_v;
97   struct XidNameMapping const * const   map = map_v;
98
99   return *xid - map->xid;
100 }
101
102 static char const *
103 lookupContext(xid_t xid)
104 {
105   struct XidNameMapping *res;
106
107   res = Vector_search(&xid_name_mapping, &xid, cmpMap);
108
109   if (res==0) {
110     vcCfgStyle          style = vcCFG_AUTO;
111     char const *        vcfg  = vc_getVserverByCtx(xid, &style, 0);
112
113     res = Vector_insert(&xid_name_mapping, &xid, cmpMap);
114     if (vcfg) res->name = vc_getVserverName(vcfg, style);
115     else      res->name = 0;
116     res->xid = xid;
117
118     free(const_cast(char *)(vcfg));
119   }
120
121   assert(res!=0);
122   
123   return res->name;
124 }
125
126 static xid_t
127 getFileContext(char const *name)
128 {
129   xid_t         res;
130   uint32_t      mask = VC_IATTR_XID;
131   
132   if (vc_get_iattr(name, &res, 0, &mask)==-1)
133     perror("vc_get_iattr()");
134
135   return (mask&VC_IATTR_XID) ? res : VC_NOCTX;
136 }
137
138 bool
139 handleFile(char const *name, char const *display_name)
140 {
141   xid_t         ctx = 0;
142   char          buf[MAX(sizeof(ctx)*3+1, 20)];
143   bool          need_write = true;
144
145   memset(buf, ' ', sizeof buf);
146
147 #if 1
148   ctx = getFileContext(name);
149 #else
150 #  warning Compiling in debug-code
151   ctx = random() % 10 + 49213;
152 #endif
153   
154   if (ctx==VC_NOCTX) {
155     memcpy(buf, "!!ERR!!", 7);
156     Vwrite(1, buf, sizeof buf);
157     need_write = false;
158   }
159   else if (global_args->do_mapping) {
160     char const *        vname = lookupContext(ctx);
161     if (!vname) buf[0] = '\0';
162     else {
163       size_t            l = strlen(vname);
164       if (l<sizeof(buf)) Vwrite(1, buf, sizeof(buf)-l);
165       Vwrite(1, vname, l);
166
167       need_write = false;
168     }
169   }
170
171   if (need_write) {
172     size_t      l = utilvserver_fmt_ulong(buf, ctx);
173     if (l<sizeof(buf)) Vwrite(1, buf+l, sizeof(buf)-l);
174     Vwrite(1, buf, l);
175   }
176
177   Vwrite(1, "  ", 2);
178   Vwrite(1, display_name, strlen(display_name));
179   Vwrite(1, "\n", 1);
180
181   return ctx!=VC_NOCTX;
182 }