minor/medium cleanups
[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[] = "Rsu";
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             " [-Rsu] [--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             "Please report bugs to " PACKAGE_BUGREPORT "\n");
63   exit(res);
64 }
65
66 void
67 showVersion()
68 {
69   WRITE_MSG(1,
70             "setattr " VERSION " -- sets vserver specific file attributes\n"
71             "This program is part of " PACKAGE_STRING "\n\n"
72             "Copyright (C) 2004 Enrico Scholz\n"
73             VERSION_COPYRIGHT_DISCLAIMER);
74   exit(0);
75 }
76
77 void
78 fixupParams(struct Arguments * args, int argc)
79 {
80   if (optind==argc) {
81     WRITE_MSG(2, "No filename given; use '--help' for more information\n");
82     exit(1);
83   }
84
85   set_mask = 0;
86   if (args->immutable) set_mask |= VC_IMMUTABLE_FILE_FL;
87   if (args->immulink)  set_mask |= VC_IMMUTABLE_LINK_FL;
88   
89   if (args->do_set) del_mask = 0;
90   else              del_mask = VC_IMMUTABLE_ALL;
91
92   if (args->do_unset) {
93     del_mask = set_mask;
94     set_mask = 0;
95   }
96 }
97
98 static bool
99 setFlags(char const *name, char const *display_name,
100          struct stat const *exp_st)
101 {
102   int           fd = open(name, O_RDONLY);
103   int           res = false;
104   
105   if (fd==-1) {
106     perror("open()");
107     return false;
108   }
109
110   if (!exp_st ||
111       !checkForRace(fd, name, exp_st))
112     goto err;
113
114   if (vc_X_set_ext2flags(fd, set_mask, del_mask)==-1) {
115     perror(display_name);
116     goto err;
117   }
118
119   res = true;
120
121   err:
122   close(fd);
123   return res;
124 }
125
126 bool
127 handleFile(char const *name, char const * display_name,
128            struct stat const *exp_st)
129 {
130   if (S_ISLNK(exp_st->st_mode)) return true;
131   
132   return setFlags(name, display_name, exp_st);
133 }