diff options
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | main.c | 42 |
3 files changed, 40 insertions, 8 deletions
@@ -1,4 +1,6 @@ -svs +cvn +.cvn/ + **/*.o **/*.out **/*.swp @@ -1,5 +1,5 @@ CC = cc -TARGET = svs +TARGET = cvn SRC = main.c OBJ = $(SRC:.c=.o) @@ -1,8 +1,14 @@ +#include <errno.h> +#include <fcntl.h> #include <stdio.h> #include <string.h> #include <unistd.h> -#define DEFAULT_BRANCH_NAME "master" +#include <sys/stat.h> +#include <sys/types.h> + +#define REPO ".cvn" +#define INDEX "index" static inline void init(int argc, char *argv[]); @@ -35,22 +41,46 @@ int main(int argc, char *argv[]) return 0; } -void init(int argc, char *argv[]) +static inline void init(int argc, char *argv[]) { - int opt; char *branch; + int opt, repo_fd, idx_fd; optind = 1; - branch = DEFAULT_BRANCH_NAME; + branch = "master"; while ((opt = getopt(argc, argv, "b:")) != -1) { switch (opt) { case 'b': branch = optarg; break; - default: return; + default: + break; } } - printf("Initialized repository, branch: %s\n", branch); + + 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); } |
