added and documented '-x' switch
[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   { "immutable", no_argument, 0, CMD_IMMUTABLE },
41   { "immulink",  no_argument, 0, CMD_IMMULINK },
42   { 0,0,0,0 }
43 };
44
45 char const              CMDLINE_OPTIONS_SHORT[] = "Rsux";
46
47 static long     set_mask = 0;
48 static long     del_mask = VC_IMMUTABLE_ALL;
49
50 void
51 showHelp(int fd, char const *cmd, int res)
52 {
53   WRITE_MSG(fd, "Usage:  ");
54   WRITE_STR(fd, cmd);
55   WRITE_MSG(fd,
56             " [-Rsux] [--immutable] [--immulink] [--] <file>+\n\n"
57             " Options:\n"
58             "   -R  ...  recurse through directories\n"
59             "   -s  ...  set flag only; when only one of the '--immu*' options\n"
60             "            is given, do not delete the other ones\n"
61             "   -u  ...  revert operation and unset the given flags\n"
62             "   -x  ...  do not cross filesystems\n\n"
63             "Please report bugs to " PACKAGE_BUGREPORT "\n");
64   exit(res);
65 }
66
67 void
68 showVersion()
69 {
70   WRITE_MSG(1,
71             "setattr " VERSION " -- sets vserver specific file attributes\n"
72             "This program is part of " PACKAGE_STRING "\n\n"
73             "Copyright (C) 2004 Enrico Scholz\n"
74             VERSION_COPYRIGHT_DISCLAIMER);
75   exit(0);
76 }
77
78 void
79 fixupParams(struct Arguments * args, int argc)
80 {
81   if (optind==argc) {
82     WRITE_MSG(2, "No filename given; use '--help' for more information\n");
83     exit(1);
84   }
85
86   set_mask = 0;
87   if (args->immutable) set_mask |= VC_IMMUTABLE_FILE_FL;
88   if (args->immulink)  set_mask |= VC_IMMUTABLE_LINK_FL;
89   
90   if (args->do_set) del_mask = 0;
91   else              del_mask = VC_IMMUTABLE_ALL;
92
93   if (args->do_unset) {
94     del_mask = set_mask;
95     set_mask = 0;
96   }
97 }
98
99 static bool
100 setFlags(char const *name, char const *display_name,
101          struct stat const *exp_st)
102 {
103   int           fd = open(name, O_RDONLY);
104   int           res = false;
105   
106   if (fd==-1) {
107     perror("open()");
108     return false;
109   }
110
111   if (!exp_st ||
112       !checkForRace(fd, name, exp_st))
113     goto err;
114
115   if (vc_X_set_ext2flags(fd, set_mask, del_mask)==-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_ISLNK(exp_st->st_mode)) return true;
132   
133   return setFlags(name, display_name, exp_st);
134 }