char *json_to_path(json_t *object, baton_error_t *error) { char *path = NULL; init_baton_error(error); const char *collection = get_collection_value(object, error); if (error->code != 0) goto error; if (represents_collection(object)) { path = make_dir_path(collection, error); } else { const char *data_object = get_data_object_value(object, error); path = make_file_path(collection, data_object, error); } if (error->code != 0) goto error; return path; error: if (path) free(path); return NULL; }
char *json_to_local_path(json_t *object, baton_error_t *error) { char *path = NULL; init_baton_error(error); const char *directory = get_directory_value(object, error); if (error->code != 0) goto error; const char *filename = get_file_value(object, error); if (error->code != 0) goto error; const char *data_object = get_data_object_value(object, error); if (error->code != 0) goto error; if (directory && filename) { path = make_file_path(directory, filename, error); } else if (directory && data_object) { path = make_file_path(directory, data_object, error); } else if (filename) { path = make_file_path(".", filename, error); } else if (data_object) { path = make_file_path(".", data_object, error); } else if (directory) { path = make_dir_path(directory, error); } else { path = make_dir_path(".", error); } if (error->code != 0) goto error; return path; error: if (path) free(path); return NULL; }
char *json_to_collection_path(json_t *object, baton_error_t *error) { char *path = NULL; init_baton_error(error); const char *collection = get_collection_value(object, error); if (error->code != 0) goto error; path = make_dir_path(collection, error); if (error->code != 0) goto error; return path; error: if (path) free(path); return NULL; }
/* Ensure the specified directory exists. If not, create it or die. */ static void check_dir_path(const char *filepath, const char *fp_desc, unsigned char use_basename) { struct stat st; int res = 0; char tmp_path[MAX_PATH_LEN]; char *ndx; /* * FIXME: We shouldn't use a hard-coded dir-separator here. */ /* But first make sure we are using an absolute path. */ if(*filepath != PATH_SEP) { log_msg(LOG_ERR, "Configured %s directory (%s) is not an absolute path.", fp_desc, filepath ); exit(EXIT_FAILURE); } /* If this is a file path that we want to use only the basename, strip * the trailing filename here. */ if(use_basename && ((ndx = strrchr(filepath, PATH_SEP)) != NULL)) strlcpy(tmp_path, filepath, (ndx-filepath)+1); else strlcpy(tmp_path, filepath, MAX_PATH_LEN); /* At this point, we should make the path is more than just the * PATH_SEP. If it is not, silently return. */ if(strlen(tmp_path) < 2) return; /* Make sure we have a valid directory. */ res = stat(tmp_path, &st); if(res != 0) { if(errno == ENOENT) { log_msg(LOG_WARNING, "%s directory: %s does not exist. Attempting to create it.", fp_desc, tmp_path ); /* Directory does not exist, so attempt to create it. */ res = make_dir_path(tmp_path); if(res != 0) { log_msg(LOG_ERR, "Unable to create %s directory: %s (error: %i)", fp_desc, tmp_path, errno ); exit(EXIT_FAILURE); } log_msg(LOG_ERR, "Successfully created %s directory: %s", fp_desc, tmp_path ); } else { log_msg(LOG_ERR, "Stat of %s returned error %i", tmp_path, errno ); exit(EXIT_FAILURE); } } else { /* It is a file, but is it a directory? */ if(! S_ISDIR(st.st_mode)) { log_msg(LOG_ERR, "Specified %s directory: %s is NOT a directory\n\n", fp_desc, tmp_path ); exit(EXIT_FAILURE); } } }