示例#1
0
文件: gfls.c 项目: krichter722/gfarm
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);
}
示例#2
0
/*
 * common routine for gfrep and gfrepbe_client.
 * other programs/libraries shouldn't use this function.
 */
char *
gfs_client_rep_filelist_send(
	char *server_name, struct xxx_connection *to_server,
	char *message_prefix,
	int n, gfarm_stringlist *files, gfarm_stringlist *sections)
{
	char *e;
	int i;

	/*
	 * XXX FIXME: This should be done in parallel during replication.
	 */
	for (i = 0; i < n; i++) {
		char *gfarm_file = gfarm_stringlist_elem(files, i);
		char *section = gfarm_stringlist_elem(sections, i);

		if (section == NULL)
			section = "";
		e = xxx_proto_send(to_server, "ss", gfarm_file, section);
		if (e != NULL) {
			fprintf(stderr, "%s: "
			    "error while sending requests to %s on %s: %s\n",
			    message_prefix, server_name,
			    gfarm_host_get_self_name(), e);
			return (e);
		}
	}
	/* end of requests mark */
	e = xxx_proto_send(to_server, "ss", "", "");
	if (e != NULL) {
		fprintf(stderr, "%s: "
		    "error while sending end of requests to %s on %s: %s\n",
		    message_prefix, server_name, gfarm_host_get_self_name(),
		    e);
		return (e);
	}
	e = xxx_proto_flush(to_server);
	if (e != NULL) {
		fprintf(stderr, "%s: "
		    "error while flushing requests to %s on %s: %s\n",
		    message_prefix, server_name, gfarm_host_get_self_name(),
		    e);
		return (e);
	}
	return (NULL);
}
示例#3
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);
}
示例#4
0
文件: gfrep.c 项目: ddk50/gfarm_v2
int
main(int argc, char *argv[])
{
	char *src_hostfile = NULL, *dst_hostfile = NULL;
	gfarm_stringlist paths;
	gfs_glob_t types;
	int mode_src_ch = 0, mode_dst_ch = 0, parallel = -1;
	int i, ch;
	gfarm_error_t e;
	const char *errmsg, *errmsg2 = NULL;
	struct gfrep_arg gfrep_arg;
	struct flist flist;

	if (argc >= 1)
		program_name = basename(argv[0]);
	memset(&gfrep_arg, 0, sizeof(gfrep_arg));
	memset(&flist, 0, sizeof(flist));
	flist.src_domain = "";
	flist.dst_domain = "";

	e = gfarm_initialize(&argc, &argv);
	error_check(e);

#ifdef _OPENMP
	while ((ch = getopt(argc, argv, "h:j:mnqvxS:D:H:N:?")) != -1) {
#else
	while ((ch = getopt(argc, argv, "h:mnqvxS:D:H:N:?")) != -1) {
#endif
		switch (ch) {
		case 'h':
			src_hostfile = optarg;
			conflict_check(&mode_src_ch, ch);
			break;
#ifdef _OPENMP
		case 'j':
			parallel = strtol(optarg, NULL, 0);
			break;
#endif
		case 'm':
			act = &migrate_mode;
			break;
		case 'n':
			opt_noexec = 1;
			break;
		case 'q':
			opt_quiet = 1;
			break;
		case 'v':
			opt_verbose = 1;
			break;
		case 'x':
			opt_remove = 1;
			break;
		case 'S':
			flist.src_domain = optarg;
			conflict_check(&mode_src_ch, ch);
			break;
		case 'D':
			flist.dst_domain = optarg;
			conflict_check(&mode_dst_ch, ch);
			break;
		case 'H':
			dst_hostfile = optarg;
			conflict_check(&mode_dst_ch, ch);
			break;
		case 'N':
			opt_nrep = strtol(optarg, NULL, 0);
			break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	/* make writing-to-stderr atomic, for GfarmFS-FUSE log output */
	setvbuf(stderr, NULL, _IOLBF, 0);

	if (!opt_quiet) {
		printf("constructing file list...");
		fflush(stdout);
	}

	e = gfarm_stringlist_init(&paths);
	if (e == GFARM_ERR_NO_ERROR) {
		e = gfs_glob_init(&types);
		if (e == GFARM_ERR_NO_ERROR) {
			for (i = 0; i < argc; i++)
				gfs_glob(argv[i], &paths, &types);
			gfs_glob_free(&types);
		}
	}
	error_check(e);

	e = gfarm_list_init(&flist.slist);
	error_check(e);
	e = gfarm_list_init(&flist.dlist);
	error_check(e);
	flist.srchash = gfarm_hash_table_alloc(HOSTHASH_SIZE,
		gfarm_hash_casefold, gfarm_hash_key_equal_casefold);
	if (flist.srchash == NULL)
		error_check(GFARM_ERR_NO_MEMORY);

	e = create_hosthash_from_file(src_hostfile,
		HOSTHASH_SIZE, &flist.src_hosthash);
	error_check(e);
	e = create_hosthash_from_file(dst_hostfile,
		HOSTHASH_SIZE, &flist.dst_hosthash);
	error_check(e);

	for (i = 0; i < gfarm_stringlist_length(&paths); i++) {
		char *file = gfarm_stringlist_elem(&paths, i), *realpath = NULL;

		e = gfarm_realpath_by_gfarm2fs(file, &realpath);
		if (e == GFARM_ERR_NO_ERROR)
			file = realpath;
		e = gfarm_foreach_directory_hierarchy(
			create_filelist, NULL, NULL, file, &flist);
		free(realpath);
		if (e != GFARM_ERR_NO_ERROR)
			break;
	}
	gfarm_stringlist_free_deeply(&paths);
	error_check(e);

	if (!opt_quiet)
		printf(" done\n");
	if (opt_verbose) {
		printf("files to be replicated\n");
		print_file_list(&flist.slist);
	}
	if (opt_verbose && opt_remove) {
		printf("files having too many replicas\n");
		print_file_list(&flist.dlist);
	}
	if (gfarm_list_length(&flist.slist) <= 0
	    && (!opt_remove || gfarm_list_length(&flist.dlist) <= 0))
		exit(0); /* no file */

	/* replicate files */
	e = gfarm_hash_to_string_array(
		flist.srchash, &gfrep_arg.nsrc, &gfrep_arg.src);
	error_check(e);
	gfarm_hash_table_free(flist.srchash);

	if (!opt_quiet) {
		printf("investigating hosts...");
		fflush(stdout);
	}
	e = create_hostlist_by_domain_and_hash(
		gfarm_list_length(&flist.slist) > 0 ?
		gfarm_list_elem(&flist.slist, 0) :
		gfarm_list_elem(&flist.dlist, 0),
		flist.dst_domain, flist.dst_hosthash,
		&gfrep_arg.ndst, &gfrep_arg.dst, &gfrep_arg.dst_port);
	error_check(e);
	if (!opt_quiet)
		printf(" done\n");

	errmsg = pfor_list(act, &flist.slist, parallel, &gfrep_arg);
	gfarm_list_free(&flist.slist);

	/* remove file replicas */
	if (opt_remove)
		errmsg2 = pfor_list(
			&remove_mode, &flist.dlist, parallel, &gfrep_arg);
	gfarm_list_free(&flist.dlist);
	if (errmsg == NULL)
		errmsg = errmsg2;
	if (errmsg != NULL)
		fprintf(stderr, "%s\n", errmsg), exit(EXIT_FAILURE);

	gfarm_strings_free_deeply(gfrep_arg.nsrc, gfrep_arg.src);
	gfarm_strings_free_deeply(gfrep_arg.ndst, gfrep_arg.dst);
	free(gfrep_arg.dst_port);
	e = gfarm_terminate();
	error_check(e);

	return (0);
}
示例#5
0
int
main(int argc, char **argv)
{
	gfarm_int32_t interleave_factor = 0;
	gfarm_int32_t ndivisions = 1;
	int recv_stripe_sync = 0;
	int send_stripe_sync = 0;
	int file_sync_stripe = 0;
	struct xxx_connection *from_frontend, *to_frontend;
	struct xxx_connection *from_server, *to_server;
	char *e, *src_host, *my_canonical_name;
	char **gfrcmd_paths, **gfrep_backend_server_paths;
	int c, i, n, replication_method_save;
	gfarm_stringlist files, sections;
	struct gfarm_path_info *path_infos;
	gfarm_int32_t *results;
	struct sockaddr server_addr;

	e = gfarm_initialize(&argc, &argv);
	if (e != NULL) {
		fprintf(stderr, "%s: gfarm_initialize on %s: %s\n",
		    program_name, my_name, e);
		fatal();
	}
	while ((c = getopt(argc, argv, "i:n:rsS:")) != -1) {
		switch (c) {
		case 'i':
			interleave_factor = atoi(optarg);
			break;
		case 'n':
			ndivisions = atoi(optarg);
			break;
		case 'r':
			recv_stripe_sync = 1;
			break;
		case 's':
			send_stripe_sync = 1;
			break;
		case 'S':
			file_sync_stripe = atoi(optarg);
			break;
		default:
			fprintf(stderr, "%s: unknown option -%c\n",
			    program_name, c);
			usage();
			/*NOTREACHED*/
		}
	}
	argc -= optind;
	argv += optind;
	my_name = gfarm_host_get_self_name();
	if (argc != 1) {
		fprintf(stderr, "%s: missing server name argument on %s\n",
		    program_name, my_name);
		usage();
		/*NOTREACHED*/
	}
	src_host = argv[0];

	e = gfarm_host_get_canonical_self_name(&my_canonical_name);
	if (e != NULL) {
		fprintf(stderr, "%s: host %s isn't a filesystem node: %s\n",
			program_name, my_name, e);
		fatal();
	}

	replication_method_save = gfarm_replication_get_method();
	gfarm_replication_set_method(GFARM_REPLICATION_BOOTSTRAP_METHOD);

	e = gfarm_url_program_deliver(gfrcmd_url,
	    1, &my_canonical_name, &gfrcmd_paths);
	if (e != NULL) {
		fprintf(stderr, "%s: cannot deliver %s to host %s on %s: %s\n",
		    program_name, gfrcmd_url, my_canonical_name, my_name, e);
		fatal();
	}

	e = gfarm_url_program_deliver(gfrep_backend_server,
	    1, &src_host, &gfrep_backend_server_paths);
	if (e != NULL) {
		fprintf(stderr, "%s: cannot deliver %s to host %s on %s: %s\n",
		    program_name, gfrep_backend_server, src_host, my_name, e);
		fatal();
	}

	gfarm_replication_set_method(replication_method_save);

	e = gfs_client_rep_backend_invoke(
	    src_host, gfrcmd_paths[0], gfrep_backend_server_paths[0], NULL,
	    GFS_CLIENT_REP_ALGORITHM_LATEST,
	    ndivisions, interleave_factor, 0, send_stripe_sync, 0,
	    program_name,
	    &from_server, &to_server, &backend);
	if (e != NULL)
		fatal();

	e = gfarm_host_address_get(src_host, 0, &server_addr, NULL);
	if (e != NULL) {
		fprintf(stderr, "%s: %s: %s\n", program_name, src_host, e);
		fatal();
	}

	e = gfarm_netparam_config_get_long(&gfarm_netparam_file_sync_rate,
	    src_host, (struct sockaddr *)&server_addr, &file_sync_rate);
	if (e != NULL) {/* shouldn't happen */
		fprintf(stderr,
		    "%s: get netparam file_sync_rate on %s (%s): %s\n",
		    program_name, my_name, src_host, e);
		fatal();
	}

	/* XXX read-only connection */
	e = xxx_fd_connection_new(STDIN_FILENO, &from_frontend);
	if (e != NULL) {
		fprintf(stderr, "%s: %s for stdin\n", program_name, e);
		fatal();
	}
	/* XXX write-only connection */
	e = xxx_fd_connection_new(STDOUT_FILENO, &to_frontend);
	if (e != NULL) {
		fprintf(stderr, "%s: %s for stdout\n", program_name, e);
		fatal();
	}

	e = gfs_client_rep_filelist_receive(from_frontend,
	    &n, &files, &sections, program_name);
	if (e != NULL)
		fatal();

	e = gfs_client_rep_filelist_send(src_host, to_server, program_name,
	    n, &files, &sections);
	if (e != NULL)
		fatal();

	results = malloc(sizeof(*results) * n);
	if (results == NULL) {
		fprintf(stderr, "%s: no memory for %d ints on %s\n",
		    program_name, n, my_name);
		fatal();
	}

	/* make current directory == spool_root */
	if (chdir(gfarm_spool_root) == -1) {
		fprintf(stderr, "%s: chdir(%s) on %s: %s\n",
		    program_name, gfarm_spool_root, my_name,
		    e);
		fatal();
	}

	path_infos = malloc(sizeof(*path_infos) * n);
	if (results == NULL) {
		fprintf(stderr, "%s: no memory for %d path_info on %s\n",
		    program_name, n, my_name);
		fatal();
	}
	for (i = 0; i < n; i++) {
		e = gfarm_path_info_get(gfarm_stringlist_elem(&files, i),
		    &path_infos[i]);
		results[i] = e == NULL ? GFS_ERROR_NOERROR :
			gfs_string_to_proto_error(e);
	}

	umask(0); /* don't mask, just use the 3rd parameter of open(2) */

	session(src_host, &server_addr, from_server, to_server,
	    my_canonical_name,
	    ndivisions, interleave_factor, file_sync_stripe, recv_stripe_sync,
	    n, &files, &sections, path_infos, results);

#ifdef HACK_FOR_BWC
	/*
	 * If this program fails with fatal(), or is killed by a signal,
	 * this metadata update isn't executed. (So, #undef HACK_FOR_BWC)
	 */
	for (i = 0; i < n; i++) {
		char *file = gfarm_stringlist_elem(&files, i);
		char *section = gfarm_stringlist_elem(&sections, i);
		struct gfarm_file_section_copy_info fci;

		if (results[i] == GFS_ERROR_NOERROR) {
			e = gfarm_file_section_copy_info_set(file, section,
			    my_canonical_name, &fci);
			if (e != NULL)
				results[i] = gfs_string_to_proto_error(e);
		}
	}
#endif

	for (i = 0; i < n; i++)
		xxx_proto_send(to_frontend, "i", results[i]);
	xxx_proto_flush(to_frontend);

	e = gfarm_terminate();
	if (e != NULL) {
		fprintf(stderr, "%s: gfarm_terminate on %s: %s\n",
		    program_name, my_name, e);
		fatal();
	}
	return (0);
}
示例#6
0
void
session(char *server_name, struct sockaddr *server_addr,
	struct xxx_connection *from_server, struct xxx_connection *to_server,
	char *my_canonical_name,
	int ndivisions, int interleave_factor,
	int file_sync_stripe, int recv_stripe_sync,
	int n, gfarm_stringlist *files, gfarm_stringlist *sections,
	struct gfarm_path_info *path_infos, gfarm_int32_t *results)
{
	char *e, *pathname;
	int i, eof, ofd, mode;
	gfarm_int32_t r, port;
	int *socks, disksync_cycle = 0;
	struct xxx_connection **conns;
	struct sockaddr_in *sin = (struct sockaddr_in *)server_addr;
	file_offset_t size;

	socks = malloc(sizeof(*socks) * ndivisions);
	conns = malloc(sizeof(*conns) * ndivisions);
	if (socks == NULL || conns == NULL) {
		fprintf(stderr,
		    "%s: no memory for %d connections to %s on %s\n",
		     program_name, ndivisions, server_name, my_name);
		fatal();
	}

	/*
	 * XXX FIXME: this port may be blocked by firewall.
	 * This should be implemented via gfsd connection passing service.
	 */

	e = xxx_proto_recv(from_server, 0, &eof, "i", &port);
	if (e != NULL || eof) {
		fprintf(stderr,
		    "%s: while reading port number from %s on %s: %s\n",
		     program_name, server_name, my_name,
		    e != NULL ? e : "unexpected EOF");
		fatal();
	}
	sin->sin_port = htons(port);

	/* sanity */
	for (i = 0; i < ndivisions; i++)
		socks[i] = -1;
	/* XXX FIXME: make connections in parallel */
	for (i = 0; i < ndivisions; i++) {
		struct xxx_connection *tmp_conn;
		int tmp_sock;
		gfarm_int32_t index;

		tmp_sock = socket(PF_INET, SOCK_STREAM, 0);
		if (tmp_sock == -1) {
			fprintf(stderr,
			    "%s: while opening sockets on %s: %s\n",
			     program_name, my_name, strerror(errno));
			fatal();
		}
		/* XXX FIXME canonical_name? */
		e = gfarm_sockopt_apply_by_name_addr(tmp_sock,
		    server_name, server_addr);
		if (e != NULL) {
			fprintf(stderr, "%s: setsockopt to %s on %s: %s\n",
			    program_name, server_name, my_name, e);
		}
		if (connect(tmp_sock, server_addr, sizeof(*server_addr))== -1){
			fprintf(stderr,
			    "%s: while connecting to %s on %s: %s\n",
			    program_name, server_name, my_name, e);
			fatal();
		}
		/* XXX read-only connection */
		e = xxx_fd_connection_new(tmp_sock, &tmp_conn);
		if (e != NULL) {
			fprintf(stderr,
			    "%s: while allocating memory to %s on %s: %s\n",
			    program_name, server_name, my_name, e);
			fatal();
		}
		e = gfarm_auth_request(tmp_conn, server_name, server_addr,
		    NULL);
		if (e != NULL) {
			fprintf(stderr,
			    "%s: authorization request to %s on %s: %s\n",
			    program_name, server_name, my_name, e);
			fatal();
		}
		/* XXX call shutdown(2) here? */
		e = xxx_proto_recv(tmp_conn, 1, &eof, "i", &index);
		if (e != NULL || eof) {
			fprintf(stderr,
			    "%s: while reading port index from %s on %s: %s\n",
			    program_name, server_name, my_name,
			    e != NULL ? e : "unexpected EOF");
			fatal();
		}
		if (index < 0 || index >= n) {
			fprintf(stderr,
			    "%s: invalid port index %d from %s on %s\n",
			    program_name, index, server_name, my_name);
			fatal();
		}
		/* sanity */
		if (socks[index] != -1) {
			fprintf(stderr,
			    "%s: duplicate port index %d from %s on %s\n",
			    program_name, index, server_name, my_name);
			fatal();
		}
		socks[index] = tmp_sock;
		conns[index] = tmp_conn;
	}

	for (i = 0; i < n; i++) {
		char *file = gfarm_stringlist_elem(files, i);
		char *section = gfarm_stringlist_elem(sections, i);

		/* NOTE: assumes current directory == spool_root */
		if (*section == '\0') {
			pathname = file;
		} else {
			pathname = malloc(strlen(file) + strlen(section) + 2);
			if (pathname == NULL) {
				fprintf(stderr,
				    "%s: no memory for pathname %s:%s"
				    " to replicate it from %s to %s\n",
				     program_name, file, section, server_name,
				     my_name);
				fatal();
			}
			sprintf(pathname, "%s:%s", file, section);
		}
		/* NOTE: just==TRUE here */
		e = xxx_proto_recv(conns[0], 1, &eof, "i", &r);
		if (e != NULL || eof) {
			fprintf(stderr,
			    "%s: while reading status "
			    "to replicate %s from %s to %s: %s\n",
			     program_name, pathname,
			    server_name, my_name,
			    e != NULL ? e : "unexpected EOF");
			fatal();
		}
		if (r != GFS_ERROR_NOERROR) {
			if (results[i] == GFS_ERROR_NOERROR)
				results[i] = r;
			fprintf(stderr, "%s: replicate %s from %s to %s: %s\n",
			    program_name, pathname, server_name, my_name,
			    gfs_proto_error_string(r));
			if (*section != '\0')
				free(pathname);
			continue;
		}
		/* NOTE: just==TRUE here */
		e = xxx_proto_recv(conns[0], 1, &eof, "o", &size);
		if (e != NULL || eof) {
			fprintf(stderr,
			    "%s: while reading file size "
			    "to replicate %s from %s to %s: %s\n",
			    program_name, pathname, server_name, my_name,
			    e != NULL ? e : "unexpected EOF");
			fatal();
		}
		/* XXX FIXME - FT! */
		/* XXX FIXME - owner! */
		/* XXX FIXME - mode! */
		if (results[i] == GFS_ERROR_NOERROR) {
			mode =
			    (path_infos[i].status.st_mode & GFARM_S_ALLPERM);

			/*
			 * XXX FIXME
			 * if the owner of a file is not the same,
			 * permit a group/other write access - This
			 * should be fixed in the next major release.
			 */
			if (strcmp(path_infos[i].status.st_user,
			    gfarm_get_global_username()) != 0) {
				/* don't allow setuid/setgid */
				mode = (mode | 022) & 0777;
			}

			ofd = open(pathname, O_CREAT|O_WRONLY, mode);
			/* FT */
			if (ofd == -1 && try_to_make_parent_dir(pathname))
				ofd = open(pathname, O_CREAT|O_WRONLY, mode);
			if (ofd == -1) {
				results[i] = gfs_errno_to_proto_error(errno);
				fprintf(stderr,
				    "%s: cannot create file"
				    "to replicate %s from %s to %s: %s\n",
				    program_name, pathname, server_name,
				    my_name, strerror(errno));
			}
		}
		if (results[i] != GFS_ERROR_NOERROR) {
			/* XXX FIXME: just waste. detect this error before */
			ofd = open("/dev/null", O_WRONLY);
			if (ofd == -1) {
				fprintf(stderr,
				    "%s: cannot open /dev/null"
				    "to replicate %s from %s to %s: %s\n",
				    program_name, pathname,
				    server_name, my_name, strerror(errno));
				fatal();
			}
		}

		e = transfer(ofd, size,
		    ndivisions, interleave_factor,
		    file_sync_stripe, recv_stripe_sync,
		    conns, socks, &disksync_cycle, &r);
		if (e != NULL) {
			fprintf(stderr,
			    "%s: while replicating %s from %s to %s: %s\n",
			    program_name, pathname, server_name, my_name, e);
			fatal();
		}
		if (r != GFS_ERROR_NOERROR) {
			if (results[i] == GFS_ERROR_NOERROR)
				results[i] = r;
			fprintf(stderr,
			    "%s: while writing contents "
			    "to replicate %s from %s to %s: %s\n",
			    program_name, pathname, server_name, my_name,
			    gfs_proto_error_string(r));
		}

		/* NOTE: just==TRUE here */
		e = xxx_proto_recv(conns[0], 1, &eof, "i", &r);
		if (e != NULL || eof) {
			fprintf(stderr,
			    "%s: while reading status "
			    "to replicate %s from %s to %s: %s\n",
			     program_name, pathname, server_name, my_name,
			    e != NULL ? e : "unexpected EOF");
			fatal();
		}
		if (r != GFS_ERROR_NOERROR) {
			if (results[i] == GFS_ERROR_NOERROR)
				results[i] = r;
			fprintf(stderr,
			    "%s: replicated %s from %s to %s: %s\n",
			    program_name, pathname, server_name, my_name,
			    gfs_proto_error_string(r));
		}

#ifndef HACK_FOR_BWC
		/* register metadata */
		if (results[i] == GFS_ERROR_NOERROR) {
			struct gfarm_file_section_copy_info fci;

			e = gfarm_file_section_copy_info_set(file, section,
			    my_canonical_name, &fci);
			if (e != NULL)
				results[i] = gfs_string_to_proto_error(e);
		}
#endif

		if (results[i] != GFS_ERROR_NOERROR)
			unlink(pathname);
		close(ofd);
		if (*section != '\0')
			free(pathname);
	}

	for (i = 0; i < ndivisions; i++)
		xxx_connection_free(conns[i]);
	free(conns);
	free(socks);
}
示例#7
0
文件: gfrm.c 项目: krichter722/gfarm
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);
}
示例#8
0
int
main(int argc, char **argv)
{
	extern char *optarg;
	extern int optind;
	int argc_save = argc;
	char **argv_save = argv;
	char *e, *section = NULL;
	int i, ch, error = 0;
	gfarm_stringlist paths;
	gfs_glob_t types;
	int argc_expanded;

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

	while ((ch = getopt(argc, argv, "I:")) != -1) {
		switch (ch) {
		case 'I':
			section = optarg;
			break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

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

	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);
	}
	for (i = 0; i < argc; i++)
		gfs_glob(argv[i], &paths, &types);

	argc_expanded = gfarm_stringlist_length(&paths);
	for (i = 0; i < argc_expanded; i++) {
		char *p = gfarm_stringlist_elem(&paths, i);

		if (argc_expanded > 1)
			printf("%s:\n", p);
		if (display_replica_catalog_section(p, section) != NULL)
			error = 1;
		if (argc_expanded > 1 && i < argc_expanded - 1)
			printf("\n");
	}
	gfs_glob_free(&types);
	gfarm_stringlist_free_deeply(&paths);
	e = gfarm_terminate();
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", program_name, e);
		exit(1);
	}
	return (error);
}
示例#9
0
文件: gfsck.c 项目: krichter722/gfarm
int
main(int argc, char *argv[])
{
	extern int optind;
	int c, i, error = 0;
	gfarm_stringlist paths;
	gfs_glob_t types;
	char *e;

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

	while ((c = getopt(argc, argv, "hv?")) != EOF) {
		switch (c) {
		case 'v':
			option_verbose = 1;
			break;
		case 'h':
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

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

	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);
	}
	for (i = 0; i < argc; i++)
		gfs_glob(argv[i], &paths, &types);

	for (i = 0; i < gfarm_stringlist_length(&paths); i++) {
		char *url = gfarm_stringlist_elem(&paths, i);

		e = gfsck_dir("", url);
		if (e != NULL) {
			fprintf(stderr, "%s: %s\n", url, e);
			error = 1;
		}
	}
	gfs_glob_free(&types);
	gfarm_stringlist_free_deeply(&paths);

	e = gfarm_terminate();
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", program_name, e);
		exit(1);
	}
	exit(error);
}
示例#10
0
static char *
replicate_so_and_symlink(char *dir, size_t size)
{
	char *canonic_path, *gfarm_url, *so_pat, *lpath;
	char *e, *e_save;
	int i, rv;
	gfarm_stringlist paths;
	gfs_glob_t types;
	static char so_pat_template[] = "/*.so*";

	e = gfs_mntpath_canonicalize(dir, size, &canonic_path);
	if (e != NULL)
		return (e);
	e = gfarm_path_canonical_to_url(canonic_path, &gfarm_url);
	free(canonic_path);
	if (e != NULL)
		return (e);

	so_pat = malloc(strlen(gfarm_url) + sizeof(so_pat_template));
	if (so_pat == NULL) {
		free(gfarm_url);
		return (GFARM_ERR_NO_MEMORY);
	}
	sprintf(so_pat, "%s%s", gfarm_url, so_pat_template);
	free(gfarm_url);

	e_save = gfarm_stringlist_init(&paths);
	if (e_save != NULL)
		goto free_so_pat;
	e_save = gfs_glob_init(&types);
	if (e_save != NULL)
		goto free_paths;
	e_save = gfs_glob(so_pat, &paths, &types);
	if (e_save != NULL)
		goto free_types;

	/* XXX - should replicate so files in parallel */
	for (i = 0; i < gfarm_stringlist_length(&paths); i++) {
		char *gfarm_so, *local_so;

		gfarm_so = gfarm_stringlist_elem(&paths, i);
		if (strcmp(gfarm_so, so_pat) == 0) /* no "*.so*" file in dir */
			break;
		e = gfarm_url_execfile_replicate_to_local(gfarm_so, &lpath);
		if (e != NULL) {
			fprintf(stderr, "%s: %s\n", gfarm_so, e);
			if (e_save == NULL)
				e_save = e;
			if (e == GFARM_ERR_NO_MEMORY)
				break;
			continue;
		}

		/* create a symlink */
		e = gfarm_url_localize(gfarm_so, &local_so);
		if (e != NULL) {
			fprintf(stderr, "%s: %s\n", gfarm_so, e);
			if (e_save == NULL)
				e_save = e;
			if (e == GFARM_ERR_NO_MEMORY)
				break;
			free(lpath);
			continue;
		}
		unlink(local_so);
		rv = symlink(lpath, local_so);
		if (rv == -1) {
			perror(local_so);
			if (e_save == NULL)
				e_save = gfarm_errno_to_error(errno);
		}
		free(local_so);
		free(lpath);
	}
free_types:
	gfs_glob_free(&types);
free_paths:
	gfarm_stringlist_free_deeply(&paths);
free_so_pat:
	free(so_pat);
	return (e_save);
}
示例#11
0
void
session(struct xxx_connection *from_client, struct xxx_connection *to_client,
	int algorithm_version, int ndivisions, int interleave_factor,
	int send_stripe_sync,
	int n, gfarm_stringlist *files, gfarm_stringlist *sections)
{
	char *e, *pathname;
	gfarm_int32_t r;
	int i, listening_sock, *socks, ifd;
	long file_read_size;
	struct xxx_connection **conns;
	struct gfs_client_rep_rate_info **rinfos = NULL;
	file_offset_t file_size;
	struct stat st;

	struct sockaddr_in listen_addr, client_addr;
	socklen_t listen_addr_len, client_addr_len;

	if (rate_limit != 0) {
		rinfos = malloc(sizeof(*rinfos) * ndivisions);
		if (rinfos == NULL) {
			fprintf(stderr,
			    "%s: no memory for %d rate limits "
			    "on gfrebe_server %s\n",
			    program_name, ndivisions, my_name);
			fatal();
		}
		for (i = 0; i < ndivisions; i++) {
			rinfos[i] =
			    gfs_client_rep_rate_info_alloc(rate_limit);
			if (rinfos[i] == NULL) {
				fprintf(stderr,
				    "%s: no memory for %d/%d rate limit "
				    "on gfrebe_server %s\n",
				    program_name, i, ndivisions, my_name);
				fatal();
			}
		}
	}

	socks = malloc(sizeof(*socks) * ndivisions);
	conns = malloc(sizeof(*conns) * ndivisions);
	if (socks == NULL || conns == NULL) {
		if (socks != NULL)
			free(socks);
		if (conns != NULL)
			free(conns);
		fprintf(stderr,
		    "%s: no memory for %d connections on gfrebe_server %s\n",
		     program_name, ndivisions, my_name);
		fatal();
	}

	/*
	 * XXX FIXME: this port may be blocked by firewall.
	 * This should be implemented via gfsd connection passing service.
	 */

	listening_sock = open_accepting_socket();
	listen_addr_len = sizeof(listen_addr);
	if (getsockname(listening_sock,
	    (struct sockaddr *)&listen_addr, &listen_addr_len) == -1) {
		fprintf(stderr, "%s: getsockname: %s\n",
		    program_name, strerror(errno));
		fatal();
	}
	e = xxx_proto_send(to_client, "i", htons(listen_addr.sin_port));
	if (e == NULL)
		e = xxx_proto_flush(to_client);
	if (e != NULL) {
		fprintf(stderr,
		    "%s: while sending port number on %s: %s\n",
		     program_name, my_name, e);
		fatal();
	}

	/* XXX FIXME: make connections in parallel */
	for (i = 0; i < ndivisions; i++) {
		client_addr_len = sizeof(client_addr);
		socks[i] = accept(listening_sock,
		   (struct sockaddr *)&client_addr, &client_addr_len);
		if (socks[i] == -1) {
			if (errno == EINTR) {
				--i;
				continue;
			}
			fprintf(stderr,
			    "%s: while accepting sockets on %s: %s\n",
			     program_name, my_name, strerror(errno));
			fatal();
		}
		e = xxx_fd_connection_new(socks[i], &conns[i]);
		if (e != NULL) {
			fprintf(stderr,
			    "%s: while allocating connection %d on %s: %s\n",
			     program_name, i, my_name, e);
			fatal();
		}
		e = gfarm_authorize(conns[i], 1, NULL, NULL, NULL);
		if (e != NULL) {
			fprintf(stderr,
			    "%s: authorization on %s: %s\n",
			     program_name, my_name, e);
			fatal();
		}
		e = xxx_proto_send(conns[i], "i", i);
		if (e == NULL)
			e = xxx_proto_flush(conns[i]);
		if (e != NULL) {
			fprintf(stderr,
			    "%s: sending connection index on %s: %s\n",
			     program_name, my_name, e);
			fatal();
		}
	}
	close(listening_sock);

	e = gfarm_netparam_config_get_long(&gfarm_netparam_file_read_size,
	    NULL, (struct sockaddr *)&client_addr, &file_read_size);
	if (e != NULL) { /* shouldn't happen */
		fprintf(stderr, "%s: get netparam file_read_size on %s: %s\n",
		    program_name, my_name, e);
		fatal();
	}

	e = gfarm_netparam_config_get_long(&gfarm_netparam_rate_limit,
	    NULL, (struct sockaddr *)&client_addr, &rate_limit);
	if (e != NULL) { /* shouldn't happen */
		fprintf(stderr, "%s: get netparam rate_limit on %s: %s\n",
		    program_name, my_name, e);
		fatal();
	}

	for (i = 0; i < n; i++) {
		char *file = gfarm_stringlist_elem(files, i);
		char *section = gfarm_stringlist_elem(sections, i);

		/* NOTE: assumes current directory == spool_root */
		if (*section == '\0') {
			pathname = file;
		} else {
			pathname = malloc(strlen(file) + strlen(section) + 2);
			if (pathname == NULL) {
				fprintf(stderr,
				    "%s: no memory for pathname %s:%s"
				    " to replicate on %s\n",
				     program_name, file, section, my_name);
				fatal();
			}
			sprintf(pathname, "%s:%s", file, section);
		}
		ifd = open(pathname, O_RDONLY);
		if (ifd == -1) {
			r = gfs_errno_to_proto_error(errno);
		} else if (fstat(ifd, &st) == -1) {
			r = gfs_errno_to_proto_error(errno);
		} else {
			r = GFS_ERROR_NOERROR;
			file_size = st.st_size;
		}
		e = xxx_proto_send(conns[0], "i", r);
		if (e != NULL) {
			fprintf(stderr, "%s: send reply on %s\n",
			    program_name, my_name);
			fatal();
		}
		if (r != GFS_ERROR_NOERROR)
			continue;
		e = xxx_proto_send(conns[0], "o", file_size);
		if (e == NULL)
			e = xxx_proto_flush(conns[0]);
		if (e != NULL) {
			fprintf(stderr, "%s: send reply on %s\n",
			    program_name, my_name);
			fatal();
		}
		e = transfer(pathname, ifd, file_size, algorithm_version,
		    ndivisions, interleave_factor, file_read_size,
		    send_stripe_sync, conns, socks, rinfos,
		    &r);
		close(ifd);
		e = xxx_proto_send(conns[0], "i", r);
		if (e != NULL) {
			fprintf(stderr, "%s: send reply on %s\n",
			    program_name, my_name);
			fatal();
		}
		if (*section != '\0')
			free(pathname);
	}
	e = xxx_proto_flush(conns[0]);
	if (e != NULL) {
		fprintf(stderr, "%s: send reply on %s\n",
		    program_name, my_name);
		fatal();
	}

	for (i = 0; i < ndivisions; i++)
		xxx_connection_free(conns[i]);
	free(conns);
	free(socks);

	if (rinfos != NULL) {
		for (i = 0; i < ndivisions; i++)
			gfs_client_rep_rate_info_free(rinfos[i]);
		free(rinfos);
	}
}
示例#12
0
文件: gfls.c 项目: krichter722/gfarm
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);
}