commit ad6e6f2a9d6895de9f387b2bd9de8a7e7a476d2b
parent 971d310371efeafa11a353cfead3aed9cfdc7a9c
Author: finwo <finwo@pm.me>
Date: Sat, 14 Mar 2026 00:48:30 +0100
Added repository clean-cache command
Diffstat:
5 files changed, 144 insertions(+), 91 deletions(-)
diff --git a/README.md b/README.md
@@ -15,10 +15,10 @@ Global options:
Commands:
a(dd) Add a new dependency to the project
i(nstall) Install all the project's dependencies
- i(nit) Initialize a new project with a .dep file
- l(icense) Show license information
+ init Initialize a new project with a .dep file
+ license Show license information
r(epo(sitory)) Repository management
- h(elp) [topic] Show this help or the top-level info about a command
+ help [topic] Show this help or the top-level info about a command
Help topics:
global This help text
@@ -121,6 +121,12 @@ To remove a repository:
dep repository remove myorg
```
+To clean the cache of downloaded repository manifests:
+
+```sh
+dep repository clean-cache
+```
+
Creating Repositories
--------------------
diff --git a/src/command/add/main.c b/src/command/add/main.c
@@ -9,45 +9,12 @@
#include "cofyc/argparse.h"
#include "command/command.h"
+#include "common/fs-utils.h"
#include "common/github-utils.h"
#include "common/net-utils.h"
-#define REPO_DIR_DEFAULT "/.config/finwo/dep/repositories.d/"
-#define CACHE_DIR_DEFAULT "/.config/finwo/dep/repositories.cache/"
#define CACHE_MAX_AGE_SECONDS (7 * 24 * 60 * 60)
-static char *get_repo_dir(void) {
- const char *home = getenv("HOME");
- if (!home) {
- fprintf(stderr, "Error: HOME environment variable not set\n");
- return NULL;
- }
- size_t len = strlen(home) + strlen(REPO_DIR_DEFAULT) + 1;
- char *path = malloc(len);
- if (!path) {
- fprintf(stderr, "Error: out of memory\n");
- return NULL;
- }
- snprintf(path, len, "%s%s", home, REPO_DIR_DEFAULT);
- return path;
-}
-
-static char *get_cache_dir(void) {
- const char *home = getenv("HOME");
- if (!home) {
- fprintf(stderr, "Error: HOME environment variable not set\n");
- return NULL;
- }
- size_t len = strlen(home) + strlen(CACHE_DIR_DEFAULT) + 1;
- char *path = malloc(len);
- if (!path) {
- fprintf(stderr, "Error: out of memory\n");
- return NULL;
- }
- snprintf(path, len, "%s%s", home, CACHE_DIR_DEFAULT);
- return path;
-}
-
static unsigned long hash_string(const char *str) {
unsigned long hash = 5381;
int c;
@@ -72,38 +39,6 @@ static int is_cache_outdated(const char *cache_path) {
return file_age > CACHE_MAX_AGE_SECONDS;
}
-static int mkdir_recursive(const char *path) {
- char tmp[PATH_MAX];
- char *p = NULL;
- size_t len;
-
- snprintf(tmp, sizeof(tmp), "%s", path);
- len = strlen(tmp);
- if (tmp[len - 1] == '/') {
- tmp[len - 1] = '\0';
- }
-
- for (p = tmp + 1; *p; p++) {
- if (*p == '/') {
- *p = '\0';
- mkdir(tmp, 0755);
- *p = '/';
- }
- }
- return mkdir(tmp, 0755);
-}
-
-static char *trim_whitespace(char *str) {
- while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') str++;
- if (*str == '\0') return str;
- char *end = str + strlen(str) - 1;
- while (end > str && (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')) {
- *end = '\0';
- end--;
- }
- return str;
-}
-
static int extract_version_from_depname(const char *depname_with_version, char *depname, char *version,
size_t depname_size, size_t version_size) {
const char *at_pos = strchr(depname_with_version, '@');
@@ -456,7 +391,7 @@ void __attribute__((constructor)) cmd_add_setup(void) {
}
cmd->next = commands;
cmd->fn = cmd_add;
- static const char *add_names[] = {"add", NULL};
+ static const char *add_names[] = {"add", "a", NULL};
cmd->name = add_names;
commands = cmd;
}
diff --git a/src/command/repository/main.c b/src/command/repository/main.c
@@ -8,28 +8,12 @@
#include "../command.h"
#include "cofyc/argparse.h"
-
-#define REPO_DIR_DEFAULT "/.config/finwo/dep/repositories.d/"
-
-static char *get_repo_dir(void) {
- const char *home = getenv("HOME");
- if (!home) {
- fprintf(stderr, "Error: HOME environment variable not set\n");
- return NULL;
- }
- size_t len = strlen(home) + strlen(REPO_DIR_DEFAULT) + 1;
- char *path = malloc(len);
- if (!path) {
- fprintf(stderr, "Error: out of memory\n");
- return NULL;
- }
- snprintf(path, len, "%s%s", home, REPO_DIR_DEFAULT);
- return path;
-}
+#include "common/fs-utils.h"
static int cmd_repository_list(int argc, const char **argv);
static int cmd_repository_add(int argc, const char **argv);
static int cmd_repository_remove(int argc, const char **argv);
+static int cmd_repository_clean_cache(int argc, const char **argv);
static const char *const usages[] = {
"repository <subcommand> [options]",
@@ -48,9 +32,10 @@ static int cmd_repository(int argc, const char **argv) {
// If no subcommand provided, show available subcommands
if (argc < 1) {
printf("Available subcommands:\n");
- printf(" list List the names of the repositories\n");
- printf(" add Add a repository: add <name> <url>\n");
- printf(" remove Remove a repository: remove <name>\n");
+ printf(" list List the names of the repositories\n");
+ printf(" add Add a repository: add <name> <url>\n");
+ printf(" remove Remove a repository: remove <name>\n");
+ printf(" clean-cache Remove all cached manifest files\n");
return 0;
}
@@ -61,6 +46,8 @@ static int cmd_repository(int argc, const char **argv) {
return cmd_repository_add(argc - 1, argv + 1);
} else if (!strcmp(argv[0], "remove")) {
return cmd_repository_remove(argc - 1, argv + 1);
+ } else if (!strcmp(argv[0], "clean-cache")) {
+ return cmd_repository_clean_cache(argc - 1, argv + 1);
} else {
fprintf(stderr, "Error: unknown subcommand '%s'\n", argv[0]);
return 1;
@@ -272,6 +259,45 @@ static int cmd_repository_remove(int argc, const char **argv) {
return 0;
}
+static int cmd_repository_clean_cache(int argc, const char **argv) {
+ (void)argc;
+ (void)argv;
+
+ char *cache_dir = get_cache_dir();
+ if (!cache_dir) {
+ return 1;
+ }
+
+ DIR *dir = opendir(cache_dir);
+ if (!dir) {
+ free(cache_dir);
+ return 0;
+ }
+
+ struct dirent *entry;
+ int removed = 0;
+
+ while ((entry = readdir(dir)) != NULL) {
+ if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue;
+
+ char filepath[PATH_MAX];
+ snprintf(filepath, sizeof(filepath), "%s%s", cache_dir, entry->d_name);
+
+ struct stat st;
+ if (stat(filepath, &st) < 0 || !S_ISREG(st.st_mode)) continue;
+
+ if (remove(filepath) == 0) {
+ removed++;
+ }
+ }
+
+ closedir(dir);
+ free(cache_dir);
+
+ printf("Removed %d cached manifest file%s.\n", removed, removed == 1 ? "" : "s");
+ return 0;
+}
+
// Register the repository command with the command system
void __attribute__((constructor)) cmd_repository_setup(void) {
struct cmd_struct *cmd = calloc(1, sizeof(struct cmd_struct));
diff --git a/src/common/fs-utils.c b/src/common/fs-utils.c
@@ -0,0 +1,72 @@
+#include "fs-utils.h"
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+char *get_repo_dir(void) {
+ const char *home = getenv("HOME");
+ if (!home) {
+ fprintf(stderr, "Error: HOME environment variable not set\n");
+ return NULL;
+ }
+ size_t len = strlen(home) + strlen(REPO_DIR_DEFAULT) + 1;
+ char *path = malloc(len);
+ if (!path) {
+ fprintf(stderr, "Error: out of memory\n");
+ return NULL;
+ }
+ snprintf(path, len, "%s%s", home, REPO_DIR_DEFAULT);
+ return path;
+}
+
+char *get_cache_dir(void) {
+ const char *home = getenv("HOME");
+ if (!home) {
+ fprintf(stderr, "Error: HOME environment variable not set\n");
+ return NULL;
+ }
+ size_t len = strlen(home) + strlen(CACHE_DIR_DEFAULT) + 1;
+ char *path = malloc(len);
+ if (!path) {
+ fprintf(stderr, "Error: out of memory\n");
+ return NULL;
+ }
+ snprintf(path, len, "%s%s", home, CACHE_DIR_DEFAULT);
+ return path;
+}
+
+int mkdir_recursive(const char *path) {
+ char tmp[PATH_MAX];
+ char *p = NULL;
+ size_t len;
+
+ snprintf(tmp, sizeof(tmp), "%s", path);
+ len = strlen(tmp);
+ if (tmp[len - 1] == '/') {
+ tmp[len - 1] = '\0';
+ }
+
+ for (p = tmp + 1; *p; p++) {
+ if (*p == '/') {
+ *p = '\0';
+ mkdir(tmp, 0755);
+ *p = '/';
+ }
+ }
+ return mkdir(tmp, 0755);
+}
+
+char *trim_whitespace(char *str) {
+ while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') str++;
+ if (*str == '\0') return str;
+ char *end = str + strlen(str) - 1;
+ while (end > str && (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')) {
+ *end = '\0';
+ end--;
+ }
+ return str;
+}
diff --git a/src/common/fs-utils.h b/src/common/fs-utils.h
@@ -0,0 +1,14 @@
+#ifndef FS_UTILS_H
+#define FS_UTILS_H
+
+#include <stddef.h>
+
+#define REPO_DIR_DEFAULT "/.config/finwo/dep/repositories.d/"
+#define CACHE_DIR_DEFAULT "/.config/finwo/dep/repositories.cache/"
+
+char *get_repo_dir(void);
+char *get_cache_dir(void);
+int mkdir_recursive(const char *path);
+char *trim_whitespace(char *str);
+
+#endif // FS_UTILS_H