session.c (2490B)
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 <stdio.h> /* asprintf(3) */ 18 #include <stdlib.h> /* EXIT_* */ 19 #include <limits.h> /* PATH_MAX */ 20 #include <unistd.h> /* getcwd(3) */ 21 #include <err.h> /* errx(3) */ 22 23 #include "defaults.h" 24 #include "config.h" 25 #include "session.h" 26 27 extern int objdb_baseline_get_ops(struct objdb_ops **); 28 extern int dircache_simple_get_ops(struct dircache_ops **); 29 30 int 31 baseline_session_begin(struct session *s, u_int8_t options) 32 { 33 char *config = NULL; 34 35 objdb_baseline_get_ops(&(s->db_ops)); 36 dircache_simple_get_ops(&(s->dc_ops)); 37 38 if (getcwd((char *)s->cwd, sizeof(s->cwd)) == NULL) 39 errx(EXIT_FAILURE, "error, can not read current directory."); 40 41 if (options & SESSION_NONINIT) { 42 return EXIT_SUCCESS; 43 } 44 45 s->repo_rootdir = baseline_repo_get_rootdir(); 46 s->repo_baselinedir = baseline_repo_get_baselinedir(); 47 48 if (s->repo_rootdir == NULL) 49 errx(EXIT_FAILURE, "error, no repository was found."); 50 if (s->db_ops->open(&(s->db_ctx), BASELINE_DB, s->repo_baselinedir) == EXIT_FAILURE) 51 return EXIT_FAILURE; 52 if (s->dc_ops->open(&(s->dc_ctx), s->db_ctx, s->db_ops, s->repo_rootdir, s->repo_baselinedir) == EXIT_FAILURE) 53 return EXIT_FAILURE; 54 55 /* load configurations */ 56 asprintf(&config, "%s/%s", s->repo_baselinedir, BASELINE_CONFIGFILE); 57 if (baseline_config_load(config) == EXIT_FAILURE) 58 errx(EXIT_FAILURE, "failed to load configurations."); 59 free(config); 60 61 /* get current branch */ 62 s->dc_ops->branch_get(s->dc_ctx, (char **)&(s->branch)); 63 64 return EXIT_SUCCESS; 65 } 66 67 int 68 baseline_session_end(struct session *s) 69 { 70 if (s->db_ops->close(s->db_ctx) == EXIT_FAILURE) 71 return EXIT_FAILURE; 72 if (s->dc_ops->close(s->dc_ctx) == EXIT_FAILURE) 73 return EXIT_FAILURE; 74 return EXIT_SUCCESS; 75 } 76