Exemplo n.º 1
0
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);
}
Exemplo n.º 2
0
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);
}
Exemplo n.º 3
0
char *
gfs_glob(char *pattern,	gfarm_stringlist *paths, gfs_glob_t *types)
{
	char *s, *p = NULL, *e = NULL;
	int len, n = gfarm_stringlist_length(paths);
	char path_buffer[GLOB_PATH_BUFFER_SIZE + 1];

	pattern = gfarm_url_prefix_skip(pattern);
	if (*pattern == '~') {
		if (pattern[1] == '\0' || pattern[1] == '/') {
			s = gfarm_get_global_username();
			len = strlen(s);
			pattern++;
		} else {
			s = pattern + 1;
			len = strcspn(s, "/");
			pattern += 1 + len;
		}
		p = malloc(1 + len + strlen(pattern) + 1);
		if (p == NULL) {
			e = GFARM_ERR_PATHNAME_TOO_LONG;
		} else {
			p[0] = '/';
			memcpy(p + 1, s, len);
			strcpy(p + 1 + len, pattern);
			pattern = p;
		}
	} else {
		strcpy(path_buffer, ".");
	}
	if (e == NULL) {
		e = gfs_glob_sub(path_buffer, path_buffer, pattern,
		    paths, types);
	}
	if (gfarm_stringlist_length(paths) <= n) {
		gfarm_stringlist_add(paths, gfarm_url_prefix_add(pattern));
		gfs_glob_add(types, GFS_DT_UNKNOWN);
	}
	if (p != NULL)
		free(p);
	return (e);
}
Exemplo n.º 4
0
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);
}
Exemplo n.º 5
0
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);
}
Exemplo n.º 6
0
static void
remove_cwd_entries()
{
	char *e;
	char cwdbf[PATH_MAX * 2];
	int i;
	GFS_Dir dir;
	struct gfs_dirent *entry;
	gfarm_stringlist entry_list;

	e = gfs_getcwd(cwdbf, sizeof(cwdbf));
	if (e != NULL) {
		fprintf(stderr, "%s\n", e);
		return;
	}
	e = gfs_opendir(".", &dir);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", cwdbf, e);
		return;
	}
	e = gfarm_stringlist_init(&entry_list);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", cwdbf, e);
		return;
	}
	while ((e = gfs_readdir(dir, &entry)) == NULL && entry != NULL) {
		char *p;

		if (entry->d_name[0] == '.' && (entry->d_name[1] == '\0' ||
		    (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
			continue; /* "." or ".." */
	 	p = strdup(entry->d_name);
		if (p == NULL) {
			fprintf(stderr, "%s\n", GFARM_ERR_NO_MEMORY);
			exit (1);
		}
		e = gfarm_stringlist_add(&entry_list, p);
		if (e != NULL) {
			fprintf(stderr, "%s/%s: %s\n",
					cwdbf, entry->d_name, e);
		}
	}
	if (e != NULL)
		fprintf(stderr, "%s: %s\n", cwdbf, e);
	gfs_closedir(dir);
	for (i = 0; i < gfarm_stringlist_length(&entry_list); i++) {
		struct gfs_stat gs;
		char *path = gfarm_stringlist_elem(&entry_list, i);

		e = gfs_stat(path, &gs);
		if (e != NULL) {
			fprintf(stderr, "%s/%s: %s\n", cwdbf, path, e);
			continue;
		}
		if (GFARM_S_ISREG(gs.st_mode)) {
			char *url;

			url = gfarm_url_prefix_add(path);
			if (url == NULL) {
				fprintf(stderr, "%s\n", GFARM_ERR_NO_MEMORY);
				exit (1);
			}
			e = gfs_unlink(url);
			if (e != NULL)
				fprintf(stderr, "%s/%s: %s\n", cwdbf, path, e);
			free(url);
		} else if (GFARM_S_ISDIR(gs.st_mode)) {
			e = gfs_chdir(path);
			if (e != NULL) {
				fprintf(stderr, "%s/%s: %s\n", cwdbf, path, e);
				continue;
			}
			remove_cwd_entries();
			e = gfs_chdir("..");
			if (e != NULL) {
				fprintf(stderr, "%s: %s\n", cwdbf, e);
				exit (1);
			}
			e = gfs_rmdir(path);
			if (e != NULL)
				fprintf(stderr, "%s/%s: %s\n", cwdbf, path, e);
		}
		gfs_stat_free(&gs);
	}
	gfarm_stringlist_free_deeply(&entry_list);
}
Exemplo n.º 7
0
static char *
gfs_glob_sub(char *path_buffer, char *path_tail, char *pattern,
	gfarm_stringlist *paths, gfs_glob_t *types)
{
	char *s, *e, *e_save = NULL;
	int i, nomagic, dirpos = -1;
	GFS_Dir dir;
	struct gfs_dirent *entry;
	struct gfs_stat st;

	for (i = 0; pattern[i] != '\0'; i++) {
		if (pattern[i] == '\\') {
			if (pattern[i + 1] != '\0' &&
			    pattern[i + 1] != '/')
				i++;
		} else if (pattern[i] == '/') {
			dirpos = i;
		} else if (pattern[i] == '?' || pattern[i] == '*') {
			break;
		} else if (pattern[i] == '[') {
			if (glob_charset_parse(pattern, i + 1, NULL))
				break;
		}
	}
	if (pattern[i] == '\0') { /* no magic */
		if (path_tail - path_buffer + strlen(pattern) >
		    GLOB_PATH_BUFFER_SIZE)
			return (GFARM_ERR_PATHNAME_TOO_LONG);
		glob_pattern_to_name(path_tail, pattern, strlen(pattern));
		e = gfs_stat(path_buffer, &st);
		if (e != NULL)
			return (e);
		s = gfarm_url_prefix_add(path_buffer);
		if (s == NULL)
			return (GFARM_ERR_NO_MEMORY);
		gfarm_stringlist_add(paths, s);
		if (GFARM_S_ISDIR(st.st_mode))
			gfs_glob_add(types, GFS_DT_DIR);
		else
			gfs_glob_add(types, GFS_DT_REG);
		return (NULL);
	}
	nomagic = i;
	if (dirpos >= 0) {
		int dirlen = dirpos == 0 ? 1 : dirpos;

		if (path_tail - path_buffer + dirlen > GLOB_PATH_BUFFER_SIZE)
			return (GFARM_ERR_PATHNAME_TOO_LONG);
		glob_pattern_to_name(path_tail, pattern, dirlen);
		path_tail += strlen(path_tail);
	}
	dirpos++;
	for (i = nomagic; pattern[i] != '\0'; i++) {
		if (pattern[i] == '\\') {
			if (pattern[i + 1] != '\0' &&
			    pattern[i + 1] != '/')
				i++;
		} else if (pattern[i] == '/') {
			break;
		} else if (pattern[i] == '?' || pattern[i] == '*') {
		} else if (pattern[i] == '[') {
			glob_charset_parse(pattern, i + 1, &i);
		}
	}
	e = gfs_opendir(path_buffer, &dir);
	if (e != NULL)
		return (e);
	if (path_tail > path_buffer && path_tail[-1] != '/') {
		if (path_tail - path_buffer + 1 > GLOB_PATH_BUFFER_SIZE)
			return (GFARM_ERR_PATHNAME_TOO_LONG);
		*path_tail++ = '/';
	}
	while ((e = gfs_readdir(dir, &entry)) == NULL && entry != NULL) {
		if (!glob_name_match(entry->d_name, &pattern[dirpos],
		    i - dirpos))
			continue;
		if (path_tail - path_buffer + strlen(entry->d_name) >
		    GLOB_PATH_BUFFER_SIZE) {
			if (e_save == NULL)
				e_save = GFARM_ERR_PATHNAME_TOO_LONG;
			continue;
		}
		strcpy(path_tail, entry->d_name);
		if (pattern[i] == '\0') {
			s = gfarm_url_prefix_add(path_buffer);
			if (s == NULL)
				return (GFARM_ERR_NO_MEMORY);
			gfarm_stringlist_add(paths, s);
			gfs_glob_add(types, entry->d_type);
			continue;
		}
		e = gfs_glob_sub(path_buffer, path_tail + strlen(path_tail),
		    pattern + i, paths, types);
		if (e_save == NULL)
			e_save = e;
	}
	gfs_closedir(dir);
	return (e_save);
}
Exemplo n.º 8
0
int
main(int argc, char **argv)
{
	char *e;
	gfarm_stringlist paths;
	gfs_glob_t types;
	int i, c, exit_code = EXIT_SUCCESS;
	extern char *optarg;

	if (argc > 0)
		program_name = basename(argv[0]);
	e = gfarm_initialize(&argc, &argv);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", program_name, e);
		exit(EXIT_FAILURE);
	}

	if (isatty(STDOUT_FILENO)) {
		char *s = getenv("COLUMNS");
#ifdef TIOCGWINSZ
		struct winsize win;
#endif

		if (s != NULL)
			screen_width = strtol(s, NULL, 0);
#ifdef TIOCGWINSZ
		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
		    win.ws_col > 0)
			screen_width = win.ws_col;
#endif
		option_output_format = OF_MULTI_COLUMN;
	} else {
		option_output_format = OF_ONE_PER_LINE;
	}
	while ((c = getopt(argc, argv, "1CFRSTdlrt?")) != -1) {
		switch (c) {
		case '1': option_output_format = OF_ONE_PER_LINE; break;
		case 'C': option_output_format = OF_MULTI_COLUMN; break;
		case 'F': option_type_suffix = 1; break;
		case 'R': option_recursive = 1; break;
		case 'S': option_sort_order = SO_SIZE; break;
		case 'T': option_complete_time = 1; break;
		case 'd': option_directory_itself =  1; break;
		case 'l': option_output_format = OF_LONG; break;
		case 'r': option_reverse_sort = 1; break;
		case 't': option_sort_order = SO_MTIME; break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	e = gfarm_stringlist_init(&paths);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", program_name, e);
		exit(EXIT_FAILURE);
	}
	e = gfs_glob_init(&types);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", program_name, e);
		exit(EXIT_FAILURE);
	}
	if (argc < 1) {
		gfarm_stringlist_add(&paths, "gfarm:.");
		gfs_glob_add(&types, GFS_DT_DIR);
	} else {
		for (i = 0; i < argc; i++) {
			int last;

			/* do not treat glob error as an error */
			gfs_glob(argv[i], &paths, &types);

			last = gfs_glob_length(&types) - 1;
			if (last >= 0 && gfs_glob_elem(&types, last) ==
			    GFS_DT_UNKNOWN) {
				/*
				 * Currently, this only happens if there is
				 * no file which matches with argv[i].
				 * In such case, the number of entries which
				 * were added by gfs_glob() is 1.
				 * But also please note that GFS_DT_UNKNOWN
				 * may happen on other case in future.
				 */
				struct gfs_stat s;
				char *path = gfarm_stringlist_elem(&paths,
				    last);

				e = gfs_stat(path, &s);
				if (e != NULL) {
					fprintf(stderr, "%s: %s\n", path, e);
					exit_code = EXIT_FAILURE;
					/* remove last entry */
					/* XXX: FIXME layering violation */
					free(paths.array[last]);
					paths.length--;
					types.length--;
				} else {
					GFS_GLOB_ELEM(types, last) =
					    GFARM_S_ISDIR(s.st_mode) ?
					    GFS_DT_DIR : GFS_DT_REG;
					gfs_stat_free(&s);
				}
			}
		}
	}
	if (gfarm_stringlist_length(&paths) > 0) {
		int need_newline = 0;

#if 1
		if (list(&paths, &types, &need_newline) != NULL) {
			/* warning is already printed in list() */
			exit_code = EXIT_FAILURE;
		}
#else
		for (i = 0; i < gfarm_stringlist_length(&paths); i++)
			printf("<%s>\n", gfarm_stringlist_elem(&paths, i));
#endif
	}
	return (exit_code);
}
Exemplo n.º 9
0
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);
}
Exemplo n.º 10
0
/*
 * common routine for gfrepbe_client and gfrepbe_server.
 * other programs/libraries shouldn't use this function.
 */
