[PATCH v7 0/3]
v7 cover [PATCH v7 0/3]
This version of the patch adds improved error outputs for various weird situations that can come up during symlink resolution. It also improves the documentation, following suggestions by Junio Hamano.
PATCH v7 1/3 tree-walk: learn get_tree_entry_follow_symlinks
From: David Turner <[email protected]>
Add a new function, get_tree_entry_follow_symlinks, to tree-walk.[ch]. The function is not yet used. It will be used to implement git cat-file --batch --follow-symlinks.
The function locates an object by path, following symlinks in the repository. If the symlinks lead outside the repository, the function reports this to the caller.
--- tree-walk.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tree-walk.h | 18 ++++++ 2 files changed, 224 insertions(+)
tree-walk.c +206 -0
@@ -415,6 +415,12 @@ int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)return error; } +struct dir_state { +void *tree; +unsigned long size; +unsigned char sha1[20 ]; +}; +static int find_tree_entry (struct tree_desc *t,const char *name,unsigned char *result,unsigned *mode) {int namelen =strlen (name);
@@ -478,6 +484,206 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned chreturn retval; } +/* +* This is Linux's built-in max for the number of symlinks to follow. +* That limit, of course, does not affect git, but it's a reasonable +* choice. +*/ +#define GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS 40 + +/** +* Find a tree entry by following symlinks in tree_sha (which is +* assumed to be the root of the repository). In the event that a +* symlink points outside the repository (e.g. a link to /foo or a +* root-level link to ../foo), the portion of the link which is +* outside the repository will be returned in result_path, and *mode +* will be set to 0. It is assumed that result_path is uninitialized. +* If there are no symlinks, or the end result of the symlink chain +* points to an object inside the repository, result will be filled in +* with the sha1 of the found object, and *mode will hold the mode of +* the object. +* +* See the code for enum follow_symlink_result for a description of +* the return values. +*/ +enum follow_symlinks_result get_tree_entry_follow_symlinks (unsigned char *tree_sha1,const char *name,unsigned char *result,struct strbuf *result_path,unsigned *mode) +{ +int retval =MISSING_OBJECT ; +struct dir_state *parents =NULL ; +size_t parents_alloc =0 ; +ssize_t parents_nr =0 ; +unsigned char current_tree_sha1[20 ]; +struct strbuf namebuf =STRBUF_INIT ; +struct tree_desc t = {0 }; +int follows_remaining =GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS ; +int i; + + result_path->buf =0 ; + result_path->alloc =0 ; + result_path->len =0 ; +strbuf_addstr (&namebuf, name); +hashcpy (current_tree_sha1, tree_sha1); + +while (1 ) { +int find_result; +char *first_slash; +char *remainder =NULL ; + +if (!t.buffer) { +void *tree; +unsigned char root[20 ]; +unsigned long size; + tree =read_object_with_reference (current_tree_sha1, + tree_type, &size, + root); +if (!tree) + goto done; + +ALLOC_GROW (parents, parents_nr +1 , parents_alloc); + parents[parents_nr].tree = tree; + parents[parents_nr].size = size; +hashcpy (parents[parents_nr].sha1, root); + parents_nr++; + +if (namebuf.buf[0 ] =='\0' ) { +hashcpy (result, root); + retval =FOUND ; + goto done; + } + +if (!size) + goto done; + +/* descend */ +init_tree_desc (&t, tree, size); + } + +/* Handle symlinks to e.g. a//b by removing leading slashes */ +while (namebuf.buf[0 ] =='/' ) { +strbuf_remove (&namebuf,0 ,1 ); + } + +/* Split namebuf into a first component and a remainder */ +if ((first_slash =strchr (namebuf.buf,'/' ))) { + *first_slash =0 ; + remainder = first_slash +1 ; + } + +if (!strcmp (namebuf.buf,".." )) { +struct dir_state *parent; +/* +* We could end up with .. in the namebuf if it +* appears in a symlink. +*/ + +if (parents_nr ==1 ) { +if (remainder) + *first_slash ='/' ; +strbuf_add (result_path, namebuf.buf, + namebuf.len); + *mode =0 ; + retval =FOUND ; + goto done; + } + parent = &parents[parents_nr -1 ]; +free (parent->tree); + parents_nr--; + parent = &parents[parents_nr -1 ]; +init_tree_desc (&t, parent->tree, parent->size); +strbuf_remove (&namebuf,0 , remainder ?3 :2 ); +continue ; + } + +/* We could end up here via a symlink to dir/.. */ +if (namebuf.buf[0 ] =='\0' ) { +hashcpy (result, parents[parents_nr -1 ].sha1); + retval =FOUND ; + goto done; + } + +/* Look up the first (or only) path component in the tree. */ + find_result =find_tree_entry (&t, namebuf.buf, + current_tree_sha1, mode); +if (find_result) { + goto done; + } + +if (S_ISDIR (*mode)) { +if (!remainder) { +hashcpy (result, current_tree_sha1); + retval =FOUND ; + goto done; + } +/* Descend the tree */ + t.buffer =NULL ; +strbuf_remove (&namebuf,0 , +1 + first_slash - namebuf.buf); + }else if (S_ISREG (*mode)) { +if (!remainder) { +hashcpy (result, current_tree_sha1); + retval =FOUND ; + }else { + retval =NOT_DIR ; + } + goto done; + }else if (S_ISLNK (*mode)) { +/* Follow a symlink */ +unsigned long link_len; +size_t len; +char *contents, *contents_start; +struct dir_state *parent; +enum object_type type; + +if (follows_remaining-- ==0 ) { +/* Too many symlinks followed */ + retval =SYMLINK_LOOP ; + goto done; + } + +/* +* At this point, we have followed at a least +* one symlink, so on error we need to report this. +*/ + retval =DANGLING_SYMLINK ; + + contents =read_sha1_file (current_tree_sha1, &type, + &link_len); + +if (!contents) + goto done; + +if (contents[0 ] =='/' ) { +strbuf_addstr (result_path, contents); + *mode =0 ; + retval =FOUND ; + goto done; + } + +if (remainder) + len = first_slash - namebuf.buf; +else + len = namebuf.len; + + contents_start = contents; + + parent = &parents[parents_nr -1 ]; +init_tree_desc (&t, parent->tree, parent->size); +strbuf_splice (&namebuf,0 , len, + contents_start, link_len); +if (remainder) + namebuf.buf[link_len] ='/' ; +free (contents); + } + } +done: +for (i =0 ; i < parents_nr; i++) +free (parents[i].tree); +free (parents); + +strbuf_release (&namebuf); +return retval; +} + static int match_entry(const struct pathspec_item *item, const struct name_entry *entry, int pathlen, const char *match, int matchlen,
tree-walk.h +18 -0
@@ -40,6 +40,24 @@ struct traverse_info;typedef int (*traverse_callback_t )(int n,unsigned long mask,unsigned long dirmask,struct name_entry *entry,struct traverse_info *);int traverse_trees (int n,struct tree_desc *t,struct traverse_info *info); +enum follow_symlinks_result { +FOUND =0 ,/* This includes out-of-tree links */ +MISSING_OBJECT =-1 ,/* The initial symlink is missing */ +DANGLING_SYMLINK =-2 ,/* +* The initial symlink is there, but +* (transitively) points to a missing +* in-tree file +*/ +SYMLINK_LOOP =-3 , +NOT_DIR =-4 ,/* +* Somewhere along the symlink chain, a path is +* requested which contains a file as a +* non-final element. +*/ +}; + +enum follow_symlinks_result get_tree_entry_follow_symlinks (unsigned char *tree_sha1,const char *name,unsigned char *result,struct strbuf *result_path,unsigned *mode); +struct traverse_info {struct traverse_info *prev;struct name_entry name;
Signature · 1 line
2.0.4.315.gad8727a-twtrsrc
PATCH v7 2/3 sha1_name: get_sha1_with_context learns to follow symlinks
In reply to [email protected]From: David Turner <[email protected]>
Wire up get_sha1_with_context to call get_tree_entry_follow_symlinks when GET_SHA1_FOLLOW_SYMLINKS is passed in flags. G_S_FOLLOW_SYMLINKS is incompatible with G_S_ONLY_TO_DIE because the diagnosis that ONLY_TO_DIE triggers does not at present consider symlinks, and it would be a significant amount of additional code to allow it to do so.
--- cache.h | 20 +++++++++++++------- sha1_name.c | 20 +++++++++++++++----- 2 files changed, 28 insertions(+), 12 deletions(-)
cache.h +13 -7
@@ -922,15 +922,21 @@ struct object_context {unsigned char tree[20 ];char path[PATH_MAX ];unsigned mode; +/* +* symlink_path is only used by get_tree_entry_follow_symlinks, +* and only for symlinks that point outside the repository. +*/ +struct strbuf symlink_path; }; -#define GET_SHA1_QUIETLY 01 -#define GET_SHA1_COMMIT 02 -#define GET_SHA1_COMMITTISH 04 -#define GET_SHA1_TREE 010 -#define GET_SHA1_TREEISH 020 -#define GET_SHA1_BLOB 040 -#define GET_SHA1_ONLY_TO_DIE 04000 +#define GET_SHA1_QUIETLY 01 +#define GET_SHA1_COMMIT 02 +#define GET_SHA1_COMMITTISH 04 +#define GET_SHA1_TREE 010 +#define GET_SHA1_TREEISH 020 +#define GET_SHA1_BLOB 040 +#define GET_SHA1_FOLLOW_SYMLINKS 0100 +#define GET_SHA1_ONLY_TO_DIE 04000extern int get_sha1 (const char *str,unsigned char *sha1);extern int get_sha1_commit (const char *str,unsigned char *sha1);
sha1_name.c +15 -5
@@ -1434,11 +1434,19 @@ static int get_sha1_with_context_1(const char *name, new_filename =resolve_relative_path (filename);if (new_filename) filename = new_filename; - ret =get_tree_entry (tree_sha1, filename, sha1, &oc->mode); -if (ret && only_to_die) { -diagnose_invalid_sha1_path (prefix, filename, - tree_sha1, - name, len); +if (flags &GET_SHA1_FOLLOW_SYMLINKS ) { + ret =get_tree_entry_follow_symlinks (tree_sha1, + filename, sha1, &oc->symlink_path, + &oc->mode); + }else { + ret =get_tree_entry (tree_sha1, filename, + sha1, &oc->mode); +if (ret && only_to_die) { +diagnose_invalid_sha1_path (prefix, + filename, + tree_sha1, + name, len); + } }hashcpy (oc->tree, tree_sha1);strlcpy (oc->path, filename,sizeof (oc->path));
@@ -1469,5 +1477,7 @@ void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)int get_sha1_with_context (const char *str,unsigned flags,unsigned char *sha1,struct object_context *orc) { +if (flags &GET_SHA1_FOLLOW_SYMLINKS && flags &GET_SHA1_ONLY_TO_DIE ) +die ("BUG: incompatible flags for get_sha1_with_context" );return get_sha1_with_context_1 (str, flags,NULL , sha1, orc); }
Signature · 1 line
2.0.4.315.gad8727a-twtrsrc
PATCH v7 3/3 cat-file: add --follow-symlinks to --batch
In reply to [email protected]From: David Turner <[email protected]>
This wires the in-repo-symlink following code through to the cat-file builtin. In the event of an out-of-repo link, cat-file will print the link in a new format.
--- Documentation/git-cat-file.txt | 98 +++++++++++++++++++- builtin/cat-file.c | 46 ++++++++- t/t1006-cat-file.sh | 205 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 343 insertions(+), 6 deletions(-)
Documentation/git-cat-file.txt +97 -1
@@ -10,7 +10,7 @@ SYNOPSIS -------- [verse] 'git cat-file' (-t | -s | -e | -p | <type> | --textconv ) <object> -'git cat-file' (--batch | --batch-check) < <list-of-objects> +'git cat-file' (--batch | --batch-check) [--follow-symlinks] < <list-of-objects> DESCRIPTION -----------
@@ -69,6 +69,61 @@ OPTIONS not be combined with any other options or arguments. See the section `BATCH OUTPUT` below for details. +--follow-symlinks:: + With --batch or --batch-check, follow symlinks inside the + repository when requesting objects with extended SHA-1 + expressions of the form tree-ish:path-in-tree. Instead of + providing output about the link itself, provide output about + the linked-to object. If a symlink points outside the + tree-ish (e.g. a link to /foo or a root-level link to ../foo), + the portion of the link which is outside the tree will be + printed. +[normal] + This option does not (currently) work correctly when an + object in the index is specified (e.g. `:link` instead of + `HEAD:link`) rather than one in the tree. +[normal] + This option cannot be used unless `--batch` or `--batch-check` + is used, because it would be impossible to distinguish between + the output for an out-of-repo symlink, and the contents of a + blob. + +For example, consider a git repository containing: + +-- + f: a file containing "hello\n" + link: a symlink to f + dir/link: a symlink to ../f + plink: a symlink to ../f + alink: a symlink to /etc/passwd +-- +For a regular file `f`, `echo HEAD:f | git cat-file --batch` would print +-- + ce013625030ba8dba906f756967f9e9ca394464a blob 6 +-- + +And `echo HEAD:link | git cat-file --batch --follow-symlinks` would print +the same thing, as would `HEAD:dir/link`, as they both point at `HEAD:f`. + +Without `--follow-symlinks`, these would print data about the +symlink itself. In the case of `HEAD:link`, you would see + +-- + 4d1ae35ba2c8ec712fa2a379db44ad639ca277bd blob 1 +-- + +Both `plink` and `alink` point outside the tree, so they would +respectively print: + +-- + symlink 4 + ../f + + symlink 11 + /etc/passwd +-- + + OUTPUT ------ If '-t' is specified, one of the <type>.
@@ -148,6 +203,47 @@ the repository, then `cat-file` will ignore any custom format and print: <object> SP missing LF ------------ +If --follow-symlinks is used, and a symlink in the repository points +outside the repository, then `cat-file` will ignore any custom format +and print: + +------------ +symlink SP <size> LF +<symlink> LF +------------ + +The symlink will either be absolute (beginning with a /), or relative +to the tree root. For instance, if dir/link points to ../../foo, then +<symlink> will be ../foo. <size> is the size of the symlink in bytes. + +If --follow-symlinks is used, the following error messages will be +displayed: + +------------ +<object> SP missing LF +------------ +is printed when the initial symlink requested does not exist. + +------------ +dangling SP <size> LF +<object> LF +------------ +is printed when the initial symlink exists, but something that +it (transitive-of) points to does not. + +------------ +loop SP <size> LF +<object> LF +------------ +is printed for symlink loops (or any symlinks that +require more than 40 link resolutions to resolve). + +------------ +notdir SP <size> LF +<object> LF +------------ +is printed when, during symlink resolution, a file is used as a +directory name. CAVEATS -------
builtin/cat-file.c +41 -5
86 lines not on this page · raw message
t/t1006-cat-file.sh +205 -0
215 lines not on this page · raw message
Signature · 1 line
2.0.4.315.gad8727a-twtrsrc
Re: [PATCH v7 3/3] cat-file: add --follow-symlinks to --batch
Quoted 8 lines · [email protected] writes:
From: David Turner <[email protected]>
This wires the in-repo-symlink following code through to the cat-file builtin. In the event of an out-of-repo link, cat-file will print the link in a new format.
Signed-off-by David Turner <[email protected]>---
Thanks.
Documentation/git-cat-file.txt OPTIONS · line 82
Quoted 7 lines
+--follow-symlinks::
Documentation/git-cat-file.txt +6
-0+ With --batch or --batch-check, follow symlinks inside the + repository when requesting objects with extended SHA-1 +... + printed. +[normal] + This option does not (currently) work correctly when an
Nice; didn't realize that "[normal]" trick would work, but why not.
Documentation/git-cat-file.txt OPTIONS · line 91
Quoted 9 lines
Documentation/git-cat-file.txt +9
-0+ object in the index is specified (e.g. `:link` instead of + `HEAD:link`) rather than one in the tree. +[normal] + This option cannot be used unless `--batch` or `--batch-check` + is used, because it would be impossible to distinguish between + the output for an out-of-repo symlink, and the contents of a + blob. + +For example, consider a git repository containing:
... but I am not sure about this paragraph. Does it align and look to be at the same level as the first paragraph that begins with "With --batch or --batch-check" (asking because I do not have an access to a good environment to check the resulting HTML easily right now)?
builtin/cat-file.c static int batch_one_object(const char *obj_name, struct batch_options *opt, · line 246
Quoted 3 lines
builtin/cat-file.c +3
-0+ result =get_sha1_with_context (obj_name, flags, data->sha1, &ctx); +if (result !=FOUND ) { +switch (result) {
switch (result) {Quoted 5 lines
builtin/cat-file.c +5
-0+case MISSING_OBJECT : +printf ("%s missing\n" , obj_name); +break ; +case DANGLING_SYMLINK : + printf("dangling %"PRIuMAX"\n%s\n", strlen(obj_name),
builtin/cat-file.c static int batch_one_object(const char *obj_name, struct batch_options *opt, · line 261
Quoted 10 lines
builtin/cat-file.c +10
-0+ obj_name); +break ; +case SYMLINK_LOOP : +printf ("loop %" PRIuMAX"\n%s\n" ,strlen (obj_name), + obj_name); +break ; +case NOT_DIR : +printf ("notdir %" PRIuMAX"\n%s\n" ,strlen (obj_name), + obj_name); +break ;
Don't all these lengths need to be cast to uintmax_t, like the "symlink" one below?
By the way, I noticed that my compiler is so stupid that it does not realize this "switch (result)" is inside "if (result != FOUND)" and missing "case FOUND:" is perfectly fine. The remainder of the code I have been compiling with -Wswitch, but this one makes me to drop it.
Quoted 11 lines
builtin/cat-file.c +9
-0+ } +fflush (stdout); +return 0 ; + } + +if (ctx.mode ==0 ) { +printf ("symlink %" PRIuMAX"\n%s\n" , + (uintmax_t )ctx.symlink_path.len, + ctx.symlink_path.buf);fflush (stdout);return 0 ;
Quoted 2 lines · [email protected] writes:
This version of the patch adds improved error outputs for various weird situations that can come up during symlink resolution.
Overall these looked sensible. I have been resisting the temptation to merge this to my tree since around v5, primarily because I did not want to resolve merge conflicts with kn/cat-file-literally more than once, but I think this version looks solid enough that whatever rerere will remember will be applicable to later iteration if one is ever needed.
Hence I queued it on 'pu' for now; I haven't checked how the doc formats, though.
Thanks.
Re: [PATCH v7 1/3] tree-walk: learn get_tree_entry_follow_symlinks
In reply to [email protected]tree-walk.c int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch · line 650
Quoted 8 lines · On Tue, May 12, 2015 at 09:49:57PM -0400, [email protected] wrote:
tree-walk.c +7
-0+ }else if (S_ISLNK (*mode)) { +/* Follow a symlink */ +unsigned long link_len; +size_t len; +char *contents, *contents_start; + contents =read_sha1_file (current_tree_sha1, &type, + &link_len);
Here we have potentially allocated a buffer for contents...
tree-walk.c int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch · line 653
Quoted 3 lines
tree-walk.c +3
-0+ +if (!contents) + goto done;
No need to free here, it's NULL...
tree-walk.c int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch · line 660
Quoted 7 lines
tree-walk.c +7
-0+ +if (contents[0 ] =='/' ) { +strbuf_addstr (result_path, contents); + *mode =0 ; + retval =FOUND ; + goto done; + }
But here we leak it on "goto done".
Quoted 5 lines
tree-walk.c +5
-0+if (remainder) + len = first_slash - namebuf.buf; +else + len = namebuf.len; +
tree-walk.c int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch · line 676
Quoted 10 lines
tree-walk.c +10
-0+ contents_start = contents; + + parent = &parents[parents_nr -1 ]; +init_tree_desc (&t, parent->tree, parent->size); +strbuf_splice (&namebuf,0 , len, + contents_start, link_len); +if (remainder) + namebuf.buf[link_len] ='/' ; +free (contents); + }
And this code path calls free(), so it is good.
So I think you just need a free() in the conditional above.
-Peff