Commit deeca23839609f94e076f97f16d1d7cde09daf3b

Stefan Sperling 2018-03-12T20:41:51

in is_git_repo(), actually verify presence of git repository dirs and files

diff --git a/lib/repository.c b/lib/repository.c
index 630a646..c13711c 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -15,6 +15,7 @@
  */
 
 #include <sys/queue.h>
+#include <sys/stat.h>
 
 #include <limits.h>
 #include <stdlib.h>
@@ -110,11 +111,30 @@ is_git_repo(struct got_repository *repo)
 	char *path_objects = got_repo_get_path_objects(repo);
 	char *path_refs = got_repo_get_path_refs(repo);
 	char *path_head = get_path_head(repo);
-	int ret;
+	int ret = 0;
+	struct stat sb;
 
-	ret = (path_git != NULL) && (path_objects != NULL) &&
-	    (path_refs != NULL) && (path_head != NULL);
+	if (lstat(path_git, &sb) == -1)
+		goto done;
+	if (!S_ISDIR(sb.st_mode))
+		goto done;
+
+	if (lstat(path_objects, &sb) == -1)
+		goto done;
+	if (!S_ISDIR(sb.st_mode))
+		goto done;
 
+	if (lstat(path_refs, &sb) == -1)
+		goto done;
+	if (!S_ISDIR(sb.st_mode))
+		goto done;
+
+	if (lstat(path_head, &sb) == -1)
+		goto done;
+	if (!S_ISREG(sb.st_mode))
+		goto done;
+	ret = 1;
+done:
 	free(path_git);
 	free(path_objects);
 	free(path_refs);