#include #include #include #include #include #include #include #define REPO ".cvn" #define INDEX "index" static inline void init(int argc, char *argv[]); struct command { char *name; void (*func)(int argc, char *argv[]); }; struct command cmd[] = { {"init", init}, {NULL, NULL} }; int main(int argc, char *argv[]) { uint8_t i; if (argc < 2) { fprintf(stderr, "Usage: %s []\n", argv[0]); return 1; } 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[]) { char *branch; int opt, repo_fd, idx_fd; optind = 1; branch = "master"; while ((opt = getopt(argc, argv, "b:")) != -1) { switch (opt) { case 'b': branch = optarg; break; default: break; } } if (mkdir(REPO, 0755) == -1) { if (errno != EEXIST) { perror("Failed to create repository"); return; } } if ((repo_fd = open(REPO, O_RDONLY | O_DIRECTORY)) == -1) { perror("Failed to open repository"); return; } if ((idx_fd = openat(repo_fd, INDEX, O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) { if (errno != EEXIST) { close(repo_fd); perror("Failed to create index"); return; } } close(idx_fd); close(repo_fd); printf("Initialized repository in %s\n", REPO); }