Пример #1
0
static char *
remove_whole_file_or_dir(char *path, int is_recursive)
{
	char *e, *e2;
	struct gfs_stat gs;
	char cwdbuf[PATH_MAX * 2];

	e = gfs_stat(path, &gs);
	if (e == GFARM_ERR_NO_FRAGMENT_INFORMATION)
		e2 = gfs_unlink(path);	
	if (e != NULL)
		return (e);

	if (GFARM_S_ISREG(gs.st_mode)) {
		e = gfs_unlink(path);
	} else if (GFARM_S_ISDIR(gs.st_mode)) {
		if (!is_recursive)
	        	return (GFARM_ERR_IS_A_DIRECTORY);
		e = gfs_getcwd(cwdbuf, sizeof(cwdbuf));
		if (e != NULL)
			return (e);
		e = gfs_chdir(path);
		if (e != NULL)
			return (e);
		remove_cwd_entries();
		e = gfs_chdir_canonical(cwdbuf);
		if (e != NULL)
			return (e);
		e = gfs_rmdir(path);
	}
	gfs_stat_free(&gs);
	return (e);
}
Пример #2
0
static char *
display_replica_catalog(char *gfarm_url)
{
	char *gfarm_file, *e, *e_save = NULL;
	int i, j, nsections;
	struct gfarm_file_section_info *sections;
	struct gfs_stat st;

	e = gfarm_url_make_path(gfarm_url, &gfarm_file);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", gfarm_url, e);
		return (e);
	}
	e = gfs_stat(gfarm_url, &st);
	if (e != NULL) {
		free(gfarm_file);
		fprintf(stderr, "%s: %s\n", gfarm_url, e);
		return (e);
	}
	if (!GFARM_S_ISREG(st.st_mode)) {
		free(gfarm_file);
		gfs_stat_free(&st);
		fprintf(stderr, "%s: not a file\n", gfarm_url);
		return (e);
	}
	if ((st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) != 0) { /* program? */
		e = gfarm_file_section_info_get_all_by_file(
		    gfarm_file, &nsections, &sections);
	} else {
		e = gfarm_file_section_info_get_sorted_all_serial_by_file(
		    gfarm_file, &nsections, &sections);
	}
	gfs_stat_free(&st);
	if (e != NULL) {
		free(gfarm_file);
		fprintf(stderr, "%s: %s\n", gfarm_url, e);
		return (e);
	}
	for (i = 0; i < nsections; i++) {
		int ncopies;
		struct gfarm_file_section_copy_info *copies;

		e = gfarm_file_section_copy_info_get_all_by_section(
		    gfarm_file, sections[i].section, &ncopies, &copies);
		if (e != NULL) {
			fprintf(stderr, "%d: %s\n", i, e);
			if (e_save == NULL)
				e_save = e;
			continue;
		}
		printf("%s:", sections[i].section);
		for (j = 0; j < ncopies; j++)
			printf(" %s", copies[j].hostname);
		gfarm_file_section_copy_info_free_all(ncopies, copies);
		printf("\n");
	}
	gfarm_file_section_info_free_all(nsections, sections);
	free(gfarm_file);
	return (e_save);	
}
Пример #3
0
int
__lchown(const char *path, uid_t owner, gid_t group)
{
	const char *e;
	char *url;
	struct gfs_stat s;

	_gfs_hook_debug_v(fprintf(stderr, "Hooking __lchown(%s, %d, %d)\n",
				  path, uid, group));

	if (!gfs_hook_is_url(path, &url))
		return (__syscall_lchown(path, owner, group));

	_gfs_hook_debug(fprintf(stderr, "GFS: Hooking __lchown(%s, %d, %d)\n",
				path, owner, group));
	/* XXX - gfs_lstat is not supported */
	e = gfs_stat(url, &s);
	free(url);
	if (e == NULL) {
		if (strcmp(s.st_user, gfarm_get_global_username()) != 0)
			e = GFARM_ERR_OPERATION_NOT_PERMITTED; /* EPERM */
		/* XXX - do nothing */
		gfs_stat_free(&s);
	}	
	if (e == NULL)
		return (0);

	_gfs_hook_debug(fprintf(stderr, "GFS: __lchown: %s\n", e));
	errno = gfarm_error_to_errno(e);
	return (-1);
}
Пример #4
0
static char *
gfs_pio_view_global_stat(GFS_File gf, struct gfs_stat *status)
{
	struct gfs_file_global_context *gc = gf->view_context;

	return (gfs_stat(gc->url, status));
}
Пример #5
0
static int
fixfrag(char *pathname, const char *gfarm_prefix)
{
	char *gfarm_url, *sec, *gfarm_file, *e;
	struct gfs_stat gst;
	int r = 1;

	gfarm_url = append_prefix_pathname(gfarm_prefix, pathname);
	if (gfarm_url == NULL) {
		print_errmsg(pathname, NULL, "not enough memory");
		return (r);
	}

	/* divide into file and section parts. */
	e = split_file_and_section(gfarm_url, &sec);
	if (e != NULL) {
		print_errmsg(pathname, NULL, e);
		delete_invalid_file_or_directory(pathname);
		goto error_gfarm_url;
	}

	e = gfs_stat(gfarm_url, &gst);
	if (e == NULL) {
		if (!GFARM_S_ISREG(gst.st_mode)) {
			gfs_stat_free(&gst);
			print_errmsg(gfarm_url, NULL, "not a regular file");
			delete_invalid_file_or_directory(pathname);
			goto error_gfarm_url;
		}
		gfs_stat_free(&gst);
	}
	else
		/* permit no fragment case */;

	e = gfarm_url_make_path(gfarm_url, &gfarm_file);
	if (e != NULL) {
		print_errmsg(gfarm_url, sec, e);
		delete_invalid_file_or_directory(pathname);
		goto error_gfarm_url;
	}

	/* check whether the fragment is already registered. */
	e = fixfrag_i(gfarm_url, pathname, gfarm_file, sec);
	if (e != NULL && e != GFARM_ERR_ALREADY_EXISTS)
		goto error_gfarm_file;

	r = 0;

error_gfarm_file:
	free(gfarm_file);
error_gfarm_url:	
	free(gfarm_url);
	return (r);
}
Пример #6
0
static char *
gfs_pio_view_global_stat(GFS_File gf, struct gfs_stat *status)
{
	struct gfs_file_global_context *gc = gf->view_context;
	char *e;

	e = gfs_stat(gc->url, status);
	if (e != NULL)
		return (e);

	return (NULL);
}
Пример #7
0
static char *
gfsck_dir(char *gfarm_dir, char *file)
{
	char *e, *e_save = NULL, *gfarm_url;
	struct gfs_stat gsb;
	GFS_Dir gdir;
	struct gfs_dirent *gdent;

	gfarm_url = malloc(strlen(gfarm_dir) + strlen(file) + 2);
	if (gfarm_url == NULL)
		return (GFARM_ERR_NO_MEMORY);
	if (gfarm_dir[0] == '\0')
		sprintf(gfarm_url, "%s", file);
	else
		sprintf(gfarm_url, "%s/%s", gfarm_dir, file);

	e = gfs_stat(gfarm_url, &gsb);
	if (e != NULL) {
		free(gfarm_url);
		return (e);
	}
	if (GFARM_S_ISREG(gsb.st_mode)) {
		gfs_stat_free(&gsb);
		e = gfsck_file(gfarm_url);
		free(gfarm_url);
		return (e);
	}
	if (!GFARM_S_ISDIR(gsb.st_mode)) {
		gfs_stat_free(&gsb);
		free(gfarm_url);
		return ("unknown file type");
	}
	gfs_stat_free(&gsb);

	e = gfs_opendir(gfarm_url, &gdir);
	if (e != NULL) {
		free(gfarm_url);
		return (e);
	}
	while ((e = gfs_readdir(gdir, &gdent)) == NULL && gdent != NULL) {
		e = gfsck_dir(gfarm_url, gdent->d_name);
		if (e != NULL) {
			fprintf(stderr, "%s/%s: %s\n",
				gfarm_url, gdent->d_name, e);
			if (e_save == NULL)
				e_save = e;
		}
	}
	(void)gfs_closedir(gdir);
	free(gfarm_url);

	return (e_save);
}
Пример #8
0
static char *
display_replica_catalog(char *gfarm_url)
{
	char *gfarm_file, *e, *e_save;
	int i, nsections;
	struct gfarm_file_section_info *sections;
	struct gfs_stat st;
	gfarm_mode_t mode;

	e = gfs_stat(gfarm_url, &st);
	if (e != NULL)
		return (e);
	mode = st.st_mode;
	gfs_stat_free(&st);

	if (!GFARM_S_ISREG(mode))
		return ("not a regular file");

	e = gfarm_url_make_path(gfarm_url, &gfarm_file);
	if (e != NULL)
		return (e);

	if ((mode & (S_IXUSR|S_IXGRP|S_IXOTH)) != 0) { /* program? */
		e_save = gfarm_file_section_info_get_all_by_file(
		    gfarm_file, &nsections, &sections);
	} else {
		e_save = gfarm_file_section_info_get_sorted_all_serial_by_file(
		    gfarm_file, &nsections, &sections);
	}
	if (e_save != NULL)
		goto free_gfarm_file;

	for (i = 0; i < nsections; i++) {
		e = display_section_copies(gfarm_file, sections[i].section);
		if (e != NULL) {
			if (e_save == NULL)
				e_save = e;
			fprintf(stderr, "%s: %s\n", sections[i].section, e);
		}
	}
	gfarm_file_section_info_free_all(nsections, sections);
free_gfarm_file:
	free(gfarm_file);
	return (e_save);
}
Пример #9
0
static char *
remove_whole_file_or_dir(char *path,
	Unlink_Ops ops, void *closure, int is_recursive)
{
	struct gfs_stat gs;
	char *e, cwdbuf[PATH_MAX * 2];
	const char *b;
	gfarm_mode_t mode;

	b = gfarm_path_dir_skip(gfarm_url_prefix_skip(path));
	if (b[0] == '.' && (b[1] == '\0' || (b[1] == '.' && b[2] == '\0')))
		return ("cannot remove \'.\' or \'..\'");

	e = gfs_stat(path, &gs);
	if (e == GFARM_ERR_NO_FRAGMENT_INFORMATION)
		return (ops->unlink(path, closure));
	if (e != NULL)
		return (e);

	mode = gs.st_mode;
	gfs_stat_free(&gs);

	if (GFARM_S_ISREG(mode)) {
		e = ops->unlink(path, closure);
	} else if (GFARM_S_ISDIR(mode)) {
		if (!is_recursive)
			return (GFARM_ERR_IS_A_DIRECTORY);
		e = gfs_getcwd(cwdbuf, sizeof(cwdbuf));
		if (e != NULL)
			return (e);
		e = gfs_chdir(path);
		if (e != NULL)
			return (e);
		remove_cwd_entries(ops, closure);
		e = gfs_chdir_canonical(cwdbuf);
		if (e != NULL)
			return (e);
		e = ops->rmdir(path, closure);
	}
	return (e);
}
Пример #10
0
char *
gfs_chdir(const char *dir)
{
	char *e, *canonic_path;
	struct gfs_stat st;

	if ((e = gfs_stat(dir, &st)) != NULL)
		return (e);
	if (!GFARM_S_ISDIR(st.st_mode)) {
		gfs_stat_free(&st);
		return (GFARM_ERR_NOT_A_DIRECTORY);
	}
	gfs_stat_free(&st);

	e = gfarm_canonical_path(gfarm_url_prefix_skip(dir), &canonic_path);
	if (e != NULL)
		return (e);
	e = gfs_chdir_canonical(canonic_path);
	free (canonic_path);
	return (e);
}
Пример #11
0
static char *
display_replica_catalog_section(char *gfarm_url, char *section)
{
	char *gfarm_file, *e;
	int j, ncopies;
	struct gfarm_file_section_copy_info *copies;
	struct gfs_stat st;

	if (section == NULL)
		return (display_replica_catalog(gfarm_url));

	e = gfs_stat(gfarm_url, &st);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", gfarm_url, e);
		return (e);
	}
	gfs_stat_free(&st);

	e = gfarm_url_make_path(gfarm_url, &gfarm_file);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", gfarm_url, e);
		return (e);
	}
	e = gfarm_file_section_copy_info_get_all_by_section(
		gfarm_file, section, &ncopies, &copies);
	if (e != NULL) {
		fprintf(stderr, "%s\n", e);
		free(gfarm_file);
		return (e);
	}
	j = 0;
	printf("%s", copies[j].hostname);
	for (++j; j < ncopies; ++j)
		printf(" %s", copies[j].hostname);
	printf("\n");
	gfarm_file_section_copy_info_free_all(ncopies, copies);
	free(gfarm_file);

	return (NULL);
}
Пример #12
0
char *
gfs_opendir(const char *path, GFS_Dir *dirp)
{
	char *e, *canonic_path;
	struct node *n;
	struct gfs_dir *dir;
	struct gfs_stat st;

	/* gfs_stat() -> gfarm_path_info_get() makes the dircache consistent */
	if ((e = gfs_stat(path, &st)) != NULL)
		return (e);
	if (!GFARM_S_ISDIR(st.st_mode)) {
		gfs_stat_free(&st);
		return (GFARM_ERR_NOT_A_DIRECTORY);
	}
	gfs_stat_free(&st);

	e = gfarm_canonical_path(gfarm_url_prefix_skip(path), &canonic_path);
	if (e != NULL)
		return (e);

	e = lookup_relative(root, canonic_path, NODE_FLAG_IS_DIR,
	    GFARM_INODE_LOOKUP, &n);
	free(canonic_path);
	if (e != NULL)
		return (e);

	dir = malloc(sizeof(struct gfs_dir));
	if (dir == NULL)
		return (GFARM_ERR_NO_MEMORY);
	dir->dir = n;
	gfarm_hash_iterator_begin(n->u.d.children, &dir->iterator);
	dir->index = 0;
	*dirp = dir;

	++opendir_count;
	/* XXX if someone removed a path, while opening a directory... */
	return (NULL);
}
Пример #13
0
/* this returns uncached result, but enter the result to the cache */
gfarm_error_t
gfs_stat_caching(const char *path, struct gfs_stat *st)
{
	gfarm_error_t e;
	struct timeval now;

	e = gfs_stat(path, st);
	if (e != GFARM_ERR_NO_ERROR)
		return (e);

	gettimeofday(&now, NULL);
	if ((e = gfs_stat_cache_enter_internal(path, st, &now)) !=
	    GFARM_ERR_NO_ERROR) {
		/*
		 * It's ok to fail in entering the cache,
		 * since it's merely cache.
		 */
		gflog_warning(GFARM_MSG_UNUSED,
		    "gfs_stat_caching: failed to cache %s: %s",
		    path, gfarm_error_string(e));
	}
	return (GFARM_ERR_NO_ERROR);
}
Пример #14
0
char *
do_stats(char *prefix, int *np, char **files, struct gfs_stat *stats,
	struct ls_entry *ls)
{
	int i, n = *np, m, prefix_len, space;
	char *namep, buffer[PATH_MAX * 2 + 1], *e, *e_save = NULL;

