diff --git a/cgit.c b/cgit.c
index 63b3d4a..ee7ebfe 100644
--- a/cgit.c
+++ b/cgit.c
@@ -316,6 +316,22 @@ static void config_cb(const char *name, const char *value)
add_mimetype(arg, value);
else if (!strcmp(name, "include"))
parse_configfile(expand_macros(value), config_cb);
+ // cgit-magenta:
+ else if (skip_prefix(name, "snapshot.", &arg)) {
+ if (!strcmp(arg, "root-dir")) {
+ ctx.cfg.snapshot.save_on_disk_root_dir = xstrdup(value);
+ } else if (!strcmp(arg, "max-compression-threads")) {
+ ctx.cfg.snapshot.max_compression_threads = atoi(value);
+ } else if (!strcmp(arg, "compression-level-zstd")) {
+ ctx.cfg.snapshot.compression_level_zstd = atoi(value);
+ } else if (!strcmp(arg, "compression-level-xz")) {
+ ctx.cfg.snapshot.compression_level_xz = atoi(value);
+ } else if (!strcmp(arg, "save-on-disk")) {
+ ctx.cfg.snapshot.save_on_disk = atoi(value);
+ } else if (!strcmp(arg, "save-on-disk-max-idle-age")) {
+ ctx.cfg.snapshot.save_on_disk_max_idle_age = atoi(value);
+ }
+ }
}
static void querystring_cb(const char *name, const char *value)
@@ -371,7 +387,8 @@ static void querystring_cb(const char *name, const char *value)
ctx.qry.context = atoi(value);
} else if (!strcmp(name, "ignorews")) {
ctx.qry.ignorews = atoi(value);
- } else if (!strcmp(name, "follow")) {
+ }
+ else if (!strcmp(name, "follow")) {
ctx.qry.follow = atoi(value);
}
}
@@ -456,6 +473,7 @@ static void prepare_context(void)
// cgit-magenta:
ctx.cfg.snapshot.save_on_disk_max_idle_age = 1 * 24 * 60 * 60; // 1 day
ctx.cfg.snapshot.save_on_disk = 1;
+ ctx.cfg.snapshot.save_on_disk_root_dir = "";
ctx.cfg.snapshot.max_compression_threads = 0;
ctx.cfg.snapshot.compression_level_zstd = 3;
ctx.cfg.snapshot.compression_level_xz = 6;
diff --git a/cgit.h b/cgit.h
index 44d74b5..49b2c2e 100644
--- a/cgit.h
+++ b/cgit.h
@@ -198,8 +198,11 @@ struct cgit_query {
struct cgit_config_snapshot
{
+ // Disk settings
bool save_on_disk;
int save_on_disk_max_idle_age; // in seconds
+ char *save_on_disk_root_dir;
+ // Compression settings
int max_compression_threads;
int compression_level_zstd;
int compression_level_xz;
diff --git a/filter.c b/filter.c
index 8fc14e1..0bcd5d7 100644
--- a/filter.c
+++ b/filter.c
@@ -51,6 +51,7 @@ static int open_exec_filter(struct cgit_filter *base, va_list ap)
close(pipe_fh[1]);
chk_non_negative(dup2(pipe_fh[0], STDIN_FILENO),
"Unable to use pipe as STDIN");
+ fprintf(stderr, "exec: %s\n", filter->cmd);
execvp(filter->cmd, filter->argv);
die_errno("Unable to exec subprocess %s", filter->cmd);
}
diff --git a/ui-snapshot.c b/ui-snapshot.c
index cf8b0d8..eeb6fad 100644
--- a/ui-snapshot.c
+++ b/ui-snapshot.c
@@ -69,6 +69,7 @@ static int write_compressed_tar_archive(const char *hex,
cgit_open_filter(&f.base);
rv = write_tar_archive(hex, prefix);
cgit_close_filter(&f.base);
+ fprintf(stderr, "%s %s %p %d\n", hex, prefix, filter_argv, rv);
return rv;
}
@@ -161,9 +162,88 @@ const unsigned cgit_snapshot_format_bit(const struct cgit_snapshot_format *f)
return BIT(f - &cgit_snapshot_formats[0]);
}
+int mkdir_recursive(char* path)
+{
+ char *p = path;
+ while (*p) {
+ if (*p == '/') {
+ *p = '\0';
+ if (mkdir(path, 0755) < 0) {
+ if (errno != EEXIST) {
+ fprintf(stderr, "[cgit] Error creating directory %s: %s (%d)\n",
+ path, strerror(errno), errno);
+ return -1;
+ }
+ }
+ *p = '/';
+ }
+ p++;
+ }
+ if (mkdir(path, 0755) < 0) {
+ if (errno != EEXIST) {
+ fprintf(stderr, "[cgit] Error creating directory %s: %s (%d)\n",
+ path, strerror(errno), errno);
+ return -1;
+ }
+ }
+ return 0;
+}
+
+char *get_snapshot_disk_path()
+{
+ if (!ctx.cfg.snapshot.save_on_disk)
+ return NULL;
+
+ // return ctx.cfg.snapshot.save_on_disk_root_dir "/" "";
+ char *path = xstrfmt("%s/%s", ctx.cfg.snapshot.save_on_disk_root_dir, ctx.repo->path);
+ if (mkdir(path, 0755) < 0) {
+ fprintf(stderr, "[cgit] Error creating snapshot directory %s: %s (%d)\n",
+ path, strerror(errno), errno);
+ free(path);
+ return NULL;
+ }
+ return path;
+}
+
+bool print_cached_file()
+{
+ char* path = get_snapshot_disk_path();
+ if (!path)
+ return false;
+
+ fprintf(stderr, "[cgit] Checking for cached file %s\n", path);
+
+ if (stat(path, NULL) < 0) {
+ if (errno == ENOENT) {
+ free(path);
+ return false; // No cached file found
+ }
+ fprintf(stderr, "[cgit] Error accessing cache file %s: %s (%d)\n",
+ path, strerror(errno), errno);
+ free(path);
+ return false;
+ }
+}
+
+static int start_cache_file()
+{
+ // if (!ctx.cfg.snapshot.save_on_disk)
+ // return 0;
+ //
+ // char* path = get_snapshot_disk_path();
+ // if (!path)
+ // return -1;
+ //
+ // freopen("output.txt", "a+", stdout);
+}
+static void end_cache_file()
+{
+
+}
+
static int make_snapshot(const struct cgit_snapshot_format *format,
- const char *hex, const char *prefix,
- const char *filename)
+ const char *hex, const char *prefix,
+ const char *filename)
{
struct object_id oid;
@@ -181,8 +261,14 @@ static int make_snapshot(const struct cgit_snapshot_format *format,
ctx.page.mimetype = xstrdup(format->mimetype);
ctx.page.filename = xstrdup(filename);
cgit_print_http_headers();
+
+ if (print_cached_file())
+ return 0;
+
+ start_cache_file();
init_archivers();
format->write_func(hex, prefix);
+ end_cache_file();
return 0;
}
|