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.
20 /* Dump extended attributes */
21 static void dump_xattr_string(FILE *out, const char *str, int len)
26 /* check: is string "printable enough?" */
27 for (i = 0; i < len; i++)
31 if (printable <= len*7/8)
34 for (i = 0; i < len; i++)
36 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
37 (unsigned char)str[i]);
39 fprintf(out, "%02x ", (unsigned char)str[i]);
42 static int dump_attr(char *name, char *value, size_t value_len, void *data)
47 dump_xattr_string(out, name, strlen(name));
48 fprintf(out, " = \"");
49 dump_xattr_string(out, value, value_len);
50 fprintf(out, "\" (%zu)\n", value_len);
55 void dump_inode_attributes(FILE *out, ext2_ino_t ino)
57 struct ext2_xattr_handle *h;
61 err = ext2fs_xattrs_open(current_fs, ino, &h);
65 err = ext2fs_xattrs_read(h);
69 err = ext2fs_xattrs_count(h, &sz);
73 fprintf(out, "Extended attributes:\n");
74 err = ext2fs_xattrs_iterate(h, dump_attr, out);
79 err = ext2fs_xattrs_close(&h);
82 void do_list_xattr(int argc, char **argv)
87 printf("%s: Usage: %s <file>\n", argv[0],
92 if (check_fs_open(argv[0]))
95 ino = string_to_inode(argv[1]);
99 dump_inode_attributes(stdout, ino);
102 void do_get_xattr(int argc, char **argv)
105 struct ext2_xattr_handle *h;
113 while ((i = getopt(argc, argv, "f:")) != -1) {
118 fp = fopen(optarg, "w");
125 printf("%s: Usage: %s <file> <attr> [-f outfile]\n",
131 if (optind != argc - 2) {
132 printf("%s: Usage: %s <file> <attr> [-f outfile]\n", argv[0],
137 if (check_fs_open(argv[0]))
140 ino = string_to_inode(argv[optind]);
144 err = ext2fs_xattrs_open(current_fs, ino, &h);
148 err = ext2fs_xattrs_read(h);
152 err = ext2fs_xattr_get(h, argv[optind + 1], (void **)&buf, &buflen);
157 fwrite(buf, buflen, 1, fp);
159 dump_xattr_string(stdout, buf, buflen);
163 ext2fs_free_mem(&buf);
165 ext2fs_xattrs_close(&h);
167 com_err(argv[0], err, "while getting extended attribute");
173 void do_set_xattr(int argc, char **argv)
176 struct ext2_xattr_handle *h;
184 while ((i = getopt(argc, argv, "f:")) != -1) {
189 fp = fopen(optarg, "r");
196 printf("%s: Usage: %s <file> <attr> [-f infile | "
197 "value]\n", argv[0], argv[0]);
202 if (optind != argc - 2 && optind != argc - 3) {
203 printf("%s: Usage: %s <file> <attr> [-f infile | value>]\n",
208 if (check_fs_open(argv[0]))
210 if (check_fs_read_write(argv[0]))
212 if (check_fs_bitmaps(argv[0]))
215 ino = string_to_inode(argv[optind]);
219 err = ext2fs_xattrs_open(current_fs, ino, &h);
223 err = ext2fs_xattrs_read(h);
228 err = ext2fs_get_mem(current_fs->blocksize, &buf);
231 buflen = fread(buf, 1, current_fs->blocksize, fp);
233 buf = argv[optind + 2];
234 buflen = strlen(argv[optind + 2]);
237 err = ext2fs_xattr_set(h, argv[optind + 1], buf, buflen);
241 err = ext2fs_xattrs_write(h);
246 ext2fs_xattrs_close(&h);
248 com_err(argv[0], err, "while setting extended attribute");
252 ext2fs_free_mem(&buf);
256 void do_rm_xattr(int argc, char **argv)
259 struct ext2_xattr_handle *h;
264 printf("%s: Usage: %s <file> <attrs>...\n", argv[0], argv[0]);
268 if (check_fs_open(argv[0]))
270 if (check_fs_read_write(argv[0]))
272 if (check_fs_bitmaps(argv[0]))
275 ino = string_to_inode(argv[1]);
279 err = ext2fs_xattrs_open(current_fs, ino, &h);
283 err = ext2fs_xattrs_read(h);
287 for (i = 2; i < argc; i++) {
291 err = ext2fs_xattr_remove(h, argv[i]);
296 err = ext2fs_xattrs_write(h);
300 ext2fs_xattrs_close(&h);
302 com_err(argv[0], err, "while removing extended attribute");