baseline

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

cmd-add.c (2132B)


      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/stat.h>		/* stat(2) */
     18 #include <stdio.h>
     19 #include <stdlib.h>		/* EXIT_FAILURE */
     20 #include <string.h>		/* strstr(3) */
     21 #include <limits.h>		/* PATH_MAX */
     22 #include <err.h>		/* errx(3) */
     23 
     24 #include "session.h"
     25 #include "cmd.h"
     26 
     27 
     28 static char *
     29 xrealpath(const char *path)
     30 {
     31 	char abspath[PATH_MAX+1];
     32 	if (realpath(path, abspath) == NULL)
     33 		errx(EXIT_FAILURE, "error, failed to resolve path.");
     34 	return strdup(abspath);
     35 }
     36 
     37 /*
     38  * checks if first is subdir of second
     39  */
     40 static int
     41 is_subdir_of(const char *first, const char *second)
     42 {
     43 	if (first == NULL || second == NULL)
     44 		return 0;
     45 	if (strstr(first, second) == first)
     46 		return 1;
     47 	return 0;
     48 }
     49 
     50 int
     51 cmd_add(int argc, char **argv)
     52 {
     53 	char *path, *real;
     54 	struct session s;
     55 	struct stat st;
     56 
     57 	baseline_session_begin(&s, 0);
     58 
     59 	path = argv[1];
     60 	if (path == NULL)
     61 		errx(EXIT_FAILURE, "error, no path specified.");
     62 	/* all paths send to the backends must be non-relative paths */
     63 	real = xrealpath(path);
     64 	if (!is_subdir_of(real, s.repo_rootdir))
     65 		errx(EXIT_FAILURE, "error, can not add files from outside the repository.");
     66 	if (stat(path, &st) == -1)
     67 		errx(EXIT_FAILURE, "error, \'%s\' does not exist.", path);
     68 	if (s.dc_ops->insert(s.dc_ctx, real) == EXIT_FAILURE)
     69 		errx(EXIT_FAILURE, "failed to add \'%s\'.", path);
     70 
     71 	baseline_session_end(&s);
     72 	return EXIT_SUCCESS;
     73 }
     74