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 if (strcmp(name, "system.data") != 0) {
49 fprintf(out, " = \"");
50 dump_xattr_string(out, value, value_len);
53 fprintf(out, " (%zu)\n", value_len);
58 void dump_inode_attributes(FILE *out, ext2_ino_t ino)
60 struct ext2_xattr_handle *h;
64 err = ext2fs_xattrs_open(current_fs, ino, &h);
68 err = ext2fs_xattrs_read(h);
72 err = ext2fs_xattrs_count(h, &sz);
76 fprintf(out, "Extended attributes:\n");
77 err = ext2fs_xattrs_iterate(h, dump_attr, out);
82 err = ext2fs_xattrs_close(&h);
85 void do_list_xattr(int argc, char **argv)
90 printf("%s: Usage: %s <file>\n", argv[0],
95 if (check_fs_open(argv[0]))
98 ino = string_to_inode(argv[1]);
102 dump_inode_attributes(stdout, ino);
105 void do_get_xattr(int argc, char **argv)
108 struct ext2_xattr_handle *h;
116 while ((i = getopt(argc, argv, "f:")) != -1) {
121 fp = fopen(optarg, "w");
128 printf("%s: Usage: %s <file> <attr> [-f outfile]\n",
134 if (optind != argc - 2) {
135 printf("%s: Usage: %s <file> <attr> [-f outfile]\n", argv[0],
140 if (check_fs_open(argv[0]))
143 ino = string_to_inode(argv[optind]);
147 err = ext2fs_xattrs_open(current_fs, ino, &h);
151 err = ext2fs_xattrs_read(h);
155 err = ext2fs_xattr_get(h, argv[optind + 1], (void **)&buf, &buflen);
160 fwrite(buf, buflen, 1, fp);
162 dump_xattr_string(stdout, buf, buflen);
166 ext2fs_free_mem(&buf);
168 ext2fs_xattrs_close(&h);
170 com_err(argv[0], err, "while getting extended attribute");
176 void do_set_xattr(int argc, char **argv)
179 struct ext2_xattr_handle *h;
187 while ((i = getopt(argc, argv, "f:")) != -1) {
192 fp = fopen(optarg, "r");
199 printf("%s: Usage: %s <file> <attr> [-f infile | "
200 "value]\n", argv[0], argv[0]);
205 if (optind != argc - 2 && optind != argc - 3) {
206 printf("%s: Usage: %s <file> <attr> [-f infile | value>]\n",
211 if (check_fs_open(argv[0]))
213 if (check_fs_read_write(argv[0]))
215 if (check_fs_bitmaps(argv[0]))
218 ino = string_to_inode(argv[optind]);
222 err = ext2fs_xattrs_open(current_fs, ino, &h);
226 err = ext2fs_xattrs_read(h);
231 err = ext2fs_get_mem(current_fs->blocksize, &buf);
234 buflen = fread(buf, 1, current_fs->blocksize, fp);
236 buf = argv[optind + 2];
237 buflen = strlen(argv[optind + 2]);
240 err = ext2fs_xattr_set(h, argv[optind + 1], buf, buflen);
244 err = ext2fs_xattrs_write(h);
249 ext2fs_xattrs_close(&h);
251 com_err(argv[0], err, "while setting extended attribute");
255 ext2fs_free_mem(&buf);
259 void do_rm_xattr(int argc, char **argv)
262 struct ext2_xattr_handle *h;
267 printf("%s: Usage: %s <file> <attrs>...\n", argv[0], argv[0]);
271 if (check_fs_open(argv[0]))
273 if (check_fs_read_write(argv[0]))
275 if (check_fs_bitmaps(argv[0]))
278 ino = string_to_inode(argv[1]);
282 err = ext2fs_xattrs_open(current_fs, ino, &h);
286 err = ext2fs_xattrs_read(h);
290 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");