This commit was generated by cvs2svn to compensate for changes in r120,
[util-vserver.git] / src / showattr.c
1 // $Id: showattr.c,v 1.1.4.1 2003/11/18 22:31:10 ensc Exp $
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 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23 #include "compat.h"
24
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31
32 #include "ext2fs.h"
33
34
35 // Patch to help compile this utility on unpatched kernel source
36 #ifndef EXT2_IMMUTABLE_FILE_FL
37         #define EXT2_IMMUTABLE_FILE_FL  0x00000010
38         #define EXT2_IMMUTABLE_LINK_FL  0x00008000
39 #endif
40
41 /*
42         Get the extended attributes of a file
43 */
44 static int getext2flags (const char *fname, long *flags)
45 {
46         int ret = -1;
47         int fd = open (fname,O_RDONLY);
48         if (fd == -1){
49                 fprintf (stderr,"Can't open file %s (%s)\n",fname,strerror(errno));
50         }else{
51                 *flags = 0;
52                 ret = ioctl (fd,EXT2_IOC_GETFLAGS,flags);
53                 close (fd);
54                 if (ret == -1){
55                         fprintf (stderr,"Can't get ext2 flags on file %s (%s)\n"
56                                 ,fname,strerror(errno));
57                 }
58         }
59         return ret;
60 }
61
62 /*
63         Set the extended attributes of a file
64 */
65 static int setext2flags (const char *fname, long flags)
66 {
67         int ret = -1;
68         int fd = open (fname,O_RDONLY);
69         if (fd == -1){
70                 fprintf (stderr,"Can't open file %s (%s)\n",fname,strerror(errno));
71         }else{
72                 ret = ioctl (fd,EXT2_IOC_SETFLAGS,&flags);
73                 close (fd);
74                 if (ret == -1){
75                         fprintf (stderr,"Can't set ext2 flags on file %s (%s)\n"
76                                 ,fname,strerror(errno));
77                 }
78         }
79         return ret;
80 }
81
82
83 int main (int argc, char *argv[])
84 {
85         int ret = -1;
86         if (argc <= 1){
87                 fprintf (stderr
88                         ,"showattr file ...\n"
89                          "\n"
90                          "Presents extended file attribute.\n"
91                          "\n"
92                          "setattr --immutable --immulink file ...\n"
93                          "\n"
94                          "Sets the extended file attributes.\n"
95                          "\n"
96                          "These utilities exist as an interim until lsattr and\n"
97                          "chattr are updated.\n"
98                         );
99         }else if (strstr(argv[0],"showattr")!=NULL){
100                 int i;
101                 for (i=1; i<argc; i++){
102                         long flags;
103                         ret = getext2flags (argv[i],&flags);
104                         if (ret == -1){
105                                 break;
106                         }else{
107                                 printf ("%s\t%08lx\n",argv[i],flags);
108                         }
109                 }
110         }else if (strstr(argv[0],"setattr")!=NULL){
111                 long flags = 0;
112                 int  i;
113                 ret = 0;
114                 for (i=1; i<argc; i++){
115                         const char *arg = argv[i];
116                         if (strncmp(arg,"--",2)==0){
117                                 if (strcmp(arg,"--immutable")==0){
118                                         flags |= EXT2_IMMUTABLE_FILE_FL;
119                                 }else if (strcmp(arg,"--immulink")==0){
120                                         flags |= EXT2_IMMUTABLE_LINK_FL;
121                                 }else{
122                                         fprintf (stderr,"Invalid option %s\n",arg);
123                                         ret = -1;
124                                         break;
125                                 }
126                         }else{
127                                 ret = setext2flags (arg,flags);
128                                 if (ret == -1){
129                                         break;
130                                 }
131                         }
132                 }
133         }
134         return ret;
135 }
136