int record_selection(const App& app, const string& history_file) { ofstream ofile(history_file, ofstream::app); if (!ofile) return FILE_OPEN_FAILURE; ofile << app.app_id() << std::endl; ofile.close(); trim_history(history_file); return 0; }
void trim_files(time_t cutoff) { filelist_t *fwalk; FILE *infd, *outfd; char outfn[PATH_MAX]; struct stat st; struct utimbuf tstamp; int itemno = 0; /* We have a list of files to trim, so process them */ for (fwalk = flhead; (fwalk); fwalk = fwalk->next) { dbgprintf("Processing %s\n", fwalk->fname); itemno++; if (progressinfo && ((itemno % progressinfo) == 0)) showprogress(itemno); if (fwalk->ftype == F_DROPIT) { /* It's an orphan, and we want to delete it */ unlink(fwalk->fname); continue; } if (stat(fwalk->fname, &st) == -1) { errprintf("Cannot stat input file %s: %s\n", fwalk->fname, strerror(errno)); continue; } tstamp.actime = time(NULL); tstamp.modtime = st.st_mtime; infd = fopen(fwalk->fname, "r"); if (infd == NULL) { errprintf("Cannot open input file %s: %s\n", fwalk->fname, strerror(errno)); continue; } if (outdir) { sprintf(outfn, "%s/%s", outdir, fwalk->fname); } else { sprintf(outfn, "%s.tmp", fwalk->fname); } outfd = fopen(outfn, "w"); if (outfd == NULL) { errprintf("Cannot create output file %s: %s\n", outfn, strerror(errno)); fclose(infd); continue; } trim_history(infd, outfd, fwalk->ftype, cutoff); if (fwalk->ftype == F_ALLEVENTS) { char pidfn[PATH_MAX]; FILE *fd; long pid = -1; sprintf(pidfn, "%s/hobbitd_history.pid", xgetenv("BBSERVERLOGS")); fd = fopen(pidfn, "r"); if (fd) { char l[100]; fgets(l, sizeof(l), fd); fclose(fd); pid = atol(l); } if (pid > 0) kill(pid, SIGHUP); } fclose(infd); fclose(outfd); utime(outfn, &tstamp); /* So the access time is consistent with the last update */ /* Final check to make sure the file didn't change while we were processing it */ if ((stat(fwalk->fname, &st) == 0) && (st.st_mtime == tstamp.modtime)) { if (!outdir) rename(outfn, fwalk->fname); } else { errprintf("File %s changed while processing it - not trimmed\n", fwalk->fname); unlink(outfn); } } }