From 2b81cf5a5eb32c5e500c892fd1178aef624bded5 Mon Sep 17 00:00:00 2001 From: David Bartley Date: Sat, 9 May 2009 03:29:22 -0400 Subject: [PATCH] Add ability to print acl's from ls * src/ls.c: Add ability to print acl's from ls. --- src/ls.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/ls.c b/src/ls.c index 795d1ed..8ee797e 100644 --- a/src/ls.c +++ b/src/ls.c @@ -193,6 +193,10 @@ struct fileinfo /* For long listings, true if the file has an access control list, or an SELinux security context. */ enum acl_type acl_type; + + /* For long listings, the text of the access control lists. */ + char *acl; + char *defacl; }; #define LEN_STR_PAIR(s) sizeof (s) - 1, s @@ -468,6 +472,11 @@ static uintmax_t file_output_block_size = 1; strange characters in file names. */ static bool dired; +/* Print acl's; optionally print short acl's. */ +static bool print_acl; +static bool print_short_acl; +static bool print_acl_indents; + /* `none' means don't mention the type of files. `slash' means mention directories only, with a '/'. `file_type' means mention file types. @@ -775,6 +784,8 @@ enum static struct option const long_options[] = { + {"acl", no_argument, NULL, 'e'}, + {"short-acl", no_argument, NULL, 'E'}, {"all", no_argument, NULL, 'a'}, {"escape", no_argument, NULL, 'b'}, {"directory", no_argument, NULL, 'd'}, @@ -1600,7 +1611,7 @@ decode_switches (int argc, char **argv) { int oi = -1; int c = getopt_long (argc, argv, - "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UXZ1", + "abcdefghiklmnopqrstuvw:xABCDEFGHI:LNQRST:UXZ1", long_options, &oi); if (c == -1) break; @@ -1623,6 +1634,10 @@ decode_switches (int argc, char **argv) immediate_dirs = true; break; + case 'e': + print_acl = true; + break; + case 'f': /* Same as enabling -a -U and disabling -l -s. */ ignore_mode = IGNORE_MINIMAL; @@ -1739,6 +1754,11 @@ decode_switches (int argc, char **argv) dired = true; break; + case 'E': + print_acl = true; + print_short_acl = true; + break; + case 'F': indicator_style = classify; break; @@ -1915,6 +1935,12 @@ decode_switches (int argc, char **argv) } } + if (print_acl) + { + print_acl_indents = isatty (STDOUT_FILENO); + format = long_format; + } + max_idx = MAX (1, line_length / MIN_COLUMN_WIDTH); filename_quoting_options = clone_quoting_options (NULL); @@ -2629,6 +2655,10 @@ clear_files (void) free (f->linkname); if (f->scontext != UNKNOWN_SECURITY_CONTEXT) freecon (f->scontext); + if (f->acl) + free_acl_text (f->acl); + if (f->defacl) + free_acl_text (f->defacl); } cwd_n_used = 0; @@ -2805,7 +2835,22 @@ gobble_file (char const *name, enum filetype type, ino_t inode, if (err == 0 && format == long_format) { - int n = file_has_acl (absolute_name, &f->stat); + int n; + + if (print_acl) + { + int flags = 0; + if (print_short_acl) + flags |= GL_ACL_SHORT_FORMAT; + if (numeric_ids) + flags |= GL_ACL_NUMERIC_IDS; + if (print_acl_indents) + flags |= GL_ACL_PRINT_INDENTS; + n = file_has_acl (absolute_name, &f->stat, flags, + &f->acl, &f->defacl); + } + else + n = file_has_acl (absolute_name, &f->stat, 0, NULL, NULL); err = (n < 0); have_acl = (0 < n); } @@ -3532,6 +3577,27 @@ format_group_width (gid_t g) return format_user_or_group_width (numeric_ids ? NULL : getgroup (g), g); } +/* Print an access control list. */ +static int +format_acl (char *text, const char *prefix) +{ + int flags = 0; + + char *line; + line = strtok (text, "\n"); + if (line) + { + do + { + DIRED_PUTCHAR ('\n'); + DIRED_FPUTS_LITERAL (" ", stdout); + DIRED_FPUTS (prefix, stdout, strlen (prefix)); + DIRED_FPUTS (line, stdout, strlen (line)); + } + while (line = strtok (NULL, "\n")); + } +} + /* Print information about F in long format. */ @@ -3758,6 +3824,11 @@ print_long_format (const struct fileinfo *f) } else if (indicator_style != none) print_type_indicator (f->stat_ok, f->stat.st_mode, f->filetype); + + if (f->acl) + format_acl (f->acl, ""); + if (f->defacl) + format_acl (f->defacl, _("default:")); } /* Output to OUT a quoted representation of the file name NAME, @@ -4537,6 +4608,12 @@ Mandatory arguments to long options are mandatory for short options too.\n\ -D, --dired generate output designed for Emacs' dired mode\n\ "), stdout); fputs (_("\ + -e, --acl print any access control lists of each file\n\ + (implies long listing format)\n\ + -E, --short-acl like -e, but print a shorter form of the access\n\ + control list if possible\n\ +"), stdout); + fputs (_("\ -f do not sort, enable -aU, disable -ls --color\n\ -F, --classify append indicator (one of */=>@|) to entries\n\ --file-type likewise, except do not append `*'\n\ -- 1.5.6.5