summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c42
1 files changed, 36 insertions, 6 deletions
diff --git a/main.c b/main.c
index ddf094e..d9c3c53 100644
--- a/main.c
+++ b/main.c
@@ -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);
}