Skip to content

Commit b9e125e

Browse files
Miklos Vajnagitster
authored andcommitted
builtin-clone: use strbuf in clone_local() and copy_or_link_directory()
Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 32716a2 commit b9e125e

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

builtin-clone.c

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -183,36 +183,38 @@ static void setup_reference(const char *repo)
183183
free(ref_git_copy);
184184
}
185185

186-
static void copy_or_link_directory(char *src, char *dest)
186+
static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest)
187187
{
188188
struct dirent *de;
189189
struct stat buf;
190190
int src_len, dest_len;
191191
DIR *dir;
192192

193-
dir = opendir(src);
193+
dir = opendir(src->buf);
194194
if (!dir)
195-
die("failed to open %s\n", src);
195+
die("failed to open %s\n", src->buf);
196196

197-
if (mkdir(dest, 0777)) {
197+
if (mkdir(dest->buf, 0777)) {
198198
if (errno != EEXIST)
199-
die("failed to create directory %s\n", dest);
200-
else if (stat(dest, &buf))
201-
die("failed to stat %s\n", dest);
199+
die("failed to create directory %s\n", dest->buf);
200+
else if (stat(dest->buf, &buf))
201+
die("failed to stat %s\n", dest->buf);
202202
else if (!S_ISDIR(buf.st_mode))
203-
die("%s exists and is not a directory\n", dest);
203+
die("%s exists and is not a directory\n", dest->buf);
204204
}
205205

206-
src_len = strlen(src);
207-
src[src_len] = '/';
208-
dest_len = strlen(dest);
209-
dest[dest_len] = '/';
206+
strbuf_addch(src, '/');
207+
src_len = src->len;
208+
strbuf_addch(dest, '/');
209+
dest_len = dest->len;
210210

211211
while ((de = readdir(dir)) != NULL) {
212-
strcpy(src + src_len + 1, de->d_name);
213-
strcpy(dest + dest_len + 1, de->d_name);
214-
if (stat(src, &buf)) {
215-
warning ("failed to stat %s\n", src);
212+
strbuf_setlen(src, src_len);
213+
strbuf_addstr(src, de->d_name);
214+
strbuf_setlen(dest, dest_len);
215+
strbuf_addstr(dest, de->d_name);
216+
if (stat(src->buf, &buf)) {
217+
warning ("failed to stat %s\n", src->buf);
216218
continue;
217219
}
218220
if (S_ISDIR(buf.st_mode)) {
@@ -221,17 +223,17 @@ static void copy_or_link_directory(char *src, char *dest)
221223
continue;
222224
}
223225

224-
if (unlink(dest) && errno != ENOENT)
225-
die("failed to unlink %s\n", dest);
226+
if (unlink(dest->buf) && errno != ENOENT)
227+
die("failed to unlink %s\n", dest->buf);
226228
if (!option_no_hardlinks) {
227-
if (!link(src, dest))
229+
if (!link(src->buf, dest->buf))
228230
continue;
229231
if (option_local)
230-
die("failed to create link %s\n", dest);
232+
die("failed to create link %s\n", dest->buf);
231233
option_no_hardlinks = 1;
232234
}
233-
if (copy_file(dest, src, 0666))
234-
die("failed to copy file to %s\n", dest);
235+
if (copy_file(dest->buf, src->buf, 0666))
236+
die("failed to copy file to %s\n", dest->buf);
235237
}
236238
closedir(dir);
237239
}
@@ -240,17 +242,19 @@ static const struct ref *clone_local(const char *src_repo,
240242
const char *dest_repo)
241243
{
242244
const struct ref *ret;
243-
char src[PATH_MAX];
244-
char dest[PATH_MAX];
245+
struct strbuf src = STRBUF_INIT;
246+
struct strbuf dest = STRBUF_INIT;
245247
struct remote *remote;
246248
struct transport *transport;
247249

248250
if (option_shared)
249251
add_to_alternates_file(src_repo);
250252
else {
251-
snprintf(src, PATH_MAX, "%s/objects", src_repo);
252-
snprintf(dest, PATH_MAX, "%s/objects", dest_repo);
253-
copy_or_link_directory(src, dest);
253+
strbuf_addf(&src, "%s/objects", src_repo);
254+
strbuf_addf(&dest, "%s/objects", dest_repo);
255+
copy_or_link_directory(&src, &dest);
256+
strbuf_release(&src);
257+
strbuf_release(&dest);
254258
}
255259

256260
remote = remote_get(src_repo);

0 commit comments

Comments
 (0)