1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-06-10 00:36:35 +02:00

resolve_symlink(): take a strbuf parameter

Change resolve_symlink() to take a strbuf rather than a string as
parameter.  This simplifies the code and removes an arbitrary pathname
length restriction.  It also means that lock_file's filename field no
longer needs to be initialized to a large size.

Helped-by: Torsten Bögershausen <tboegi@web.de>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Michael Haggerty 2014-10-01 12:28:34 +02:00 committed by Junio C Hamano
parent 5025d8450a
commit 6cad805332

View File

@ -109,58 +109,47 @@ static char *last_path_elm(char *p)
#define MAXDEPTH 5 #define MAXDEPTH 5
/* /*
* p = path that may be a symlink * path contains a path that might be a symlink.
* s = full size of p
* *
* If p is a symlink, attempt to overwrite p with a path to the real * If path is a symlink, attempt to overwrite it with a path to the
* file or directory (which may or may not exist), following a chain of * real file or directory (which may or may not exist), following a
* symlinks if necessary. Otherwise, leave p unmodified. * chain of symlinks if necessary. Otherwise, leave path unmodified.
* *
* This is a best-effort routine. If an error occurs, p will either be * This is a best-effort routine. If an error occurs, path will
* left unmodified or will name a different symlink in a symlink chain * either be left unmodified or will name a different symlink in a
* that started with p's initial contents. * symlink chain that started with the original path.
*
* Always returns p.
*/ */
static void resolve_symlink(struct strbuf *path)
static char *resolve_symlink(char *p, size_t s)
{ {
int depth = MAXDEPTH; int depth = MAXDEPTH;
static struct strbuf link = STRBUF_INIT; static struct strbuf link = STRBUF_INIT;
while (depth--) { while (depth--) {
if (strbuf_readlink(&link, p, strlen(p)) < 0) if (strbuf_readlink(&link, path->buf, path->len) < 0)
break; break;
if (is_absolute_path(link.buf)) { if (is_absolute_path(link.buf))
/* absolute path simply replaces p */ /* absolute path simply replaces p */
if (link.len < s) strbuf_reset(path);
strcpy(p, link.buf);
else { else {
warning("%s: symlink too long", p);
break;
}
} else {
/* /*
* link is a relative path, so replace the * link is a relative path, so replace the
* last element of p with it. * last element of p with it.
*/ */
char *r = (char *)last_path_elm(p); char *r = last_path_elm(path->buf);
if (r - p + link.len < s) strbuf_setlen(path, r - path->buf);
strcpy(r, link.buf);
else {
warning("%s: symlink too long", p);
break;
}
} }
strbuf_addbuf(path, &link);
} }
strbuf_reset(&link); strbuf_reset(&link);
return p;
} }
/* Make sure errno contains a meaningful value on error */ /* Make sure errno contains a meaningful value on error */
static int lock_file(struct lock_file *lk, const char *path, int flags) static int lock_file(struct lock_file *lk, const char *path, int flags)
{ {
size_t pathlen = strlen(path);
if (!lock_file_list) { if (!lock_file_list) {
/* One-time initialization */ /* One-time initialization */
sigchain_push_common(remove_lock_file_on_signal); sigchain_push_common(remove_lock_file_on_signal);
@ -175,7 +164,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
lk->fd = -1; lk->fd = -1;
lk->active = 0; lk->active = 0;
lk->owner = 0; lk->owner = 0;
strbuf_init(&lk->filename, PATH_MAX); strbuf_init(&lk->filename, pathlen + LOCK_SUFFIX_LEN);
lk->next = lock_file_list; lk->next = lock_file_list;
lock_file_list = lk; lock_file_list = lk;
lk->on_list = 1; lk->on_list = 1;
@ -185,11 +174,9 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
path); path);
} }
strbuf_addstr(&lk->filename, path); strbuf_add(&lk->filename, path, pathlen);
if (!(flags & LOCK_NODEREF)) { if (!(flags & LOCK_NODEREF))
resolve_symlink(lk->filename.buf, lk->filename.alloc); resolve_symlink(&lk->filename);
strbuf_setlen(&lk->filename, strlen(lk->filename.buf));
}
strbuf_addstr(&lk->filename, LOCK_SUFFIX); strbuf_addstr(&lk->filename, LOCK_SUFFIX);
lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666); lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
if (lk->fd < 0) { if (lk->fd < 0) {