int main(int argc, char **argv) { gfarm_error_t e; char *opt_domain = ""; char *opt_mount_point = NULL; char *opt_file = NULL; int opt_metadata_only = 0; int opt_long_format = 0; int opt_nhosts = 0; int opt_write_mode = 0; int opt_create_mode = 0; int c, i, available_nhosts, nhosts, *ports; struct gfarm_host_sched_info *available_hosts; char *path, **hosts, *realpath = NULL; if (argc >= 1) program_name = basename(argv[0]); e = gfarm_initialize(&argc, &argv); if (e != GFARM_ERR_NO_ERROR) { fprintf(stderr, "%s: %s\n", program_name, gfarm_error_string(e)); exit(1); } while ((c = getopt(argc, argv, "D:LMP:cf:ln:w")) != -1) { switch (c) { case 'D': opt_domain = optarg; break; case 'L': gfarm_schedule_search_mode_use_loadavg(); break; case 'M': opt_metadata_only = 1; break; case 'P': opt_mount_point = optarg; break; case 'c': opt_create_mode = 1; break; case 'f': opt_file = optarg; break; case 'l': opt_long_format = 1; break; case 'n': opt_nhosts = parse_opt_long(optarg, c, "<nhosts>"); if (opt_nhosts <= 0) { fprintf(stderr, "%s: invalid value: -%c %d\n", program_name, c, opt_nhosts); usage(); } break; case 'w': opt_write_mode = 1; break; default: usage(); } } argc -= optind; argv += optind; if (argc != 0) usage(); if (opt_mount_point != NULL && opt_file != NULL) { fprintf(stderr, "%s: -P and -f option cannot be specified at once.\n", program_name); usage(); } if (opt_file != NULL) { e = gfarm_realpath_by_gfarm2fs(opt_file, &realpath); if (e == GFARM_ERR_NO_ERROR) path = realpath; else path = opt_file; if (opt_create_mode) { GFS_File gf; e = gfs_pio_create(path, GFARM_FILE_WRONLY, 0666, &gf); if (e != GFARM_ERR_NO_ERROR) { fprintf(stderr, "%s: creating \"%s\": %s\n", program_name, path, gfarm_error_string(e)); exit(1); } e = gfs_pio_close(gf); if (e != GFARM_ERR_NO_ERROR) { fprintf(stderr, "%s: gfs_pio_close(\"%s\"): %s\n", program_name, path, gfarm_error_string(e)); /* exit(1); */ } /* NOTE: this may leave an empty file with ncopy==0 */ } e = gfarm_schedule_hosts_domain_by_file(path, opt_write_mode ? GFARM_FILE_RDWR : GFARM_FILE_RDONLY, opt_domain, &available_nhosts, &available_hosts); } else { path = opt_mount_point == NULL ? "." : opt_mount_point; e = gfarm_realpath_by_gfarm2fs(path, &realpath); if (e == GFARM_ERR_NO_ERROR) path = realpath; e = gfarm_schedule_hosts_domain_all(path, opt_domain, &available_nhosts, &available_hosts); } if (e != GFARM_ERR_NO_ERROR) { fprintf(stderr, "%s: metadata scheduling: %s\n", program_name, gfarm_error_string(e)); exit(1); } nhosts = opt_nhosts > 0 ? opt_nhosts : available_nhosts; GFARM_MALLOC_ARRAY(hosts, nhosts); GFARM_MALLOC_ARRAY(ports, nhosts); if (hosts == NULL || ports == NULL) { fprintf(stderr, "%s: cannot allocate memory for %d hosts.\n", program_name, nhosts); exit(1); } if (opt_metadata_only) { if (nhosts > available_nhosts) nhosts = available_nhosts; for (i = 0; i < nhosts; i++) { hosts[i] = available_hosts[i].host; ports[i] = available_hosts[i].port; } } else if (opt_write_mode) { e = gfarm_schedule_hosts_acyclic_to_write(path, available_nhosts, available_hosts, &nhosts, hosts, ports); } else { e = gfarm_schedule_hosts_acyclic(path, available_nhosts, available_hosts, &nhosts, hosts, ports); } free(realpath); if (e != GFARM_ERR_NO_ERROR) { fprintf(stderr, "%s: client side scheduling: %s\n", program_name, gfarm_error_string(e)); exit(1); } for (i = 0; i < nhosts; i++) { printf("%s", hosts[i]); if (opt_long_format) printf("\t%d", ports[i]); putchar('\n'); } free(hosts); free(ports); e = gfarm_terminate(); if (e != GFARM_ERR_NO_ERROR) { fprintf(stderr, "%s: %s\n", program_name, gfarm_error_string(e)); exit(1); } exit(0); }
int webgfarm_api_v1_write_to_file(request_rec *r) { // get filepath from request char *filepath = webgfarm_api_v1_getfilepath(r); if (filepath == NULL || strlen(filepath) == 0) { return HTTP_FORBIDDEN; } gfarm_error_t gerr; GFS_File gfs_file; webgfarm_config *wconfig = ap_get_module_config(r->server->module_config, &webgfarm_module); if (wconfig->write_redirect) { char *redirect_url = apr_palloc(r->pool, sizeof (char)*WEBGFARM_BUFFER_SIZE); const char *port; int available_nhosts; struct gfarm_host_sched_info *available_hosts; gerr = gfarm_schedule_hosts_domain_by_file(filepath, GFARM_FILE_RDONLY, "", &available_nhosts, &available_hosts); if (available_nhosts > 0) { if (gerr == GFARM_ERR_NO_ERROR) { int *ports; char **hosts; int nhosts = available_nhosts; int i; GFARM_MALLOC_ARRAY(hosts, available_nhosts); GFARM_MALLOC_ARRAY(ports, available_nhosts); gerr = gfarm_schedule_hosts_acyclic_to_write(filepath, available_nhosts, available_hosts, &nhosts, hosts, ports); if (nhosts > 0 && gerr == GFARM_ERR_NO_ERROR) { for (i = 0; i < nhosts; i++) { port = apr_table_get(wconfig->hosts, hosts[i]); if (port != NULL) { if (strcmp(wconfig->localhost, hosts[i]) != 0) { if (wconfig->ssl) { snprintf(redirect_url, WEBGFARM_BUFFER_SIZE, "https://%s:%s%s", hosts[i], port, r->uri); } else { snprintf(redirect_url, WEBGFARM_BUFFER_SIZE, "http://%s:%s%s", hosts[i], port, r->uri); } apr_table_set(r->headers_out, "Location", redirect_url); free(ports); free(hosts); return HTTP_TEMPORARY_REDIRECT; } else { break; } } } } free(ports); free(hosts); } else if (gerr != GFARM_ERR_NO_SUCH_FILE_OR_DIRECTORY) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "gfarm_schedule_hosts_domain_by_file can not get the hosts of the file(%s) with error: %s", filepath, gfarm_error_string(gerr)); return HTTP_INTERNAL_SERVER_ERROR; } } } // open if (r->method_number == M_POST) { gerr = gfs_pio_open(filepath, GFARM_FILE_WRONLY | GFARM_FILE_APPEND, &gfs_file); } else if (r->method_number == M_PUT) { gerr = gfs_pio_open(filepath, GFARM_FILE_WRONLY | GFARM_FILE_TRUNC, &gfs_file); if (gerr == GFARM_ERR_NO_SUCH_FILE_OR_DIRECTORY) { gerr = gfs_pio_create(filepath, GFARM_FILE_WRONLY, 0644, &gfs_file); } } else { return HTTP_INTERNAL_SERVER_ERROR; } if (gerr != GFARM_ERR_NO_ERROR) { switch (gerr) { case GFARM_ERR_NO_SUCH_FILE_OR_DIRECTORY: return HTTP_NOT_FOUND; break; case GFARM_ERR_PERMISSION_DENIED: return HTTP_FORBIDDEN; break; //FIXEME: Support more error; default: return HTTP_INTERNAL_SERVER_ERROR; } } // get input body size; int in_size; const char *clen = apr_table_get(r->headers_in, "Content-Length"); if (clen != NULL) { in_size = strtol(clen, NULL, 0); if (in_size >= WEBGFFARM_MAX_POST_SIZE) { gfs_pio_close(gfs_file); return HTTP_BAD_REQUEST; } } else { gfs_pio_close(gfs_file); return HTTP_BAD_REQUEST; } if (in_size != 0) { apr_off_t size = 0; char *buf; if (util_read(r, &buf, &size) != OK) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[write] failed reading POST body"); gfs_pio_close(gfs_file); return HTTP_INTERNAL_SERVER_ERROR; } in_size = (int) size; if (buf == NULL) { return HTTP_BAD_REQUEST; } // write buffer int write_size; gerr = gfs_pio_write(gfs_file, buf, in_size, &write_size); if (gerr != GFARM_ERR_NO_ERROR) { if (in_size != write_size) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "[write] gfs_pio_write failure..(in: %d, out: %d)", (int) in_size, (int) write_size); } gerr = gfs_pio_close(gfs_file); return HTTP_INTERNAL_SERVER_ERROR; } } // close gerr = gfs_pio_close(gfs_file); if (gerr != GFARM_ERR_NO_ERROR) { return HTTP_INTERNAL_SERVER_ERROR; } else { return OK; } }