diff --git a/README.md b/README.md index c9e6244..ee5f2f5 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,19 @@ __WARNING:__ not implemented yet ------------------------------------------------------------------------------- +#### No subdirectories `-T` + +If no subdirectories mode is enabled, there will not be a separate subdirectory +for each slug, instead slugs will be suffixed by .txt + +``` +fiche -T +``` + +__Default value:__ not set + +------------------------------------------------------------------------------- + ### Running as a service There's a simple systemd example: diff --git a/fiche.c b/fiche.c index 2255685..418d56d 100644 --- a/fiche.c +++ b/fiche.c @@ -117,7 +117,7 @@ static void *handle_connection(void *args); * @arg extra_length additional length that was added to speed-up the * generation process * - * This function is used in connection with create_directory function + * This function is used in connection with create_slug_path function * It generates strings that are used to create a directory for * user-provided data. If directory already exists, we ask this function * to generate another slug with increased size. @@ -126,13 +126,14 @@ static void generate_slug(char **output, uint8_t length, uint8_t extra_length); /** - * @brief Creates a directory at requested path using requested slug + * @brief Creates a slug directory with requested slug + @ @remarks Only ensures that path is available if using no-subdirs mode * @returns 0 if succeded, 1 if failed or dir already existed * * @arg output_dir root directory for all pastes * @arg slug directory name for a particular paste */ -static int create_directory(char *output_dir, char *slug); +static int create_slug_path(char *output_dir, char *slug, char no_subdirs); /** @@ -141,7 +142,7 @@ static int create_directory(char *output_dir, char *slug); * @arg data Buffer with data received from the user * @arg path Path at which file containing data from the buffer will be created */ -static int save_to_file(uint8_t *data, char *output_dir, char *slug); +static int save_to_file(uint8_t *data, char *output_dir, char *slug, char no_subdirs); // Logging-related @@ -210,7 +211,9 @@ void fiche_init(Fiche_Settings *settings) { // path to banlist NULL, // path to whitelist - NULL + NULL, + // no subdirectories + 0 }; // Copy default settings to provided instance @@ -612,8 +615,7 @@ static void *handle_connection(void *args) { } } - while(create_directory(c->settings->output_dir_path, slug) != 0); - + while(create_slug_path(c->settings->output_dir_path, slug, c->settings->no_subdirs) != 0); // Slug generation failed, we have to finish here if (!slug) { @@ -630,7 +632,7 @@ static void *handle_connection(void *args) { // Save to file failed, we have to finish here - if ( save_to_file(buffer, c->settings->output_dir_path, slug) != 0 ) { + if ( save_to_file(buffer, c->settings->output_dir_path, slug, c->settings->no_subdirs) != 0 ) { print_error("Couldn't save a file!"); print_separator(); @@ -702,29 +704,39 @@ static void generate_slug(char **output, uint8_t length, uint8_t extra_length) { } -static int create_directory(char *output_dir, char *slug) { +static int create_slug_path(char *output_dir, char *slug, char no_subdirs) { if (!slug) { return -1; } - // Additional byte is for the slash - size_t len = strlen(output_dir) + strlen(slug) + 2; + // Additional byte is for the slash, for no_subdirs 4 additional bytes for suffix + size_t len = no_subdirs ? strlen(output_dir) + strlen(slug) + 6 : strlen(output_dir) + strlen(slug) + 2; // Generate a path char *path = malloc(len); if (!path) { return -1; } - snprintf(path, len, "%s%s%s", output_dir, "/", slug); + if (no_subdirs) { + snprintf(path, len, "%s%s%s%s", output_dir, "/", slug, ".txt"); + } else { + snprintf(path, len, "%s%s%s", output_dir, "/", slug); + } // Create output directory, just in case mkdir(output_dir, S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP); + int r; + // Create slug directory - const int r = mkdir( - path, - S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP - ); + if (no_subdirs) { + r = access(path, F_OK) == 0; + } else { + r = mkdir( + path, + S_IRWXU | S_IRGRP | S_IROTH | S_IXOTH | S_IXGRP + ); + } free(path); @@ -732,11 +744,13 @@ static int create_directory(char *output_dir, char *slug) { } -static int save_to_file(uint8_t *data, char *output_dir, char *slug) { +static int save_to_file(uint8_t *data, char *output_dir, char *slug, char no_subdirs) { char *file_name = "index.txt"; - // Additional 2 bytes are for 2 slashes - size_t len = strlen(output_dir) + strlen(slug) + strlen(file_name) + 3; + // Additional 2 bytes are for 2 slashes, 4 bytes for suffix + size_t len = no_subdirs ? + strlen(output_dir) + strlen(file_name) + 6 : + strlen(output_dir) + strlen(slug) + strlen(file_name) + 3; // Generate a path char *path = malloc(len); @@ -744,7 +758,11 @@ static int save_to_file(uint8_t *data, char *output_dir, char *slug) { return -1; } - snprintf(path, len, "%s%s%s%s%s", output_dir, "/", slug, "/", file_name); + if (no_subdirs) { + snprintf(path, len, "%s%s%s%s", output_dir, "/", slug, ".txt"); + } else { + snprintf(path, len, "%s%s%s%s%s", output_dir, "/", slug, "/", file_name); + } // Attempt file saving FILE *f = fopen(path, "w"); diff --git a/fiche.h b/fiche.h index 4b8c958..0f7f1fc 100644 --- a/fiche.h +++ b/fiche.h @@ -79,6 +79,11 @@ typedef struct Fiche_Settings { */ char *whitelist_path; + /** + * @brief Don't include subdirectory name + */ + char no_subdirs; + } Fiche_Settings; diff --git a/main.c b/main.c index 269633c..cb9fc23 100644 --- a/main.c +++ b/main.c @@ -9,7 +9,7 @@ Live example: http://termbin.com ------------------------------------------------------------------------------- -usage: fiche [-DepbsdolBuw]. +usage: fiche [-DepbsdolBuwT]. [-D] [-e] [-d domain] [-p port] [-s slug size] [-o output directory] [-B buffer size] [-u user name] [-l log file] [-b banlist] [-w whitelist] @@ -44,7 +44,7 @@ int main(int argc, char **argv) { // Parse input arguments int c; - while ((c = getopt(argc, argv, "D6eSp:b:s:d:o:l:B:u:w:")) != -1) { + while ((c = getopt(argc, argv, "D6eSp:b:s:d:o:l:B:u:w:T")) != -1) { switch (c) { // domain @@ -110,10 +110,17 @@ int main(int argc, char **argv) { } break; + // slug mode text files + case 'T': + { + fs.no_subdirs = 1; + } + break; + // Display help in case of any unsupported argument default: { - printf("usage: fiche [-dpsoBulbw].\n"); + printf("usage: fiche [-dpsoBulbwT].\n"); printf(" [-d domain] [-p port] [-s slug size]\n"); printf(" [-o output directory] [-B buffer size] [-u user name]\n"); printf(" [-l log file] [-b banlist] [-w whitelist]\n");