~vcs-imports/busybox/trunk

« back to all changes in this revision

Viewing changes to miscutils/getfattr.c

  • Committer: Denys Vlasenko
  • Author(s): Roger Knecht
  • Date: 2022-06-30 15:18:12 UTC
  • Revision ID: git-v1:20a4f70ecaad79bb932af09b7317a058872cd867
tree: new applet

Adds the tree program to list directories and files in a tree structure.

function                                             old     new   delta
tree_print                                             -     343    +343
scandir64                                              -     330    +330
scandir                                                -     330    +330
tree_main                                              -      86     +86
.rodata                                           105150  105228     +78
packed_usage                                       34511   34557     +46
alphasort64                                            -      31     +31
alphasort                                              -      31     +31
strcoll                                                -       5      +5
applet_names                                        2801    2806      +5
applet_main                                         1616    1620      +4
applet_suid                                          101     102      +1
applet_install_loc                                   202     203      +1
------------------------------------------------------------------------------
(add/remove: 11/0 grow/shrink: 6/0 up/down: 1291/0)          Total: 1291 bytes

Signed-off-by: Roger Knecht <rknecht@pm.me>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * getfattr - get extended attributes of filesystem objects.
3
 
 *
4
 
 * Copyright (C) 2023 by LoveSy <lovesykun@gmail.com>
5
 
 *
6
 
 * Licensed under GPLv2, see file LICENSE in this source tree.
7
 
 */
8
 
//config:config GETFATTR
9
 
//config:       bool "getfattr (12.3 kb)"
10
 
//config:       default y
11
 
//config:       help
12
 
//config:       Get extended attributes on files
13
 
 
14
 
//applet:IF_GETFATTR(APPLET_NOEXEC(getfattr, getfattr, BB_DIR_USR_BIN, BB_SUID_DROP, getfattr))
15
 
 
16
 
//kbuild:lib-$(CONFIG_GETFATTR) += getfattr.o
17
 
 
18
 
#include <stdio.h>
19
 
#include <sys/xattr.h>
20
 
#include "libbb.h"
21
 
 
22
 
//usage:#define getfattr_trivial_usage
23
 
//usage:       "[-h] {-d|-n ATTR} FILE...\n"
24
 
//usage:#define getfattr_full_usage "\n\n"
25
 
//usage:       "Get extended attributes"
26
 
//usage:     "\n"
27
 
//usage:     "\n        -h              Do not follow symlinks"
28
 
//usage:     "\n        -d              Dump all attributes"
29
 
//usage:     "\n        -n ATTR         Get attribute ATTR"
30
 
 
31
 
enum {
32
 
        OPT_h = (1 << 0),
33
 
        OPT_d = (1 << 1),
34
 
        OPT_n = (1 << 2),
35
 
};
36
 
 
37
 
static int print_attr(const char *file, const char *name, char **buf, size_t *bufsize)
38
 
{
39
 
        ssize_t len;
40
 
 
41
 
        if (*bufsize == 0)
42
 
                goto grow;
43
 
 again:
44
 
        len = ((option_mask32 &  OPT_h) ? lgetxattr: getxattr)(file, name, *buf, *bufsize);
45
 
        if (len < 0) {
46
 
                if (errno != ERANGE)
47
 
                        return len;
48
 
 grow:
49
 
                *bufsize = (*bufsize * 2) + 1024;
50
 
                *buf = xrealloc(*buf, *bufsize);
51
 
                goto again;
52
 
        }
53
 
        printf("%s=\"%.*s\"\n", name, len, *buf);
54
 
        return 0;
55
 
}
56
 
 
57
 
static ssize_t list_attr(const char *file, char **list, size_t *listsize)
58
 
{
59
 
        ssize_t len;
60
 
 
61
 
        if (*listsize == 0)
62
 
                goto grow;
63
 
 again:
64
 
        len = ((option_mask32 &  OPT_h) ? llistxattr : listxattr)(file, *list, *listsize);
65
 
        if (len < 0) {
66
 
                if (errno != ERANGE)
67
 
                        return len;
68
 
 grow:
69
 
                *listsize = (*listsize * 2) + 1024;
70
 
                *list = xrealloc(*list, *listsize);
71
 
                goto again;
72
 
        }
73
 
        return len;
74
 
}
75
 
 
76
 
int getfattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
77
 
int getfattr_main(int argc UNUSED_PARAM, char **argv)
78
 
{
79
 
        const char *name;
80
 
        exitcode_t status;
81
 
        int opt;
82
 
        char *buf = NULL;
83
 
        size_t bufsize = 0;
84
 
        char *list = NULL;
85
 
        size_t listsize = 0;
86
 
 
87
 
        opt = getopt32(argv, "^"
88
 
                "hdn:"
89
 
                /* Min one arg; -d and -n are exclusive */
90
 
                "\0" "-1:n--d:d--n"
91
 
                        //getfattr 2.5.1 does not enforce this: ":d:n" /* exactly one of -n or -d is required */
92
 
                , &name
93
 
        );
94
 
        argv += optind;
95
 
        status = EXIT_SUCCESS;
96
 
 
97
 
        do {
98
 
                int r;
99
 
//getfattr 2.5.1 with no -n/-d defaults to -d
100
 
                if (!(opt & OPT_n)) {
101
 
                        ssize_t len = list_attr(*argv, &list, &listsize);
102
 
                        if (len < 0)
103
 
                                goto err;
104
 
                        if (len > 0) {
105
 
                                char *key;
106
 
                                printf("# file: %s\n", *argv);
107
 
                                key = list;
108
 
                                while (len > 0) {
109
 
                                        ssize_t keylen;
110
 
                                        r = print_attr(*argv, key, &buf, &bufsize);
111
 
                                        if (r)
112
 
                                                goto err;
113
 
                                        keylen = strlen(key) + 1;
114
 
                                        key += keylen;
115
 
                                        len -= keylen;
116
 
                                }
117
 
                                bb_putchar('\n');
118
 
                        }
119
 
                } else {
120
 
                        printf("# file: %s\n", *argv);
121
 
                        r = print_attr(*argv, name, &buf, &bufsize);
122
 
                        if (r) {
123
 
 err:
124
 
                                bb_simple_perror_msg(*argv);
125
 
                                status = EXIT_FAILURE;
126
 
                                continue;
127
 
                        }
128
 
                        bb_putchar('\n');
129
 
                }
130
 
        } while (*++argv);
131
 
 
132
 
        if (ENABLE_FEATURE_CLEAN_UP)
133
 
                free(buf);
134
 
 
135
 
        fflush_stdout_and_exit(status);
136
 
}