dpl_status_t dpl_cdmi_req_add_metadata(dpl_req_t *req, const dpl_dict_t *metadata, int append) { dpl_status_t ret, ret2; struct cdmi_req_add_md_arg arg; memset(&arg, 0, sizeof (arg)); if (append) { //iterate metadata object arg.field = dpl_sbuf_new(30); if (NULL == arg.field) { ret = DPL_ENOMEM; goto end; } ret2 = dpl_dict_iterate(metadata, cb_cdmi_req_add_metadata, &arg); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } ret2 = dpl_sbuf_add(arg.field, "", 1); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } ret2 = dpl_req_set_subresource(req, arg.field->buf); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } } ret2 = dpl_req_add_metadata(req, metadata); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } ret = DPL_SUCCESS; end: if (NULL != arg.field) dpl_sbuf_free(arg.field); return ret; }
/** * get metadata from values * * @param values * @param metadatap * @param sysmdp * * @return */ dpl_status_t dpl_cdmi_get_metadata_from_values(const dpl_dict_t *values, dpl_dict_t **metadatap, dpl_sysmd_t *sysmdp) { dpl_dict_t *metadata = NULL; dpl_status_t ret, ret2; struct metadata_conven mc; if (metadatap) { metadata = dpl_dict_new(13); if (NULL == metadata) { ret = DPL_ENOMEM; goto end; } } memset(&mc, 0, sizeof (mc)); mc.metadata = metadata; mc.sysmdp = sysmdp; if (sysmdp) sysmdp->mask = 0; ret2 = dpl_dict_iterate(values, cb_values_iterate, &mc); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } if (NULL != metadatap) { *metadatap = metadata; metadata = NULL; } ret = DPL_SUCCESS; end: if (NULL != metadata) dpl_dict_free(metadata); return ret; }
dpl_status_t dpl_posix_setattr(const char *path, const dpl_dict_t *metadata, const dpl_sysmd_t *sysmd) { dpl_status_t ret, ret2; int iret; if (sysmd) { switch (sysmd->mask) { case DPL_SYSMD_MASK_SIZE: iret = truncate(path, sysmd->size); if (-1 == iret) { perror("truncate"); ret = DPL_FAILURE; goto end; } break ; case DPL_SYSMD_MASK_CANNED_ACL: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_STORAGE_CLASS: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_ATIME: case DPL_SYSMD_MASK_MTIME: { struct utimbuf times; times.actime = sysmd->atime; times.modtime = sysmd->mtime; iret = utime(path, ×); if (-1 == iret) { perror("utime"); ret = DPL_FAILURE; goto end; } } break ; case DPL_SYSMD_MASK_CTIME: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_ETAG: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_LOCATION_CONSTRAINT: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_OWNER: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_GROUP: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_ACL: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_ID: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_PARENT_ID: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_FTYPE: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_ENTERPRISE_NUMBER: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_PATH: ret = DPL_ENOTSUPP; goto end; case DPL_SYSMD_MASK_VERSION: ret = DPL_ENOTSUPP; goto end; } } if (metadata) { ret2 = dpl_dict_iterate(metadata, cb_posix_setattr, (char *) path); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } } ret = DPL_SUCCESS; end: return ret; }
/** * parse a value into a suitable metadata or sysmd * * @param key * @param val * @param metadatum_func * @param cb_arg * @param metadata * @param sysmdp * * @return */ dpl_status_t dpl_cdmi_get_metadatum_from_value(const char *key, dpl_value_t *val, dpl_metadatum_func_t metadatum_func, void *cb_arg, dpl_dict_t *metadata, dpl_sysmd_t *sysmdp) { dpl_status_t ret, ret2; dpl_dict_var_t *var; dpl_cdmi_object_id_t obj_id; DPRINTF("key=%s val.type=%d\n", key, val->type); if (val == NULL) { ret = DPL_EINVAL; goto end; } if (sysmdp) { if (!strcmp(key, "objectID")) { if (DPL_VALUE_STRING != val->type) { ret = DPL_EINVAL; goto end; } ret2 = dpl_cdmi_string_to_object_id(dpl_sbuf_get_str(val->string), &obj_id); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } ret2 = dpl_cdmi_opaque_to_string(&obj_id, sysmdp->id); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } sysmdp->mask |= DPL_SYSMD_MASK_ID; sysmdp->enterprise_number = obj_id.enterprise_number; sysmdp->mask |= DPL_SYSMD_MASK_ENTERPRISE_NUMBER; } else if (!strcmp(key, "parentID")) { if (DPL_VALUE_STRING != val->type) { ret = DPL_EINVAL; goto end; } if (strcmp(dpl_sbuf_get_str(val->string), "")) { ret2 = dpl_cdmi_string_to_object_id(dpl_sbuf_get_str(val->string), &obj_id); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } ret2 = dpl_cdmi_opaque_to_string(&obj_id, sysmdp->parent_id); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } sysmdp->mask |= DPL_SYSMD_MASK_PARENT_ID; } } else if (!strcmp(key, "objectType")) { if (DPL_VALUE_STRING != val->type) { ret = DPL_EINVAL; goto end; } sysmdp->mask |= DPL_SYSMD_MASK_FTYPE; sysmdp->ftype = dpl_cdmi_content_type_to_ftype(dpl_sbuf_get_str(val->string)); } } if (!strcmp(key, "metadata")) { //this is the metadata object if (DPL_VALUE_SUBDICT != val->type) { ret = DPL_EINVAL; goto end; } if (sysmdp) { //some sysmds are stored in metadata var = dpl_dict_get(val->subdict, "cdmi_mtime"); if (NULL != var) { if (DPL_VALUE_STRING != var->val->type) { ret = DPL_EINVAL; goto end; } sysmdp->mask |= DPL_SYSMD_MASK_MTIME; sysmdp->mtime = dpl_iso8601totime(dpl_sbuf_get_str(var->val->string)); } var = dpl_dict_get(val->subdict, "cdmi_atime"); if (NULL != var) { if (DPL_VALUE_STRING != var->val->type) { ret = DPL_EINVAL; goto end; } sysmdp->mask |= DPL_SYSMD_MASK_ATIME; sysmdp->atime = dpl_iso8601totime(dpl_sbuf_get_str(var->val->string)); } var = dpl_dict_get(val->subdict, "cdmi_size"); if (NULL != var) { if (DPL_VALUE_STRING != var->val->type) { ret = DPL_EINVAL; goto end; } sysmdp->mask |= DPL_SYSMD_MASK_SIZE; sysmdp->size = strtoull(dpl_sbuf_get_str(var->val->string), NULL, 0); } } if (metadata) { struct metadata_list_arg arg; arg.metadatum_func = metadatum_func; arg.metadata = metadata; arg.cb_arg = cb_arg; //iterate metadata object ret2 = dpl_dict_iterate(val->subdict, cb_metadata_list, &arg); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } } } ret = DPL_SUCCESS; end: return ret; }
int cmd_get(int argc, char **argv) { int ret; char opt; char *path = NULL; dpl_dict_t *metadata = NULL; struct get_data get_data; int do_stdout = 0; int kflag = 0; char *local_file = NULL; int start = -1; int start_inited = 0; int end = -1; int end_inited = 0; int mflag = 0; memset(&get_data, 0, sizeof (get_data)); get_data.fd = -1; var_set("status", "1", VAR_CMD_SET, NULL); optind = 0; while ((opt = getopt(argc, argv, usage_getoptstr(get_usage))) != -1) switch (opt) { case 'm': mflag = 1; break ; case 's': start = strtol(optarg, NULL, 0); start_inited = 1; break ; case 'e': end = strtol(optarg, NULL, 0); end_inited = 1; break ; case 'k': kflag = 1; break ; case '?': default: usage_help(&get_cmd); return SHELL_CONT; } argc -= optind; argv += optind; if (start_inited != end_inited) { fprintf(stderr, "please provide -s and -e\n"); return SHELL_CONT; } if (2 == argc) { path = argv[0]; local_file = argv[1]; } else if (1 == argc) { path = argv[0]; local_file = rindex(path, '/'); if (NULL != local_file) local_file++; else local_file = path; } else { usage_help(&get_cmd); return SHELL_CONT; } if (!strcmp(local_file, "-")) { get_data.fd = 1; do_stdout = 1; } else if ('|' == local_file[0]) { get_data.pipe = popen(local_file + 1, "w"); if (NULL == get_data.pipe) { fprintf(stderr, "pipe failed\n"); goto end; } } else { ret = access(local_file, F_OK); if (0 == ret) { if (1 == ask_for_confirmation("file already exists, overwrite?")) return SHELL_CONT; } get_data.fd = open(local_file, O_WRONLY|O_CREAT, 0600); if (-1 == get_data.fd) { perror("open"); goto end; } } if (1 == start_inited && 1 == end_inited) { char *data_buf; u_int data_len; ret = dpl_openread_range(ctx, path, (1 == kflag ? DPL_VFILE_FLAG_ENCRYPT : 0u), NULL, start, end, &data_buf, &data_len, &metadata); if (DPL_SUCCESS != ret) { fprintf(stderr, "status: %s (%d)\n", dpl_status_str(ret), ret); goto end; } ret = write_all(get_data.fd, data_buf, data_len); free(data_buf); if (0 != ret) { fprintf(stderr, "short write\n"); goto end; } if (1 == mflag) dpl_dict_iterate(metadata, cb_print_metadata, NULL); } else { ret = dpl_openread(ctx, path, (1 == kflag ? DPL_VFILE_FLAG_ENCRYPT : 0u), NULL, cb_get_buffered, &get_data, &metadata); if (DPL_SUCCESS != ret) { fprintf(stderr, "status: %s (%d)\n", dpl_status_str(ret), ret); goto end; } if (1 == mflag) dpl_dict_iterate(metadata, cb_print_metadata, NULL); } var_set("status", "0", VAR_CMD_SET, NULL); end: if (NULL != metadata) dpl_dict_free(metadata); if (0 == do_stdout) { if (-1 != get_data.fd) close(get_data.fd); if (NULL != get_data.pipe) pclose(get_data.pipe); } return SHELL_CONT; }
dpl_status_t dpl_posix_get_metadatum_from_value(const char *key, dpl_value_t *val, dpl_metadatum_func_t metadatum_func, void *cb_arg, dpl_dict_t *metadata, dpl_sysmd_t *sysmdp) { dpl_status_t ret, ret2; int iret; char buf[256]; struct metadata_conven mc = { .metadata=metadata, .sysmdp=sysmdp }; if (sysmdp) { if (!strcmp(key, "atime")) { assert(val->type == DPL_VALUE_STRING); sysmdp->atime = strtoul(dpl_sbuf_get_str(val->string), NULL, 0); sysmdp->mask |= DPL_SYSMD_MASK_ATIME; } else if (!strcmp(key, "mtime")) { assert(val->type == DPL_VALUE_STRING); sysmdp->mtime = strtoul(dpl_sbuf_get_str(val->string), NULL, 0); sysmdp->mask |= DPL_SYSMD_MASK_MTIME; } else if (!strcmp(key, "ctime")) { assert(val->type == DPL_VALUE_STRING); sysmdp->ctime = strtoul(dpl_sbuf_get_str(val->string), NULL, 0); sysmdp->mask |= DPL_SYSMD_MASK_CTIME; } else if (!strcmp(key, "size")) { assert(val->type == DPL_VALUE_STRING); sysmdp->size = strtoul(dpl_sbuf_get_str(val->string), NULL, 0); sysmdp->mask |= DPL_SYSMD_MASK_SIZE; } else if (!strcmp(key, "uid")) { uid_t uid; struct passwd pwd, *pwdp; assert(val->type == DPL_VALUE_STRING); uid = atoi(dpl_sbuf_get_str(val->string)); iret = getpwuid_r(uid, &pwd, buf, sizeof (buf), &pwdp); if (iret == -1) { perror("getpwuid"); ret = DPL_FAILURE; goto end; } snprintf(sysmdp->owner, sizeof (sysmdp->owner), "%s", pwdp->pw_name); sysmdp->mask |= DPL_SYSMD_MASK_OWNER; } else if (!strcmp(key, "gid")) { gid_t gid; struct group grp, *grpp; assert(val->type == DPL_VALUE_STRING); gid = atoi(dpl_sbuf_get_str(val->string)); iret = getgrgid_r(gid, &grp, buf, sizeof (buf), &grpp); if (iret == -1) { perror("getgrgid"); ret = DPL_FAILURE; goto end; } snprintf(sysmdp->group, sizeof (sysmdp->group), "%s", grpp->gr_name); sysmdp->mask |= DPL_SYSMD_MASK_GROUP; } else if (!strcmp(key, "ino")) { assert(val->type == DPL_VALUE_STRING); snprintf(sysmdp->id, sizeof (sysmdp->id), "%s", dpl_sbuf_get_str(val->string)); sysmdp->mask |= DPL_SYSMD_MASK_ID; } } if (!strcmp(key, "xattr")) { //this is the metadata object if (DPL_VALUE_SUBDICT != val->type) { ret = DPL_EINVAL; goto end; } if (metadata) { if (metadatum_func) { ret2 = metadatum_func(cb_arg, key, val); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } } //add xattr's md into metadata ret2 = dpl_dict_iterate(val->subdict, cb_posix_get_metadatum_from_xattr_value, &mc); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } } } ret = DPL_SUCCESS; end: return ret; }
dpl_status_t dpl_posix_get_buffered(dpl_ctx_t *ctx, const char *bucket, const char *resource, const char *subresource, const dpl_option_t *option, dpl_ftype_t object_type, const dpl_condition_t *condition, const dpl_range_t *range, dpl_metadatum_func_t metadatum_func, dpl_dict_t **metadatap, dpl_sysmd_t *sysmdp, dpl_buffer_func_t buffer_func, void *cb_arg, char **locationp) { dpl_status_t ret, ret2; char path[MAXPATHLEN]; int fd = -1; int iret; char buf[8192]; ssize_t cc; dpl_dict_t *all_mds = NULL; struct get_conven gc; DPL_TRACE(ctx, DPL_TRACE_BACKEND, ""); memset(&gc, 0, sizeof (gc)); gc.metadata = dpl_dict_new(13); if (NULL == gc.metadata) { ret = DPL_ENOMEM; goto end; } gc.sysmdp = sysmdp; gc.metadatum_func = metadatum_func; gc.buffer_func = buffer_func; gc.cb_arg = cb_arg; snprintf(path, sizeof (path), "/%s/%s", ctx->base_path ? ctx->base_path : "", resource); switch (object_type) { case DPL_FTYPE_UNDEF: case DPL_FTYPE_CAP: case DPL_FTYPE_DOM: case DPL_FTYPE_DIR: case DPL_FTYPE_CHRDEV: case DPL_FTYPE_BLKDEV: case DPL_FTYPE_FIFO: case DPL_FTYPE_SOCKET: case DPL_FTYPE_SYMLINK: ret = DPL_EINVAL; goto end; case DPL_FTYPE_ANY: case DPL_FTYPE_REG: fd = open(path, O_RDONLY); if (-1 == fd) { perror("open"); ret = DPL_FAILURE; goto end; } break ; } if (range) { iret = lseek(fd, range->start, SEEK_SET); if (-1 == iret) { perror("lseek"); ret = DPL_FAILURE; goto end; } } ret2 = dpl_posix_head_raw(ctx, bucket, resource, subresource, option, object_type, condition, &all_mds, locationp); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } ret2 = dpl_dict_iterate(all_mds, cb_get_value, &gc); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } while (1) { cc = read(fd, buf, sizeof (buf)); if (-1 == cc) { perror("read"); ret = DPL_FAILURE; goto end; } ret2 = cb_get_buffer(&gc, buf, cc); if (DPL_SUCCESS != ret2) { ret = ret2; goto end; } if (0 == cc) break ; } if (NULL != metadatap) { *metadatap = gc.metadata; gc.metadata = NULL; } ret = DPL_SUCCESS; end: if (NULL != all_mds) dpl_dict_free(all_mds); if (NULL != gc.metadata) dpl_dict_free(gc.metadata); if (-1 == fd) close(fd); DPL_TRACE(ctx, DPL_TRACE_BACKEND, "ret=%d", ret); return ret; }