* provide st_mode at vc_set_iattr_compat() call
[util-vserver.git] / util-vserver / src / lsxid.c
1 // $Id$    --*- 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 <lib/vserver.h>
27 #include <lib/vserver-internal.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36
37 struct option const
38 CMDLINE_OPTIONS[] = {
39   { "help",     no_argument,  0, CMD_HELP },
40   { "version",  no_argument,  0, CMD_VERSION },
41   { 0,0,0,0 }
42 };
43
44 char const              CMDLINE_OPTIONS_SHORT[] = "Radnx";
45
46 void
47 showHelp(int fd, char const *cmd, int res)
48 {
49   WRITE_MSG(fd, "Usage:  ");
50   WRITE_STR(fd, cmd);
51   WRITE_MSG(fd,
52             " [-Radnx] [--] <file>*\n\n"
53             " Options:\n"
54             "   -R  ...  recurse through directories\n"
55             "   -a  ...  display files starting with '.' also\n"
56             "   -d  ...  list directories like other files instead of\n"
57             "            listing their content\n"
58             "   -n  ...  do not try to do xid -> vserver-name mapping\n"
59             "   -x  ...  do not cross filesystems\n\n"
60             "Please report bugs to " PACKAGE_BUGREPORT "\n");
61   exit(res);
62 }
63
64 void
65 showVersion()
66 {
67   WRITE_MSG(1,
68             "lsxid " VERSION " -- shows the context which is associated to a file\n"
69             "This program is part of " PACKAGE_STRING "\n\n"
70             "Copyright (C) 2004 Enrico Scholz\n"
71             VERSION_COPYRIGHT_DISCLAIMER);
72   exit(0);
73 }
74
75 void
76 fixupParams(struct Arguments UNUSED * args, int UNUSED argc)
77 {
78 }
79
80 static xid_t
81 getFileContext(char const *name, struct stat const *exp_st)
82 {
83   xid_t         res;
84   uint32_t      mask = VC_IATTR_XID;
85   
86   if (vc_get_iattr_compat(name, exp_st->st_dev, exp_st->st_ino,
87                           &res, 0, &mask, &exp_st->st_mode)==-1)
88     perror("vc_get_iattr_compat()");
89
90   return (mask&VC_IATTR_XID) ? res : VC_NOCTX;
91 }
92
93 bool
94 handleFile(char const *name, char const *display_name,
95            struct stat const *exp_st)
96 {
97   xid_t         ctx = 0;
98   char          buf[MAX(sizeof(ctx)*3+1, 20)];
99   bool          need_write = true;
100
101   memset(buf, ' ', sizeof buf);
102
103   ctx = getFileContext(name, exp_st);
104   
105   if (ctx==VC_NOCTX) {
106     memcpy(buf, "!!ERR!!", 7);
107     write(1, buf, sizeof buf);
108     need_write = false;
109   }
110   else if (global_args->do_mapping) {
111     char const *        vname = vc_getVserverByCtx(ctx, 0,0);
112     if (!vname) buf[0] = '\0';
113     else {
114       size_t            l = strlen(vname);
115       if (l<sizeof(buf)) write(1, buf, sizeof(buf)-l);
116       write(1, vname, l);
117
118       free((char *)vname);
119       need_write = false;
120     }
121   }
122
123   if (need_write) {
124     size_t      l = utilvserver_fmt_ulong(buf, ctx);
125     if (l<sizeof(buf)) write(1, buf+l, sizeof(buf)-l);
126     write(1, buf, l);
127   }
128
129   write(1, "  ", 2);
130   write(1, display_name, strlen(display_name));
131   write(1, "\n", 1);
132
133   return ctx!=VC_NOCTX;
134 }