From a4759bf9a667016ba77e9551920bdcaa029bc366 Mon Sep 17 00:00:00 2001 From: Tom Dwaggy Date: Thu, 8 Dec 2016 22:52:11 -0500 Subject: [PATCH 1/3] Add -T flag for specifying to not create subdirectories. --- fiche.c | 33 +++++++++++++++++++++++++++------ fiche.h | 2 ++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/fiche.c b/fiche.c index 9a60195..8c39612 100644 --- a/fiche.c +++ b/fiche.c @@ -9,7 +9,7 @@ Live example: http://code.solusipse.net/ ------------------------------------------------------------------------------- -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] @@ -18,6 +18,8 @@ usage: fiche [-DepbsdolBuw]. -e option is for using an extended character set for the URL +-T option is for setting fiche to use a .txt file + Compile with Makefile or manually with -O2 and -pthread flags. To install use `make install` command. @@ -144,8 +146,12 @@ void *thread_connection(void *args) char slug[SLUG_SIZE+8]; generate_url(buffer, slug, SLUG_SIZE+8, data); save_log(slug, data.ip_address, data.hostname); - char response[strlen(slug) + strlen(DOMAIN) + 2]; - snprintf(response, sizeof response, "%s%s\n", DOMAIN, slug); + char response[strlen(slug) + strlen(DOMAIN) + 6]; + if(NO_CREATE_DIRECTORY) { + snprintf(response, sizeof response, "%s%s%s\n", DOMAIN, slug, FILE_EXTENSION); + } else { + snprintf(response, sizeof response, "%s%s\n", DOMAIN, slug); + } if (write(connection_socket, response, strlen(response)) < 0) printf("Error writing on stream socket\n"); } @@ -435,6 +441,9 @@ void generate_url(char *buffer, char *slug, size_t slug_length, struct client_da int create_directory(char *slug) { + if(NO_CREATE_DIRECTORY) + return 0; + char *directory = malloc(strlen(BASEDIR) + strlen(slug) + sizeof(char) + 1); snprintf(directory, strlen(BASEDIR) + strlen(slug) + sizeof(char) + 1, "%s%s%s", BASEDIR, "/", slug); @@ -451,10 +460,19 @@ void save_to_file(char *slug, char *buffer, struct client_data data) { char *directory = malloc(strlen(BASEDIR) + strlen(slug) + 11 * sizeof(char) + 1 ); - snprintf(directory, strlen(BASEDIR) + strlen(slug) + 11 * sizeof(char) + 1, "%s%s%s%s", BASEDIR , "/", slug, "/index.txt"); + if(NO_CREATE_DIRECTORY) { + snprintf(directory, strlen(BASEDIR) + strlen(slug) + 11 * sizeof(char) + 1, "%s%s%s%s", BASEDIR , "/", slug, ".txt"); + } else { + snprintf(directory, strlen(BASEDIR) + strlen(slug) + 11 * sizeof(char) + 1, "%s%s%s%s", BASEDIR , "/", slug, "/index.txt"); + } FILE *fp; fp = fopen(directory, "w"); + if (fp == NULL) { + printf("Failed to open file %s.\n", directory); + exit(1); + } + fprintf(fp, "%s", buffer); fclose(fp); @@ -524,7 +542,7 @@ void parse_parameters(int argc, char **argv) { 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) { case 'D': @@ -568,8 +586,11 @@ void parse_parameters(int argc, char **argv) WHITEFILE = optarg; load_list(WHITEFILE, 1); break; + case 'T': + NO_CREATE_DIRECTORY = 1; + break; default: - printf("usage: fiche [-D6epbsdSolBuw].\n"); + printf("usage: fiche [-D6epbsdSolBuwT].\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"); diff --git a/fiche.h b/fiche.h index 9ef6d9c..896de4b 100644 --- a/fiche.h +++ b/fiche.h @@ -66,6 +66,8 @@ int BUFSIZE = 32768; int QUEUE_SIZE = 500; char DOMAIN[128] = "localhost/"; char symbols[67] = "abcdefghijklmnopqrstuvwxyz0123456789"; +char* FILE_EXTENSION = ".txt"; +int NO_CREATE_DIRECTORY = 0; unsigned int time_seed; From e4da667e99ee3d2f26b38ccc1eb4093ec4051fc8 Mon Sep 17 00:00:00 2001 From: Tom the Dragon Date: Thu, 8 Dec 2016 23:20:15 -0500 Subject: [PATCH 2/3] Add documentation regarding the -T option --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 250d69b..bdc18b5 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ providing fiche-based service all the time on this address `solusipse.net` and t ## Server-side usage ## ``` -usage: fiche [-D6epbsdSolBuw]. +usage: fiche [-D6epbsdSolBuwT]. [-d domain] [-p port] [-s slug size] [-o output directory] [-B buffer size] [-u user name] [-l log file] [-b banlist] [-w whitelist] @@ -197,6 +197,15 @@ fiche -6 ----------------- +#### No Subdirectories #### + +This is a matter of taste, files will be placed in the output +directory with a .txt extension. + +fiche -T + +----------------- + #### Examples #### Logging connections with banlist: From d5fde16308b5cdd7d67f733fb3d2339afd7f2d73 Mon Sep 17 00:00:00 2001 From: Tom Dwaggy Date: Thu, 14 Sep 2017 01:35:58 -0400 Subject: [PATCH 3/3] Add option to disable subdirectory creation. --- README.md | 13 +++++++++++++ fiche.c | 58 ++++++++++++++++++++++++++++++++++++------------------- fiche.h | 5 +++++ main.c | 13 ++++++++++--- 4 files changed, 66 insertions(+), 23 deletions(-) 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");