baseline

yet another open-source distributed versioning control system
Log | Files | Refs

cmd-init.c (2569B)


      1 /*
      2  * Copyright (c) 2014 Mohamed Aslan <maslan@sce.carleton.ca>
      3  *
      4  * Permission to use, copy, modify, and distribute this software for any
      5  * purpose with or without fee is hereby granted, provided that the above
      6  * copyright notice and this permission notice appear in all copies.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #include <sys/types.h>
     18 #include <sys/stat.h>
     19 #include <stdio.h>
     20 #include <stdlib.h> /* EXIT_FAILURE */
     21 #include <string.h> /* strstr(3) */
     22 #include <limits.h> /* PATH_MAX */
     23 #include <fcntl.h>  /* open(2) */
     24 #include <unistd.h> /* getcwd(3) */
     25 #include <err.h>    /* errx(3) */
     26 
     27 #include "defaults.h"
     28 #include "session.h"
     29 #include "config.h"
     30 #include "cmd.h"
     31 
     32 /*
     33  * creates a new repository in the given path.
     34  */
     35 static int
     36 init(struct session *s) {
     37 	char *baseline_path, *config_path;
     38 
     39 	/* create '.baseline' dir */
     40 	asprintf(&baseline_path, "%s/%s", s->cwd, BASELINE_DIR);
     41 	if (mkdir(baseline_path, S_IRUSR | S_IWUSR | S_IXUSR) == -1) {
     42 		return EXIT_FAILURE;
     43 	}
     44 	/* create '.baseline/config file */
     45 	asprintf(&config_path, "%s/config", baseline_path);
     46 	if (baseline_config_create(config_path) == EXIT_FAILURE)
     47 		return EXIT_FAILURE;
     48 	/* FIXME: error checks */
     49 
     50 	/* let objdb initialize it's structures too */
     51 	s->db_ops->open(&(s->db_ctx), BASELINE_DB, baseline_path);
     52 	s->db_ops->init(s->db_ctx);
     53 	/* as well as dircache */
     54 	s->dc_ops->open(&(s->dc_ctx), s->db_ctx, s->db_ops, s->cwd, baseline_path);
     55 	s->dc_ops->init(s->dc_ctx);
     56 
     57 	free(config_path);
     58 	free(baseline_path);
     59 	return EXIT_SUCCESS;
     60 }
     61 
     62 int
     63 cmd_init(int argc, char **argv)
     64 {
     65 	struct session s;
     66 
     67 	baseline_session_begin(&s, SESSION_NONINIT);
     68 	s.repo_rootdir = baseline_repo_get_rootdir();
     69 
     70 	if (s.repo_rootdir != NULL)
     71 		errx(EXIT_SUCCESS, "repository already initialized.");
     72 	if (exists(s.repo_baselinedir))
     73 		errx(EXIT_FAILURE, "error, corrupted repository already exists.");
     74 	if (init(&s) == EXIT_FAILURE) 
     75 		errx(EXIT_FAILURE, "error, could not initialize repository.");
     76 	printf("baseline: repository successfully initialized at \'%s\'.\n", s.cwd);
     77 
     78 	baseline_session_end(&s);
     79 	return EXIT_SUCCESS;
     80 }
     81