# [PATCH 1/1] bugfix for git-checkout-cache --prefix=/symlink/export_dir/ -a

4 messages from 2005-05-23 to 2005-05-24. Participants: David Greaves, Linus Torvalds, Junio C Hamano.
Thread: https://gitlist.dev/t/704

## David Greaves, 2005-05-23 08:49

Subject: [PATCH 1/1] bugfix for git-checkout-cache --prefix=/symlink/export_dir/ -a
Message-ID: <E1Da8d0-0002GR-4m@ash.dgreaves.com>
URL: https://gitlist.dev/e/E1Da8d0-0002GR-4m%40ash.dgreaves.com

```
If there's a prefix then allow symlinks to directories in it.
This fixes a bug where
  git-checkout-cache --prefix=/symlink/export_dir/ -a
otherwise fails.

Signed-off-by: David Greaves <david@dgreaves.com>

---
commit 18006ac69ae9db97b7d96fd0bfb1bdb6893e318d
tree 37580a3d32262d2cee7d28b20474c0f2ceb6faaa
parent 2aef5bbae99aeba3551408eae13faea02bf55b67
author David Greaves <david@dgreaves.com> Mon, 23 May 2005 09:42:04 +0100
committer David Greaves <david@dgreaves.com> Mon, 23 May 2005 09:42:04 +0100

 checkout-cache.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)

Index: checkout-cache.c
===================================================================
--- 99282828d5af15b0af0d0eac13a5b1194e88342c/checkout-cache.c  (mode:100644)
+++ 37580a3d32262d2cee7d28b20474c0f2ceb6faaa/checkout-cache.c  (mode:100644)
@@ -37,20 +37,25 @@
 #include "cache.h"
 
 static int force = 0, quiet = 0, not_new = 0, refresh_cache = 0;
+const char *base_dir = "";
 
 static void create_directories(const char *path)
 {
 	int len = strlen(path);
 	char *buf = xmalloc(len + 1);
 	const char *slash = path;
+	int baselen = strlen(base_dir);
 
 	while ((slash = strchr(slash+1, '/')) != NULL) {
+		struct stat st;
 		len = slash - path;
 		memcpy(buf, path, len);
 		buf[len] = 0;
+		if (slash - path <= baselen &&
+		    !stat(buf, &st) && S_ISDIR(st.st_mode))
+			continue; /* allow symlinks only in --prefix */
 		if (mkdir(buf, 0755)) {
 			if (errno == EEXIST) {
-				struct stat st;
 				if (!lstat(buf, &st) && S_ISDIR(st.st_mode))
 					continue; /* ok */
 				if (force && !unlink(buf) && !mkdir(buf, 0755))
@@ -229,7 +234,6 @@
 int main(int argc, char **argv)
 {
 	int i, force_filename = 0;
-	const char *base_dir = "";
 	struct cache_file cache_file;
 	int newfd = -1;
 

```

## Linus Torvalds, 2005-05-23 18:45

Subject: Re: [PATCH 1/1] bugfix for git-checkout-cache --prefix=/symlink/export_dir/ -a
Message-ID: <Pine.LNX.4.58.0505231145190.2307@ppc970.osdl.org>
URL: https://gitlist.dev/e/Pine.LNX.4.58.0505231145190.2307%40ppc970.osdl.org
In-Reply-To: <E1Da8d0-0002GR-4m@ash.dgreaves.com>

