summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2026-03-11 21:36:37 +0800
committerSadeep Madurange <sadeep@asciimx.com>2026-03-11 21:36:37 +0800
commitc76dfbea014f756d08bf8d1abed3daaed5f87809 (patch)
tree2e2cd84af410938c15004fbf576fa34cebf2ce30
parente0500692be6a8775c0a3c83984fc48d445a5f1d8 (diff)
downloadcvn-c76dfbea014f756d08bf8d1abed3daaed5f87809.tar.gz
wip: status command.
-rw-r--r--main.c69
1 files changed, 43 insertions, 26 deletions
diff --git a/main.c b/main.c
index d639f07..32232c5 100644
--- a/main.c
+++ b/main.c
@@ -11,18 +11,18 @@
#include "mem.h"
#include "stack.h"
-#define MAX_DEPTH 256
+#define CVN ".cvn"
+#define INDEX CVN "/index"
-#define CVN_DIR ".cvn"
-#define INDEX_FILE "index"
-
-#define HASH_LEN SHA1_DIGEST_LENGTH
-#define HASH_INPUT_LEN 4096
+#define MAX_DEPTH 256
+#define HASH_INPUT_LEN 4096
+#define HASH_LEN SHA1_DIGEST_LENGTH
static inline void init(int argc, char *argv[]);
static inline void status(int argc, char *argv[]);
-static inline void update_index(void);
+static inline void scan_index(void);
+static inline void scan_wtree(void);
static inline int ignore(const char *path);
struct command {
@@ -42,21 +42,20 @@ int main(int argc, char *argv[])
if (argc < 2)
errx(1, "Usage: %s <command> [<args>]", argv[0]);
-
+
for (i = 0; cmd[i].name != NULL; i++) {
if (strcmp(argv[1], cmd[i].name) == 0) {
cmd[i].func(argc - 1, argv + 1);
return 0;
}
}
-
return 0;
}
static inline void init(int argc, char *argv[])
{
+ int opt, fd;
char *branch;
- int opt, repo_fd, idx_fd;
optind = 1;
while ((opt = getopt(argc, argv, "b:")) != -1) {
@@ -69,30 +68,46 @@ static inline void init(int argc, char *argv[])
}
}
- if (mkdir(CVN_DIR, 0755) == -1) {
+ if (mkdir(CVN, 0755) == -1) {
if (errno != EEXIST)
err(1, "Failed to create repository");
}
- if ((repo_fd = open(CVN_DIR, O_RDONLY | O_DIRECTORY)) == -1)
- err(1, "Failed to open repository");
-
- if ((idx_fd = openat(repo_fd, INDEX_FILE,
- O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) {
- if (errno != EEXIST) {
- close(repo_fd);
+ if ((fd = open(INDEX, O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) {
+ if (errno != EEXIST)
err(1, "Failed to create index");
- }
} else
- close(idx_fd);
+ close(fd);
- close(repo_fd);
- printf("Initialized repository in %s\n", CVN_DIR);
+ printf("Initialized repository in %s\n", CVN);
}
static inline void status(int argc, char *argv[])
{
- update_index();
+ scan_index();
+ scan_wtree();
+}
+
+static inline void scan_index(void)
+{
+ int fd;
+ FILE *fp;
+ char *line;
+ size_t len;
+ ssize_t bytes_read;
+
+ if ((fd = open(INDEX, O_RDONLY)) == -1)
+ err(1, "Failed to open index");
+
+ if (!(fp = fdopen(fd, "r")))
+ err(1, "fdopen");
+
+ while ((bytes_read = getline(&line, &len, fp)) != -1) {
+ printf("idx: %s (%zu)\n", line, bytes_read);
+ }
+
+ if (bytes_read == -1 && ferror(fp))
+ err(1, "Failed to read %s", INDEX);
}
struct hashed_file {
@@ -208,7 +223,7 @@ int compute_hash(const char * path, const struct stat *st,
unsigned char hash[HASH_LEN];
const char *filename = path + ftwbuf->base;
- if (ignore(filename))
+ if (ignore(path))
return 0;
mode = st->st_mode;
@@ -228,7 +243,7 @@ int compute_hash(const char * path, const struct stat *st,
return 0;
}
-static inline void update_index(void)
+static inline void scan_wtree(void)
{
if (nftw(".", compute_hash, MAX_DEPTH, FTW_DEPTH | FTW_PHYS) == -1)
err(1, "nftw");
@@ -236,6 +251,8 @@ static inline void update_index(void)
static inline int ignore(const char *path)
{
- return strcmp(path, CVN_DIR) == 0;
+ return memcmp(path, "./.cvn", 6) == 0 ||
+ strcmp(path, "./cvn") == 0 ||
+ strcmp(path, ".") == 0;
}