char * list(gfarm_stringlist *paths, gfs_glob_t *types, int *need_newline) { char *e, *e_save = NULL; gfarm_stringlist dirs, files; int i, nfiles, ndirs; if (option_directory_itself) { return (list_files("", gfarm_stringlist_length(paths), GFARM_STRINGLIST_STRARRAY(*paths), need_newline)); } e = gfarm_stringlist_init(&dirs); if (e != NULL) { fprintf(stderr, "%s: %s\n", program_name, e); return (e); } e = gfarm_stringlist_init(&files); if (e != NULL) { fprintf(stderr, "%s: %s\n", program_name, e); gfarm_stringlist_free(&dirs); return (e); } for (i = 0; i < gfarm_stringlist_length(paths); i++) { char *path = gfarm_stringlist_elem(paths, i); if (gfs_glob_elem(types, i) == GFS_DT_DIR) gfarm_stringlist_add(&dirs, path); else gfarm_stringlist_add(&files, path); } nfiles = gfarm_stringlist_length(&files); ndirs = gfarm_stringlist_length(&dirs); if (nfiles > 0) { e = list_files("", nfiles, GFARM_STRINGLIST_STRARRAY(files), need_newline); /* warning is already printed in list_files() */ if (e_save == NULL) e_save = e; } gfarm_stringlist_free(&files); if (nfiles == 0 && ndirs == 1) { e = list_dir("", gfarm_stringlist_elem(&dirs, 0), need_newline); /* warning is already printed in list_dir() */ } else { e = list_dirs("", ndirs, GFARM_STRINGLIST_STRARRAY(dirs), need_newline); /* warning is already printed in list_dirs() */ } if (e_save == NULL) e_save = e; gfarm_stringlist_free(&dirs); return (e_save); }
void gfarm_stringlist_free_deeply(gfarm_stringlist *listp) { int i, length = gfarm_stringlist_length(listp); for (i = 0; i < length; i++) { if (listp->array[i] != NULL) free(listp->array[i]); } gfarm_stringlist_free(listp); }
static gfarm_error_t gfarm_hash_to_string_array(struct gfarm_hash_table *hash, int *array_lengthp, char ***arrayp) { struct gfarm_hash_iterator iter; struct gfarm_hash_entry *entry; gfarm_stringlist ls; char *ent, **array; gfarm_error_t e; e = gfarm_stringlist_init(&ls); if (e != GFARM_ERR_NO_ERROR) return (e); for (gfarm_hash_iterator_begin(hash, &iter); !gfarm_hash_iterator_is_end(&iter); gfarm_hash_iterator_next(&iter)) { entry = gfarm_hash_iterator_access(&iter); if (entry != NULL) { ent = strdup(gfarm_hash_entry_key(entry)); if (ent == NULL) e = GFARM_ERR_NO_MEMORY; else e = gfarm_stringlist_add(&ls, ent); } if (e != GFARM_ERR_NO_ERROR) goto stringlist_free; } array = gfarm_strings_alloc_from_stringlist(&ls); if (array == NULL) { e = GFARM_ERR_NO_MEMORY; goto stringlist_free; } *array_lengthp = gfarm_stringlist_length(&ls); *arrayp = array; stringlist_free: if (e == GFARM_ERR_NO_ERROR) gfarm_stringlist_free(&ls); else gfarm_stringlist_free_deeply(&ls); return (e); }
char * gfarm_import_fragment_config_read(char *config, int *np, char ***hosttabp, file_offset_t **sizetabp, int *error_linep) { char *e, **host_table, line[1024]; int i, table_size = TABLE_SIZE_INITIAL; file_offset_t *size_table = malloc(sizeof(size_table[0]) * table_size); file_offset_t *stab; gfarm_stringlist host_list; FILE *fp; *error_linep = -1; if (size_table == NULL) return (GFARM_ERR_NO_MEMORY); e = gfarm_stringlist_init(&host_list); if (e != NULL) { free(size_table); return (e); } if (strcmp(config, "-") == 0) { fp = stdin; } else if ((fp = fopen(config, "r")) == NULL) { gfarm_stringlist_free(&host_list); free(size_table); return (GFARM_ERR_NO_SUCH_OBJECT); } for (i = 0; fgets(line, sizeof(line), fp) != NULL; i++) { int l = strlen(line); char *s, *t, *host; file_offset_t size; if (l > 0 && line[l - 1] == '\n') line[--l] = '\0'; size = string_to_file_offset(line, &s); if (s == line) { e = "fragment size expected"; *error_linep = i + 1; goto error; } while (isspace(*s)) s++; if (*s == '\0') { e = "fragment hostname expected"; *error_linep = i + 1; goto error; } for (t = s; *t != '\0' && !isspace(*t); t++) ; *t = '\0'; host = strdup(s); if (host == NULL) { e = GFARM_ERR_NO_MEMORY; *error_linep = i + 1; goto error; } e = gfarm_stringlist_add(&host_list, host); if (e != NULL) { *error_linep = i + 1; goto error; } if (i >= table_size) { table_size += TABLE_SIZE_DELTA; stab = realloc(size_table, sizeof(size_table[0]) * table_size); if (stab == NULL) { e = GFARM_ERR_NO_MEMORY; *error_linep = i + 1; goto error; } size_table = stab; } size_table[i] = size; } if (i == 0) { e = "empty file"; goto error; } host_table = gfarm_strings_alloc_from_stringlist(&host_list); if (host_table == NULL) { e = GFARM_ERR_NO_MEMORY; goto error; } if (i < table_size) { stab = realloc(size_table, sizeof(size_table[0]) * i); if (stab == NULL) { e = GFARM_ERR_NO_MEMORY; goto error; } memcpy(stab, size_table, sizeof(size_table[0]) * i); size_table = stab; } /* * do not call gfarm_stringlist_free_deeply() here, * because the strings are passed to *host_table. */ gfarm_stringlist_free(&host_list); /* no limit on last fragment */ size_table[i - 1] = FILE_OFFSET_T_MAX; *np = i; *hosttabp = host_table; *sizetabp = size_table; if (strcmp(config, "-") != 0) fclose(fp); return (NULL); error: if (strcmp(config, "-") != 0) fclose(fp); gfarm_stringlist_free_deeply(&host_list); free(size_table); return (e); }
char * gfarm_hostlist_read(char *filename, int *np, char ***host_table_p, int *error_linep) { gfarm_stringlist host_list; FILE *fp; int i; char *e, line[1024]; *error_linep = -1; e = gfarm_stringlist_init(&host_list); if (e != NULL) return (e); if (strcmp(filename, "-") == 0) { fp = stdin; } else if ((fp = fopen(filename, "r")) == NULL) { gfarm_stringlist_free(&host_list); return (GFARM_ERR_NO_SUCH_OBJECT); } for (i = 0; fgets(line, sizeof(line), fp) != NULL; i++) { int l = strlen(line); char *s, *t, *host; if (l > 0 && line[l - 1] == '\n') line[--l] = '\0'; for (s = line; isspace(*s); s++) ; if (*s == '\0') { e = "hostname expected"; *error_linep = i + 1; goto error; } for (t = s; *t != '\0' && !isspace(*t); t++) ; *t = '\0'; host = strdup(s); if (host == NULL) { e = GFARM_ERR_NO_MEMORY; *error_linep = i + 1; goto error; } e = gfarm_stringlist_add(&host_list, host); if (e != NULL) { free(host); *error_linep = i + 1; goto error; } } if (i == 0) { e = "empty file"; goto error; } *np = gfarm_stringlist_length(&host_list); *host_table_p = gfarm_strings_alloc_from_stringlist(&host_list); if (e != NULL) goto error; /* * do not call gfarm_stringlist_free_deeply() here, * because the strings are passed to *host_table. */ gfarm_stringlist_free(&host_list); if (strcmp(filename, "-") != 0) fclose(fp); return (NULL); error: if (strcmp(filename, "-") != 0) fclose(fp); gfarm_stringlist_free_deeply(&host_list); return (e); }
char * list_dir(char *prefix, char *dirname, int *need_newline) { char *e, *s, *path, *e_save = NULL; gfarm_stringlist names; gfs_glob_t types; GFS_Dir dir; struct gfs_dirent *entry; int len = strlen(prefix) + strlen(dirname); path = malloc(len + 1 + 1); if (path == NULL) return (GFARM_ERR_NO_MEMORY); sprintf(path, "%s%s", prefix, dirname); e = gfarm_stringlist_init(&names); if (e != NULL) { fprintf(stderr, "%s: %s\n", program_name, e); free(path); return (e); } e = gfs_glob_init(&types); if (e != NULL) { fprintf(stderr, "%s: %s\n", program_name, e); gfarm_stringlist_free(&names); free(path); return (e); } e = gfs_opendir(path, &dir); if (e != NULL) { fprintf(stderr, "%s: %s\n", path, e); gfs_glob_free(&types); gfarm_stringlist_free(&names); free(path); return (e); } while ((e = gfs_readdir(dir, &entry)) == NULL && entry != NULL) { s = strdup(entry->d_name); if (s == NULL) { e = GFARM_ERR_NO_MEMORY; break; } gfarm_stringlist_add(&names, s); gfs_glob_add(&types, entry->d_type); } if (e != NULL) { fprintf(stderr, "%s%s: %s\n", prefix, dirname, e); e_save = e; } gfs_closedir(dir); if (*gfarm_path_dir_skip(gfarm_url_prefix_skip(path)) != '\0') { path[len] = '/'; path[len + 1] = '\0'; } e = list_files(path, gfarm_stringlist_length(&names), GFARM_STRINGLIST_STRARRAY(names), need_newline); if (e_save == NULL) e_save = e; if (option_recursive) { int i; for (i = 0; i < gfarm_stringlist_length(&names); i++) { s = GFARM_STRINGLIST_STRARRAY(names)[i]; if (s[0] == '.' && (s[1] == '\0' || (s[1] == '.' && s[2] == '\0'))) continue; if (gfs_glob_elem(&types, i) == GFS_DT_DIR) { e = list_dirs(path, 1, &s, need_newline); if (e_save == NULL) e_save = e; } } } gfs_glob_free(&types); gfarm_stringlist_free_deeply(&names); free(path); return (e_save); }
gfarm_error_t gfarm_modify_host(const char *hostname, int port, char **hostaliases, char *architecture, int ncpu, int flags, int add_aliases) { gfarm_error_t e, e2; struct gfarm_host_info hi; int host_info_needs_free = 0; gfarm_stringlist aliases; if (port == 0 || *hostaliases == NULL || architecture == NULL || ncpu < 1 || flags == -1 || add_aliases) { e = gfm_client_host_info_get_by_names(gfarm_metadb_server, 1, &hostname, &e2, &hi); if (e != GFARM_ERR_NO_ERROR) return (e); if (e2 != GFARM_ERR_NO_ERROR) return (e2); host_info_needs_free = 1; if (!add_aliases) { /* XXX - do check_hostaliases() here, too. */ hostaliases = hostaliases; } else { e = check_hostaliases( gfarm_strarray_length(hostaliases), hostaliases); if (e != GFARM_ERR_NO_ERROR) goto free_host_info; e = gfarm_stringlist_init(&aliases); if (e != GFARM_ERR_NO_ERROR) goto free_host_info; if (hi.hostaliases != NULL) { e = gfarm_stringlist_cat(&aliases, hi.hostaliases); if (e != GFARM_ERR_NO_ERROR) goto free_aliases; } if (hostaliases != NULL) { e = gfarm_stringlist_cat(&aliases, hostaliases); if (e != GFARM_ERR_NO_ERROR) goto free_aliases; } e = gfarm_stringlist_add(&aliases, NULL); if (e != GFARM_ERR_NO_ERROR) goto free_aliases; hostaliases = GFARM_STRINGLIST_STRARRAY(aliases); } if (port == 0) port = hi.port; if (architecture == NULL) architecture = hi.architecture; if (ncpu < 1) ncpu = hi.ncpu; if (flags == -1) flags = hi.flags; } e = update_host(hostname, port, gfarm_strarray_length(hostaliases), hostaliases, architecture, ncpu, flags, gfm_client_host_info_modify); #if 0 /* XXX FIXME not yet in v2 */ if (e == GFARM_ERR_NO_ERROR && !add_aliases && *hostaliases == NULL) e = gfarm_host_info_remove_hostaliases(hostname); #endif free_aliases: if (add_aliases) gfarm_stringlist_free(&aliases); free_host_info: if (host_info_needs_free) gfarm_host_info_free(&hi); return (e); }