Skip to content

Commit 6cad805

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5025d84 commit 6cad805

File tree

1 file changed

+22
-35
lines changed

1 file changed

+22
-35
lines changed

lockfile.c

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -109,58 +109,47 @@ static char *last_path_elm(char *p)
109109
#define MAXDEPTH 5
110110

111111
/*
112-
* p = path that may be a symlink
113-
* s = full size of p
112+
* path contains a path that might be a symlink.
114113
*
115-
* If p is a symlink, attempt to overwrite p with a path to the real
116-
* file or directory (which may or may not exist), following a chain of
117-
* symlinks if necessary. Otherwise, leave p unmodified.
114+
* If path is a symlink, attempt to overwrite it with a path to the
115+
* real file or directory (which may or may not exist), following a
116+
* chain of symlinks if necessary. Otherwise, leave path unmodified.
118117
*
119-
* This is a best-effort routine. If an error occurs, p will either be
120-
* left unmodified or will name a different symlink in a symlink chain
121-
* that started with p's initial contents.
122-
*
123-
* Always returns p.
118+
* This is a best-effort routine. If an error occurs, path will
119+
* either be left unmodified or will name a different symlink in a
120+
* symlink chain that started with the original path.
124121
*/
125-
126-
static char *resolve_symlink(char *p, size_t s)
122+
static void resolve_symlink(struct strbuf *path)
127123
{
128124
int depth = MAXDEPTH;
129125
static struct strbuf link = STRBUF_INIT;
130126

131127
while (depth--) {
132-
if (strbuf_readlink(&link, p, strlen(p)) < 0)
128+
if (strbuf_readlink(&link, path->buf, path->len) < 0)
133129
break;
134130

135-
if (is_absolute_path(link.buf)) {
131+
if (is_absolute_path(link.buf))
136132
/* absolute path simply replaces p */
137-
if (link.len < s)
138-
strcpy(p, link.buf);
139-
else {
140-
warning("%s: symlink too long", p);
141-
break;
142-
}
143-
} else {
133+
strbuf_reset(path);
134+
else {
144135
/*
145136
* link is a relative path, so replace the
146137
* last element of p with it.
147138
*/
148-
char *r = (char *)last_path_elm(p);
149-
if (r - p + link.len < s)
150-
strcpy(r, link.buf);
151-
else {
152-
warning("%s: symlink too long", p);
153-
break;
154-
}
139+
char *r = last_path_elm(path->buf);
140+
strbuf_setlen(path, r - path->buf);
155141
}
142+
143+
strbuf_addbuf(path, &link);
156144
}
157145
strbuf_reset(&link);
158-
return p;
159146
}
160147

161148
/* Make sure errno contains a meaningful value on error */
162149
static int lock_file(struct lock_file *lk, const char *path, int flags)
163150
{
151+
size_t pathlen = strlen(path);
152+
164153
if (!lock_file_list) {
165154
/* One-time initialization */
166155
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)
175164
lk->fd = -1;
176165
lk->active = 0;
177166
lk->owner = 0;
178-
strbuf_init(&lk->filename, PATH_MAX);
167+
strbuf_init(&lk->filename, pathlen + LOCK_SUFFIX_LEN);
179168
lk->next = lock_file_list;
180169
lock_file_list = lk;
181170
lk->on_list = 1;
@@ -185,11 +174,9 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
185174
path);
186175
}
187176

188-
strbuf_addstr(&lk->filename, path);
189-
if (!(flags & LOCK_NODEREF)) {
190-
resolve_symlink(lk->filename.buf, lk->filename.alloc);
191-
strbuf_setlen(&lk->filename, strlen(lk->filename.buf));
192-
}
177+
strbuf_add(&lk->filename, path, pathlen);
178+
if (!(flags & LOCK_NODEREF))
179+
resolve_symlink(&lk->filename);
193180
strbuf_addstr(&lk->filename, LOCK_SUFFIX);
194181
lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
195182
if (lk->fd < 0) {

0 commit comments

Comments
 (0)