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