int nc_delete_mp(const char * path, int basepe) { NC *ncp; int status; size_t chunk = 512; ncp = new_NC(&chunk); if(ncp == NULL) return NC_ENOMEM; #if defined(LOCKNUMREC) /* && _CRAYMPP */ if (status = NC_init_pe(ncp, basepe)) { return status; } #else /* * !_CRAYMPP, only pe 0 is valid */ if(basepe != 0) return NC_EINVAL; #endif status = ncio_open(path, NC_NOWRITE, 0, 0, &ncp->chunk, &ncp->nciop, 0); if(status) goto unwind_alloc; assert(ncp->flags == 0); status = nc_get_NC(ncp); if(status != NC_NOERR) { /* Not a netcdf file, don't delete */ /* ??? is this the right semantic? what if it was just too big? */ (void) ncio_close(ncp->nciop, 0); } else { /* ncio_close does the unlink */ status = ncio_close(ncp->nciop, 1); /* ncio_close does the unlink */ } ncp->nciop = NULL; unwind_alloc: free_NC(ncp); return status; }
main(int ac, char *av[]) { char *path = ""; ncio *nciop; char linebuf[128]; off_t offset; size_t extent; void *vp; int status = ENOERR; int verbose = 0; int flags = 0; int create = 0; off_t igeto = 0; size_t igetsz = 0; size_t initialsz = 0; int doUnlink = 0; size_t sizehint = NC_SIZEHINT_DEFAULT; { extern int optind; extern int opterr; extern char *optarg; int ch; opterr = 1; while ((ch = getopt(ac, av, "vwcnLSUo:i:I:s:")) != EOF) switch (ch) { case 'v': verbose = 1; break; case 'w': flags |= NC_WRITE; break; case 'c': create = 1; break; case 'n': create = 1; flags |= NC_NOCLOBBER; break; case 'L': flags |= NC_LOCK; break; case 'S': flags |= NC_SHARE; break; case 'U': doUnlink = 1; break; case 'o': igeto = argscale(optarg, "igeto"); break; case 'i': igetsz = argscale(optarg, "igetsz"); break; case 'I': initialsz = argscale(optarg, "initialsz"); break; case 's': sizehint = argscale(optarg, "sizehint"); break; case '?': usage(av[0]); break; } /* last arg, the file name, is required */ if(ac - optind <= 0) usage(av[0]) ; path = av[optind]; } if(!create) { status = ncio_open(path, flags, igeto, igetsz, &sizehint, &nciop, &vp); if(status != ENOERR) { fprintf(stderr, "ncio_open: %s: %s\n", path, strerror(status)); return(EXIT_FAILURE); } } else { status = ncio_create(path, flags, initialsz, igeto, igetsz, &sizehint, &nciop, &vp); if(status != ENOERR) { fprintf(stderr, "ncio_create: %s: %s\n", path, strerror(status)); return(EXIT_FAILURE); } } while(fgets(linebuf, sizeof(linebuf), stdin) != NULL) { offset = 0; extent = 0; if(*linebuf == '#') continue; /* comment */ if(sscanf(linebuf, "rel 0x%lx", &offset) == 1 || sscanf(linebuf, "rel %ld", &offset) == 1) { if(verbose) printf("- rel %8ld\n", offset); if(!riu_pop(offset, 0)) continue; status = nciop->rel(nciop, offset, 0); if(status) { fprintf(stderr, "- rel error: %s\n", strerror(status)); continue; } } else if(sscanf(linebuf, "relm 0x%lx", &offset) == 1 || sscanf(linebuf, "relm %ld", &offset) == 1) { if(verbose) printf("- relm %8ld\n", offset); if(!riu_pop(offset, 1)) continue; status = nciop->rel(nciop, offset, RGN_MODIFIED); if(status) { fprintf(stderr, "- relm %8ld error: %s\n", offset, strerror(status)); continue; } } else if(sscanf(linebuf, "get 0x%lx %ld", &offset, &extent) == 2 || sscanf(linebuf, "get %ld %ld", &offset, &extent) == 2) { if(verbose) printf("- get %10ld %8ld\n", offset, extent); status = nciop->get(nciop, offset, extent, 0, &vp); if(status) { fprintf(stderr, "- get error: %s\n", strerror(status)); continue; } riu_push(offset, extent, vp); } else if(sscanf(linebuf, "getw 0x%lx %ld", &offset, &extent) == 2 || sscanf(linebuf, "getw %ld %ld", &offset, &extent) == 2) { if(verbose) printf("- getw %10ld %8ld\n", offset, extent); status = nciop->get(nciop, offset, extent, RGN_WRITE, &vp); if(status) { fprintf(stderr, "- getw error: %s\n", strerror(status)); continue; } riu_push(offset, extent, vp); } else if(strchr(linebuf, 'q') != NULL) break; else printf("???\n"); } status = ncio_close(nciop, doUnlink); if(status != ENOERR) { fprintf(stderr, "ncio_close(%s): %s: %s\n", doUnlink ? "doUnlink" : "", path, strerror(status)); return(EXIT_FAILURE); } return(EXIT_SUCCESS); }
int nc__open_mp(const char * path, int ioflags, int basepe, size_t *chunksizehintp, int *ncid_ptr) { NC *ncp; int status; #if ALWAYS_NC_SHARE /* DEBUG */ fSet(ioflags, NC_SHARE); #endif ncp = new_NC(chunksizehintp); if(ncp == NULL) return NC_ENOMEM; #if defined(LOCKNUMREC) /* && _CRAYMPP */ if (status = NC_init_pe(ncp, basepe)) { return status; } #else /* * !_CRAYMPP, only pe 0 is valid */ if(basepe != 0) return NC_EINVAL; #endif status = ncio_open(path, ioflags, 0, 0, &ncp->chunk, &ncp->nciop, 0); if(status) goto unwind_alloc; assert(ncp->flags == 0); if(fIsSet(ncp->nciop->ioflags, NC_SHARE)) { /* * NC_SHARE implies sync up the number of records as well. * (File format version one.) * Note that other header changes are not shared * automatically. Some sort of IPC (external to this package) * would be used to trigger a call to nc_sync(). */ fSet(ncp->flags, NC_NSYNC); } status = nc_get_NC(ncp); if(status != NC_NOERR) goto unwind_ioc; add_to_NCList(ncp); if(chunksizehintp != NULL) *chunksizehintp = ncp->chunk; *ncid_ptr = ncp->nciop->fd; return NC_NOERR; unwind_ioc: (void) ncio_close(ncp->nciop, 0); ncp->nciop = NULL; /*FALLTHRU*/ unwind_alloc: free_NC(ncp); return status; }