]> git.proxmox.com Git - libgit2.git/blame - src/fileops.c
Merge pull request #844 from arrbee/init-extended
[libgit2.git] / src / fileops.c
CommitLineData
bb742ede 1/*
5e0de328 2 * Copyright (C) 2009-2012 the libgit2 contributors
bb742ede
VM
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
5ee2fe77 7#include "common.h"
ec250c6e 8#include "fileops.h"
2e29957a 9#include <ctype.h>
ec250c6e 10
ce8cd006 11int git_futils_mkpath2file(const char *file_path, const mode_t mode)
55ffebe3 12{
ca1b6e54
RB
13 return git_futils_mkdir(
14 file_path, NULL, mode, GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST);
55ffebe3
VM
15}
16
97769280 17int git_futils_mktmp(git_buf *path_out, const char *filename)
72a3fe42
VM
18{
19 int fd;
20
97769280
RB
21 git_buf_sets(path_out, filename);
22 git_buf_puts(path_out, "_git2_XXXXXX");
23
24 if (git_buf_oom(path_out))
1a481123 25 return -1;
72a3fe42 26
1a481123
VM
27 if ((fd = p_mkstemp(path_out->ptr)) < 0) {
28 giterr_set(GITERR_OS,
ae9e29fd 29 "Failed to create temporary file '%s'", path_out->ptr);
1a481123
VM
30 return -1;
31 }
72a3fe42 32
f978b748 33 return fd;
72a3fe42
VM
34}
35
ce8cd006 36int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode)
7dd8a9f7 37{
1a481123 38 int fd;
55ffebe3 39
1a481123
VM
40 if (git_futils_mkpath2file(path, dirmode) < 0)
41 return -1;
42
43 fd = p_creat(path, mode);
44 if (fd < 0) {
ae9e29fd 45 giterr_set(GITERR_OS, "Failed to create file '%s'", path);
1a481123
VM
46 return -1;
47 }
48
49 return fd;
55ffebe3
VM
50}
51
33127043 52int git_futils_creat_locked(const char *path, const mode_t mode)
1549cba9 53{
09719c50 54 int fd;
55
56#ifdef GIT_WIN32
57 wchar_t* buf;
58
59 buf = gitwin_to_utf16(path);
60 fd = _wopen(buf, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode);
61 git__free(buf);
62#else
63 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode);
64#endif
65
1a481123 66 if (fd < 0) {
ae9e29fd 67 giterr_set(GITERR_OS, "Failed to create locked file '%s'", path);
1a481123
VM
68 return -1;
69 }
70
71 return fd;
1549cba9
RG
72}
73
ce8cd006 74int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode)
1549cba9 75{
1a481123
VM
76 if (git_futils_mkpath2file(path, dirmode) < 0)
77 return -1;
1549cba9 78
f79026b4 79 return git_futils_creat_locked(path, mode);
ec250c6e
AE
80}
81
deafee7b
RB
82int git_futils_open_ro(const char *path)
83{
84 int fd = p_open(path, O_RDONLY);
85 if (fd < 0) {
d0a920a6 86 if (errno == ENOENT || errno == ENOTDIR)
deafee7b
RB
87 fd = GIT_ENOTFOUND;
88 giterr_set(GITERR_OS, "Failed to open '%s'", path);
89 }
90 return fd;
91}
92
f79026b4 93git_off_t git_futils_filesize(git_file fd)
ec250c6e 94{
7dd8a9f7 95 struct stat sb;
deafee7b
RB
96
97 if (p_fstat(fd, &sb)) {
98 giterr_set(GITERR_OS, "Failed to stat file descriptor");
99 return -1;
100 }
5ad739e8 101
ec250c6e
AE
102 return sb.st_size;
103}
4188d28f 104
b6c93aef
RB
105mode_t git_futils_canonical_mode(mode_t raw_mode)
106{
107 if (S_ISREG(raw_mode))
108 return S_IFREG | GIT_CANONICAL_PERMS(raw_mode);
109 else if (S_ISLNK(raw_mode))
110 return S_IFLNK;
b6c93aef
RB
111 else if (S_ISGITLINK(raw_mode))
112 return S_IFGITLINK;
7c7ff7d1
RB
113 else if (S_ISDIR(raw_mode))
114 return S_IFDIR;
b6c93aef
RB
115 else
116 return 0;
117}
118
13224ea4 119int git_futils_readbuffer_updated(git_buf *buf, const char *path, time_t *mtime, int *updated)
75d58430
RJ
120{
121 git_file fd;
90d4d2f0 122 size_t len;
c3da9f06 123 struct stat st;
75d58430 124
13224ea4 125 assert(buf && path && *path);
75d58430 126
c3da9f06
CMN
127 if (updated != NULL)
128 *updated = 0;
75d58430 129
ae9e29fd
RB
130 if ((fd = git_futils_open_ro(path)) < 0)
131 return fd;
c3da9f06 132
13224ea4 133 if (p_fstat(fd, &st) < 0 || S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) {
e1de726c 134 p_close(fd);
1a481123
VM
135 giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path);
136 return -1;
13224ea4 137 }
c3da9f06
CMN
138
139 /*
140 * If we were given a time, we only want to read the file if it
141 * has been modified.
142 */
13224ea4 143 if (mtime != NULL && *mtime >= st.st_mtime) {
e1de726c 144 p_close(fd);
13224ea4
VM
145 return 0;
146 }
c3da9f06
CMN
147
148 if (mtime != NULL)
149 *mtime = st.st_mtime;
c3da9f06
CMN
150
151 len = (size_t) st.st_size;
152
13224ea4 153 git_buf_clear(buf);
90d4d2f0 154
13224ea4 155 if (git_buf_grow(buf, len + 1) < 0) {
e1de726c
RB
156 p_close(fd);
157 return -1;
75d58430
RJ
158 }
159
13224ea4
VM
160 buf->ptr[len] = '\0';
161
162 while (len > 0) {
163 ssize_t read_size = p_read(fd, buf->ptr, len);
164
165 if (read_size < 0) {
e1de726c 166 p_close(fd);
ae9e29fd 167 giterr_set(GITERR_OS, "Failed to read descriptor for '%s'", path);
1a481123 168 return -1;
13224ea4
VM
169 }
170
171 len -= read_size;
172 buf->size += read_size;
75d58430
RJ
173 }
174
f79026b4 175 p_close(fd);
75d58430 176
c3da9f06
CMN
177 if (updated != NULL)
178 *updated = 1;
179
13224ea4 180 return 0;
c3da9f06
CMN
181}
182
13224ea4 183int git_futils_readbuffer(git_buf *buf, const char *path)
97769280 184{
13224ea4 185 return git_futils_readbuffer_updated(buf, path, NULL, NULL);
97769280
RB
186}
187
ce8cd006 188int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
19a30a3f 189{
deafee7b
RB
190 if (git_futils_mkpath2file(to, dirmode) < 0)
191 return -1;
19a30a3f 192
deafee7b
RB
193 if (p_rename(from, to) < 0) {
194 giterr_set(GITERR_OS, "Failed to rename '%s' to '%s'", from, to);
195 return -1;
196 }
197
198 return 0;
19a30a3f
VM
199}
200
f79026b4 201int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
20e7f426 202{
f79026b4 203 return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
20e7f426
SP
204}
205
74fa4bfa
RB
206int git_futils_mmap_ro_file(git_map *out, const char *path)
207{
0d0fa7c3
RB
208 git_file fd = git_futils_open_ro(path);
209 git_off_t len;
da9abdd6 210 int result;
0d0fa7c3
RB
211
212 if (fd < 0)
213 return fd;
214
215 len = git_futils_filesize(fd);
deafee7b
RB
216 if (!git__is_sizet(len)) {
217 giterr_set(GITERR_OS, "File `%s` too large to mmap", path);
218 return -1;
219 }
0d0fa7c3 220
da9abdd6 221 result = git_futils_mmap_ro(out, fd, 0, (size_t)len);
74fa4bfa
RB
222 p_close(fd);
223 return result;
224}
225
f79026b4 226void git_futils_mmap_free(git_map *out)
20e7f426 227{
f79026b4 228 p_munmap(out);
20e7f426
SP
229}
230
ca1b6e54
RB
231int git_futils_mkdir(
232 const char *path,
233 const char *base,
234 mode_t mode,
235 uint32_t flags)
1a5204a7 236{
97769280 237 git_buf make_path = GIT_BUF_INIT;
ca1b6e54
RB
238 ssize_t root = 0;
239 char lastch, *tail;
dc07184f 240
ca1b6e54
RB
241 /* build path and find "root" where we should start calling mkdir */
242 if (git_path_join_unrooted(&make_path, path, base, &root) < 0)
243 return -1;
dc07184f 244
ca1b6e54
RB
245 if (make_path.size == 0) {
246 giterr_set(GITERR_OS, "Attempt to create empty path");
247 goto fail;
97769280 248 }
f0b2bfe5 249
ca1b6e54
RB
250 /* remove trailing slashes on path */
251 while (make_path.ptr[make_path.size - 1] == '/') {
252 make_path.size--;
253 make_path.ptr[make_path.size] = '\0';
254 }
412de9a6 255
ca1b6e54
RB
256 /* if we are not supposed to made the last element, truncate it */
257 if ((flags & GIT_MKDIR_SKIP_LAST) != 0)
258 git_buf_rtruncate_at_char(&make_path, '/');
259
260 /* if we are not supposed to make the whole path, reset root */
261 if ((flags & GIT_MKDIR_PATH) == 0)
262 root = git_buf_rfind(&make_path, '/');
263
264 /* clip root to make_path length */
265 if (root >= (ssize_t)make_path.size)
266 root = (ssize_t)make_path.size - 1;
0e26202c
RB
267 if (root < 0)
268 root = 0;
ca1b6e54
RB
269
270 tail = & make_path.ptr[root];
271
272 while (*tail) {
273 /* advance tail to include next path component */
274 while (*tail == '/')
275 tail++;
276 while (*tail && *tail != '/')
277 tail++;
278
279 /* truncate path at next component */
280 lastch = *tail;
281 *tail = '\0';
282
283 /* make directory */
284 if (p_mkdir(make_path.ptr, mode) < 0 &&
285 (errno != EEXIST || (flags & GIT_MKDIR_EXCL) != 0))
286 {
287 giterr_set(GITERR_OS, "Failed to make directory '%s'",
288 make_path.ptr);
289 goto fail;
d5f25204 290 }
40c44d2f 291
ca1b6e54
RB
292 /* chmod if requested */
293 if ((flags & GIT_MKDIR_CHMOD_PATH) != 0 ||
294 ((flags & GIT_MKDIR_CHMOD) != 0 && lastch == '\0'))
295 {
296 if (p_chmod(make_path.ptr, mode) < 0) {
297 giterr_set(GITERR_OS, "Failed to set permissions on '%s'",
298 make_path.ptr);
299 goto fail;
300 }
301 }
f0b2bfe5 302
ca1b6e54 303 *tail = lastch;
a993e4fe 304 }
40c44d2f 305
97769280 306 git_buf_free(&make_path);
ca1b6e54 307 return 0;
77c3999c 308
ca1b6e54
RB
309fail:
310 git_buf_free(&make_path);
311 return -1;
312}
77c3999c 313
ca1b6e54
RB
314int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
315{
316 return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH);
170d3f2f 317}
318
97769280 319static int _rmdir_recurs_foreach(void *opaque, git_buf *path)
42b3a460 320{
1a2b8725 321 git_directory_removal_type removal_type = *(git_directory_removal_type *)opaque;
555aa453 322
323 assert(removal_type == GIT_DIRREMOVAL_EMPTY_HIERARCHY
324 || removal_type == GIT_DIRREMOVAL_FILES_AND_DIRS
325 || removal_type == GIT_DIRREMOVAL_ONLY_EMPTY_DIRS);
42b3a460 326
1a481123 327 if (git_path_isdir(path->ptr) == true) {
deafee7b
RB
328 if (git_path_direach(path, _rmdir_recurs_foreach, opaque) < 0)
329 return -1;
330
331 if (p_rmdir(path->ptr) < 0) {
54bdc64a 332 if (removal_type == GIT_DIRREMOVAL_ONLY_EMPTY_DIRS && (errno == ENOTEMPTY || errno == EEXIST))
555aa453 333 return 0;
334
deafee7b
RB
335 giterr_set(GITERR_OS, "Could not remove directory '%s'", path->ptr);
336 return -1;
337 }
42b3a460 338
deafee7b 339 return 0;
858dba58
VM
340 }
341
555aa453 342 if (removal_type == GIT_DIRREMOVAL_FILES_AND_DIRS) {
deafee7b
RB
343 if (p_unlink(path->ptr) < 0) {
344 giterr_set(GITERR_OS, "Could not remove directory. File '%s' cannot be removed", path->ptr);
345 return -1;
346 }
347
348 return 0;
349 }
350
555aa453 351 if (removal_type == GIT_DIRREMOVAL_EMPTY_HIERARCHY) {
352 giterr_set(GITERR_OS, "Could not remove directory. File '%s' still present", path->ptr);
353 return -1;
354 }
355
356 return 0;
42b3a460
MS
357}
358
1a2b8725 359int git_futils_rmdir_r(const char *path, git_directory_removal_type removal_type)
42b3a460 360{
97769280
RB
361 int error;
362 git_buf p = GIT_BUF_INIT;
363
364 error = git_buf_sets(&p, path);
deafee7b 365 if (!error)
555aa453 366 error = _rmdir_recurs_foreach(&removal_type, &p);
97769280
RB
367 git_buf_free(&p);
368 return error;
42b3a460
MS
369}
370
23059130 371#ifdef GIT_WIN32
349fb6d7
VM
372struct win32_path {
373 wchar_t path[MAX_PATH];
73b51450 374 DWORD len;
349fb6d7 375};
73b51450 376
349fb6d7 377static int win32_expand_path(struct win32_path *s_root, const wchar_t *templ)
73b51450 378{
349fb6d7
VM
379 s_root->len = ExpandEnvironmentStringsW(templ, s_root->path, MAX_PATH);
380 return s_root->len ? 0 : -1;
73b51450
RB
381}
382
349fb6d7 383static int win32_find_file(git_buf *path, const struct win32_path *root, const char *filename)
73b51450 384{
deafee7b 385 int error = 0;
73b51450 386 size_t len;
349fb6d7 387 wchar_t *file_utf16 = NULL;
73b51450
RB
388 char *file_utf8 = NULL;
389
390 if (!root || !filename || (len = strlen(filename)) == 0)
391 return GIT_ENOTFOUND;
392
393 /* allocate space for wchar_t path to file */
394 file_utf16 = git__calloc(root->len + len + 2, sizeof(wchar_t));
deafee7b 395 GITERR_CHECK_ALLOC(file_utf16);
73b51450
RB
396
397 /* append root + '\\' + filename as wchar_t */
398 memcpy(file_utf16, root->path, root->len * sizeof(wchar_t));
399
400 if (*filename == '/' || *filename == '\\')
401 filename++;
402
403 if (gitwin_append_utf16(file_utf16 + root->len - 1, filename, len + 1) !=
f46e6226 404 (int)len + 1) {
deafee7b 405 error = -1;
73b51450
RB
406 goto cleanup;
407 }
408
73b51450
RB
409 /* check access */
410 if (_waccess(file_utf16, F_OK) < 0) {
411 error = GIT_ENOTFOUND;
412 goto cleanup;
413 }
414
415 /* convert to utf8 */
416 if ((file_utf8 = gitwin_from_utf16(file_utf16)) == NULL)
deafee7b
RB
417 error = -1;
418 else {
73b51450
RB
419 git_path_mkposix(file_utf8);
420 git_buf_attach(path, file_utf8, 0);
421 }
422
423cleanup:
424 git__free(file_utf16);
73b51450
RB
425 return error;
426}
427#endif
428
429int git_futils_find_system_file(git_buf *path, const char *filename)
430{
349fb6d7
VM
431#ifdef GIT_WIN32
432 struct win32_path root;
9cde607c 433
349fb6d7 434 if (win32_expand_path(&root, L"%PROGRAMFILES%\\Git\\etc\\") < 0 ||
29ef309e
RB
435 root.path[0] == L'%') /* i.e. no expansion happened */
436 {
437 giterr_set(GITERR_OS, "Cannot locate the system's Program Files directory");
349fb6d7
VM
438 return -1;
439 }
440
29ef309e 441 if (win32_find_file(path, &root, filename) < 0) {
0b956819 442 giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename);
29ef309e
RB
443 git_buf_clear(path);
444 return GIT_ENOTFOUND;
445 }
446
349fb6d7
VM
447 return 0;
448
449#else
cb8a7961
VM
450 if (git_buf_joinpath(path, "/etc", filename) < 0)
451 return -1;
73b51450 452
1a481123 453 if (git_path_exists(path->ptr) == true)
deafee7b 454 return 0;
73b51450
RB
455
456 git_buf_clear(path);
0b956819 457 giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename);
349fb6d7
VM
458 return GIT_ENOTFOUND;
459#endif
460}
73b51450 461
349fb6d7
VM
462int git_futils_find_global_file(git_buf *path, const char *filename)
463{
73b51450 464#ifdef GIT_WIN32
349fb6d7
VM
465 struct win32_path root;
466
467 if (win32_expand_path(&root, L"%USERPROFILE%\\") < 0 ||
29ef309e
RB
468 root.path[0] == L'%') /* i.e. no expansion happened */
469 {
470 giterr_set(GITERR_OS, "Cannot locate the user's profile directory");
349fb6d7
VM
471 return -1;
472 }
473
29ef309e 474 if (win32_find_file(path, &root, filename) < 0) {
0b956819 475 giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
29ef309e
RB
476 git_buf_clear(path);
477 return GIT_ENOTFOUND;
478 }
479
349fb6d7 480 return 0;
73b51450 481#else
349fb6d7
VM
482 const char *home = getenv("HOME");
483
484 if (home == NULL) {
485 giterr_set(GITERR_OS, "Global file lookup failed. "
486 "Cannot locate the user's home directory");
487 return -1;
488 }
489
490 if (git_buf_joinpath(path, home, filename) < 0)
491 return -1;
492
493 if (git_path_exists(path->ptr) == false) {
0b956819 494 giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
349fb6d7
VM
495 git_buf_clear(path);
496 return GIT_ENOTFOUND;
497 }
498
499 return 0;
73b51450
RB
500#endif
501}
8651c10f
BS
502
503int git_futils_fake_symlink(const char *old, const char *new)
504{
505 int retcode = GIT_ERROR;
506 int fd = git_futils_creat_withpath(new, 0755, 0644);
507 if (fd >= 0) {
508 retcode = p_write(fd, old, strlen(old));
509 p_close(fd);
510 }
511 return retcode;
512}
ca1b6e54 513
85bd1746 514static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done)
ca1b6e54
RB
515{
516 int error = 0;
517 char buffer[4096];
518 ssize_t len = 0;
519
520 while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0)
521 /* p_write() does not have the same semantics as write(). It loops
522 * internally and will return 0 when it has completed writing.
523 */
524 error = p_write(ofd, buffer, len);
525
526 if (len < 0) {
527 giterr_set(GITERR_OS, "Read error while copying file");
528 error = (int)len;
529 }
530
85bd1746 531 if (close_fd_when_done) {
ca1b6e54
RB
532 p_close(ifd);
533 p_close(ofd);
534 }
535
536 return error;
537}
538
85bd1746 539int git_futils_cp(const char *from, const char *to, mode_t filemode)
ca1b6e54
RB
540{
541 int ifd, ofd;
542
ca1b6e54
RB
543 if ((ifd = git_futils_open_ro(from)) < 0)
544 return ifd;
545
546 if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
547 if (errno == ENOENT || errno == ENOTDIR)
548 ofd = GIT_ENOTFOUND;
549 giterr_set(GITERR_OS, "Failed to open '%s' for writing", to);
550 p_close(ifd);
551 return ofd;
552 }
553
85bd1746 554 return cp_by_fd(ifd, ofd, true);
ca1b6e54
RB
555}
556
85bd1746 557static int cp_link(const char *from, const char *to, size_t link_size)
ca1b6e54
RB
558{
559 int error = 0;
560 ssize_t read_len;
85bd1746 561 char *link_data = git__malloc(link_size + 1);
ca1b6e54
RB
562 GITERR_CHECK_ALLOC(link_data);
563
85bd1746
RB
564 read_len = p_readlink(from, link_data, link_size);
565 if (read_len != (ssize_t)link_size) {
ca1b6e54
RB
566 giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from);
567 error = -1;
568 }
569 else {
570 link_data[read_len] = '\0';
571
572 if (p_symlink(link_data, to) < 0) {
573 giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'",
574 link_data, to);
575 error = -1;
576 }
577 }
578
579 git__free(link_data);
580 return error;
581}
582
583typedef struct {
584 const char *to_root;
585 git_buf to;
586 ssize_t from_prefix;
587 uint32_t flags;
588 uint32_t mkdir_flags;
589 mode_t dirmode;
590} cp_r_info;
591
592static int _cp_r_callback(void *ref, git_buf *from)
593{
594 cp_r_info *info = ref;
595 struct stat from_st, to_st;
596 bool exists = false;
597
598 if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 &&
599 from->ptr[git_path_basename_offset(from)] == '.')
600 return 0;
601
602 if (git_buf_joinpath(
603 &info->to, info->to_root, from->ptr + info->from_prefix) < 0)
604 return -1;
605
606 if (p_lstat(info->to.ptr, &to_st) < 0) {
607 if (errno != ENOENT) {
608 giterr_set(GITERR_OS,
609 "Could not access %s while copying files", info->to.ptr);
610 return -1;
611 }
612 } else
613 exists = true;
614
615 if (git_path_lstat(from->ptr, &from_st) < 0)
616 return -1;
617
618 if (S_ISDIR(from_st.st_mode)) {
619 int error = 0;
620 mode_t oldmode = info->dirmode;
621
622 /* if we are not chmod'ing, then overwrite dirmode */
623 if ((info->flags & GIT_CPDIR_CHMOD) == 0)
624 info->dirmode = from_st.st_mode;
625
626 /* make directory now if CREATE_EMPTY_DIRS is requested and needed */
627 if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0)
628 error = git_futils_mkdir(
629 info->to.ptr, NULL, info->dirmode, info->mkdir_flags);
630
631 /* recurse onto target directory */
632 if (!exists || S_ISDIR(to_st.st_mode))
633 error = git_path_direach(from, _cp_r_callback, info);
634
635 if (oldmode != 0)
636 info->dirmode = oldmode;
637
638 return error;
639 }
640
641 if (exists) {
642 if ((info->flags & GIT_CPDIR_OVERWRITE) == 0)
643 return 0;
644
645 if (p_unlink(info->to.ptr) < 0) {
646 giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'",
647 info->to.ptr);
648 return -1;
649 }
650 }
651
652 /* Done if this isn't a regular file or a symlink */
653 if (!S_ISREG(from_st.st_mode) &&
654 (!S_ISLNK(from_st.st_mode) ||
655 (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0))
656 return 0;
657
658 /* Make container directory on demand if needed */
659 if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
660 git_futils_mkdir(
661 info->to.ptr, NULL, info->dirmode, info->mkdir_flags) < 0)
662 return -1;
663
664 /* make symlink or regular file */
665 if (S_ISLNK(from_st.st_mode))
85bd1746 666 return cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size);
ca1b6e54 667 else
85bd1746 668 return git_futils_cp(from->ptr, info->to.ptr, from_st.st_mode);
ca1b6e54
RB
669}
670
671int git_futils_cp_r(
672 const char *from,
673 const char *to,
674 uint32_t flags,
675 mode_t dirmode)
676{
677 int error;
678 git_buf path = GIT_BUF_INIT;
679 cp_r_info info;
680
681 if (git_buf_sets(&path, from) < 0)
682 return -1;
683
684 info.to_root = to;
685 info.flags = flags;
686 info.dirmode = dirmode;
687 info.from_prefix = path.size;
688 git_buf_init(&info.to, 0);
689
690 /* precalculate mkdir flags */
691 if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) {
692 info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST;
693 if ((flags & GIT_CPDIR_CHMOD) != 0)
694 info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH;
695 } else {
696 info.mkdir_flags =
697 ((flags & GIT_CPDIR_CHMOD) != 0) ? GIT_MKDIR_CHMOD : 0;
698 }
699
700 error = _cp_r_callback(&info, &path);
701
702 git_buf_free(&path);
703
704 return error;
705}