mirror of
https://github.com/sist2app/sist2.git
synced 2026-01-17 08:26:29 +00:00
190 lines
4.8 KiB
C
190 lines
4.8 KiB
C
#include "cli.h"
|
|
|
|
#define DEFAULT_OUTPUT "index.sist2/"
|
|
#define DEFAULT_CONTENT_SIZE 4096
|
|
#define DEFAULT_QUALITY 5
|
|
#define DEFAULT_SIZE 500
|
|
#define DEFAULT_REWRITE_URL ""
|
|
|
|
#define DEFAULT_ES_URL "http://localhost:9200"
|
|
|
|
#define DEFAULT_BIND_ADDR "localhost"
|
|
#define DEFAULT_PORT "4090"
|
|
|
|
|
|
scan_args_t *scan_args_create() {
|
|
scan_args_t *args = calloc(sizeof(scan_args_t), 1);
|
|
return args;
|
|
}
|
|
|
|
int scan_args_validate(scan_args_t *args, int argc, const char **argv) {
|
|
if (argc < 2) {
|
|
fprintf(stderr, "Required positional argument: PATH.\n");
|
|
return 1;
|
|
}
|
|
|
|
char *abs_path = abspath(argv[1]);
|
|
if (abs_path == NULL) {
|
|
fprintf(stderr, "File not found: %s\n", argv[1]);
|
|
return 1;
|
|
} else {
|
|
args->path = abs_path;
|
|
}
|
|
|
|
if (args->incremental != NULL) {
|
|
abs_path = abspath(args->incremental);
|
|
if (abs_path == NULL) {
|
|
fprintf(stderr, "File not found: %s\n", args->incremental);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
if (args->quality == 0) {
|
|
args->quality = DEFAULT_QUALITY;
|
|
} else if (args->quality < 1 || args->quality > 31) {
|
|
fprintf(stderr, "Invalid quality: %f\n", args->quality);
|
|
return 1;
|
|
}
|
|
|
|
if (args->size == 0) {
|
|
args->size = DEFAULT_SIZE;
|
|
} else if (args->size <= 0) {
|
|
fprintf(stderr, "Invalid size: %d\n", args->size);
|
|
return 1;
|
|
}
|
|
|
|
if (args->content_size == 0) {
|
|
args->content_size = DEFAULT_CONTENT_SIZE;
|
|
} else if (args->content_size <= 0) {
|
|
fprintf(stderr, "Invalid content-size: %d\n", args->content_size);
|
|
return 1;
|
|
}
|
|
|
|
if (args->threads == 0) {
|
|
args->threads = 1;
|
|
} else if (args->threads < 0) {
|
|
fprintf(stderr, "Invalid threads: %d\n", args->threads);
|
|
return 1;
|
|
}
|
|
|
|
if (args->output == NULL) {
|
|
args->output = malloc(strlen(DEFAULT_OUTPUT) + 1);
|
|
strcpy(args->output, DEFAULT_OUTPUT);
|
|
} else {
|
|
args->output = expandpath(args->output);
|
|
}
|
|
|
|
int ret = mkdir(args->output, S_IRUSR | S_IWUSR | S_IXUSR);
|
|
if (ret != 0) {
|
|
fprintf(stderr, "Invalid output: '%s' (%s).\n", args->output, strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
if (args->name == NULL) {
|
|
args->name = g_path_get_basename(args->output);
|
|
}
|
|
|
|
if (args->rewrite_url == NULL) {
|
|
args->rewrite_url = DEFAULT_REWRITE_URL;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
#ifndef SIST_SCAN_ONLY
|
|
int index_args_validate(index_args_t *args, int argc, const char **argv) {
|
|
|
|
if (argc < 2) {
|
|
fprintf(stderr, "Required positional argument: PATH.\n");
|
|
return 1;
|
|
}
|
|
|
|
char *index_path = abspath(argv[1]);
|
|
if (index_path == NULL) {
|
|
fprintf(stderr, "File not found: %s\n", argv[1]);
|
|
return 1;
|
|
} else {
|
|
args->index_path = argv[1];
|
|
}
|
|
|
|
if (args->es_url == NULL) {
|
|
args->es_url = DEFAULT_ES_URL;
|
|
}
|
|
|
|
if (args->script_path != NULL) {
|
|
struct stat info;
|
|
int res = stat(args->script_path, &info);
|
|
|
|
if (res == -1) {
|
|
fprintf(stderr, "Error opening script file '%s': %s\n", args->script_path, strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
int fd = open(args->script_path, O_RDONLY);
|
|
if (fd == -1) {
|
|
fprintf(stderr, "Error opening script file '%s': %s\n", args->script_path, strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
args->script = malloc(info.st_size + 1);
|
|
res = read(fd, args->script, info.st_size);
|
|
if (res == -1) {
|
|
fprintf(stderr, "Error reading script file '%s': %s\n", args->script_path, strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
*(args->script + info.st_size) = '\0';
|
|
close(fd);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int web_args_validate(web_args_t *args, int argc, const char **argv) {
|
|
|
|
if (argc < 2) {
|
|
fprintf(stderr, "Required positional argument: PATH.\n");
|
|
return 1;
|
|
}
|
|
|
|
if (args->es_url == NULL) {
|
|
args->es_url = DEFAULT_ES_URL;
|
|
}
|
|
|
|
if (args->bind == NULL) {
|
|
args->bind = DEFAULT_BIND_ADDR;
|
|
}
|
|
|
|
if (args->port == NULL) {
|
|
args->port = DEFAULT_PORT;
|
|
}
|
|
|
|
if (args->credentials != NULL) {
|
|
args->b64credentials = onion_base64_encode(args->credentials, (int)strlen(args->credentials));
|
|
//Remove trailing newline
|
|
*(args->b64credentials + strlen(args->b64credentials) - 1) = '\0';
|
|
}
|
|
|
|
args->index_count = argc - 1;
|
|
args->indices = argv + 1;
|
|
|
|
for (int i = 0; i < args->index_count; i++) {
|
|
char *abs_path = abspath(args->indices[i]);
|
|
if (abs_path == NULL) {
|
|
fprintf(stderr, "File not found: %s\n", abs_path);
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
index_args_t *index_args_create() {
|
|
index_args_t *args = calloc(sizeof(index_args_t), 1);
|
|
return args;
|
|
}
|
|
|
|
web_args_t *web_args_create() {
|
|
web_args_t *args = calloc(sizeof(web_args_t), 1);
|
|
return args;
|
|
}
|
|
#endif
|
|
|