aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/getfattr.c
blob: bf2c04c874a0b8dd72429ef55c06aa79cdd0863f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* getfattr.c - Read POSIX extended attributes.
 *
 * Copyright 2016 Android Open Source Project.
 *
 * No standard

USE_GETFATTR(NEWTOY(getfattr, "(only-values)dhn:", TOYFLAG_USR|TOYFLAG_BIN))

config GETFATTR
  bool "getfattr"
  default n
  help
    usage: getfattr [-d] [-h] [-n NAME] FILE...

    Read POSIX extended attributes.

    -d	Show values as well as names
    -h	Do not dereference symbolic links
    -n	Show only attributes with the given name
    --only-values	Don't show names
*/

#define FOR_getfattr
#include "toys.h"

GLOBALS(
  char *n;
)

// TODO: factor out the lister and getter loops and use them in cp too.
static void do_getfattr(char *file)
{
  ssize_t (*getter)(const char *, const char *, void *, size_t) = getxattr;
  ssize_t (*lister)(const char *, char *, size_t) = listxattr;
  char **sorted_keys;
  ssize_t keys_len;
  char *keys, *key;
  int i, key_count;

  if (FLAG(h)) {
    getter = lgetxattr;
    lister = llistxattr;
  }

  // Collect the keys.
  while ((keys_len = lister(file, NULL, 0))) {
    if (keys_len == -1) perror_msg("listxattr failed");
    keys = xmalloc(keys_len);
    if (lister(file, keys, keys_len) == keys_len) break;
    free(keys);
  }

  if (keys_len == 0) return;

  // Sort the keys.
  for (key = keys, key_count = 0; key-keys < keys_len; key += strlen(key)+1)
    key_count++;
  sorted_keys = xmalloc(key_count * sizeof(char *));
  for (key = keys, i = 0; key-keys < keys_len; key += strlen(key)+1)
    sorted_keys[i++] = key;
  qsort(sorted_keys, key_count, sizeof(char *), qstrcmp);

  if (!FLAG(only_values)) printf("# file: %s\n", file);

  for (i = 0; i < key_count; i++) {
    key = sorted_keys[i];

    if (TT.n && strcmp(TT.n, key)) continue;

    if (FLAG(d) || FLAG(only_values)) {
      ssize_t value_len;
      char *value = NULL;

      while ((value_len = getter(file, key, NULL, 0))) {
        if (value_len == -1) perror_msg("getxattr failed");
        value = xzalloc(value_len+1);
        if (getter(file, key, value, value_len) == value_len) break;
        free(value);
      }

      if (FLAG(only_values)) {
        if (value) printf("%s", value);
      } else if (!value) puts(key);
      else printf("%s=\"%s\"\n", key, value);
      free(value);
    } else puts(key); // Just list names.
  }

  if (!FLAG(only_values)) xputc('\n');
  free(sorted_keys);
}

void getfattr_main(void)
{
  char **s;

  for (s=toys.optargs; *s; s++) do_getfattr(*s);
}