renamed *.ic to *.hc
[util-vserver.git] / util-vserver / src / setattr.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/fmt.h>
27 #include <lib/vserver.h>
28 #include <lib/vserver-internal.h>
29
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34
35
36 struct option const
37 CMDLINE_OPTIONS[] = {
38   { "help",     no_argument,  0, CMD_HELP },
39   { "version",  no_argument,  0, CMD_VERSION },
40   { "immu",        no_argument, 0, CMD_IMMU  },
41   { "admin",       no_argument, 0, CMD_ADMIN },
42   { "watch",       no_argument, 0, CMD_WATCH },
43   { "hide",        no_argument, 0, CMD_HIDE  },
44   { "barrier",     no_argument, 0, CMD_BARRIER },
45   { "~immu",       no_argument, 0, CMD_UNSET_IMMU  },
46   { "~admin",      no_argument, 0, CMD_UNSET_ADMIN },
47   { "~watch",      no_argument, 0, CMD_UNSET_WATCH },
48   { "~hide",       no_argument, 0, CMD_UNSET_HIDE  },
49   { "~barrier",    no_argument, 0, CMD_UNSET_BARRIER },
50   { 0,0,0,0 }
51 };
52
53 char const              CMDLINE_OPTIONS_SHORT[] = "Rx";
54
55 void
56 showHelp(int fd, char const *cmd, int res)
57 {
58   WRITE_MSG(fd, "Usage:  ");
59   WRITE_STR(fd, cmd);
60   WRITE_MSG(fd,
61             " [-Rx] [--[~]immu] [--[~]admin] [--[~]watch] [--[~]hide] [--] <file>+\n\n"
62             " Options:\n"
63             "   -R  ...  recurse through directories\n"
64             "   -x  ...  do not cross filesystems\n\n"
65             "Please report bugs to " PACKAGE_BUGREPORT "\n");
66   exit(res);
67 }
68
69 void
70 showVersion()
71 {
72   WRITE_MSG(1,
73             "setattr " VERSION " -- sets vserver specific file attributes\n"
74             "This program is part of " PACKAGE_STRING "\n\n"
75             "Copyright (C) 2004 Enrico Scholz\n"
76             VERSION_COPYRIGHT_DISCLAIMER);
77   exit(0);
78 }
79
80 void
81 fixupParams(struct Arguments * args, int argc)
82 {
83   if (optind==argc) {
84     WRITE_MSG(2, "No filename given; use '--help' for more information\n");
85     exit(1);
86   }
87
88   args->do_display_dir = false;
89 }
90
91 static bool
92 setFlags(char const *name, char const *display_name,
93          struct stat const *exp_st)
94 {
95   int           fd = open(name, O_RDONLY);
96   int           res = false;
97   int           rc;
98
99   if (fd==-1) {
100     perror("open()");
101     return false;
102   }
103
104   // this is still needed... the file must be open so that vc_set_iattr()
105   // operates on a known file/inode
106   if (!exp_st ||
107       !checkForRace(fd, name, exp_st))
108     goto err;
109
110   rc = vc_set_iattr_compat(name, exp_st->st_dev, exp_st->st_ino,
111                            0,
112                            global_args->set_mask & ~global_args->del_mask,
113                            global_args->set_mask|global_args->del_mask);
114
115   if (rc==-1) {
116     perror(display_name);
117     goto err;
118   }
119
120   res = true;
121
122   err:
123   close(fd);
124   return res;
125 }
126
127 bool
128 handleFile(char const *name, char const * display_name,
129            struct stat const *exp_st)
130 {
131   if (!(S_ISREG(exp_st->st_mode) || S_ISDIR(exp_st->st_mode))) return true;
132   
133   return setFlags(name, display_name, exp_st);
134 }