/* * Copyright 2020 Ryan Kavanagh * Cleanup hosted temporary files * * Permission to use, copy, modify, and/or distribute this software for * any purpose with or without fee is hereby granted, provided that the * above copyright notice and this permission notice appear in all * copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include const double mind = 30.0; const double maxd = 90.0; const double max_size = 33554432; // 32MB char* paths[2] = {"/var/www/users/~tmp", NULL}; int main(int argc, char * const argv[]) { FTS* file_system; FTSENT* node; struct stat attr; double cutoff = 0; double file_age = 0; time_t curr_time = time(NULL); int verbose, dryrun, ch; char* exclude = ""; verbose = 0; dryrun = 0; while ((ch = getopt(argc, argv, "vnx:")) != -1) { switch (ch) { case 'v': verbose = 1; break; case 'n': dryrun = 1; break; case 'x': exclude = optarg; } } argc -= optind; argv += optind; if ((file_system = fts_open(paths, FTS_NOCHDIR, NULL)) == NULL) errx(1, NULL); while ((node = fts_read(file_system)) != NULL) { if (node->fts_errno) err(node->fts_errno, "%s", node->fts_path); switch (node->fts_info) { case FTS_F: if (stat(node->fts_path, &attr) == -1) err(1, NULL); if (strcmp(node->fts_name, exclude) == 0) { if (verbose) printf("%s excluded\n", node->fts_path); break; } file_age = difftime(curr_time, attr.st_mtime) / 86400.0; cutoff = mind + (mind - maxd) * pow((double) attr.st_size / (double) max_size - 1, 3.0); if (verbose) printf("%s age %fd cuttoff %fd size %lldB\n", node->fts_path, file_age, cutoff, attr.st_size); if (file_age >= cutoff) { if (!dryrun) unlink(node->fts_path); if (verbose) printf("%s deleted\n", node->fts_path); } break; default: break; } } fts_close(file_system); return 0; }