Add new arg for program: --enable-cache-dir=<DIR>
This commit is contained in:
parent
55d3a61c0c
commit
ffc8e99f73
2 changed files with 42 additions and 0 deletions
|
@ -21,6 +21,14 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// libc includes.
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
// Posix includes.
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
void print_usage(void) {
|
void print_usage(void) {
|
||||||
puts("Usage:");
|
puts("Usage:");
|
||||||
puts(" -p <port> | --port <port>");
|
puts(" -p <port> | --port <port>");
|
||||||
|
@ -30,6 +38,7 @@ void print_usage(void) {
|
||||||
puts(" For example: --req-header-to-print=User-Agent");
|
puts(" For example: --req-header-to-print=User-Agent");
|
||||||
puts(" Note that this option is case-insensitive");
|
puts(" Note that this option is case-insensitive");
|
||||||
puts(" --enable-reload-config-on-change");
|
puts(" --enable-reload-config-on-change");
|
||||||
|
puts(" --enable-cache-dir=<DIR>");
|
||||||
}
|
}
|
||||||
|
|
||||||
Args parse_args(int32_t argc, char **argv) {
|
Args parse_args(int32_t argc, char **argv) {
|
||||||
|
@ -69,6 +78,36 @@ Args parse_args(int32_t argc, char **argv) {
|
||||||
}
|
}
|
||||||
} else if (strcmp(argv[0], "--enable-reload-config-on-change") == 0) {
|
} else if (strcmp(argv[0], "--enable-reload-config-on-change") == 0) {
|
||||||
args.flags |= 2;
|
args.flags |= 2;
|
||||||
|
} else if (strncmp(argv[0], "--enable-cache-dir=", 19) == 0) {
|
||||||
|
args.cache_dir = argv[0] + 19;
|
||||||
|
// Check if it actually is an existing directory.
|
||||||
|
DIR *d = opendir(args.cache_dir);
|
||||||
|
if (d == NULL) {
|
||||||
|
if (errno == ENOENT) {
|
||||||
|
printf(
|
||||||
|
"Directory \"%s\" doesn't exist, creating it...\n",
|
||||||
|
args.cache_dir);
|
||||||
|
int ret = mkdir(
|
||||||
|
args.cache_dir,
|
||||||
|
S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
||||||
|
if (ret == -1) {
|
||||||
|
fprintf(
|
||||||
|
stderr,
|
||||||
|
"ERROR Failed to create new directory (errno %d)\n",
|
||||||
|
errno);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fprintf(
|
||||||
|
stderr,
|
||||||
|
"ERROR Failed to open directory \"%s\" (errno %d)!\n",
|
||||||
|
args.cache_dir,
|
||||||
|
errno);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("Directory \"%s\" exists.\n", args.cache_dir);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
puts("ERROR: Invalid args!\n");
|
puts("ERROR: Invalid args!\n");
|
||||||
print_usage();
|
print_usage();
|
||||||
|
|
|
@ -34,6 +34,9 @@ typedef struct Args {
|
||||||
const char *config_file;
|
const char *config_file;
|
||||||
// Needs to be free'd.
|
// Needs to be free'd.
|
||||||
SDArchiverLinkedList *list_of_headers_to_log;
|
SDArchiverLinkedList *list_of_headers_to_log;
|
||||||
|
// Non-NULL if cache-dir is specified and cache is to be used.
|
||||||
|
// Does not need to be free'd since it points to a string in argv.
|
||||||
|
const char *cache_dir;
|
||||||
} Args;
|
} Args;
|
||||||
|
|
||||||
void print_usage(void);
|
void print_usage(void);
|
||||||
|
|
Loading…
Reference in a new issue