/* * make temporary file permanent, returns the new name. */ char *savetemp(char *name, char *newname) { int i, overwrite; if (strcmp(name, newname) == 0) return name; for (i = 0; i < MAXTMPF; ++i) if (tmpf[i].flags && strcmp(tmpf[i].path, name) == 0) break; if (i < MAXTMPF) { if (strlen(name) < 4 || name[strlen(name) - 3] != TMP_EXT) { if (verbose) fprintf(pgpout, "savetemp: not renaming '%s' to '%s'\n", name, newname); return name; /* return original file name */ } } newname = ck_dup_output(newname, FALSE, TRUE); if (newname==NULL) return(NULL); if (verbose) fprintf(pgpout, "savetemp: renaming '%s' to '%s'\n", name, newname); if (rename2(name, newname) < 0) { /* errorLvl = UNKNOWN_FILE_ERROR; */ fprintf(pgpout, LANG("Can't create output file '%s'\n"), newname); return NULL; } if (i < MAXTMPF) tmpf[i].flags = 0; return newname; } /* savetemp */
int main(int argc, char **argv) { int sep = __pmPathSeparator(); int sts; int c; char *p; char pmnsfile[MAXPATHLEN]; char outfname[MAXPATHLEN]; struct stat sbuf; if ((p = getenv("PMNS_DEFAULT")) != NULL) { strncpy(pmnsfile, p, MAXPATHLEN); pmnsfile[MAXPATHLEN-1]= '\0'; } else { snprintf(pmnsfile, sizeof(pmnsfile), "%s%c" "pmns" "%c" "root", pmGetConfig("PCP_VAR_DIR"), sep, sep); } while ((c = pmgetopt_r(argc, argv, &opts)) != EOF) { switch (c) { case 'd': /* duplicate PMIDs are OK */ fprintf(stderr, "%s: Warning: -d deprecated, duplicate PMNS names allowed by default\n", pmProgname); break; case 'D': /* debug flag */ if ((sts = __pmParseDebug(opts.optarg)) < 0) { fprintf(stderr, "%s: unrecognized debug flag specification (%s)\n", pmProgname, opts.optarg); opts.errors++; } else { pmDebug |= sts; } break; case 'n': /* alternative name space file */ strncpy(pmnsfile, opts.optarg, MAXPATHLEN); pmnsfile[MAXPATHLEN-1]= '\0'; break; case '?': default: opts.errors++; break; } } if (opts.errors || opts.optind > argc - 1) { pmUsageMessage(&opts); exit(1); } if ((sts = pmLoadASCIINameSpace(pmnsfile, 1)) < 0) { fprintf(stderr, "%s: Error: pmLoadASCIINameSpace(%s, 1): %s\n", pmProgname, pmnsfile, pmErrStr(sts)); exit(1); } { __pmnsTree *t; t = __pmExportPMNS(); if (t == NULL) { /* sanity check - shouldn't ever happen */ fprintf(stderr, "Exported PMNS is NULL !"); exit(1); } root = t->root; } while (opts.optind < argc) { delpmns(root, fullname = argv[opts.optind]); opts.optind++; } /* * from here on, ignore SIGHUP, SIGINT and SIGTERM to protect * the integrity of the new ouput file */ __pmSetSignalHandler(SIGHUP, SIG_IGN); __pmSetSignalHandler(SIGINT, SIG_IGN); __pmSetSignalHandler(SIGTERM, SIG_IGN); snprintf(outfname, sizeof(outfname), "%s.new", pmnsfile); if ((outf = fopen(outfname, "w")) == NULL) { fprintf(stderr, "%s: Error: cannot open PMNS file \"%s\" for writing: %s\n", pmProgname, outfname, osstrerror()); exit(1); } if (stat(pmnsfile, &sbuf) == 0) { /* * preserve the mode and ownership of any existing PMNS file */ chmod(outfname, sbuf.st_mode & ~S_IFMT); #if defined(HAVE_CHOWN) if (chown(outfname, sbuf.st_uid, sbuf.st_gid) < 0) fprintf(stderr, "%s: chown(%s, ...) failed: %s\n", pmProgname, outfname, osstrerror()); #endif } pmns_output(root, outf); fclose(outf); /* rename the PMNS */ if (rename2(outfname, pmnsfile) == -1) { fprintf(stderr, "%s: cannot rename \"%s\" to \"%s\": %s\n", pmProgname, outfname, pmnsfile, osstrerror()); /* remove the new PMNS */ unlink(outfname); exit(1); } exit(0); }