2 * xattrs.c --- Modify extended attributes via debugfs.
4 * Copyright (C) 2014 Oracle. This file may be redistributed
5 * under the terms of the GNU Public License.
17 #include "support/cstring.h"
21 #define PRINT_XATTR_HEX 0x01
22 #define PRINT_XATTR_RAW 0x02
23 #define PRINT_XATTR_C 0x04
24 #define PRINT_XATTR_STATFMT 0x08
25 #define PRINT_XATTR_NOQUOTES 0x10
27 /* Dump extended attributes */
28 static void print_xattr_hex(FILE *f, const char *str, int len)
32 for (i = 0; i < len; i++)
33 fprintf(f, "%02x ", (unsigned char)str[i]);
36 /* Dump extended attributes */
37 static void print_xattr_string(FILE *f, const char *str, int len, int flags)
42 if (flags & PRINT_XATTR_RAW) {
43 fwrite(str, len, 1, f);
47 if ((flags & PRINT_XATTR_C) == 0) {
48 /* check: is string "printable enough?" */
49 for (i = 0; i < len; i++)
53 if (printable <= len*7/8)
54 flags |= PRINT_XATTR_HEX;
57 if (flags & PRINT_XATTR_HEX) {
58 print_xattr_hex(f, str, len);
60 if ((flags & PRINT_XATTR_NOQUOTES) == 0)
62 print_c_string(f, str, len);
63 if ((flags & PRINT_XATTR_NOQUOTES) == 0)
68 static void print_xattr(FILE *f, char *name, char *value, size_t value_len,
71 print_xattr_string(f, name, strlen(name), PRINT_XATTR_NOQUOTES);
72 fprintf(f, " (%zu)", value_len);
73 if ((print_flags & PRINT_XATTR_STATFMT) &&
74 (strcmp(name, "system.data") == 0))
77 (!(print_flags & PRINT_XATTR_STATFMT) || (value_len < 40))) {
79 print_xattr_string(f, value, value_len, print_flags);
84 static int dump_attr(char *name, char *value, size_t value_len, void *data)
89 print_xattr(out, name, value, value_len, PRINT_XATTR_STATFMT);
93 void dump_inode_attributes(FILE *out, ext2_ino_t ino)
95 struct ext2_xattr_handle *h;
99 err = ext2fs_xattrs_open(current_fs, ino, &h);
103 err = ext2fs_xattrs_read(h);
107 err = ext2fs_xattrs_count(h, &sz);
111 fprintf(out, "Extended attributes:\n");
112 err = ext2fs_xattrs_iterate(h, dump_attr, out);
117 err = ext2fs_xattrs_close(&h);
120 void do_list_xattr(int argc, char **argv)
125 printf("%s: Usage: %s <file>\n", argv[0],
130 if (check_fs_open(argv[0]))
133 ino = string_to_inode(argv[1]);
137 dump_inode_attributes(stdout, ino);
140 void do_get_xattr(int argc, char **argv)
143 struct ext2_xattr_handle *h;
149 unsigned int handle_flags = 0;
153 while ((i = getopt(argc, argv, "Cf:rxV")) != -1) {
158 fp = fopen(optarg, "w");
165 handle_flags |= XATTR_HANDLE_FLAG_RAW;
168 print_flags |= PRINT_XATTR_HEX;
171 print_flags |= PRINT_XATTR_RAW|
172 PRINT_XATTR_NOQUOTES;
175 print_flags |= PRINT_XATTR_C;
182 if (optind != argc - 2) {
184 printf("%s: Usage: %s [-f outfile]|[-xVC] [-r] <file> <attr>\n",
190 if (check_fs_open(argv[0]))
193 ino = string_to_inode(argv[optind]);
197 err = ext2fs_xattrs_open(current_fs, ino, &h);
201 err = ext2fs_xattrs_flags(h, &handle_flags, NULL);
205 err = ext2fs_xattrs_read(h);
209 err = ext2fs_xattr_get(h, argv[optind + 1], (void **)&buf, &buflen);
214 fwrite(buf, buflen, 1, fp);
216 if (print_flags & PRINT_XATTR_RAW) {
217 if (print_flags & (PRINT_XATTR_HEX|PRINT_XATTR_C))
218 print_flags &= ~PRINT_XATTR_RAW;
219 print_xattr_string(stdout, buf, buflen, print_flags);
221 print_xattr(stdout, argv[optind + 1],
222 buf, buflen, print_flags);
227 ext2fs_free_mem(&buf);
229 ext2fs_xattrs_close(&h);
231 com_err(argv[0], err, "while getting extended attribute");
237 void do_set_xattr(int argc, char **argv)
240 struct ext2_xattr_handle *h;
244 unsigned int handle_flags = 0;
249 while ((i = getopt(argc, argv, "f:r")) != -1) {
254 fp = fopen(optarg, "r");
261 handle_flags |= XATTR_HANDLE_FLAG_RAW;
268 if (!(fp && optind == argc - 2) && !(!fp && optind == argc - 3)) {
270 printf("Usage:\t%s [-r] <file> <attr> <value>\n", argv[0]);
271 printf("\t%s -f <value_file> [-r] <file> <attr>\n", argv[0]);
275 if (check_fs_open(argv[0]))
277 if (check_fs_read_write(argv[0]))
279 if (check_fs_bitmaps(argv[0]))
282 ino = string_to_inode(argv[optind]);
286 err = ext2fs_xattrs_open(current_fs, ino, &h);
290 err = ext2fs_xattrs_flags(h, &handle_flags, NULL);
294 err = ext2fs_xattrs_read(h);
299 err = ext2fs_get_mem(current_fs->blocksize, &buf);
302 buflen = fread(buf, 1, current_fs->blocksize, fp);
304 buf = argv[optind + 2];
305 buflen = parse_c_string(buf);
308 err = ext2fs_xattr_set(h, argv[optind + 1], buf, buflen);
310 ext2fs_xattrs_close(&h);
312 com_err(argv[0], err, "while setting extended attribute");
316 ext2fs_free_mem(&buf);
320 void do_rm_xattr(int argc, char **argv)
323 struct ext2_xattr_handle *h;
328 printf("%s: Usage: %s <file> <attrs>...\n", argv[0], argv[0]);
332 if (check_fs_open(argv[0]))
334 if (check_fs_read_write(argv[0]))
336 if (check_fs_bitmaps(argv[0]))
339 ino = string_to_inode(argv[1]);
343 err = ext2fs_xattrs_open(current_fs, ino, &h);
347 err = ext2fs_xattrs_read(h);
351 for (i = 2; i < argc; i++) {
352 err = ext2fs_xattr_remove(h, argv[i]);
357 ext2fs_xattrs_close(&h);
359 com_err(argv[0], err, "while removing extended attribute");
363 * Return non-zero if the string has a minimal number of non-printable
366 static int is_mostly_printable(const char *cp, int len)
374 if (!isprint(*cp++)) {
383 static void safe_print(FILE *f, const char *cp, int len)
396 if ((ch < 32) || (ch == 0x7f)) {
398 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
404 static void dump_xattr_raw_entries(FILE *f, unsigned char *buf,
405 unsigned int start, unsigned int len,
406 unsigned value_start)
408 struct ext2_ext_attr_entry ent;
409 unsigned int off = start;
413 if ((*(__u16 *) (buf + off)) == 0) {
414 fprintf(f, "last entry found at offset %u (%04o)\n",
418 if ((off + sizeof(struct ext2_ext_attr_entry)) >= len) {
419 fprintf(f, "xattr buffer overrun at %u (len = %u)\n",
424 ext2fs_swap_ext_attr_entry(&ent,
425 (struct ext2_ext_attr_entry *) (buf + off));
427 ent = *((struct ext2_ext_attr_entry *) (buf + off));
429 fprintf(f, "offset = %d (%04o), name_len = %u, "
431 off, off, ent.e_name_len, ent.e_name_index);
432 vstart = value_start + ent.e_value_offs;
433 fprintf(f, "value_offset = %d (%04o), value_inum = %u, "
434 "value_size = %u\n", ent.e_value_offs,
435 vstart, ent.e_value_inum, ent.e_value_size);
436 off += sizeof(struct ext2_ext_attr_entry);
437 fprintf(f, "name = ");
438 if ((off + ent.e_name_len) >= len)
439 fprintf(f, "<runs off end>");
441 safe_print(f, (char *)(buf + off), ent.e_name_len);
443 if (ent.e_value_size == 0)
445 fprintf(f, "value = ");
446 if (ent.e_value_inum)
447 fprintf(f, "<ino %u>", ent.e_value_inum);
448 else if (ent.e_value_offs >= len ||
449 (vstart + ent.e_value_size) > len)
450 fprintf(f, "<runs off end>");
451 if (is_mostly_printable((char *)(buf + vstart),
453 safe_print(f, (char *)(buf + vstart),
456 fprintf(f, "<hexdump>\n");
457 do_byte_hexdump(f, (unsigned char *)(buf + vstart),
463 off += (ent.e_name_len + 3) & ~3;
467 void raw_inode_xattr_dump(FILE *f, unsigned char *buf, unsigned int len)
469 __u32 magic = ext2fs_le32_to_cpu(*((__le32 *) buf));
471 fprintf(f, "magic = %08x, length = %u, value_start =4 \n\n",
473 if (magic == EXT2_EXT_ATTR_MAGIC)
474 dump_xattr_raw_entries(f, buf, 4, len, 4);
477 void block_xattr_dump(FILE *f, unsigned char *buf, unsigned int len)
479 struct ext2_ext_attr_header header;
481 #ifdef WORDS_BIGENDIAN
482 ext2fs_swap_ext_attr_header(&header,
483 (struct ext2_ext_attr_header *) buf);
485 header = *((struct ext2_ext_attr_header *) buf);
487 fprintf(f, "magic = %08x, length = %u\n", header.h_magic, len);
488 if (header.h_magic != EXT2_EXT_ATTR_MAGIC)
490 fprintf(f, "refcount = %u, blocks = %u\n", header.h_refcount,
492 fprintf(f, "hash = %08x, checksum = %08x\n", header.h_hash,
494 fprintf(f, "reserved: %08x %08x %08x\n\n", header.h_reserved[0],
495 header.h_reserved[1], header.h_reserved[2]);
497 dump_xattr_raw_entries(f, buf,
498 sizeof(struct ext2_ext_attr_header), len, 0);