From c2bf959b3828b762c77c722f4241330d0d437f91 Mon Sep 17 00:00:00 2001 From: Ryan Kavanagh Date: Fri, 11 Sep 2020 10:40:35 -0400 Subject: temporary file host & cleanup --- LICENSE | 13 ++++++++++++ cleanup.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ptmp | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 LICENSE create mode 100644 cleanup.c create mode 100755 ptmp diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f205a83 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright 2020 Ryan Kavanagh + +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. diff --git a/cleanup.c b/cleanup.c new file mode 100644 index 0000000..da9268e --- /dev/null +++ b/cleanup.c @@ -0,0 +1,72 @@ +/* + * 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 + +const double mind = 30.0; +const double maxd = 90.0; +const double max_size = 33554432; // 32MB +char* paths[1] = {"/var/www/users/tmp"}; + +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); + + file_system = fts_open(paths, 0, NULL); + + if (file_system != NULL) { + while ((node = fts_read(file_system)) != NULL) { + switch (node->fts_info) { + case FTS_F: + if (stat(node->fts_name, &attr) == -1) + err(1, NULL); + file_age = difftime(curr_time, attr.st_mtime); + cutoff = mind + (mind - maxd) * \ + pow((double) attr.st_size / (double) max_size - 1, 3.0); + + if (file_age >= cutoff) { + // printf("%s age %fs cuttoff %fs size %lld B delete\n", \ + // node->fts_name, file_age, cutoff, attr.st_size); + unlink(node->fts_name); + } + break; + default: + break; + } + } + } + + + return 0; +} diff --git a/ptmp b/ptmp new file mode 100755 index 0000000..f54633f --- /dev/null +++ b/ptmp @@ -0,0 +1,37 @@ +#!/bin/sh +# Copyright 2020 Ryan Kavanagh +# Upload as temporary file +# +# 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. + +DEST_DIR=hades.rak.ac:public_tmp +OPT_SUFFIX="" + +while getopts s: f +do + case $f in + s) + OPT_SUFFIX=".${OPTARG}";; + esac +done +shift `expr $OPTIND - 1` + +PAD=$(md5sum $1 | cut -f1 -d' ') + +DEST_NAME="${PAD}.$1${OPT_SUFFIX}" + +scp $1 "${DEST_DIR}/${DEST_NAME}" + +echo "https://rak.ac/~tmp/${DEST_NAME}" -- cgit v1.2.3