```


On Mon, 23 May 2005, David Greaves wrote:
>
> If there's a prefix then allow symlinks to directories in it.
> This fixes a bug where
>   git-checkout-cache --prefix=/symlink/export_dir/ -a
> otherwise fails.

Hmm.. Does this alternative work for you instead?

[ Totally untested, please check for sanity first!! ]

		Linus

---
diff --git a/checkout-cache.c b/checkout-cache.c
--- a/checkout-cache.c
+++ b/checkout-cache.c
@@ -37,6 +37,8 @@
 #include "cache.h"
 
 static int force = 0, quiet = 0, not_new = 0, refresh_cache = 0;
+static const char *base_dir = "";
+static int base_dir_len = 0;
 
 static void create_directories(const char *path)
 {
@@ -51,10 +53,10 @@ static void create_directories(const cha
 		if (mkdir(buf, 0755)) {
 			if (errno == EEXIST) {
 				struct stat st;
-				if (!lstat(buf, &st) && S_ISDIR(st.st_mode))
-					continue; /* ok */
-				if (force && !unlink(buf) && !mkdir(buf, 0755))
+				if (len > base_dir_len && force && !unlink(buf) && !mkdir(buf, 0755))
 					continue;
+				if (!stat(buf, &st) && S_ISDIR(st.st_mode))
+					continue; /* ok */
 			}
 			die("cannot create directory at %s", buf);
 		}
@@ -163,11 +165,11 @@ static int write_entry(struct cache_entr
 	return 0;
 }
 
-static int checkout_entry(struct cache_entry *ce, const char *base_dir)
+static int checkout_entry(struct cache_entry *ce)
 {
 	struct stat st;
 	static char path[MAXPATHLEN+1];
-	int len = strlen(base_dir);
+	int len = base_dir_len;
 
 	memcpy(path, base_dir, len);
 	strcpy(path + len, ce->name);
@@ -194,7 +196,7 @@ static int checkout_entry(struct cache_e
 	return write_entry(ce, path);
 }
 
-static int checkout_file(const char *name, const char *base_dir)
+static int checkout_file(const char *name)
 {
 	int pos = cache_name_pos(name, strlen(name));
 	if (pos < 0) {
@@ -209,10 +211,10 @@ static int checkout_file(const char *nam
 		}
 		return -1;
 	}
-	return checkout_entry(active_cache[pos], base_dir);
+	return checkout_entry(active_cache[pos]);
 }
 
-static int checkout_all(const char *base_dir)
+static int checkout_all(void)
 {
 	int i;
 
@@ -220,7 +222,7 @@ static int checkout_all(const char *base
 		struct cache_entry *ce = active_cache[i];
 		if (ce_stage(ce))
 			continue;
-		if (checkout_entry(ce, base_dir) < 0)
+		if (checkout_entry(ce) < 0)
 			return -1;
 	}
 	return 0;
@@ -229,7 +231,6 @@ static int checkout_all(const char *base
 int main(int argc, char **argv)
 {
 	int i, force_filename = 0;
-	const char *base_dir = "";
 	struct cache_file cache_file;
 	int newfd = -1;
 
@@ -241,7 +242,7 @@ int main(int argc, char **argv)
 		const char *arg = argv[i];
 		if (!force_filename) {
 			if (!strcmp(arg, "-a")) {
-				checkout_all(base_dir);
+				checkout_all();
 				continue;
 			}
 			if (!strcmp(arg, "--")) {
@@ -272,10 +273,11 @@ int main(int argc, char **argv)
 			}
 			if (!memcmp(arg, "--prefix=", 9)) {
 				base_dir = arg+9;
+				base_dir_len = strlen(base_dir);
 				continue;
 			}
 		}
-		if (base_dir[0]) {
+		if (base_dir_len) {
 			/* when --prefix is specified we do not
 			 * want to update cache.
 			 */
@@ -285,7 +287,7 @@ int main(int argc, char **argv)
 			}
 			refresh_cache = 0;
 		}
-		checkout_file(arg, base_dir);
+		checkout_file(arg);
 	}
 
 	if (0 <= newfd &&

```

## Linus Torvalds, 2005-05-23 19:09

Subject: Re: [PATCH 1/1] bugfix for git-checkout-cache --prefix=/symlink/export_dir/ -a
Message-ID: <Pine.LNX.4.58.0505231208460.2307@ppc970.osdl.org>
URL: https://gitlist.dev/e/Pine.LNX.4.58.0505231208460.2307%40ppc970.osdl.org
In-Reply-To: <Pine.LNX.4.58.0505231145190.2307@ppc970.osdl.org>

```


On Mon, 23 May 2005, Linus Torvalds wrote:
> > otherwise fails.
> 
> Hmm.. Does this alternative work for you instead?
> 
> [ Totally untested, please check for sanity first!! ]

Btw, I'm not going to apply this, and expect that David or somebody else 
can validate it and send it back to me as "tested".

		Linus

```

## Junio C Hamano, 2005-05-24 08:51

