tar-tree: Use the prefix field of a tar header
... to store parts of the path, if possible. This allows us to avoid writing extended headers in certain cases (long pathes can only be split at '/' chars). Also adds a file to the test repo with a 100 chars long directory name. Even old versions of tar that don't understand POSIX extended headers should be able to handle this testcase. Btw.: The longest path in the kernel tree currently has 70 chars. Together with a 30 chars long prefix this would already cross the field limit of 100 chars. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <junkio@cox.net>
Rene Scharfe committed
Mar 25, 2006 at 23:21 UTC
4c691724f175573a2dc4118782744cb0e852ab41
2 files changed
+24
-3
t/t5000-tar-tree.sh
+3
@@ -34,6 +34,9 @@ test_expect_success \
34
mkdir a/bin &&
35
cp /bin/sh a/bin &&
36
ln -s a a/l1 &&
37
+ (p=long_path_to_a_file && cd a &&
38
+ for depth in 1 2 3 4 5; do mkdir $p && cd $p; done &&
39
+ echo text >file_with_long_path) &&
40
(cd a && find .) | sort >a.lst'
41
42
test_expect_success \
tar-tree.c
+21
-3
@@ -161,6 +161,16 @@ static unsigned int ustar_header_chksum(const struct ustar_header *header)
161
return chksum;
162
}
163
164
+static int get_path_prefix(const struct strbuf *path, int maxlen)
165
+{
166
+ int i = path->len;
167
+ if (i > maxlen)
168
+ i = maxlen;
169
+ while (i > 0 && path->buf[i] != '/')
170
+ i--;
171
+ return i;
172
+}
173
+
174
static void write_entry(const unsigned char *sha1, struct strbuf *path,
175
unsigned int mode, void *buffer, unsigned long size)
176
{
@@ -195,9 +205,17 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
205
return;
206
}
207
if (path->len > sizeof(header.name)) {
198
- sprintf(header.name, "%s.data", sha1_to_hex(sha1));
199
- strbuf_append_ext_header(&ext_header, "path",
200
- path->buf, path->len);
208
+ int plen = get_path_prefix(path, sizeof(header.prefix));
209
+ int rest = path->len - plen - 1;
210
+ if (plen > 0 && rest <= sizeof(header.name)) {
211
+ memcpy(header.prefix, path->buf, plen);
212
+ memcpy(header.name, path->buf + plen + 1, rest);
213
+ } else {
214
+ sprintf(header.name, "%s.data",
215
+ sha1_to_hex(sha1));
216
+ strbuf_append_ext_header(&ext_header, "path",
217
+ path->buf, path->len);
218
+ }
219
} else
220
memcpy(header.name, path->buf, path->len);
221
}