diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2026-03-14 22:27:33 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2026-03-14 22:27:33 +0800 |
| commit | 8006b2bdddf7f863cd65053be77b98935041047d (patch) | |
| tree | 074236b7cc910670a5cfe6c5c776916e50a67dde | |
| parent | c39a32d111e7cccb9ef2063d6723d91ba3755565 (diff) | |
| download | cvn-8006b2bdddf7f863cd65053be77b98935041047d.tar.gz | |
Use callback to process scan result.
| -rw-r--r-- | main.c | 44 |
1 files changed, 35 insertions, 9 deletions
@@ -23,7 +23,9 @@ static inline void init(int argc, char *argv[]); static inline void status(int argc, char *argv[]); +static inline void add(int argc, char *argv[]); +static inline void print_status(const char *file, char kind); static inline int cmp_links(const char *link1, const char *link2); static inline int cmp_content(const char *file1, const char *file2); static inline char *concat_path(const char *dir, const char *file); @@ -40,7 +42,8 @@ struct command { }; struct command cmd[] = { - {"init", init}, {"status", status}, {NULL, NULL} + {"init", init}, {"status", status}, {"add", add}, + {NULL, NULL} }; int main(int argc, char *argv[]) @@ -82,17 +85,28 @@ static inline void init(int argc, char *argv[]) printf("Repository ready\n"); } +static void (*scan_res_cb)(const char *file, char kind) = NULL; + static inline void status(int argc, char *argv[]) { + scan_res_cb = print_status; + if (nftw(HEAD, scan_head, MAX_DEPTH, FTW_PHYS) == -1) - err(1, "scan_head"); + err(1, "Failed to scan head"); if (nftw(".", scan_tree, MAX_DEPTH, FTW_PHYS) == -1) - err(1, "scan_tree"); + err(1, "Failed to scan work tree"); } -static char *path_buf = NULL; -static size_t path_buflen = 1024; +static inline void add(int argc, char *argv[]) +{ + char **paths; + + if (argc < 2) + errx(1, "Usage: %s [<files>]", argv[0]); + + paths = &argv[1]; +} int scan_head(const char * path, const struct stat *sb, int flag, struct FTW *ftwbuf) @@ -105,7 +119,7 @@ int scan_head(const char * path, const struct stat *sb, int flag, wt_path = concat_path(".", filename); if (lstat(wt_path, &wt_stat) == -1) { if (errno == ENOENT) { - printf("[D] %s\n", wt_path); + scan_res_cb(wt_path, 'D'); return 0; } else @@ -118,12 +132,13 @@ int scan_head(const char * path, const struct stat *sb, int flag, if (S_ISREG(wt_stat.st_mode)) { if (cmp_content(path, wt_path) == 1) - printf("[M] %s\n", wt_path); + scan_res_cb(wt_path, 'M'); } else if (S_ISLNK(wt_stat.st_mode)) { if (cmp_links(path, wt_path) == 1) - printf("[M] %s\n", wt_path); + scan_res_cb(wt_path, 'M'); } } + return 0; } @@ -139,8 +154,9 @@ static inline int scan_tree(const char * path, const struct stat *st, const char *filename = path + ftwbuf->base; hd_path = concat_path(HEAD, filename); if (access(hd_path, F_OK) != 0) - printf("[N] %s\n", path); + scan_res_cb(path, 'N'); } + return 0; } @@ -170,6 +186,7 @@ static inline int cmp_content(const char *file1, const char *file2) err(1, "diff error"); return rc; } + err(1, "fork"); } @@ -189,11 +206,20 @@ static inline int cmp_links(const char *link1, const char *link2) buf1[len1] = '\0'; buf2[len2] = '\0'; + return strcmp(buf1, buf2); } +static inline void print_status(const char *file, char kind) +{ + printf("[%c] %s\n", kind, file); +} + static inline char *concat_path(const char *dir, const char *file) { + static char *path_buf = NULL; + static size_t path_buflen = 1024; + size_t path_len; if (!path_buf) |
