static gs_error_t * hc_upload_content(gs_grid_storage_t *hc, struct hc_url_s *url, const char *local_path, const char *stgpol, const char *sys_metadata, int ac, gboolean is_append) { int in = -1; struct stat64 s; gs_container_t *c = NULL; gs_error_t *e = NULL; /*init the local path */ if (-1 == stat64(local_path, &s)) { e = g_malloc0(sizeof(gs_error_t)); e->code = errno; e->msg = g_strdup_printf("Cannot stat the local file (%s)\n", strerror(errno)); return e; } GRID_DEBUG("Local path %s found\n", local_path); if (-1 == (in = open(local_path, O_RDONLY|O_LARGEFILE))) { e = g_malloc0(sizeof(gs_error_t)); e->code = errno; e->msg = g_strdup_printf("Cannot open the local file (%s)\n", strerror(errno)); goto end_put; } GRID_DEBUG("Local path %s found and opened\n", local_path); if(!(c = gs_get_storage_container(hc, hc_url_get(url, HCURL_REFERENCE), NULL, ac, &e))) { g_printerr("Failed to resolve and/or create meta2 entry for reference %s\n", hc_url_get(url, HCURL_REFERENCE)); goto end_put; } /*upload the content */ if (is_append) { if (!gs_append_content(c, hc_url_get(url, HCURL_PATH), s.st_size, _feed_from_fd, &in, &e)) { goto end_put; } } else { if (!gs_upload(c, hc_url_get(url, HCURL_PATH), s.st_size, _feed_from_fd, &in, NULL, sys_metadata, stgpol, &e)) { goto end_put; } } GRID_INFO("Uploaded a new version of content [%s] in container [%s]\n\n", hc_url_get(url, HCURL_PATH), hc_url_get(url, HCURL_REFERENCE)); GRID_DEBUG("Content successfully uploaded!\n"); end_put: /** FIXME TODO XXX why not (in >= 0) or (in > -1) ? */ if (in > 1) metautils_pclose(&in); if(NULL != c) { gs_container_free(c); c = NULL; } return e; }
gs_error_t * hc_func_set_property(gs_grid_storage_t *hc, struct hc_url_s *url, char ** args) { gs_error_t *e = NULL; gs_container_t *c = NULL; gs_content_t *content = NULL; c = gs_get_storage_container(hc, hc_url_get(url, HCURL_REFERENCE), NULL, 0, &e); if (NULL != c) { if (hc_url_has(url, HCURL_PATH)) { content = gs_get_content_from_path_and_version (c, hc_url_get(url, HCURL_PATH), hc_url_get(url, HCURL_VERSION), &e); if (NULL != content) { gchar **props = g_malloc0(sizeof(gchar*) * 2); props[0] = g_strdup_printf("%s=%s", args[0], args[1]); props[1] = NULL; hc_set_content_property(content, props, &e); gs_content_free(content); g_strfreev(props); } } else { e = hc_set_container_global_property(c, args[0], args[1]); } gs_container_free(c); } return e; }
gs_error_t * hc_delete_container(gs_grid_storage_t *hc, struct hc_url_s *url, int force, int flush) { gs_error_t *e = NULL; gs_container_t *c = NULL; unsigned int flags = 0; if (force) flags |= M2V2_DESTROY_FORCE; // to flush by meta2, but without event generated //if (flush) flags |= M2V2_DESTROY_FLUSH; c = gs_get_storage_container(hc, hc_url_get(url, HCURL_REFERENCE), NULL, 0, &e); if(NULL != c) { // to flush by this process, but with event generated if (flush) { if (gs_flush_container(c, &e)) { GRID_DEBUG("Container flushed\n"); } } // destroy container if (!e) { if (gs_destroy_container_flags (c, flags, &e)) { GRID_DEBUG("Container deleted\n"); } } gs_container_free(c); } return e; }
static void test_data(struct test_data_s *pdata) { gs_error_t *gse = NULL; struct gs_grid_storage_s *gs = NULL; struct gs_container_s *container = NULL; gs = gs_grid_storage_init_flags(pdata->ns, GSCLIENT_NOINIT, 60, 60, &gse); g_assert((gs != NULL) ^ (gse != NULL)); if (!gs) g_debug("gs_grid_storage_init_flags failed : (%d) %s\n", gse->code, gse->msg); g_assert(gse == NULL); check_strings(pdata->pns, gs->ni.name); check_strings(pdata->ns, gs->full_vns); check_strings(pdata->pns, gs->physical_namespace); check_strings(pdata->vns, gs_get_virtual_namespace(gs)); container = gs_init_container(gs, pdata->refname, 0, &gse); g_assert((container != NULL) ^ (gse != NULL)); if (!container) g_debug("gs_init_container failed : (%d) %s\n", gse->code, gse->msg); g_assert(gse == NULL); check_strings(pdata->refname, container->info.name); check_strings(pdata->refhexa, container->str_cID); gs_container_free(container); gs_grid_storage_free(gs); }
gs_error_t * hc_func_delete_property(gs_grid_storage_t *hc, struct hc_url_s *url, char **keys) { gs_error_t *e = NULL; gs_container_t *c = NULL; gs_content_t *content = NULL; c = gs_get_storage_container(hc, hc_url_get(url, HCURL_REFERENCE), NULL, 0, &e); if (NULL != c) { if (hc_url_has(url, HCURL_PATH)) { content = gs_get_content_from_path_and_version (c, hc_url_get(url, HCURL_PATH), hc_url_get(url, HCURL_VERSION), &e); if (NULL != content) { hc_delete_content_property(content, keys, &e); gs_content_free(content); } } else { e = hc_del_container_global_property(c, keys[0]); } gs_container_free(c); } return e; }
gs_error_t * hc_func_list_snapshots(gs_grid_storage_t *hc, struct hc_url_s *url, int output_xml, int show_info, char **result) { gs_error_t *e = NULL; gs_container_t *c = NULL; struct list_content_s lc; redc_snapshot_t **snapshots = NULL; c = gs_get_storage_container(hc, hc_url_get(url, HCURL_REFERENCE), NULL, 0, &e); if (c != NULL) { lc.nb_elts = 0; lc.xml = output_xml; lc.show_info = show_info; lc.buffer = g_string_new(""); lc.listed = NULL; e = redc_list_snapshots(c, &snapshots); if (e == NULL) { int i = 0; redc_snapshot_t *snap = snapshots[i]; if (output_xml) { g_string_append_printf(lc.buffer, "<Container>\n" " <Name>%s</Name>\n" " <Snapshots>\n", hc_url_get(url, HCURL_REFERENCE)); } else { g_string_append_printf(lc.buffer, "#Listing snapshots of container=[%s]\n", hc_url_get(url, HCURL_REFERENCE)); } for (; snap != NULL; ++i, snap = snapshots[i]) { const char *name = redc_snapshot_get_name(snap); lc.nb_elts++; if (lc.xml) { g_string_append_printf(lc.buffer, " <Snapshot>\n"); g_string_append_printf(lc.buffer, " <Name>%s</Name>\n" " </Snapshot>\n", name); } else { g_string_append_printf(lc.buffer, "%s\n", name); } } if (output_xml) { g_string_append_printf(lc.buffer, " </Snapshots>\n" "</Container>\n"); } else { g_string_append_printf(lc.buffer, "#Total in [%s]: %i elements\n", hc_url_get(url, HCURL_REFERENCE), lc.nb_elts); } *result = g_string_free(lc.buffer, FALSE); redc_snapshot_array_clean(snapshots); } gs_container_free(c); } return e; }
gs_error_t * hc_delete_content(gs_grid_storage_t *hc, struct hc_url_s *url) { gs_error_t *e = NULL; gs_container_t *c = NULL; gs_content_t *content = NULL; c = gs_get_storage_container(hc, hc_url_get(url, HCURL_REFERENCE),NULL, 0, &e); if(NULL != c) { const gchar *version = hc_url_get(url, HCURL_VERSION); // First try content = gs_get_content_from_path_and_version (c, hc_url_get(url, HCURL_PATH), version, &e); if (content == NULL && e != NULL && e->code == CODE_CONTENT_NOTFOUND && !version) { // Last version is probably marked deleted, so a "get" without // version fails. We need to specify we want the latest, even // if it's deleted, so we can undelete it. version = HCURL_LATEST_VERSION; gs_error_free(e); e = NULL; // Second try content = gs_get_content_from_path_and_version (c, hc_url_get(url, HCURL_PATH), version, &e); } if (NULL != content) { if(gs_destroy_content (content, &e)) { GRID_DEBUG("Content %s deleted\n", hc_url_get(url, HCURL_PATH)); } gs_content_free(content); } gs_container_free(c); } return e; }
gs_error_t * hc_list_contents(gs_grid_storage_t *hc, struct hc_url_s *url, int output_xml, int show_info, char **result) { gs_error_t *e = NULL; gs_container_t *c = NULL; const gchar *snapshot = hc_url_get(url, HCURL_SNAPORVERS); struct list_content_s lc; c = gs_get_storage_container(hc, hc_url_get(url, HCURL_REFERENCE), NULL, 0, &e); if (NULL != c) { GRID_DEBUG("%s found\n", hc_url_get(url, HCURL_REFERENCE)); lc.nb_elts = 0; lc.xml = output_xml; lc.show_info = show_info; lc.buffer = g_string_new(""); lc.listed = NULL; if(output_xml) { g_string_append_printf(lc.buffer, "<Container>\n" " <Name>%s</Name>\n" " <Contents>\n", hc_url_get(url, HCURL_REFERENCE)); } else { g_string_append_printf(lc.buffer, "#Listing container=[%s]\n", hc_url_get(url, HCURL_REFERENCE)); } if (!gs_list_container_snapshot(c, NULL, _my_content_filter, &lc, snapshot, &e)) { g_printerr("Cannot list %s\n", hc_url_get(url, HCURL_REFERENCE)); g_string_free(lc.buffer, TRUE); } else { _sort_listed(&lc); GRID_DEBUG("%s listed\n", hc_url_get(url, HCURL_REFERENCE)); if(output_xml) { lc.buffer = g_string_append(lc.buffer, " </Contents>\n" "</Container>\n"); } else { g_string_append_printf(lc.buffer, "#Total in [%s]: %i elements\n", hc_url_get(url, HCURL_REFERENCE), lc.nb_elts); } *result = g_string_free(lc.buffer, FALSE); } gs_container_free(c); return e; } g_printerr("Cannot find %s\n", hc_url_get(url, HCURL_REFERENCE)); return e; }
static void test_end(gs_grid_storage_t * gs, char *nameRef, gs_container_t * container) { if (nameRef != NULL) { hc_unlink_reference_service(gs, nameRef, "meta2"); hc_delete_reference(gs, nameRef); } if (container != NULL) { gs_container_free(container); } }
static status_t _get_container_user_properties(gs_grid_storage_t *hc, struct hc_url_s *url, char ***props, gs_error_t **gserr) { GError *gerr = NULL; gboolean rc; addr_info_t *m1 = NULL; GSList *excluded = NULL; gs_container_t *c = gs_get_container(hc, hc_url_get(url, HCURL_USER), 0, gserr); if(!c) return 0; for (;;) { m1 = gs_resolve_meta1v2(hc, hc_url_get_id(url), c->info.name, 1, &excluded, &gerr); if (!m1) { *gserr = gs_error_new(CODE_INTERNAL_ERROR, "No META1 found for [%s]", hc_url_get(url, HCURL_USER)); break; } rc = meta1v2_remote_reference_get_property (m1, &gerr, url, NULL, props); if (!rc) { excluded = g_slist_prepend(excluded, m1); m1=NULL; if (gerr) { if (CODE_IS_NETWORK_ERROR(gerr->code)) { g_error_free(gerr); gerr = NULL; } else { GSERRORCAUSE(gserr, gerr, "Cannot get container user properties"); break; } } } else { break; } } if (excluded) { g_slist_foreach(excluded, addr_info_gclean, NULL); g_slist_free(excluded); } if (m1) g_free(m1); gs_container_free(c); if (gerr) g_error_free(gerr); return rc; }
gs_error_t * hc_func_copy_content(gs_grid_storage_t *hc, struct hc_url_s *url, const char *source) { gs_error_t *e = NULL; gs_container_t *c = NULL; c = gs_get_storage_container(hc, hc_url_get(url, HCURL_REFERENCE), NULL, 0, &e); if(NULL != c) { hc_copy_content(c, source, hc_url_get(url, HCURL_PATH), &e); gs_container_free(c); } return e; }
static int list_container(gs_grid_storage_t * gs, const char *cName) { int rc = -1; gs_error_t *err = NULL; gs_container_t *container = NULL; if (!gs || !cName) return -1; container = gs_get_storage_container(gs, cName, NULL, 0, &err); if (!container) { PRINT_ERROR("cannot find %s\n", cName); goto exit_label; } else PRINT_DEBUG("%s found\n", cName); /* Dump container properties */ if (flag_prop && !dump_container_properties(container)) return -1; nb_elements = 0; if (!gs_list_container(container, NULL, my_content_filter, NULL, &err)) { PRINT_ERROR("cannot download all the contents of %s\n", cName); goto exit_label; } else PRINT_DEBUG("%s listed\n", cName); g_print("total:%i elements in [grid://%s/%s/]\n", nb_elements, meta0_url, cName); rc = 0; exit_label: if (rc < 0) { if (err) { PRINT_ERROR("Failed to list [%s] cause:\n", cName); PRINT_DEBUG("\t%s\n", gs_error_get_message(err)); gs_error_free(err); } else { PRINT_ERROR("Failed to list [%s]\n", cName); } } gs_container_free(container); return rc; }
gs_error_t * hc_create_container(gs_grid_storage_t *hc, struct hc_url_s *url, const char *stgpol, const char *versioning) { GError *err = NULL; gs_error_t *e = NULL; gs_container_t *c = NULL; struct m2v2_create_params_s params = {stgpol, versioning, FALSE}; c = gs_get_storage_container2(hc, hc_url_get(url, HCURL_REFERENCE), ¶ms, 0, &e); if (c != NULL) { gchar m2[STRLEN_ADDRINFO] = {0}; addr_info_to_string(&(c->meta2_addr), m2, STRLEN_ADDRINFO); err = m2v2_remote_execute_HAS(m2, NULL, url); if (err == NULL) { e = gs_error_new(CODE_CONTAINER_EXISTS, "Failed to create container [%s]: " "container already exists in namespace [%s]\n", hc_url_get(url, HCURL_REFERENCE), hc_url_get(url, HCURL_NS)); goto end_label; } else if (err->code != CODE_CONTAINER_NOTFOUND) { GSERRORCAUSE(&e, err, "Failed to check container existence in meta2: "); goto end_label; } else { GRID_WARN("Container exists in meta1 but not in meta2"); } } gs_error_free(e); e = NULL; c = gs_get_storage_container2(hc, hc_url_get(url, HCURL_REFERENCE), ¶ms, 1, &e); if (c) GRID_INFO("Container [%s] created in namespace [%s].\n\n", hc_url_get(url, HCURL_REFERENCE), hc_url_get(url, HCURL_NS)); end_label: gs_container_free(c); g_clear_error(&err); return e; }
/* Generic function to avoid code duplication */ static gs_error_t * _hc_func_snapshot_generic(snap_func func, gs_grid_storage_t *hc, struct hc_url_s *url, void *param) { gs_error_t *e = NULL; gs_container_t *c = NULL; const gchar *snap_name = hc_url_get(url, HCURL_SNAPORVERS); if (snap_name == NULL) { GSERRORSET(&e, ERR_MISSING_SNAPSHOT_IN_URL); } else { c = gs_get_storage_container(hc, hc_url_get(url, HCURL_REFERENCE), NULL, 0, &e); if (c != NULL) { e = func(c, snap_name, param); gs_container_free(c); } } return e; }
gs_error_t * hc_dl_content(gs_grid_storage_t *hc, struct hc_url_s *url, gs_download_info_t *dl_info, int cache, gchar *stgpol) { gs_error_t *e = NULL; gs_container_t *c = NULL; c = gs_get_storage_container(hc, hc_url_get(url, HCURL_REFERENCE), NULL, 0, &e); if (c) { if (cache) { gs_download_content_by_name_full(c, hc_url_get(url, HCURL_PATH), hc_url_get(url, HCURL_SNAPORVERS), stgpol, dl_info, &e); } else { e = _dl_nocache(c, url, dl_info, stgpol); } gs_container_free(c); } else { g_printerr("Failed to resolve meta2 entry for reference %s\n", hc_url_get(url, HCURL_REFERENCE)); } return e; }
static gs_error_t* _m2_touch(struct hc_url_s *hcurl, guint32 flags) { GError* err = NULL; gs_error_t* gserr = NULL; gs_container_t* cid = NULL; gs_grid_storage_t* hc = NULL; GSList* m2list = NULL; addr_info_t* m2addr = NULL; char str_addr[STRLEN_ADDRINFO] = ""; const char* ns = NULL; const char* content = NULL; const char* container_name = NULL; ns = hc_url_get(hcurl, HCURL_NS); container_name = hc_url_get(hcurl, HCURL_REFERENCE); content = hc_url_get(hcurl, HCURL_PATH); /* init grid client */ if ((ns==NULL) || (strlen(ns)==0)) { GSERRORCODE(&gserr, -1, "Invalid namespace"); return gserr; } hc = gs_grid_storage_init(ns, &gserr); if (!hc) { return gserr; } /* init container_id */ if ((container_name==NULL) || (strlen(container_name)==0)) { GSERRORCODE(&gserr, -1, "Invalid container_name"); return gserr; } cid = gs_init_container(hc, container_name, FALSE, &gserr); if (!gserr) { if (!cid) { gs_grid_storage_free(hc); GSERRORCODE(&gserr, -1, "Invalid container_name %s/%s", ns, container_name); return gserr; } } else return gserr; /* search meta2 master */ m2list = gs_resolve_meta2(hc, C0_ID(cid), &err); if (!m2list) { GSERRORCODE(&gserr, -1, "Meta2 Resolution error for NAME=[%s] ID=[%s]", C0_NAME(cid), C0_IDSTR(cid)); if (cid) gs_container_free (cid); gs_grid_storage_free(hc); return gserr; } m2addr = (addr_info_t*) m2list->data; addr_info_to_string (m2addr, str_addr, sizeof(str_addr)); /* execute touch command */ if (content && strlen(content)>0) err = m2v2_remote_touch_content(str_addr, NULL, hcurl); else err = m2v2_remote_touch_container_ex(str_addr, NULL, hcurl, flags); /* an error occurs ? */ if (err) { GSERRORCAUSE(&gserr, err, "Failed to execute touch command"); g_clear_error(&err); } gs_grid_storage_free(hc); if (cid) gs_container_free (cid); return gserr; }
int main(int argc, char **args) { int rc; gs_error_t *err = NULL; gs_grid_storage_t *gs = NULL; gs_container_t *container = NULL; signal(SIGPIPE, sig_pipe); if (argc <= 1) { help(); return 1; } if (!parse_opt(argc, args)) { PRINT_ERROR("Cannot parse the arguments\n"); help(); return 1; } if (!conf_check()) { PRINT_ERROR("Missing parameters\n"); help(); return 1; } /*open the connection to the META0 */ gs = gs_grid_storage_init(meta0_url, &err); if (!gs) { PRINT_ERROR("grid storage error : cannot init the namespace configuration from %s\n", meta0_url); return -1; } PRINT_DEBUG("Connected to the GridStorage namespace %s\n", meta0_url); /*find the container */ container = gs_get_container(gs, container_name, 0, &err); if (!container) { PRINT_ERROR("grid storage error : cannot find the container %s : %s\n", container_name, err->msg); gs_grid_storage_free(gs); return -1; } PRINT_DEBUG("container %s found\n", container_name); switch (action) { case A_CONTAINER_GET: rc = get_service_for_container(container,&err); if (rc != 0) PRINT_ERROR("Failed to list the index services used in container [%s] : %s\n", container_name, gs_error_get_message(err)); break; case A_CONTAINER_LIST: rc = list_services_used_by_container(container,&err); if (rc != 0) PRINT_ERROR("Failed to list the index services used in container [%s] : %s\n", container_name, gs_error_get_message(err)); break; case A_LISTSRV: rc = list_services_used(container,&err); if (rc != 0) PRINT_ERROR("Failed to list the index services used in container [%s] : %s\n", container_name, gs_error_get_message(err)); break; case A_LISTCONTENT: rc = list_services_for_path(container,&err, remote_path); if (rc != 0) PRINT_ERROR("Failed to list the index services used in container [%s] for path [%s] : %s\n", container_name, remote_path, gs_error_get_message(err)); break; default: PRINT_ERROR("Action not set, please provide at least '-l' or '-c'\n"); rc = -1; break; } gs_container_free(container); gs_grid_storage_free(gs); return 0; }
int main (int argc, char ** args) { int rc = -1; gs_error_t *err = NULL; gs_grid_storage_t *hc; gs_container_t *container; char *ns; char cname[60]; srand(time(NULL)); if (argc != 2) { g_error("Usage: %s NS\n", args[0]); return rc; } ns = args[1]; bzero(cname, sizeof(cname)); g_snprintf(cname, sizeof(cname), "SOLR%d", rand()); printf("Working with container [%s]\n", cname); hc = gs_grid_storage_init( ns, &err ); if(!hc) { printf("failed to init hc\n"); return rc; } container = gs_get_container(hc, cname, 1, &err); if(!container) { printf("Failed to resolve container\n"); goto end_label; } gs_service_t **srv_array = NULL; srv_array = gs_container_service_get_available(container, "meta0", &err); char url[256]; bzero(url, sizeof(url)); gs_service_get_url(srv_array[0], url, sizeof(url)); printf("New service linked\n"); printf("service url = [%s]\n", url); if(srv_array) gs_service_free_array(srv_array); srv_array = gs_container_service_get_all(container, "meta0", &err); bzero(url, sizeof(url)); gs_service_get_url(srv_array[0], url, sizeof(url)); printf("Already linked service :\n"); printf("service url = [%s]\n", url); if(srv_array) gs_service_free_array(srv_array); end_label: if(container) { gs_container_free(container); container = NULL; } if(hc) { gs_grid_storage_free(hc); hc = NULL; } if(err) { gs_error_free(err); err= NULL; } }