	prefix_len = strlen(prefix);
	if (prefix_len > sizeof(buffer) - 1)
		prefix_len = sizeof(buffer) - 1;
	memcpy(buffer, prefix, prefix_len);
	namep = &buffer[prefix_len];
	space = sizeof(buffer) - prefix_len - 1;

	m = 0;
	for (i = 0; i < n; i++) {
		if (strlen(files[i]) <= space) {
			strcpy(namep, files[i]);
		} else {
			memcpy(namep, files[i], space);
			namep[space] = '\0';
		}
		e = gfs_stat(buffer, &stats[i]);
		if (e != NULL) {
			fprintf(stderr, "%s: %s\n", buffer, e);
			if (e_save != NULL)
				e_save = e;
			continue;
		}
		ls[m].path = files[i];
		ls[m].st = &stats[i];
		m++;
	}
	*np = m;
	return (e_save);
}
Пример #15
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);
}
Пример #16
0
static void
fixurl(const char *gfarm_url)
{
	char *gfarm_file, *local_path, *e;
	struct stat sb;
	int len_path, is_invalid = 0, is_directory = 0;
	glob_t pglob;
	char **pathp, *pat;
	struct gfs_stat gs;

	e = gfarm_canonical_path(gfarm_url_prefix_skip(gfarm_url), &gfarm_file);
	if (e != NULL) {
		/*
		 * no path info, try to delete invalid physical files
		 * or directories
		 */
		e = gfarm_canonical_path_for_creation(
			gfarm_url_prefix_skip(gfarm_url), &gfarm_file);
		if (e != NULL) {
			/* in this case, give up searching invalid files */
			print_errmsg(gfarm_url, NULL, e);
			return;
		}
		is_invalid = 1;
	}
	else {
		/* check it is a directory or not */
		e = gfs_stat(gfarm_url, &gs);
		if (e != NULL) {
			if (e != GFARM_ERR_NO_FRAGMENT_INFORMATION) {
				/* maybe permission denied */
				print_errmsg(gfarm_url, NULL, e);
				goto error_gfarm_file;
			}
			/* no fragment information case */
		}
		else {
			is_directory = GFARM_S_ISDIR(gs.st_mode);
			gfs_stat_free(&gs);
		}
	}
	/*
	 * Check local_path; if it is invalid or not a directory,
	 * delete it.  Otherwise, check it recursively.
	 */
	e = gfarm_path_localize(gfarm_file, &local_path);
	if (e == NULL && stat(local_path, &sb) == 0) {
		if (is_invalid || !is_directory || !S_ISDIR(sb.st_mode)) {
			print_errmsg(local_path, NULL,
				"invalid file or directory");
			delete_invalid_file_or_directory(local_path);
		}
		else if (chdir(local_path) == 0)
			(void)fixdir(".", gfarm_url);
		/* continue */
	}
	if (e != NULL) {
		print_errmsg(gfarm_url, NULL, e);
		goto error_gfarm_file;
	}

	/* investigate file sections */
	len_path = strlen(local_path);
	GFARM_MALLOC_ARRAY(pat, len_path + 3);
	if (pat == NULL) {
		print_errmsg(gfarm_url, NULL, "not enough memory");
		free(local_path);
		goto error_gfarm_file;
	}
	strcpy(pat, local_path);
	strcat(pat, ":*");
	free(local_path);

	pglob.gl_offs = 0;
	glob(pat, GLOB_DOOFFS, NULL, &pglob);
	free(pat);
	
	pathp = pglob.gl_pathv;
	while (*pathp) {
		char *sec = &((*pathp)[len_path + 1]);

		if (is_invalid || is_directory) {
			print_errmsg(gfarm_url, sec, "invalid file");
			delete_invalid_file_or_directory(*pathp);
			++pathp;
			continue;
		}
		(void)fixfrag_i(gfarm_url, *pathp, gfarm_file, sec);

		++pathp;
	}
	globfree(&pglob);

 error_gfarm_file:
	free(gfarm_file);
	return;
}
Пример #17
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);
}
Пример #18
0
static int
gfarm_register_file(char *gfarm_url, char *node_index, char *hostname,
	int total_nodes, char *filename, int auto_index)
{
	struct stat s;
	struct gfs_stat gs;
	char *e, *target_url = NULL;
	int executable_file = 0;

	if (stat(filename, &s) == 0) {
		if (!S_ISREG(s.st_mode)) {
			fprintf(stderr, "%s: not a regular file", filename);
			return (-1);
		}
		if ((s.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) != 0)
			executable_file = 1;
	}
	else if (strcmp(filename, "-")) {
		perror(filename);
		return (-1);
	}

	e = gfs_stat(gfarm_url, &gs);
	if (e == NULL && GFARM_S_ISDIR(gs.st_mode)) {
		char *bname;
		/* gfarm_url is a directory */

		gfs_stat_free(&gs);

		if (auto_index && total_nodes > 1 && !executable_file) {
			/*
			 * In the auto index mode, the target Gfarm URL
			 * should be a regular file when two or more local
			 * non-executable files will be registered for
			 * preventing unexpected results.
			 */
			fprintf(stderr, "%s: not a regular file.  "
				"The target Gfarm URL should be a "
				"regular file when registering two or "
				"more local files.\n", gfarm_url);
			return (-1);
		}

		bname = basename(filename);

		target_url = malloc(strlen(gfarm_url) + strlen(bname) + 2);
		if (target_url == NULL) {
			fprintf(stderr, "not enough memory\n");
			return (-1);
		}

		strcpy(target_url, gfarm_url);
		if (*gfarm_path_dir_skip(gfarm_url_prefix_skip(target_url))
		    != '\0')
			strcat(target_url, "/");
		strcat(target_url, bname);
	}
	else if (e == NULL)
		gfs_stat_free(&gs);

	if (target_url == NULL) {
		target_url = strdup(gfarm_url);
		if (target_url == NULL) {
			fprintf(stderr, "not enough memory\n");
			return (-1);
		}
	}

	if (executable_file) {
		/* register a binary executable. */

		/*
		 * In auto index case, node_index does not stand for
		 * architecture.
		 */
		if (auto_index)
			node_index = NULL;

		if (node_index == NULL) {
			if (hostname == NULL) {
				char *self_name;

				e = gfarm_host_get_canonical_self_name(
					&self_name);
				if (e != NULL) {
					fprintf(stderr, "%s: %s\n",
						gfarm_host_get_self_name(), e);
					return (-1);
				}
				node_index =
				    gfarm_host_info_get_architecture_by_host(
					    self_name);
			}
			else {
				char *c_name;

				e = gfarm_host_get_canonical_name(
					hostname, &c_name);
				if (e != NULL) {
					fprintf(stderr, "%s: %s\n",
						hostname, e);
					return (-1);
				}
				node_index = 
				    gfarm_host_info_get_architecture_by_host(
					    c_name);
				free(c_name);
			}
		}
		if (node_index == NULL) {
			fprintf(stderr,
				"%s: cannot determine the architecture "
				"of %s.\n",
				program_name, gfarm_host_get_self_name());
			return (-1);
		}
		if (total_nodes <= 0) {
			if (gfs_pio_get_node_size(&total_nodes) != NULL)
				total_nodes = 1;
		}

		if (!opt_force) {
			struct gfs_stat s;

			if (gfs_stat_section(target_url, node_index, &s)
			    == NULL) {
				gfs_stat_free(&s);
				e = "already exist";
				goto finish;
			}
		}

		e = gfarm_url_fragment_register(target_url, node_index,
			GFARM_FILE_DONTCARE, hostname, filename);
		/*
		 * XXX - gfarm_url_replicate() to replicate
		 * 'total_nodes' copies of target_url.
		 */

	} else {
		char index_str[GFARM_INT32STRLEN + 1];

		/* register a file fragment. */
		if (node_index == NULL) {
			int index;

			e = gfs_pio_get_node_rank(&index);
			if (e != NULL) {
				fprintf(stderr,
					"%s: missing -I <Gfarm index>\n",
					program_name);
				return (-1);
			}
			sprintf(index_str, "%d", index);
			node_index = index_str;
		}

		if (total_nodes <= 0) {
			e = gfs_pio_get_node_size(&total_nodes);
			if (e != NULL) {
				fprintf(stderr,
					"%s: missing -N "
					"<total num of fragments>\n",
					program_name);
				return (-1);
			}
		}

		if (!opt_force) {
			struct gfs_stat s;

			if (gfs_stat_section(target_url, node_index, &s)
			    == NULL) {
				gfs_stat_free(&s);
				e = "already exist";
				goto finish;
			}
		}

		e = gfarm_url_fragment_register(target_url, node_index,
			total_nodes, hostname, filename);
	}
 finish:
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", target_url, e);
		return (-1);
	}

	free(target_url);

	return (0);
}
Пример #19
0
int
FUNC___OPEN(const char *path, int oflag, ...)
{
	GFS_File gf;
	const char *e;
	char *url;
	va_list ap;
	mode_t mode;
	int filedes;
	struct gfs_stat gs;
	int file_exist, file_size = 0, is_directory = 0;
	int nf = -1, np;

	va_start(ap, oflag);
	mode = va_arg(ap, mode_t);
	va_end(ap);

	_gfs_hook_debug_v(fprintf(stderr,
	    "Hooking " S(FUNC___OPEN) "(%s, 0x%x)\n", path, oflag));

	if (!gfs_hook_is_url(path, &url))
		return (SYSCALL_OPEN(path, oflag, mode));

	if (gfs_hook_get_current_view() == section_view)
		e = gfs_stat_section(url, gfs_hook_get_current_section(), &gs);
	else
		e = gfs_stat(url, &gs);
	if (e == GFARM_ERR_NO_SUCH_OBJECT)
		file_exist = 0;
	else if (e == NULL) {
		file_exist = 1;
		file_size = gs.st_size;
		is_directory = GFARM_S_ISDIR(gs.st_mode);
		gfs_stat_free(&gs);
	}
	else {
		_gfs_hook_debug(fprintf(stderr,
		    "GFS: Hooking " S(FUNC___OPEN) ": gfs_stat: %s\n", e));
		/* XXX - metadata may be incomplete. anyway, continue. */
		file_exist = 1;
		file_size = 0;
		/* XXX - metadata of a directory should not be imcomplete */
		is_directory = 0; 
	}

	/*
	 * XXX - stopgap implementation of O_WRONLY/O_RDWR, O_CREAT,
	 * O_TRUNC flags
	 */
	if (!file_exist && (oflag & O_CREAT) != 0) {

		_gfs_hook_debug(fprintf(stderr,
		    "GFS: Hooking " S(FUNC___OPEN) "(%s, 0x%x, 0%o)\n",
		    url, oflag, mode));

		e = gfs_pio_create(url, oflag, mode, &gf);
		if (e == NULL)
			gfs_hook_add_creating_file(gf);
	}
	else if ((oflag & O_ACCMODE) == O_WRONLY
		 || (oflag & O_ACCMODE) == O_RDWR) {

		_gfs_hook_debug(fprintf(stderr,
		    "GFS: Hooking " S(FUNC___OPEN) "(%s, 0x%x, 0%o)\n",
		    url, oflag, mode));

		if (file_exist && (file_size == 0 || (oflag & O_TRUNC) != 0)) {
			/*
			 * XXX - O_TRUNC is basically supported in
			 * Gfarm v1 except in global file view.
			 * This case needs to be implemented in
			 * gfs_pio_set_view_global() in gfs_pio_global.c.
			 */
			if (gfs_hook_get_current_view() == global_view)
				gfs_unlink(url); /* XXX - FIXME */
			/*
			 * gfs_pio_create assumes GFARM_FILE_TRUNC.
			 * It is not necessary to set O_TRUNC explicitly.
			 */
			e = gfs_pio_create(url, oflag, mode, &gf);
			if (e == NULL)
				gfs_hook_add_creating_file(gf);
		}
		else if (file_exist) { /* read-write mode: not supported yet */
			_gfs_hook_debug(fprintf(stderr,
				"GFS: Hooking " S(FUNC___OPEN)
				": unsupported flags 0%o\n", oflag));
			free(url);
			errno = ENOSYS;
			return (-1);
		}
		else { /* no such file or directory */
			_gfs_hook_debug(fprintf(stderr,
				"GFS: Hooking " S(FUNC___OPEN)
				": no such file or directory\n"));
			free(url);
			errno = ENOENT;
			return (-1);
		}
	}
	/* XXX - end of stopgap implementation */
	else {
		_gfs_hook_debug(fprintf(stderr,
		    "GFS: Hooking " S(FUNC___OPEN) "(%s, 0x%x)\n", url, oflag));

		if (file_exist && is_directory) {
			GFS_Dir dir;

			e = gfs_opendir(url, &dir);
                        if (e == NULL) {
                                filedes = gfs_hook_insert_gfs_dir(dir, url);
                                _gfs_hook_debug(
                                        if (filedes != -1) {
                                                fprintf(stderr,
                                                    "GFS: Hooking "
                                                    S(FUNC___OPEN)
                                                    " --> %d\n", filedes);
                                        }
                                );
                                free(url);
                                return (filedes);
			}
Пример #20
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);
}
Пример #21
0
int
main(int argc, char *argv[])
{
	/* options */
	char *section = NULL;
	int nfragments = GFARM_FILE_DONTCARE; /* -1, actually */
	char *hostname = NULL;
	char *hostfile = NULL;
	char *domainname = NULL;

	char *e, *gfarm_url, *file_mode_arg;
	gfarm_mode_t file_mode = DEFAULT_FILE_MODE;
	int c, i, is_dir, index;
	struct gfs_stat gs;

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

	/*  Command options  */

	while ((c = getopt(argc, argv, "a:fh:D:I:N:?")) != -1) {
		switch (c) {
		case 'I':
		case 'a':
			section = optarg;
			break;
		case 'N':
			nfragments = strtol(optarg, NULL, 0);
			break;
		case 'h':
			hostname = optarg;
			break;
		case 'D':
			domainname = optarg;
			break;
		case 'f':
			opt_force = 1;
			break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc == 0) {
		fprintf(stderr, "%s: missing a local filename\n",
			program_name);
		usage();
	}
	if (argc == 1) {
		fprintf(stderr, "%s: missing a Gfarm URL\n",
			program_name);
		usage();
	}
	gfarm_url = argv[argc - 1];
	--argc;

	if (!gfarm_is_url(gfarm_url)) {
		fprintf(stderr, "%s: %s: %s\n",
		    program_name, gfarm_url,
		    GFARM_ERR_GFARM_URL_PREFIX_IS_MISSING);
		exit(EXIT_FAILURE);
	}

	c = 0;
	if (hostname != NULL)
		c++;
	if (hostfile != NULL)
		c++;
	if (domainname != NULL)
		c++;
	if (c > 1) {
		fprintf(stderr,
		    "%s: more than one options are specified "
		    "from -h, -H and -D\n",
		    program_name);
		usage();
	}

	/*
	 * distinguish which mode is specified:
	 * 1. program mode:
	 *	gfreg [-h <hostname>] [-a <architecture>] \
	 *		<local-program>... <gfarm-URL>
	 * 2. auto index mode:
	 *	gfreg [-h <hostname>] [-H <hostfile>] [-D <domainname>] \
	 *		<local-file>... <gfarm-URL>
	 * 3. fragment mode:
	 *	gfreg [-h <hostname>] [-N <nfragments>] -I <index> \
	 *		<local-file>... <gfarm-URL>
	 */

	e = gfs_stat(gfarm_url, &gs);
	if (e == GFARM_ERR_NO_SUCH_OBJECT) {
		is_dir = 0;
		file_mode_arg = NULL;
	} else if (e != NULL) {
		fprintf(stderr, "%s: %s: %s\n", program_name, gfarm_url, e);
		exit(EXIT_FAILURE);
	} else {
		if (GFARM_S_ISREG(gs.st_mode)) {
			is_dir = 0;
			file_mode_arg = gfarm_url;
			file_mode = gs.st_mode;
		} else if (GFARM_S_ISDIR(gs.st_mode)) {
			is_dir = 1;
			file_mode_arg = NULL;
		} else { /* defensive programming. this shouldn't happen. */
			fprintf(stderr, "%s: %s: unknown file type\n",
			    program_name, gfarm_url);
			exit(EXIT_FAILURE);
		}
		gfs_stat_free(&gs);
	}

	c = 0; /* count of "-" in the arguments */
	if (hostfile != NULL && strcmp(hostfile, STDIN_FILENAME) == 0)
		++c;
	for (i = 0; i < argc; i++) {
		int fd, fd_needs_close;
		gfarm_mode_t m;

		if (!open_file(argv[i], &fd, &fd_needs_close))
			exit(EXIT_FAILURE);
		if (!get_mode(fd, argv[i], &m))
			exit(EXIT_FAILURE);
		if (S_ISREG(m)) {
			if (file_mode_arg == NULL) {
				/*
				 * NOTE: this mode may be used for the mode
				 * to create the gfarm file.
				 */
				file_mode_arg = argv[i];
				file_mode = m & FILE_MODE_MASK;
			}
			if (((m & 0111) != 0) != ((file_mode & 0111) != 0)) {
				fprintf(stderr,
				    "%s: program and non-program are mixed in "
				    "%s and %s\n",
				    program_name, file_mode_arg, argv[i]);
				exit(EXIT_FAILURE);
			}
		} else if (fd_needs_close) {
			/* if it's "-", allow non-file (e.g. pipe) */
			fprintf(stderr, "%s: %s: not a regular file\n",
			    program_name, argv[i]);
			exit(EXIT_FAILURE);
		}
		if (fd_needs_close) {
			close(fd);
		} else if (++c > 1) {
			fprintf(stderr, "%s: `-' (stdin) is specified "
			    "multiple times\n", program_name);
			exit(EXIT_FAILURE);
		}
	}

	if ((file_mode & 0111) != 0) {
		/*
		 * program mode
		 */
		int section_alloced = 0;

		if (!is_dir && argc != 1) {
			fprintf(stderr, "%s: only one file can be specified to"
			    " register the gfarm program `%s'\n",
			    program_name, gfarm_url);
			exit(EXIT_FAILURE);
		}
		if (hostfile != NULL || domainname != NULL) {
			fprintf(stderr,
			    "%s: cannot use -%c to register programs\n", 
			    program_name, hostfile != NULL ? 'H' : 'D');
			exit(EXIT_FAILURE);
		}
		if (nfragments != GFARM_FILE_DONTCARE) {
			/*
			 * XXX - call gfarm_url_replicate() to replicate
			 * `nfragments' copies of gfarm_url:section?
			 */
			fprintf(stderr,
			    "%s: warning: option -N is currently ignored\n", 
			    program_name);
		}
		if (section != NULL) {
			;
		} else if (hostname != NULL) {
			char *canonical;

			e = gfarm_host_get_canonical_name(hostname,
			    &canonical);
			if (e != NULL) {
				if (e == GFARM_ERR_NO_SUCH_OBJECT)
					e = "not a filesystem node";
				fprintf(stderr, "%s: host %s: %s\n",
				    program_name, hostname, e);
				exit(EXIT_FAILURE);
			}
			section_alloced = 1;
			section = gfarm_host_info_get_architecture_by_host(
			    canonical);
			free(canonical);
			if (section == NULL) {
				fprintf(stderr, "%s: %s\n",
				    program_name, GFARM_ERR_NO_MEMORY);
				exit(EXIT_FAILURE);
			}
		} else if (gfarm_host_get_self_architecture(&section) != NULL){
			fprintf(stderr, "%s: missing -a option\n",
			    program_name);
			exit(EXIT_FAILURE);
		}
		for (i = 0; i < argc; i++) {
			register_program(is_dir, gfarm_url, section,
			    hostname, argv[i],
			    file_mode_arg == gfarm_url ||
			    file_mode_arg == argv[i],
			    file_mode);
		}
		if (section_alloced)
			free(section);
	} else if (section != NULL || gfs_pio_get_node_rank(&index) == NULL) {
		/*
		 * fragment mode
		 */
		if (section != NULL)
			index = strtol(section, NULL, 0);
		else if (nfragments == GFARM_FILE_DONTCARE)
			gfs_pio_get_node_size(&nfragments);
		if (!is_dir && argc != 1) {
			fprintf(stderr, "%s: only one file can be specified to"
			    " register a fragment %s of the gfarm file `%s'\n",
			    program_name, section, gfarm_url);
			exit(EXIT_FAILURE);
		}
		if (hostfile != NULL || domainname != NULL) {
			fprintf(stderr,
			    "%s: cannot use -%c with -I\n", 
			    program_name, hostfile != NULL ? 'H' : 'D');
			exit(EXIT_FAILURE);
		}
		for (i = 0; i < argc; i++) {
			register_fragment(is_dir, gfarm_url, index, nfragments,
			    hostname, argv[i],
			    file_mode_arg == gfarm_url ||
			    file_mode_arg == argv[i],
			    file_mode);
		}
	} else {
		/*
		 * auto index mode
		 */
		char **hosts = NULL;

		if (nfragments == GFARM_FILE_DONTCARE)
			nfragments = argc;
		if (nfragments != argc) {
			fprintf(stderr, "%s: local file number %d "
			    "doesn't match with -N %d\n",
			    program_name, argc, nfragments);
			exit(EXIT_FAILURE);
		}
		if (is_dir && nfragments > 1) {
			fprintf(stderr, "%s: cannot determine the file name "
			    "under the directory %s, "
			    "because multiple local file names are specifed\n",
			    program_name, gfarm_url);
			exit(EXIT_FAILURE);
		}
		if (hostname != NULL) {
			;
		} else if (hostfile != NULL) {
			int nhosts, error_line;

			e = gfarm_hostlist_read(hostfile,
			    &nhosts, &hosts, &error_line);
			if (e != NULL) {
				if (error_line != -1)
					fprintf(stderr, "%s: %s line %d: %s\n",
					    program_name,
					    hostfile, error_line, e);
				else
					fprintf(stderr, "%s: %s: %s\n",
					    program_name, hostfile, e);
				exit(EXIT_FAILURE);
			}
		} else {
			hosts = malloc(sizeof(*hosts) * nfragments);
			if (hosts == NULL) {
				fprintf(stderr, "%s: %s\n", program_name,
				    GFARM_ERR_NO_MEMORY);
				exit(EXIT_FAILURE);
			}
			if (domainname != NULL)
				e = gfarm_schedule_search_idle_by_domainname(
					domainname, nfragments, hosts);
			else
				e = gfarm_schedule_search_idle_by_all(
					nfragments, hosts);
			if (e != NULL) {
				fprintf(stderr,
				    "%s: selecting filesystem nodes: %s\n",
				    program_name, e);
				exit(EXIT_FAILURE);
			}
		}
		/* XXX - need to register in parallel? */
		for (i = 0; i < argc; i++) {
			register_fragment(is_dir, gfarm_url, i, nfragments,
			    hostname != NULL ? hostname : hosts[i], argv[i],
			    1, file_mode);
		}
		if (hostname == NULL)
			gfarm_strings_free_deeply(nfragments, hosts);
	}

	e = gfarm_terminate();
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", program_name, e);
		exit(EXIT_FAILURE);
	}

	exit(error_happened);
}
Пример #22
0
static char *
gfsck_dir(char *gfarm_dir, char *file)
{
	char *e, *gfarm_url;
	struct gfs_stat gsb;
	GFS_Dir gdir;
	struct gfs_dirent *gdent;

	gfarm_url = malloc(strlen(gfarm_dir) + strlen(file) + 2);
	if (gfarm_url == NULL)
		return (GFARM_ERR_NO_MEMORY);
	if (gfarm_dir[0] == '\0')
		sprintf(gfarm_url, "%s", file);
	else if (strcmp(gfarm_dir, GFARM_URL_PREFIX) == 0)
		sprintf(gfarm_url, "%s%s", gfarm_dir, file);
	else
		sprintf(gfarm_url, "%s/%s", gfarm_dir, file);

	e = gfs_stat(gfarm_url, &gsb);
	if (e != NULL) {
		if (e == GFARM_ERR_NO_FRAGMENT_INFORMATION) {
			/* no fragment information, remove path info */
			e = path_info_remove(gfarm_url, NULL);
		}
		free(gfarm_url);
		return (e);
	}
	if (GFARM_S_ISREG(gsb.st_mode)) {
		gfs_stat_free(&gsb);
		e = gfsck_file(gfarm_url);
		free(gfarm_url);
		return (e);
	}
	if (!GFARM_S_ISDIR(gsb.st_mode)) {
		gfs_stat_free(&gsb);
		free(gfarm_url);
		return ("unknown file type");
	}
	gfs_stat_free(&gsb);

	e = gfs_opendir(gfarm_url, &gdir);
	if (e != NULL) {
		free(gfarm_url);
		return (e);
	}
	while ((e = gfs_readdir(gdir, &gdent)) == NULL && gdent != NULL) {
		if (gdent->d_name[0] == '.' && (gdent->d_name[1] == '\0' ||
		    (gdent->d_name[1] == '.' && gdent->d_name[2] == '\0')))
			continue; /* "." or ".." */
		e = gfsck_dir(gfarm_url, gdent->d_name);
		if (e != NULL) {
			fprintf(stderr, "%s%s%s: %s\n",
				gfarm_url, 
				strcmp(gfarm_url, GFARM_URL_PREFIX) == 0
				? "" : "/", gdent->d_name, e);
			/* it is not necessary to save error */
		}
	}
	(void)gfs_closedir(gdir);
	free(gfarm_url);

	return (NULL);
}
Пример #23
0
int
gfs_hook_insert_gfs_dir(GFS_Dir dir, char *url)
{
	int fd, save_errno;
	char *e, *canonical_path;

	_gfs_hook_debug(fprintf(stderr, "GFS: insert_gfs_dir: %p\n", dir));

	/*
	 * A new file descriptor is needed to identify a hooked file
	 * descriptor.
	 */
	fd = open("/dev/null", O_RDONLY);
	if (fd == -1) {
		save_errno = errno;
		gfs_closedir(dir);
		errno = save_errno;
		return (-1);
	}
	if (fd >= MAX_GFS_FILE_BUF) {
		__syscall_close(fd);
		gfs_closedir(dir);
		errno = EMFILE;
		return (-1);
	}
	if (_gfs_file_buf[fd] != NULL) {
		__syscall_close(fd);
		gfs_closedir(dir);
		errno = EBADF; /* XXX - something broken */
		return (-1);
	}
	e = gfarm_canonical_path(gfarm_url_prefix_skip(url),
	    &canonical_path);
	if (e != NULL) {
		__syscall_close(fd);
		gfs_closedir(dir);
		errno = gfarm_error_to_errno(e);
		return (-1);
	}
        _gfs_file_buf[fd] = malloc(sizeof(*_gfs_file_buf[fd]));
        if (_gfs_file_buf[fd] == NULL) {
		free(canonical_path);
		__syscall_close(fd);
		gfs_closedir(dir);
		errno = ENOMEM;
		return (-1);
        }
        _gfs_file_buf[fd]->u.d = malloc(sizeof(*_gfs_file_buf[fd]->u.d));
	if (_gfs_file_buf[fd]->u.d == NULL) {
		free(_gfs_file_buf[fd]);
		_gfs_file_buf[fd] = NULL;
		free(canonical_path);
		__syscall_close(fd);
		gfs_closedir(dir);
		errno = ENOMEM;
		return (-1);
        }
	e = gfs_stat(url, &_gfs_file_buf[fd]->u.d->gst);
	if (e != NULL) {
		free(_gfs_file_buf[fd]->u.d);
		free(_gfs_file_buf[fd]);
		_gfs_file_buf[fd] = NULL;
		free(canonical_path);
		__syscall_close(fd);
		gfs_closedir(dir);
		errno = gfarm_error_to_errno(e);
		return (-1);
	}
	_gfs_file_buf[fd]->refcount = 1;
	_gfs_file_buf[fd]->d_type = GFS_DT_DIR;
	_gfs_file_buf[fd]->u.d->dir = dir;
	_gfs_file_buf[fd]->u.d->suspended = NULL;
	_gfs_file_buf[fd]->u.d->canonical_path = canonical_path;
	return (fd);
}
Пример #24
0
static int
fixfrag(char *pathname, const char *gfarm_prefix)
{
	char *gfarm_url, *sec, *pname, *gfarm_file, *e;
	struct gfs_stat gst;
	int r = 1;

	gfarm_url = append_prefix_pathname(gfarm_prefix, pathname);
	if (gfarm_url == NULL) {
		print_errmsg(pathname, "not enough memory");
		return (r);
	}

	/* divide into file and section parts. */
	sec = &gfarm_url[strlen(gfarm_url) - 1];
	pname = sec - strlen(pathname) + 1;
	while (sec > pname && *sec != '/') {
		if (*sec == ':') {
			*sec = '\0';
			++sec;
			break;
		}
		--sec;
	}
	if (sec == pname || *sec == '/') {
		print_errmsg(pathname, "invalid filename");
		delete_invalid_file_or_directory(pathname);
		goto error_gfarm_url;
	}

	e = gfs_stat(gfarm_url, &gst);
	if (e == NULL) {
		if (!GFARM_S_ISREG(gst.st_mode)) {
			gfs_stat_free(&gst);
			print_errmsg(gfarm_url, "not a regular file");
			delete_invalid_file_or_directory(pathname);
			goto error_gfarm_url;
		}
		gfs_stat_free(&gst);
	}
	else
		/* permit no fragment case */;

	e = gfarm_url_make_path(gfarm_url, &gfarm_file);
	if (e != NULL) {
		print_errmsg_with_section(gfarm_url, sec, e);
		delete_invalid_file_or_directory(pathname);
		goto error_gfarm_url;
	}

	/* check whether the fragment is already registered. */
	e = fixfrag_i(pathname, gfarm_file, sec);
	if (e != NULL) {
		if (e != GFARM_ERR_ALREADY_EXISTS) {
			print_errmsg_with_section(pathname, sec, e);
			delete_invalid_file_or_directory(pathname);
			goto error_gfarm_file;
		}
		else
			/* no message */;
	}
	else
		printf("%s (%s) on %s: fixed\n", gfarm_url, sec,
		       gfarm_host_get_self_name());
	r = 0;

error_gfarm_file:
	free(gfarm_file);
error_gfarm_url:	
	free(gfarm_url);
	return (r);
}
Пример #25
0
int
FUNC___OPEN(const char *path, int oflag, ...)
{
	GFS_File gf;
	const char *e;
	char *url;
	va_list ap;
	mode_t mode;
	int filedes;
	struct gfs_stat gs;
	int is_directory, save_errno;
	int nf = -1, np;

	va_start(ap, oflag);
	/*
	 * We need `int' instead of `mode_t' in va_arg() below, because
	 * sizeof(mode_t) < sizeof(int) on some platforms (e.g. FreeBSD),
	 * and gcc-3.4/gcc-4's builtin va_arg() warns the integer promotion.
	 * XXX	this doesn't work, if sizeof(mode_t) > sizeof(int),
	 *	but currently there isn't any such platform as far as we know.
	 */
	mode = va_arg(ap, int);
	va_end(ap);

	_gfs_hook_debug_v(gflog_info(
	    "Hooking " S(FUNC___OPEN) "(%s, 0%o)", path, oflag));

	if (!gfs_hook_is_url(path, &url))
		return (SYSCALL_OPEN(path, oflag, mode));

	if (gfs_hook_get_current_view() == section_view)
		e = gfs_stat_section(url, gfs_hook_get_current_section(), &gs);
	else
		e = gfs_stat(url, &gs);
	if (e == NULL) {
		is_directory = GFARM_S_ISDIR(gs.st_mode);
		gfs_stat_free(&gs);
	} else {
		/* XXX - metadata may be incomplete. anyway, continue. */
		/* XXX - metadata of a directory should not be imcomplete */
		is_directory = 0;
		if (e != GFARM_ERR_NO_SUCH_OBJECT)
			_gfs_hook_debug(gflog_info(
			    "GFS: Hooking " S(FUNC___OPEN) ": gfs_stat: %s",
			    e));
	}

	if (is_directory) {
		GFS_Dir dir;

		_gfs_hook_debug(gflog_info(
		   "GFS: Hooking " S(FUNC___OPEN) "(%s, 0%o): dir",
		    url, oflag));

		if ((oflag & (O_CREAT|O_TRUNC)) != 0 ||
		    (oflag & O_ACCMODE) != O_RDONLY) {
			free(url);
			errno = EISDIR;
			return (-1);
		}
		e = gfs_opendir(url, &dir);
		if (e == NULL) {
			filedes = gfs_hook_insert_gfs_dir(dir, url);
			save_errno = errno;
			_gfs_hook_debug(
				gflog_info("GFS: Hooking "
				    S(FUNC___OPEN) " --> %d", filedes);
			);
			free(url);
			if (filedes == -1)
				errno = save_errno;
			return (filedes);
		}
Пример #26
0
static int
fixdir(char *dir, const char *gfarm_prefix)
{
	DIR* dirp;
	struct dirent *dp;
	struct stat sb;
	char *dir1;
	char *gfarm_url, *e;
	int is_directory;
	struct gfs_stat gs;

	if (lstat(dir, &sb)) {
		perror(dir);
		return (1);
	}
	if (S_ISREG(sb.st_mode))
		return (fixfrag(dir, gfarm_prefix));

	if (!S_ISDIR(sb.st_mode)) {
		print_errmsg(dir, NULL,
			"neither a regular file nor a directory");
		delete_invalid_file_or_directory(dir);
		return (1);
	}

	/* 'dir' is a directory */
	gfarm_url = append_prefix_pathname(gfarm_prefix, dir);
	if (gfarm_url == NULL) {
		print_errmsg(dir, NULL, "not enough memory");
		return (1);
	}

	e = gfs_stat(gfarm_url, &gs);
	if (e != NULL) {
		print_errmsg(gfarm_url, NULL, e);
		delete_invalid_file_or_directory(dir);
		free(gfarm_url);
		return (1);
	}
	is_directory = GFARM_S_ISDIR(gs.st_mode);
	gfs_stat_free(&gs);
	if (!is_directory) {
		print_errmsg(gfarm_url, NULL, "invalid directory");
		delete_invalid_file_or_directory(dir);
		free(gfarm_url);
		return (1);
	}
	free(gfarm_url);

	dirp = opendir(dir);
	if (dirp == NULL) {
		perror(dir);
		return (1);
	}

	if (strcmp(dir, ".") == 0)
		dir = ""; /* just a trick */

	while ((dp = readdir(dirp)) != NULL) {
		if (strcmp(dp->d_name, ".") == 0
		    || strcmp(dp->d_name, "..") == 0)
			continue;

		GFARM_MALLOC_ARRAY(dir1, strlen(dir) + strlen(dp->d_name) + 2);
		if (dir1 == NULL) {
			print_errmsg(dp->d_name, NULL, "not enough memory");
			closedir(dirp);
			return (1);
		}
		strcpy(dir1, dir);
		if (strcmp(dir, ""))
			strcat(dir1, "/");
		strcat(dir1, dp->d_name);

		fixdir(dir1, gfarm_prefix);

		free(dir1);
	}
	return (closedir(dirp));
}
Пример #27
0
int
main(int argc, char **argv)
{
	int argc_save = argc;
	char **argv_save = argv;
	gfarm_error_t e, e_save = GFARM_ERR_NO_ERROR;
	int i, n, ch, opt_recursive = 0;
	gfarm_stringlist paths;
	gfs_glob_t types;

	if (argc >= 1)
		program_name = basename(argv[0]);

	while ((ch = getopt(argc, argv, "rR?")) != -1) {
		switch (ch) {
		case 'r':
		case 'R':
			opt_recursive = 1;
			break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	e = gfarm_initialize(&argc_save, &argv_save);
	if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "%s: %s\n", program_name,
		    gfarm_error_string(e));
		exit(1);
	}
	if (argc == 0) {
		usage();
	}

	e = gfarm_stringlist_init(&paths);
	if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "%s: %s\n", program_name,
		    gfarm_error_string(e));
		exit(EXIT_FAILURE);
	}
	e = gfs_glob_init(&types);
	if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "%s: %s\n", program_name,
		    gfarm_error_string(e));
		exit(EXIT_FAILURE);
	}
	for (i = 0; i < argc; i++)
		gfs_glob(argv[i], &paths, &types);
	gfs_glob_free(&types);

	n = gfarm_stringlist_length(&paths);
	for (i = 0; i < n; i++) {
		char *p = gfarm_stringlist_elem(&paths, i);
		struct gfs_stat st;

		if ((e = gfs_stat(p, &st)) != GFARM_ERR_NO_ERROR) {
			fprintf(stderr, "%s: %s\n", p, gfarm_error_string(e));
		} else {
			if (GFARM_S_ISREG(st.st_mode)) 
				e = display_replica_catalog(p, &st, NULL);
			else if (opt_recursive)
				e = gfarm_foreach_directory_hierarchy(
					display_replica_catalog, display_name,
					NULL, p, NULL);
			else
				fprintf(stderr, "%s: not a file\n", p);
			gfs_stat_free(&st);
			if (e_save == GFARM_ERR_NO_ERROR)
				e_save = e;
		}
	}

	gfarm_stringlist_free_deeply(&paths);
	e = gfarm_terminate();
	if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "%s: %s\n", program_name,
		    gfarm_error_string(e));
		exit(1);
	}
	return (e_save == GFARM_ERR_NO_ERROR ? 0 : 1);
}
Пример #28
0
gfarm_error_t
gfperf_create_file_on_gfarm(const char *url, char *hostname,
			    long long file_size)
{
	const char *filename;
	char *buf;
	long long leftsize;
	int ret, s;
	GFS_File fp;
	gfarm_error_t e;
	struct gfs_stat sb;

	filename = url;

	GFARM_CALLOC_ARRAY(buf, GFPERF_COPY_BUF_SIZE);
	if (buf == NULL) {
		fprintf(stderr, "can not allocate memory.\n");
		return (GFARM_ERR_NO_MEMORY);
	}

	e = gfs_stat(filename, &sb);
	if (e != GFARM_ERR_NO_SUCH_FILE_OR_DIRECTORY) {
		fprintf(stderr, "file exists: %s\n", filename);
		if (e == GFARM_ERR_NO_ERROR)
			gfs_stat_free(&sb);
		free(buf);
		return (GFARM_ERR_ALREADY_EXISTS);
	}

	e = gfs_pio_create(filename,
			   GFARM_FILE_WRONLY|GFARM_FILE_TRUNC,
			   0644,
			   &fp);
	if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "open: %s\n",
			gfarm_error_string(e));
		free(buf);
		return (e);
	}
	if (hostname != NULL) {
		/* XXX FIXME: INTERNAL FUNCTION SHOULD NOT BE USED */
		e = gfs_pio_internal_set_view_section(fp, hostname);
		if (e != GFARM_ERR_NO_ERROR) {
			fprintf(stderr,
				"gfs_pio_internal_set_view_section() error! "
				"%s: %s\n", hostname,
				gfarm_error_string(e));
			goto err_return;
		}
	}

	for (leftsize = file_size; leftsize > 0 ; leftsize -= ret) {
		s = (leftsize < GFPERF_COPY_BUF_SIZE) ?
			leftsize : GFPERF_COPY_BUF_SIZE;
		e = gfs_pio_write(fp, buf, s, &ret);
		if (e != GFARM_ERR_NO_ERROR) {
			fprintf(stderr, "write: %s\n",
				gfarm_error_string(e));
			goto err_return;
		}
	}
	e = gfs_pio_close(fp);
	if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "close: %s\n",
			gfarm_error_string(e));
	}
	free(buf);
	return (e);

err_return:
	gfs_pio_close(fp);
	free(buf);
	return (e);
}