char *
gfs_client_rep_filelist_receive(struct xxx_connection *from_client,
	int *np, gfarm_stringlist *files, gfarm_stringlist *sections,
	char *message_prefix)
{
	int eof, n;
	char *e, *file, *section;

	/*
	 * XXX FIXME: This should be done in parallel during replication.
	 */
	e = gfarm_stringlist_init(files);
	if (e != NULL) {
		fprintf(stderr, "%s: allocating memory for filenames: %s\n",
		    message_prefix, e);
		return (e);
	}
	e = gfarm_stringlist_init(sections);
	if (e != NULL) {
		fprintf(stderr, "%s: allocating memory for sections: %s\n",
		    message_prefix, e);
		return (e);
	}
	n = 0;
	for (;;) {
		e = xxx_proto_recv(from_client, 0, &eof, "ss",
		    &file, &section);
		if (e != NULL) {
			fprintf(stderr, "%s: error while receiving requests: "
			    "%s\n",
			    message_prefix, e);
			return (e);
		}
		if (eof) {
			fprintf(stderr, "%s: unexpected EOF "
			    "while receiving file:section lists\n",
			    message_prefix);
			return (GFARM_ERR_PROTOCOL);
		}
		if (*file == '\0') { /* end of list */
			if (*section != '\0') {
				fprintf(stderr, "%s: "
				    "protocol error, unexpected section = <%s>"
				    " at the end of replication list\n",
				    message_prefix, section);
				return (GFARM_ERR_PROTOCOL);
			}
			free(file);
			free(section);
			break;
		}
		e = gfarm_stringlist_add(files, file);
		if (e != NULL) {
			fprintf(stderr, "%s: %s for file %s:%s\n",
			    message_prefix, e, file, section);
			return (e);
		}
		e = gfarm_stringlist_add(sections, section);
		if (e != NULL) {
			fprintf(stderr, "%s: %s for section %s:%s\n",
			    message_prefix, e, file, section);
			return (e);
		}
		n++;
	}
	*np = n;
	return (NULL);
}
Exemplo n.º 11
0
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);
}