]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - debugfs/xattrs.c
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");
203 if (!(fp
&& optind
== argc
- 2) && !(!fp
&& optind
== argc
- 3)) {
205 printf("Usage:\t%s <file> <attr> <value>\n", argv
[0]);
206 printf("\t%s -f <value_file> <file> <attr>\n", argv
[0]);
210 if (check_fs_open(argv
[0]))
212 if (check_fs_read_write(argv
[0]))
214 if (check_fs_bitmaps(argv
[0]))
217 ino
= string_to_inode(argv
[optind
]);
221 err
= ext2fs_xattrs_open(current_fs
, ino
, &h
);
225 err
= ext2fs_xattrs_read(h
);
230 err
= ext2fs_get_mem(current_fs
->blocksize
, &buf
);
233 buflen
= fread(buf
, 1, current_fs
->blocksize
, fp
);
235 buf
= argv
[optind
+ 2];
236 buflen
= strlen(argv
[optind
+ 2]);
239 err
= ext2fs_xattr_set(h
, argv
[optind
+ 1], buf
, buflen
);
243 err
= ext2fs_xattrs_write(h
);
248 ext2fs_xattrs_close(&h
);
250 com_err(argv
[0], err
, "while setting extended attribute");
254 ext2fs_free_mem(&buf
);
258 void do_rm_xattr(int argc
, char **argv
)
261 struct ext2_xattr_handle
*h
;
266 printf("%s: Usage: %s <file> <attrs>...\n", argv
[0], argv
[0]);
270 if (check_fs_open(argv
[0]))
272 if (check_fs_read_write(argv
[0]))
274 if (check_fs_bitmaps(argv
[0]))
277 ino
= string_to_inode(argv
[1]);
281 err
= ext2fs_xattrs_open(current_fs
, ino
, &h
);
285 err
= ext2fs_xattrs_read(h
);
289 for (i
= 2; i
< argc
; i
++) {
290 err
= ext2fs_xattr_remove(h
, argv
[i
]);
295 err
= ext2fs_xattrs_write(h
);
299 ext2fs_xattrs_close(&h
);
301 com_err(argv
[0], err
, "while removing extended attribute");