static errcode_t ext2fs_inline_data_ea_get(struct ext2_inline_data *data) { struct ext2_xattr_handle *handle; errcode_t retval; data->ea_size = 0; data->ea_data = 0; retval = ext2fs_xattrs_open(data->fs, data->ino, &handle); if (retval) return retval; retval = ext2fs_xattrs_read(handle); if (retval) goto err; retval = ext2fs_xattr_get(handle, "system.data", (void **)&data->ea_data, &data->ea_size); if (retval == EXT2_ET_EA_KEY_NOT_FOUND) { data->ea_size = 0; data->ea_data = NULL; retval = 0; } else if (retval) goto err; err: (void) ext2fs_xattrs_close(&handle); return retval; }
void dump_inode_attributes(FILE *out, ext2_ino_t ino) { struct ext2_xattr_handle *h; size_t sz; errcode_t err; err = ext2fs_xattrs_open(current_fs, ino, &h); if (err) return; err = ext2fs_xattrs_read(h); if (err) goto out; err = ext2fs_xattrs_count(h, &sz); if (err || sz == 0) goto out; fprintf(out, "Extended attributes:\n"); err = ext2fs_xattrs_iterate(h, dump_attr, out); if (err) goto out; out: err = ext2fs_xattrs_close(&h); }
void do_rm_xattr(int argc, char **argv) { ext2_ino_t ino; struct ext2_xattr_handle *h; int i; errcode_t err; if (argc < 3) { printf("%s: Usage: %s <file> <attrs>...\n", argv[0], argv[0]); return; } if (check_fs_open(argv[0])) return; if (check_fs_read_write(argv[0])) return; if (check_fs_bitmaps(argv[0])) return; ino = string_to_inode(argv[1]); if (!ino) return; err = ext2fs_xattrs_open(current_fs, ino, &h); if (err) return; err = ext2fs_xattrs_read(h); if (err) goto out; for (i = 2; i < argc; i++) { size_t buflen; char *buf; err = ext2fs_xattr_remove(h, argv[i]); if (err) goto out; } err = ext2fs_xattrs_write(h); if (err) goto out; out: ext2fs_xattrs_close(&h); if (err) com_err(argv[0], err, "while removing extended attribute"); }
errcode_t ext2fs_inline_data_ea_remove(ext2_filsys fs, ext2_ino_t ino) { struct ext2_xattr_handle *handle; errcode_t retval; retval = ext2fs_xattrs_open(fs, ino, &handle); if (retval) return retval; retval = ext2fs_xattrs_read(handle); if (retval) goto err; retval = ext2fs_xattr_remove(handle, "system.data"); if (retval) goto err; retval = ext2fs_xattrs_write(handle); err: (void) ext2fs_xattrs_close(&handle); return retval; }
static errcode_t ext2fs_inline_data_ea_set(struct ext2_inline_data *data) { struct ext2_xattr_handle *handle; errcode_t retval; retval = ext2fs_xattrs_open(data->fs, data->ino, &handle); if (retval) return retval; retval = ext2fs_xattrs_read(handle); if (retval) goto err; retval = ext2fs_xattr_set(handle, "system.data", data->ea_data, data->ea_size); if (retval) goto err; retval = ext2fs_xattrs_write(handle); err: (void) ext2fs_xattrs_close(&handle); return retval; }
static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino, const char *filename) { errcode_t retval, close_retval; struct ext2_xattr_handle *handle; ssize_t size, value_size; char *list = NULL; int i; size = llistxattr(filename, NULL, 0); if (size == -1) { retval = errno; com_err(__func__, retval, _("while listing attributes of \"%s\""), filename); return retval; } else if (size == 0) { return 0; } retval = ext2fs_xattrs_open(fs, ino, &handle); if (retval) { if (retval == EXT2_ET_MISSING_EA_FEATURE) return 0; com_err(__func__, retval, _("while opening inode %u"), ino); return retval; } retval = ext2fs_get_mem(size, &list); if (retval) { com_err(__func__, retval, _("while allocating memory")); goto out; } size = llistxattr(filename, list, size); if (size == -1) { retval = errno; com_err(__func__, retval, _("while listing attributes of \"%s\""), filename); goto out; } for (i = 0; i < size; i += strlen(&list[i]) + 1) { const char *name = &list[i]; char *value; value_size = lgetxattr(filename, name, NULL, 0); if (value_size == -1) { retval = errno; com_err(__func__, retval, _("while reading attribute \"%s\" of \"%s\""), name, filename); break; } retval = ext2fs_get_mem(value_size, &value); if (retval) { com_err(__func__, retval, _("while allocating memory")); break; } value_size = lgetxattr(filename, name, value, value_size); if (value_size == -1) { ext2fs_free_mem(&value); retval = errno; com_err(__func__, retval, _("while reading attribute \"%s\" of \"%s\""), name, filename); break; } retval = ext2fs_xattr_set(handle, name, value, value_size); ext2fs_free_mem(&value); if (retval) { com_err(__func__, retval, _("while writing attribute \"%s\" to inode %u"), name, ino); break; } } out: ext2fs_free_mem(&list); close_retval = ext2fs_xattrs_close(&handle); if (close_retval) { com_err(__func__, retval, _("while closing inode %u"), ino); retval = retval ? retval : close_retval; } return retval; return 0; }
void do_set_xattr(int argc, char **argv) { ext2_ino_t ino; struct ext2_xattr_handle *h; FILE *fp = NULL; char *buf = NULL; size_t buflen; unsigned int handle_flags = 0; int i; errcode_t err; reset_getopt(); while ((i = getopt(argc, argv, "f:r")) != -1) { switch (i) { case 'f': if (fp) fclose(fp); fp = fopen(optarg, "r"); if (fp == NULL) { perror(optarg); return; } break; case 'r': handle_flags |= XATTR_HANDLE_FLAG_RAW; break; default: goto print_usage; } } if (!(fp && optind == argc - 2) && !(!fp && optind == argc - 3)) { print_usage: printf("Usage:\t%s [-r] <file> <attr> <value>\n", argv[0]); printf("\t%s -f <value_file> [-r] <file> <attr>\n", argv[0]); goto out2; } if (check_fs_open(argv[0])) goto out2; if (check_fs_read_write(argv[0])) goto out2; if (check_fs_bitmaps(argv[0])) goto out2; ino = string_to_inode(argv[optind]); if (!ino) goto out2; err = ext2fs_xattrs_open(current_fs, ino, &h); if (err) goto out2; err = ext2fs_xattrs_flags(h, &handle_flags, NULL); if (err) goto out; err = ext2fs_xattrs_read(h); if (err) goto out; if (fp) { err = ext2fs_get_mem(current_fs->blocksize, &buf); if (err) goto out; buflen = fread(buf, 1, current_fs->blocksize, fp); } else { buf = argv[optind + 2]; buflen = parse_c_string(buf); } err = ext2fs_xattr_set(h, argv[optind + 1], buf, buflen); out: ext2fs_xattrs_close(&h); if (err) com_err(argv[0], err, "while setting extended attribute"); out2: if (fp) { fclose(fp); ext2fs_free_mem(&buf); } }
void do_get_xattr(int argc, char **argv) { ext2_ino_t ino; struct ext2_xattr_handle *h; FILE *fp = NULL; char *buf = NULL; size_t buflen; int i; int print_flags = 0; unsigned int handle_flags = 0; errcode_t err; reset_getopt(); while ((i = getopt(argc, argv, "Cf:rxV")) != -1) { switch (i) { case 'f': if (fp) fclose(fp); fp = fopen(optarg, "w"); if (fp == NULL) { perror(optarg); return; } break; case 'r': handle_flags |= XATTR_HANDLE_FLAG_RAW; break; case 'x': print_flags |= PRINT_XATTR_HEX; break; case 'V': print_flags |= PRINT_XATTR_RAW| PRINT_XATTR_NOQUOTES; break; case 'C': print_flags |= PRINT_XATTR_C; break; default: goto usage; } } if (optind != argc - 2) { usage: printf("%s: Usage: %s [-f outfile]|[-xVC] [-r] <file> <attr>\n", argv[0], argv[0]); goto out2; } if (check_fs_open(argv[0])) goto out2; ino = string_to_inode(argv[optind]); if (!ino) goto out2; err = ext2fs_xattrs_open(current_fs, ino, &h); if (err) goto out2; err = ext2fs_xattrs_flags(h, &handle_flags, NULL); if (err) goto out; err = ext2fs_xattrs_read(h); if (err) goto out; err = ext2fs_xattr_get(h, argv[optind + 1], (void **)&buf, &buflen); if (err) goto out; if (fp) { fwrite(buf, buflen, 1, fp); } else { if (print_flags & PRINT_XATTR_RAW) { if (print_flags & (PRINT_XATTR_HEX|PRINT_XATTR_C)) print_flags &= ~PRINT_XATTR_RAW; print_xattr_string(stdout, buf, buflen, print_flags); } else { print_xattr(stdout, argv[optind + 1], buf, buflen, print_flags); } printf("\n"); } ext2fs_free_mem(&buf); out: ext2fs_xattrs_close(&h); if (err) com_err(argv[0], err, "while getting extended attribute"); out2: if (fp) fclose(fp); }
void do_set_xattr(int argc, char **argv) { ext2_ino_t ino; struct ext2_xattr_handle *h; FILE *fp = NULL; char *buf = NULL; size_t buflen; int i; errcode_t err; reset_getopt(); while ((i = getopt(argc, argv, "f:")) != -1) { switch (i) { case 'f': fp = fopen(optarg, "r"); if (fp == NULL) { perror(optarg); return; } break; default: printf("%s: Usage: %s <file> <attr> [-f infile | " "value]\n", argv[0], argv[0]); return; } } if (optind != argc - 2 && optind != argc - 3) { printf("%s: Usage: %s <file> <attr> [-f infile | value>]\n", argv[0], argv[0]); return; } if (check_fs_open(argv[0])) return; if (check_fs_read_write(argv[0])) return; if (check_fs_bitmaps(argv[0])) return; ino = string_to_inode(argv[optind]); if (!ino) return; err = ext2fs_xattrs_open(current_fs, ino, &h); if (err) return; err = ext2fs_xattrs_read(h); if (err) goto out; if (fp) { err = ext2fs_get_mem(current_fs->blocksize, &buf); if (err) goto out; buflen = fread(buf, 1, current_fs->blocksize, fp); } else { buf = argv[optind + 2]; buflen = strlen(argv[optind + 2]); } err = ext2fs_xattr_set(h, argv[optind + 1], buf, buflen); if (err) goto out; err = ext2fs_xattrs_write(h); if (err) goto out; out: if (fp) { fclose(fp); ext2fs_free_mem(&buf); } ext2fs_xattrs_close(&h); if (err) com_err(argv[0], err, "while setting extended attribute"); }
void do_get_xattr(int argc, char **argv) { ext2_ino_t ino; struct ext2_xattr_handle *h; FILE *fp = NULL; char *buf = NULL; size_t buflen; int i; errcode_t err; reset_getopt(); while ((i = getopt(argc, argv, "f:")) != -1) { switch (i) { case 'f': fp = fopen(optarg, "w"); if (fp == NULL) { perror(optarg); return; } break; default: printf("%s: Usage: %s <file> <attr> [-f outfile]\n", argv[0], argv[0]); return; } } if (optind != argc - 2) { printf("%s: Usage: %s <file> <attr> [-f outfile]\n", argv[0], argv[0]); return; } if (check_fs_open(argv[0])) return; ino = string_to_inode(argv[optind]); if (!ino) return; err = ext2fs_xattrs_open(current_fs, ino, &h); if (err) return; err = ext2fs_xattrs_read(h); if (err) goto out; err = ext2fs_xattr_get(h, argv[optind + 1], (void **)&buf, &buflen); if (err) goto out; if (fp) { fwrite(buf, buflen, 1, fp); fclose(fp); } else { dump_xattr_string(stdout, buf, buflen); printf("\n"); } if (buf) ext2fs_free_mem(&buf); out: ext2fs_xattrs_close(&h); if (err) com_err(argv[0], err, "while getting extended attribute"); }
static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino, const char *filename) { #ifdef HAVE_LLISTXATTR errcode_t retval, close_retval; struct ext2_inode inode; struct ext2_xattr_handle *handle; ssize_t size, value_size; char *list; int i; size = llistxattr(filename, NULL, 0); if (size == -1) { com_err(__func__, errno, "llistxattr failed on %s", filename); return errno; } else if (size == 0) { return 0; } retval = ext2fs_xattrs_open(fs, ino, &handle); if (retval) { if (retval == EXT2_ET_MISSING_EA_FEATURE) return 0; com_err(__func__, retval, "while opening inode %u", ino); return retval; } retval = ext2fs_get_mem(size, &list); if (retval) { com_err(__func__, retval, "whilst allocating memory"); goto out; } size = llistxattr(filename, list, size); if (size == -1) { com_err(__func__, errno, "llistxattr failed on %s", filename); retval = errno; goto out; } for (i = 0; i < size; i += strlen(&list[i]) + 1) { const char *name = &list[i]; char *value; value_size = getxattr(filename, name, NULL, 0); if (value_size == -1) { com_err(__func__, errno, "getxattr failed on %s", filename); retval = errno; break; } retval = ext2fs_get_mem(value_size, &value); if (retval) { com_err(__func__, retval, "whilst allocating memory"); break; } value_size = getxattr(filename, name, value, value_size); if (value_size == -1) { ext2fs_free_mem(&value); com_err(__func__, errno, "getxattr failed on %s", filename); retval = errno; break; } retval = ext2fs_xattr_set(handle, name, value, value_size); ext2fs_free_mem(&value); if (retval) { com_err(__func__, retval, "while writing xattr %u", ino); break; } } out: ext2fs_free_mem(&list); close_retval = ext2fs_xattrs_close(&handle); if (close_retval) { com_err(__func__, retval, "while closing inode %u", ino); retval = retval ? retval : close_retval; } return retval; #else /* HAVE_LLISTXATTR */ return 0; #endif /* HAVE_LLISTXATTR */ }