Whamcloud - gitweb
LU-6722 jbd: double minimum journal size for RHEL7
[tools/e2fsprogs.git] / misc / lsattr.c
1 /*
2  * lsattr.c             - List file attributes on an ext2 file system
3  *
4  * Copyright (C) 1993, 1994  Remy Card <card@masi.ibp.fr>
5  *                           Laboratoire MASI, Institut Blaise Pascal
6  *                           Universite Pierre et Marie Curie (Paris VI)
7  *
8  * This file can be redistributed under the terms of the GNU General
9  * Public License
10  */
11
12 /*
13  * History:
14  * 93/10/30     - Creation
15  * 93/11/13     - Replace stat() calls by lstat() to avoid loops
16  * 94/02/27     - Integrated in Ted's distribution
17  * 98/12/29     - Display version info only when -V specified (G M Sipe)
18  */
19
20 #define _LARGEFILE64_SOURCE
21
22 #include "config.h"
23 #include <sys/types.h>
24 #include <dirent.h>
25 #ifdef HAVE_ERRNO_H
26 #include <errno.h>
27 #endif
28 #include <fcntl.h>
29 #ifdef HAVE_GETOPT_H
30 #include <getopt.h>
31 #else
32 extern int optind;
33 extern char *optarg;
34 #endif
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/param.h>
40 #include <sys/stat.h>
41
42 #include "ext2fs/ext2_fs.h"
43 #include "et/com_err.h"
44 #include "e2p/e2p.h"
45
46 #include "support/nls-enable.h"
47 #include "../version.h"
48
49 #ifdef __GNUC__
50 #define EXT2FS_ATTR(x) __attribute__(x)
51 #else
52 #define EXT2FS_ATTR(x)
53 #endif
54
55 static const char * program_name = "lsattr";
56
57 static int all;
58 static int dirs_opt;
59 static unsigned pf_options;
60 static int recursive;
61 static int verbose;
62 static int generation_opt;
63 static int project_opt;
64
65 #ifdef _LFS64_LARGEFILE
66 #define LSTAT           lstat64
67 #define STRUCT_STAT     struct stat64
68 #else
69 #define LSTAT           lstat
70 #define STRUCT_STAT     struct stat
71 #endif
72
73 static void usage(void)
74 {
75         fprintf(stderr, _("Usage: %s [-RVadlpv] [files...]\n"), program_name);
76         exit(1);
77 }
78
79 static int list_attributes (const char * name)
80 {
81         unsigned long flags;
82         unsigned long generation;
83         unsigned long project;
84
85         if (fgetflags (name, &flags) == -1) {
86                 com_err (program_name, errno, _("While reading flags on %s"),
87                          name);
88                 return -1;
89         }
90         if (project_opt) {
91                 if (fgetproject(name, &project) == -1) {
92                         com_err (program_name, errno,
93                                  _("While reading project on %s"),
94                                  name);
95                         return -1;
96                 }
97                 printf ("%5lu ", project);
98         }
99         if (generation_opt) {
100                 if (fgetversion (name, &generation) == -1) {
101                         com_err (program_name, errno,
102                                  _("While reading version on %s"),
103                                  name);
104                         return -1;
105                 }
106                 printf ("%-10lu ", generation);
107         }
108         if (pf_options & PFOPT_LONG) {
109                 printf("%-28s ", name);
110                 print_flags(stdout, flags, pf_options);
111                 fputc('\n', stdout);
112         } else {
113                 print_flags(stdout, flags, pf_options);
114                 printf(" %s\n", name);
115         }
116         return 0;
117 }
118
119 static int lsattr_dir_proc (const char *, struct dirent *, void *);
120
121 static int lsattr_args (const char * name)
122 {
123         STRUCT_STAT     st;
124         int retval = 0;
125
126         if (LSTAT (name, &st) == -1) {
127                 com_err (program_name, errno, _("while trying to stat %s"),
128                          name);
129                 retval = -1;
130         } else {
131                 if (S_ISDIR(st.st_mode) && !dirs_opt)
132                         retval = iterate_on_dir (name, lsattr_dir_proc, NULL);
133                 else
134                         retval = list_attributes (name);
135         }
136         return retval;
137 }
138
139 static int lsattr_dir_proc (const char * dir_name, struct dirent * de,
140                             void * private EXT2FS_ATTR((unused)))
141 {
142         STRUCT_STAT     st;
143         char *path;
144         int dir_len = strlen(dir_name);
145
146         path = malloc(dir_len + strlen (de->d_name) + 2);
147         if (!path) {
148                 fputs(_("Couldn't allocate path variable in lsattr_dir_proc\n"),
149                         stderr);
150                 return -1;
151         }
152
153         if (dir_len && dir_name[dir_len-1] == '/')
154                 sprintf (path, "%s%s", dir_name, de->d_name);
155         else
156                 sprintf (path, "%s/%s", dir_name, de->d_name);
157         if (LSTAT (path, &st) == -1)
158                 perror (path);
159         else {
160                 if (de->d_name[0] != '.' || all) {
161                         list_attributes (path);
162                         if (S_ISDIR(st.st_mode) && recursive &&
163                             strcmp(de->d_name, ".") &&
164                             strcmp(de->d_name, "..")) {
165                                 printf ("\n%s:\n", path);
166                                 iterate_on_dir (path, lsattr_dir_proc, NULL);
167                                 printf ("\n");
168                         }
169                 }
170         }
171         free(path);
172         return 0;
173 }
174
175 int main (int argc, char ** argv)
176 {
177         int c;
178         int i;
179         int err, retval = 0;
180
181 #ifdef ENABLE_NLS
182         setlocale(LC_MESSAGES, "");
183         setlocale(LC_CTYPE, "");
184         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
185         textdomain(NLS_CAT_NAME);
186         set_com_err_gettext(gettext);
187 #endif
188         if (argc && *argv)
189                 program_name = *argv;
190         else
191                 usage();
192         while ((c = getopt (argc, argv, "RVadlvp")) != EOF)
193                 switch (c)
194                 {
195                         case 'R':
196                                 recursive = 1;
197                                 break;
198                         case 'V':
199                                 verbose = 1;
200                                 break;
201                         case 'a':
202                                 all = 1;
203                                 break;
204                         case 'd':
205                                 dirs_opt = 1;
206                                 break;
207                         case 'l':
208                                 pf_options = PFOPT_LONG;
209                                 break;
210                         case 'v':
211                                 generation_opt = 1;
212                                 break;
213                         case 'p':
214                                 project_opt = 1;
215                                 break;
216                         default:
217                                 usage();
218                 }
219
220         if (verbose)
221                 fprintf (stderr, "lsattr %s (%s)\n",
222                          E2FSPROGS_VERSION, E2FSPROGS_DATE);
223         if (optind > argc - 1) {
224                 if (lsattr_args (".") == -1)
225                         retval = 1;
226         } else {
227                 for (i = optind; i < argc; i++) {
228                         err = lsattr_args (argv[i]);
229                         if (err)
230                                 retval = 1;
231                 }
232         }
233         exit(retval);
234 }