Subject: [PATCH] Allow symlinks in the leading path in checkout-cache --prefix=
Message-ID: <7vacmlvwfk.fsf_-_@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vacmlvwfk.fsf_-_%40assigned-by-dhcp.cox.net
In-Reply-To: <Pine.LNX.4.58.0505231208460.2307@ppc970.osdl.org>

```
This is what Linus wrote, improving what David Greaves
originally submitted.

    Hmm.. Does this alternative work for you instead?
    [ Totally untested, please check for sanity first!! ]
    Btw, I'm not going to apply this, and expect that David or somebody else 
    can validate it and send it back to me as "tested".

I just added a test case and verified the patch works.

Author: David Greaves <david@dgreaves.com>
Author: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
---

checkout-cache.c                |   28 ++++++-----
t/t2003-checkout-cache-mkdir.sh |   95 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 110 insertions(+), 13 deletions(-)
new file (100755): t/t2003-checkout-cache-mkdir.sh

diff --git a/checkout-cache.c b/checkout-cache.c
--- a/checkout-cache.c
+++ b/checkout-cache.c
@@ -37,6 +37,8 @@
 #include "cache.h"
 
 static int force = 0, quiet = 0, not_new = 0, refresh_cache = 0;
+static const char *base_dir = "";
+static int base_dir_len = 0;
 
 static void create_directories(const char *path)
 {
@@ -51,10 +53,10 @@ static void create_directories(const cha
 		if (mkdir(buf, 0755)) {
 			if (errno == EEXIST) {
 				struct stat st;
-				if (!lstat(buf, &st) && S_ISDIR(st.st_mode))
-					continue; /* ok */
-				if (force && !unlink(buf) && !mkdir(buf, 0755))
+				if (len > base_dir_len && force && !unlink(buf) && !mkdir(buf, 0755))
 					continue;
+				if (!stat(buf, &st) && S_ISDIR(st.st_mode))
+					continue; /* ok */
 			}
 			die("cannot create directory at %s", buf);
 		}
@@ -163,11 +165,11 @@ static int write_entry(struct cache_entr
 	return 0;
 }
 
-static int checkout_entry(struct cache_entry *ce, const char *base_dir)
+static int checkout_entry(struct cache_entry *ce)
 {
 	struct stat st;
 	static char path[MAXPATHLEN+1];
-	int len = strlen(base_dir);
+	int len = base_dir_len;
 
 	memcpy(path, base_dir, len);
 	strcpy(path + len, ce->name);
@@ -194,7 +196,7 @@ static int checkout_entry(struct cache_e
 	return write_entry(ce, path);
 }
 
-static int checkout_file(const char *name, const char *base_dir)
+static int checkout_file(const char *name)
 {
 	int pos = cache_name_pos(name, strlen(name));
 	if (pos < 0) {
@@ -209,10 +211,10 @@ static int checkout_file(const char *nam
 		}
 		return -1;
 	}
-	return checkout_entry(active_cache[pos], base_dir);
+	return checkout_entry(active_cache[pos]);
 }
 
-static int checkout_all(const char *base_dir)
+static int checkout_all(void)
 {
 	int i;
 
@@ -220,7 +222,7 @@ static int checkout_all(const char *base
 		struct cache_entry *ce = active_cache[i];
 		if (ce_stage(ce))
 			continue;
-		if (checkout_entry(ce, base_dir) < 0)
+		if (checkout_entry(ce) < 0)
 			return -1;
 	}
 	return 0;
@@ -229,7 +231,6 @@ static int checkout_all(const char *base
 int main(int argc, char **argv)
 {
 	int i, force_filename = 0;
-	const char *base_dir = "";
 	struct cache_file cache_file;
 	int newfd = -1;
 
@@ -241,7 +242,7 @@ int main(int argc, char **argv)
 		const char *arg = argv[i];
 		if (!force_filename) {
 			if (!strcmp(arg, "-a")) {
-				checkout_all(base_dir);
+				checkout_all();
 				continue;
 			}
 			if (!strcmp(arg, "--")) {
@@ -272,10 +273,11 @@ int main(int argc, char **argv)
 			}
 			if (!memcmp(arg, "--prefix=", 9)) {
 				base_dir = arg+9;
+				base_dir_len = strlen(base_dir);
 				continue;
 			}
 		}
-		if (base_dir[0]) {
+		if (base_dir_len) {
 			/* when --prefix is specified we do not
 			 * want to update cache.
 			 */
@@ -285,7 +287,7 @@ int main(int argc, char **argv)
 			}
 			refresh_cache = 0;
 		}
-		checkout_file(arg, base_dir);
+		checkout_file(arg);
 	}
 
 	if (0 <= newfd &&
diff --git a/t/t2003-checkout-cache-mkdir.sh b/t/t2003-checkout-cache-mkdir.sh
new file mode 100755
--- /dev/null
+++ b/t/t2003-checkout-cache-mkdir.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git-checkout-cache --prefix test.
+
+This test makes sure that --prefix option works as advertised, and
+also verifies that such leading path may contain symlinks, unlike
+the GIT controlled paths.
+'
+
+. ./test-lib.sh
+
+test_expect_success \
+    'setup' \
+    'mkdir path1 &&
+    echo frotz >path0 &&
+    echo rezrov >path1/file1 &&
+    git-update-cache --add path0 path1/file1'
+
+test_expect_success \
+    'have symlink in place where dir is expected.' \
+    'rm -fr path0 path1 &&
+     mkdir path2 &&
+     ln -s path2 path1 &&
+     git-checkout-cache -f -a &&
+     test ! -h path1 && test -d path1 &&
+     test -f path1/file1 && test ! -f path2/file1'
+
+test_expect_success \
+    'use --prefix=path2/' \
+    'rm -fr path0 path1 path2 &&
+     mkdir path2 &&
+     git-checkout-cache --prefix=path2/ -f -a &&
+     test -f path2/path0 &&
+     test -f path2/path1/file1 &&
+     test ! -f path0 &&
+     test ! -f path1/file1'
+
+test_expect_success \
+    'use --prefix=tmp-' \
+    'rm -fr path0 path1 path2 tmp* &&
+     git-checkout-cache --prefix=tmp- -f -a &&
+     test -f tmp-path0 &&
+     test -f tmp-path1/file1 &&
+     test ! -f path0 &&
+     test ! -f path1/file1'
+
+test_expect_success \
+    'use --prefix=tmp- but with a conflicting file and dir' \
+    'rm -fr path0 path1 path2 tmp* &&
+     echo nitfol >tmp-path1 &&
+     mkdir tmp-path0 &&
+     git-checkout-cache --prefix=tmp- -f -a &&
+     test -f tmp-path0 &&
+     test -f tmp-path1/file1 &&
+     test ! -f path0 &&
+     test ! -f path1/file1'
+
+# Linus fix #1
+test_expect_success \
+    'use --prefix=tmp/orary/ where tmp is a symlink' \
+    'rm -fr path0 path1 path2 tmp* &&
+     mkdir tmp1 tmp1/orary &&
+     ln -s tmp1 tmp &&
+     git-checkout-cache --prefix=tmp/orary/ -f -a &&
+     test -d tmp1/orary &&
+     test -f tmp1/orary/path0 &&
+     test -f tmp1/orary/path1/file1 &&
+     test -h tmp'
+
+# Linus fix #2
+test_expect_success \
+    'use --prefix=tmp/orary- where tmp is a symlink' \
+    'rm -fr path0 path1 path2 tmp* &&
+     mkdir tmp1 &&
+     ln -s tmp1 tmp &&
+     git-checkout-cache --prefix=tmp/orary- -f -a &&
+     test -f tmp1/orary-path0 &&
+     test -f tmp1/orary-path1/file1 &&
+     test -h tmp'
+
+# Linus fix #3
+test_expect_success \
+    'use --prefix=tmp- where tmp-path1 is a symlink' \
+    'rm -fr path0 path1 path2 tmp* &&
+     mkdir tmp1 &&
+     ln -s tmp1 tmp-path1 &&
+     git-checkout-cache --prefix=tmp- -f -a &&
+     test -f tmp-path0 &&
+     test ! -h tmp-path1 &&
+     test -d tmp-path1 &&
+     test -f tmp-path1/file1'
+
------------------------------------------------


```
