initial checkin
[util-vserver.git] / util-vserver / src / showattr.c
1 // $Id$
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on showattr.cc by Jacques Gelinas
5 //  
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 //  
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //  
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 #include <stdio.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/ioctl.h>
26 #include <linux/ext2_fs.h>
27
28 // Patch to help compile this utility on unpatched kernel source
29 #ifndef EXT2_IMMUTABLE_FILE_FL
30         #define EXT2_IMMUTABLE_FILE_FL  0x00000010
31         #define EXT2_IMMUTABLE_LINK_FL  0x00008000
32 #endif
33
34 /*
35         Get the extended attributes of a file
36 */
37 static int getext2flags (const char *fname, long *flags)
38 {
39         int ret = -1;
40         int fd = open (fname,O_RDONLY);
41         if (fd == -1){
42                 fprintf (stderr,"Can't open file %s (%s)\n",fname,strerror(errno));
43         }else{
44                 *flags = 0;
45                 ret = ioctl (fd,EXT2_IOC_GETFLAGS,flags);
46                 close (fd);
47                 if (ret == -1){
48                         fprintf (stderr,"Can't get ext2 flags on file %s (%s)\n"
49                                 ,fname,strerror(errno));
50                 }
51         }
52         return ret;
53 }
54
55 /*
56         Set the extended attributes of a file
57 */
58 static int setext2flags (const char *fname, long flags)
59 {
60         int ret = -1;
61         int fd = open (fname,O_RDONLY);
62         if (fd == -1){
63                 fprintf (stderr,"Can't open file %s (%s)\n",fname,strerror(errno));
64         }else{
65                 ret = ioctl (fd,EXT2_IOC_SETFLAGS,&flags);
66                 close (fd);
67                 if (ret == -1){
68                         fprintf (stderr,"Can't set ext2 flags on file %s (%s)\n"
69                                 ,fname,strerror(errno));
70                 }
71         }
72         return ret;
73 }
74
75
76 int main (int argc, char *argv[])
77 {
78         int ret = -1;
79         if (argc <= 1){
80                 fprintf (stderr
81                         ,"showattr file ...\n"
82                          "\n"
83                          "Presents extended file attribute.\n"
84                          "\n"
85                          "setattr --immutable --immulink file ...\n"
86                          "\n"
87                          "Sets the extended file attributes.\n"
88                          "\n"
89                          "These utilities exist as an interim until lsattr and\n"
90                          "chattr are updated.\n"
91                         );
92         }else if (strstr(argv[0],"showattr")!=NULL){
93                 int i;
94                 for (i=1; i<argc; i++){
95                         long flags;
96                         ret = getext2flags (argv[i],&flags);
97                         if (ret == -1){
98                                 break;
99                         }else{
100                                 printf ("%s\t%08lx\n",argv[i],flags);
101                         }
102                 }
103         }else if (strstr(argv[0],"setattr")!=NULL){
104                 long flags = 0;
105                 int  i;
106                 ret = 0;
107                 for (i=1; i<argc; i++){
108                         const char *arg = argv[i];
109                         if (strncmp(arg,"--",2)==0){
110                                 if (strcmp(arg,"--immutable")==0){
111                                         flags |= EXT2_IMMUTABLE_FILE_FL;
112                                 }else if (strcmp(arg,"--immulink")==0){
113                                         flags |= EXT2_IMMUTABLE_LINK_FL;
114                                 }else{
115                                         fprintf (stderr,"Invalid option %s\n",arg);
116                                         ret = -1;
117                                         break;
118                                 }
119                         }else{
120                                 ret = setext2flags (arg,flags);
121                                 if (ret == -1){
122                                         break;
123                                 }
124                         }
125                 }
126         }
127         return ret;
128 }
129