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); }
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, §ions); } else { e = gfarm_file_section_info_get_sorted_all_serial_by_file( gfarm_file, &nsections, §ions); } 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); }
char * gfs_pio_open(const char *url, int flags, GFS_File *gfp) { char *e, *pathname; GFS_File gf; gfarm_timerval_t t1, t2; gfs_profile(gfarm_gettimerval(&t1)); if ((flags & GFARM_FILE_ACCMODE) != GFARM_FILE_RDONLY) { #if 0 /* XXX - ROOT I/O opens a new file with O_CREAT|O_RDRW mode. */ e = GFARM_ERR_OPERATION_NOT_SUPPORTED; /* XXX */ goto finish; #else flags |= GFARM_FILE_CREATE; #endif } e = gfarm_url_make_path(url, &pathname); if (e != NULL) goto finish; e = gfs_file_alloc(&gf); if (e != NULL) { free(pathname); goto finish; } gf->open_flags = flags; gf->mode |= GFS_FILE_MODE_READ; if (((flags & GFARM_FILE_ACCMODE) == GFARM_FILE_RDWR) || ((flags & GFARM_FILE_ACCMODE) == GFARM_FILE_WRONLY)) gf->mode |= GFS_FILE_MODE_WRITE; e = gfarm_path_info_get(pathname, &gf->pi); free(pathname); if (e != NULL) goto finish; e = gfarm_path_info_access(&gf->pi, GFS_R_OK); if (e != NULL) { gfarm_path_info_free(&gf->pi); gfs_file_free(gf); goto finish; } if (!GFARM_S_ISREG(gf->pi.status.st_mode)) { if (GFARM_S_ISDIR(gf->pi.status.st_mode)) e = GFARM_ERR_IS_A_DIRECTORY; else e = GFARM_ERR_OPERATION_NOT_SUPPORTED; gfarm_path_info_free(&gf->pi); gfs_file_free(gf); goto finish; } gf->mode |= GFS_FILE_MODE_NSEGMENTS_FIXED; *gfp = gf; e = NULL; finish: gfs_profile(gfarm_gettimerval(&t2)); gfs_profile(gfs_pio_open_time += gfarm_timerval_sub(&t2, &t1)); return (e); }
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); }
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); }
char * gfs_stat_canonical_path(char *gfarm_file, struct gfs_stat *s) { char *e; int i, nsections; struct gfarm_file_section_info *sections; struct gfarm_path_info pi; long ino; e = gfs_get_ino(gfarm_file, &ino); if (e != NULL) return (e); e = gfarm_path_info_get(gfarm_file, &pi); if (e != NULL) return (e); *s = pi.status; s->st_ino = ino; s->st_user = strdup(s->st_user); s->st_group = strdup(s->st_group); gfarm_path_info_free(&pi); if (s->st_user == NULL || s->st_group == NULL) { gfs_stat_free(s); return (GFARM_ERR_NO_MEMORY); } if (!GFARM_S_ISREG(s->st_mode)) return (NULL); /* regular file */ e = gfarm_file_section_info_get_all_by_file(gfarm_file, &nsections, §ions); if (e != NULL) { gfs_stat_free(s); /* * If GFARM_ERR_NO_SUCH_OBJECT is returned here, * gfs_stat() incorrectly assumes that this is a directory, * and reports GFARM_ERR_NOT_A_DIRECTORY. */ return (GFARM_ERR_NO_FRAGMENT_INFORMATION); } s->st_size = 0; for (i = 0; i < nsections; i++) s->st_size += sections[i].filesize; s->st_nsections = nsections; gfarm_file_section_info_free_all(nsections, sections); return (NULL); }
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, §ions); } else { e_save = gfarm_file_section_info_get_sorted_all_serial_by_file( gfarm_file, &nsections, §ions); } 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); }
char * gfs_stat_section(const char *gfarm_url, const char *section, struct gfs_stat *s) { char *e, *gfarm_file; struct gfarm_file_section_info sinfo; struct gfarm_path_info pi; long ino; e = gfarm_url_make_path(gfarm_url, &gfarm_file); if (e != NULL) return (e); e = gfs_get_ino(gfarm_file, &ino); if (e != NULL) { free(gfarm_file); return (e); } e = gfarm_path_info_get(gfarm_file, &pi); if (e != NULL) { free(gfarm_file); return (e); } *s = pi.status; s->st_ino = ino; s->st_user = strdup(s->st_user); s->st_group = strdup(s->st_group); gfarm_path_info_free(&pi); if (!GFARM_S_ISREG(s->st_mode)) { free(gfarm_file); return (NULL); } e = gfarm_file_section_info_get(gfarm_file, section, &sinfo); free(gfarm_file); if (e != NULL) return (e); s->st_size = sinfo.filesize; s->st_nsections = 1; gfarm_file_section_info_free(&sinfo); return (NULL); }
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); }
static gfarm_error_t display_replica_catalog(char *path, struct gfs_stat *st, void *arg) { gfarm_error_t e = GFARM_ERR_NO_ERROR; gfarm_mode_t mode; display_name(path, st, arg); mode = st->st_mode; if (GFARM_S_ISDIR(mode)) e = GFARM_ERR_IS_A_DIRECTORY; else if (!GFARM_S_ISREG(mode)) e = GFARM_ERR_FUNCTION_NOT_IMPLEMENTED; if (e == GFARM_ERR_NO_ERROR) e = display_copy(path); if (e != GFARM_ERR_NO_ERROR) fprintf(stderr, "%s\n", gfarm_error_string(e)); return (e); }
static char * gfs_pio_open_check_perm(GFS_File gf) { char *e; int check= 0; if ((gf->mode & GFS_FILE_MODE_READ) != 0) check |= GFS_R_OK; if ((gf->mode & GFS_FILE_MODE_WRITE) != 0 || (gf->open_flags & GFARM_FILE_TRUNC) != 0) check |= GFS_W_OK; e = gfarm_path_info_access(&gf->pi, check); if (e != NULL) return (e); if (!GFARM_S_ISREG(gf->pi.status.st_mode)) { if (GFARM_S_ISDIR(gf->pi.status.st_mode)) return (GFARM_ERR_IS_A_DIRECTORY); else return (GFARM_ERR_OPERATION_NOT_SUPPORTED); } return (NULL); }
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(§ion) != 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); }
static gfarm_error_t create_filelist(char *file, struct gfs_stat *st, void *arg) { struct flist *a = arg; int i, j, ncopy, src_ncopy = 0, dst_ncopy = 0; char **copy; gfarm_error_t e; if (!GFARM_S_ISREG(st->st_mode)) { if (opt_verbose) printf("%s: not a regular file, skipped\n", file); return (GFARM_ERR_NO_ERROR); } e = gfs_replica_list_by_name(file, &ncopy, ©); if (e != GFARM_ERR_NO_ERROR) return (e); /* if there is no available file replica, display error message */ if (ncopy == 0 && st->st_size > 0) { fprintf(stderr, "%s: no available file repilca\n", file); e = GFARM_ERR_NO_ERROR; goto free_copy; } for (i = 0; i < ncopy; ++i) { if ((a->src_hosthash == NULL || gfarm_hash_lookup( a->src_hosthash, copy[i], strlen(copy[i]) + 1)) && gfarm_host_is_in_domain(copy[i], a->src_domain)) { ++src_ncopy; } if ((a->dst_hosthash == NULL || gfarm_hash_lookup( a->dst_hosthash, copy[i], strlen(copy[i]) + 1)) && gfarm_host_is_in_domain(copy[i], a->dst_domain)) { ++dst_ncopy; } } /* * if there is no replica in a set of source nodes or there * are already specified number of replicas in a set of * destination nodes, do not add. */ if (src_ncopy == 0 || dst_ncopy == opt_nrep) { e = GFARM_ERR_NO_ERROR; goto free_copy; } /* add source nodes to srchash to count the number of source nodes */ for (i = 0; i < ncopy; ++i) { char *s = copy[i]; if ((a->src_hosthash == NULL || gfarm_hash_lookup( a->src_hosthash, s, strlen(s) + 1)) && gfarm_host_is_in_domain(s, a->src_domain)) gfarm_hash_enter(a->srchash, s, strlen(s)+1, 0, NULL); } /* add a file info to slist */ for (j = 0; j < opt_nrep - dst_ncopy; ++j) { e = gfarm_list_add_file_info(file, st->st_size, ncopy, copy, 0, &a->slist); if (e != GFARM_ERR_NO_ERROR) goto free_copy; } /* add a file info to dlist if too many file replicas exist */ if (dst_ncopy > opt_nrep) { e = gfarm_list_add_file_info(file, st->st_size, ncopy, copy, dst_ncopy - opt_nrep, &a->dlist); } free_copy: gfarm_strings_free_deeply(ncopy, copy); return (e); }
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); }
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); }
char * gfs_pio_create(const char *url, int flags, gfarm_mode_t mode, GFS_File *gfp) { char *e, *pathname; GFS_File gf; int pi_available = 0; mode_t mask; char *user; gfarm_timerval_t t1, t2; gfs_profile(gfarm_gettimerval(&t1)); user = gfarm_get_global_username(); if (user == NULL) { e = "gfarm_pio_create(): programming error, " "gfarm library isn't properly initialized"; goto finish; } #if 0 /* XXX - ROOT I/O opens a new file with O_CREAT|O_RDRW mode. */ if ((flags & GFARM_FILE_ACCMODE) != GFARM_FILE_WRONLY) { e = GFARM_ERR_OPERATION_NOT_SUPPORTED; /* XXX */ goto finish; } #endif mask = umask(0); umask(mask); mode &= ~mask; e = gfarm_url_make_path_for_creation(url, &pathname); if (e != NULL) goto finish; e = gfs_file_alloc(&gf); if (e != NULL) { free(pathname); goto finish; } /* gfs_pio_create() always assumes CREATE, TRUNC */ flags |= GFARM_FILE_CREATE | GFARM_FILE_TRUNC; if ((flags & (GFARM_FILE_TRUNC|GFARM_FILE_SEQUENTIAL)) != (GFARM_FILE_TRUNC|GFARM_FILE_SEQUENTIAL)) { /* MODE_READ is needed to re-calculate checksum. */ flags = (flags & ~GFARM_FILE_ACCMODE) | GFARM_FILE_RDWR; } else if ((flags & ~GFARM_FILE_ACCMODE) == GFARM_FILE_RDONLY) { flags = (flags & ~GFARM_FILE_ACCMODE) | GFARM_FILE_WRONLY; } gf->open_flags = flags; gf->mode = GFS_FILE_MODE_WRITE; if ((flags & GFARM_FILE_ACCMODE) == GFARM_FILE_RDWR) gf->mode |= GFS_FILE_MODE_READ; e = gfarm_path_info_get(pathname, &gf->pi); if (e != NULL && e != GFARM_ERR_NO_SUCH_OBJECT) { free(pathname); gfs_file_free(gf); goto finish; } if (e == NULL) { /* XXX unlink and re-create the file? */ free(pathname); e = gfarm_path_info_access(&gf->pi, GFS_W_OK); if (e != NULL) { gfarm_path_info_free(&gf->pi); gfs_file_free(gf); goto finish; } if (!GFARM_S_ISREG(gf->pi.status.st_mode)) { if (GFARM_S_ISDIR(gf->pi.status.st_mode)) e = GFARM_ERR_IS_A_DIRECTORY; else e = GFARM_ERR_OPERATION_NOT_SUPPORTED; gfarm_path_info_free(&gf->pi); gfs_file_free(gf); goto finish; } /* * XXX should check the follows: * - the mode is consistent among same job * - creator of the metainfo has same job id * - O_TRUNC / !O_TRUNC case */ mode |= GFARM_S_IFREG; if (GFARM_S_IS_PROGRAM(mode) != GFS_FILE_IS_PROGRAM(gf)) { gfarm_path_info_free(&gf->pi); gfs_file_free(gf); e = GFARM_ERR_OPERATION_NOT_PERMITTED; goto finish; } pi_available = 1; } if (!pi_available) { struct timeval now; gettimeofday(&now, NULL); gf->pi.pathname = pathname; gf->pi.status.st_mode = (GFARM_S_IFREG | mode); gf->pi.status.st_user = strdup(user); /* XXX NULL check */ gf->pi.status.st_group = strdup("*"); /* XXX for now */ gf->pi.status.st_atimespec.tv_sec = gf->pi.status.st_mtimespec.tv_sec = gf->pi.status.st_ctimespec.tv_sec = now.tv_sec; gf->pi.status.st_atimespec.tv_nsec = gf->pi.status.st_mtimespec.tv_nsec = gf->pi.status.st_ctimespec.tv_nsec = now.tv_usec * 1000; gf->pi.status.st_size = 0; gf->pi.status.st_nsections = 0; } *gfp = gf; gfs_uncachedir(); e = NULL; finish: gfs_profile(gfarm_gettimerval(&t2)); gfs_profile(gfs_pio_create_time += gfarm_timerval_sub(&t2, &t1)); return (e); }
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); }
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); }