]> git.proxmox.com Git - libgit2.git/blame - src/fileops.c
Merge pull request #1359 from martinwoodward/remove-sample-hooks
[libgit2.git] / src / fileops.c
CommitLineData
bb742ede 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
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>
997579be
SS
10#if GIT_WIN32
11#include "win32/findfile.h"
12#endif
ec250c6e 13
ce8cd006 14int git_futils_mkpath2file(const char *file_path, const mode_t mode)
55ffebe3 15{
ca1b6e54 16 return git_futils_mkdir(
331e7de9
RB
17 file_path, NULL, mode,
18 GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
55ffebe3
VM
19}
20
97769280 21int git_futils_mktmp(git_buf *path_out, const char *filename)
72a3fe42
VM
22{
23 int fd;
24
97769280
RB
25 git_buf_sets(path_out, filename);
26 git_buf_puts(path_out, "_git2_XXXXXX");
27
28 if (git_buf_oom(path_out))
1a481123 29 return -1;
72a3fe42 30
1a481123
VM
31 if ((fd = p_mkstemp(path_out->ptr)) < 0) {
32 giterr_set(GITERR_OS,
ae9e29fd 33 "Failed to create temporary file '%s'", path_out->ptr);
1a481123
VM
34 return -1;
35 }
72a3fe42 36
f978b748 37 return fd;
72a3fe42
VM
38}
39
ce8cd006 40int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode)
7dd8a9f7 41{
1a481123 42 int fd;
55ffebe3 43
1a481123
VM
44 if (git_futils_mkpath2file(path, dirmode) < 0)
45 return -1;
46
47 fd = p_creat(path, mode);
48 if (fd < 0) {
ae9e29fd 49 giterr_set(GITERR_OS, "Failed to create file '%s'", path);
1a481123
VM
50 return -1;
51 }
52
53 return fd;
55ffebe3
VM
54}
55
33127043 56int git_futils_creat_locked(const char *path, const mode_t mode)
1549cba9 57{
09719c50 58 int fd;
59
60#ifdef GIT_WIN32
6813169a 61 wchar_t buf[GIT_WIN_PATH];
09719c50 62
0f4c6175 63 git__utf8_to_16(buf, GIT_WIN_PATH, path);
09719c50 64 fd = _wopen(buf, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode);
09719c50 65#else
66 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode);
67#endif
68
1a481123 69 if (fd < 0) {
ae9e29fd 70 giterr_set(GITERR_OS, "Failed to create locked file '%s'", path);
1a481123
VM
71 return -1;
72 }
73
74 return fd;
1549cba9
RG
75}
76
ce8cd006 77int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode)
1549cba9 78{
1a481123
VM
79 if (git_futils_mkpath2file(path, dirmode) < 0)
80 return -1;
1549cba9 81
f79026b4 82 return git_futils_creat_locked(path, mode);
ec250c6e
AE
83}
84
deafee7b
RB
85int git_futils_open_ro(const char *path)
86{
87 int fd = p_open(path, O_RDONLY);
88 if (fd < 0) {
d0a920a6 89 if (errno == ENOENT || errno == ENOTDIR)
deafee7b
RB
90 fd = GIT_ENOTFOUND;
91 giterr_set(GITERR_OS, "Failed to open '%s'", path);
92 }
93 return fd;
94}
95
f79026b4 96git_off_t git_futils_filesize(git_file fd)
ec250c6e 97{
7dd8a9f7 98 struct stat sb;
deafee7b
RB
99
100 if (p_fstat(fd, &sb)) {
101 giterr_set(GITERR_OS, "Failed to stat file descriptor");
102 return -1;
103 }
5ad739e8 104
ec250c6e
AE
105 return sb.st_size;
106}
4188d28f 107
b6c93aef
RB
108mode_t git_futils_canonical_mode(mode_t raw_mode)
109{
110 if (S_ISREG(raw_mode))
111 return S_IFREG | GIT_CANONICAL_PERMS(raw_mode);
112 else if (S_ISLNK(raw_mode))
113 return S_IFLNK;
b6c93aef
RB
114 else if (S_ISGITLINK(raw_mode))
115 return S_IFGITLINK;
7c7ff7d1
RB
116 else if (S_ISDIR(raw_mode))
117 return S_IFDIR;
b6c93aef
RB
118 else
119 return 0;
120}
121
60b9d3fc
RB
122int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
123{
de590550 124 ssize_t read_size = 0;
60b9d3fc
RB
125
126 git_buf_clear(buf);
127
128 if (git_buf_grow(buf, len + 1) < 0)
129 return -1;
130
1f35e89d
RB
131 /* p_read loops internally to read len bytes */
132 read_size = p_read(fd, buf->ptr, len);
60b9d3fc 133
c859184b 134 if (read_size != (ssize_t)len) {
1f35e89d
RB
135 giterr_set(GITERR_OS, "Failed to read descriptor");
136 return -1;
60b9d3fc
RB
137 }
138
1f35e89d
RB
139 buf->ptr[read_size] = '\0';
140 buf->size = read_size;
141
60b9d3fc
RB
142 return 0;
143}
144
145int git_futils_readbuffer_updated(
744cc03e 146 git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated)
75d58430
RJ
147{
148 git_file fd;
c3da9f06 149 struct stat st;
744cc03e 150 bool changed = false;
75d58430 151
13224ea4 152 assert(buf && path && *path);
75d58430 153
c3da9f06
CMN
154 if (updated != NULL)
155 *updated = 0;
75d58430 156
ae9e29fd
RB
157 if ((fd = git_futils_open_ro(path)) < 0)
158 return fd;
c3da9f06 159
13224ea4 160 if (p_fstat(fd, &st) < 0 || S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) {
e1de726c 161 p_close(fd);
1a481123
VM
162 giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path);
163 return -1;
13224ea4 164 }
c3da9f06
CMN
165
166 /*
744cc03e
RB
167 * If we were given a time and/or a size, we only want to read the file
168 * if it has been modified.
c3da9f06 169 */
744cc03e
RB
170 if (size && *size != (size_t)st.st_size)
171 changed = true;
172 if (mtime && *mtime != st.st_mtime)
173 changed = true;
174 if (!size && !mtime)
175 changed = true;
176
177 if (!changed) {
e1de726c 178 p_close(fd);
13224ea4
VM
179 return 0;
180 }
c3da9f06
CMN
181
182 if (mtime != NULL)
183 *mtime = st.st_mtime;
744cc03e
RB
184 if (size != NULL)
185 *size = (size_t)st.st_size;
c3da9f06 186
60b9d3fc 187 if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) {
e1de726c
RB
188 p_close(fd);
189 return -1;
75d58430
RJ
190 }
191
f79026b4 192 p_close(fd);
75d58430 193
c3da9f06
CMN
194 if (updated != NULL)
195 *updated = 1;
196
13224ea4 197 return 0;
c3da9f06
CMN
198}
199
13224ea4 200int git_futils_readbuffer(git_buf *buf, const char *path)
97769280 201{
744cc03e 202 return git_futils_readbuffer_updated(buf, path, NULL, NULL, NULL);
97769280
RB
203}
204
ce8cd006 205int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
19a30a3f 206{
deafee7b
RB
207 if (git_futils_mkpath2file(to, dirmode) < 0)
208 return -1;
19a30a3f 209
deafee7b
RB
210 if (p_rename(from, to) < 0) {
211 giterr_set(GITERR_OS, "Failed to rename '%s' to '%s'", from, to);
212 return -1;
213 }
214
215 return 0;
19a30a3f
VM
216}
217
f79026b4 218int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
20e7f426 219{
f79026b4 220 return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
20e7f426
SP
221}
222
74fa4bfa
RB
223int git_futils_mmap_ro_file(git_map *out, const char *path)
224{
0d0fa7c3
RB
225 git_file fd = git_futils_open_ro(path);
226 git_off_t len;
da9abdd6 227 int result;
0d0fa7c3
RB
228
229 if (fd < 0)
230 return fd;
231
232 len = git_futils_filesize(fd);
deafee7b
RB
233 if (!git__is_sizet(len)) {
234 giterr_set(GITERR_OS, "File `%s` too large to mmap", path);
235 return -1;
236 }
0d0fa7c3 237
da9abdd6 238 result = git_futils_mmap_ro(out, fd, 0, (size_t)len);
74fa4bfa
RB
239 p_close(fd);
240 return result;
241}
242
f79026b4 243void git_futils_mmap_free(git_map *out)
20e7f426 244{
f79026b4 245 p_munmap(out);
20e7f426
SP
246}
247
ca1b6e54
RB
248int git_futils_mkdir(
249 const char *path,
250 const char *base,
251 mode_t mode,
252 uint32_t flags)
1a5204a7 253{
331e7de9 254 int error = -1;
97769280 255 git_buf make_path = GIT_BUF_INIT;
ca1b6e54
RB
256 ssize_t root = 0;
257 char lastch, *tail;
dc07184f 258
ca1b6e54
RB
259 /* build path and find "root" where we should start calling mkdir */
260 if (git_path_join_unrooted(&make_path, path, base, &root) < 0)
261 return -1;
dc07184f 262
ca1b6e54
RB
263 if (make_path.size == 0) {
264 giterr_set(GITERR_OS, "Attempt to create empty path");
265 goto fail;
97769280 266 }
f0b2bfe5 267
ca1b6e54
RB
268 /* remove trailing slashes on path */
269 while (make_path.ptr[make_path.size - 1] == '/') {
270 make_path.size--;
271 make_path.ptr[make_path.size] = '\0';
272 }
412de9a6 273
ca1b6e54
RB
274 /* if we are not supposed to made the last element, truncate it */
275 if ((flags & GIT_MKDIR_SKIP_LAST) != 0)
276 git_buf_rtruncate_at_char(&make_path, '/');
277
278 /* if we are not supposed to make the whole path, reset root */
279 if ((flags & GIT_MKDIR_PATH) == 0)
280 root = git_buf_rfind(&make_path, '/');
281
282 /* clip root to make_path length */
283 if (root >= (ssize_t)make_path.size)
284 root = (ssize_t)make_path.size - 1;
0e26202c
RB
285 if (root < 0)
286 root = 0;
ca1b6e54
RB
287
288 tail = & make_path.ptr[root];
289
290 while (*tail) {
291 /* advance tail to include next path component */
292 while (*tail == '/')
293 tail++;
294 while (*tail && *tail != '/')
295 tail++;
296
297 /* truncate path at next component */
298 lastch = *tail;
299 *tail = '\0';
300
301 /* make directory */
331e7de9 302 if (p_mkdir(make_path.ptr, mode) < 0) {
6c72035f
PK
303 int already_exists = 0;
304
305 switch (errno) {
306 case EEXIST:
307 if (!lastch && (flags & GIT_MKDIR_VERIFY_DIR) != 0 &&
308 !git_path_isdir(make_path.ptr)) {
331e7de9
RB
309 giterr_set(
310 GITERR_OS, "Existing path is not a directory '%s'",
311 make_path.ptr);
312 error = GIT_ENOTFOUND;
313 goto fail;
314 }
6c72035f
PK
315
316 already_exists = 1;
317 break;
318 case ENOSYS:
319 /* Solaris can generate this error if you try to mkdir
320 * a path which is already a mount point. In that case,
321 * the path does already exist; but it's not implied by
322 * the definition of the error, so let's recheck */
323 if (git_path_isdir(make_path.ptr)) {
324 already_exists = 1;
325 break;
326 }
327
328 /* Fall through */
329 errno = ENOSYS;
330 default:
331 giterr_set(GITERR_OS, "Failed to make directory '%s'",
331e7de9 332 make_path.ptr);
331e7de9 333 goto fail;
6c72035f
PK
334 }
335
336 if (already_exists && (flags & GIT_MKDIR_EXCL) != 0) {
337 giterr_set(GITERR_OS, "Directory already exists '%s'",
331e7de9 338 make_path.ptr);
6c72035f 339 error = GIT_EEXISTS;
331e7de9
RB
340 goto fail;
341 }
d5f25204 342 }
40c44d2f 343
ca1b6e54
RB
344 /* chmod if requested */
345 if ((flags & GIT_MKDIR_CHMOD_PATH) != 0 ||
346 ((flags & GIT_MKDIR_CHMOD) != 0 && lastch == '\0'))
347 {
348 if (p_chmod(make_path.ptr, mode) < 0) {
349 giterr_set(GITERR_OS, "Failed to set permissions on '%s'",
350 make_path.ptr);
351 goto fail;
352 }
353 }
f0b2bfe5 354
ca1b6e54 355 *tail = lastch;
a993e4fe 356 }
40c44d2f 357
97769280 358 git_buf_free(&make_path);
ca1b6e54 359 return 0;
77c3999c 360
ca1b6e54
RB
361fail:
362 git_buf_free(&make_path);
331e7de9 363 return error;
ca1b6e54 364}
77c3999c 365
ca1b6e54
RB
366int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
367{
368 return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH);
170d3f2f 369}
370
331e7de9 371typedef struct {
ad9a921b 372 const char *base;
7e5c8a5b 373 size_t baselen;
331e7de9
RB
374 uint32_t flags;
375 int error;
376} futils__rmdir_data;
377
378static int futils__error_cannot_rmdir(const char *path, const char *filemsg)
42b3a460 379{
331e7de9
RB
380 if (filemsg)
381 giterr_set(GITERR_OS, "Could not remove directory. File '%s' %s",
382 path, filemsg);
383 else
384 giterr_set(GITERR_OS, "Could not remove directory '%s'", path);
555aa453 385
331e7de9
RB
386 return -1;
387}
deafee7b 388
ad9a921b
RB
389static int futils__rm_first_parent(git_buf *path, const char *ceiling)
390{
391 int error = GIT_ENOTFOUND;
392 struct stat st;
393
394 while (error == GIT_ENOTFOUND) {
395 git_buf_rtruncate_at_char(path, '/');
396
397 if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0)
398 error = 0;
cccacac5 399 else if (p_lstat_posixly(path->ptr, &st) == 0) {
ad9a921b
RB
400 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
401 error = p_unlink(path->ptr);
402 else if (!S_ISDIR(st.st_mode))
403 error = -1; /* fail to remove non-regular file */
404 } else if (errno != ENOTDIR)
405 error = -1;
406 }
407
408 if (error)
409 futils__error_cannot_rmdir(path->ptr, "cannot remove parent");
410
411 return error;
412}
413
331e7de9
RB
414static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
415{
ad9a921b 416 struct stat st;
331e7de9 417 futils__rmdir_data *data = opaque;
555aa453 418
cccacac5 419 if ((data->error = p_lstat_posixly(path->ptr, &st)) < 0) {
ad9a921b
RB
420 if (errno == ENOENT)
421 data->error = 0;
422 else if (errno == ENOTDIR) {
423 /* asked to remove a/b/c/d/e and a/b is a normal file */
424 if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0)
425 data->error = futils__rm_first_parent(path, data->base);
426 else
427 futils__error_cannot_rmdir(
428 path->ptr, "parent is not directory");
429 }
430 else
431 futils__error_cannot_rmdir(path->ptr, "cannot access");
432 }
433
434 else if (S_ISDIR(st.st_mode)) {
331e7de9
RB
435 int error = git_path_direach(path, futils__rmdir_recurs_foreach, data);
436 if (error < 0)
437 return (error == GIT_EUSER) ? data->error : error;
438
439 data->error = p_rmdir(path->ptr);
440
441 if (data->error < 0) {
442 if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
443 (errno == ENOTEMPTY || errno == EEXIST))
444 data->error = 0;
445 else
446 futils__error_cannot_rmdir(path->ptr, NULL);
deafee7b 447 }
858dba58
VM
448 }
449
331e7de9
RB
450 else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) {
451 data->error = p_unlink(path->ptr);
deafee7b 452
331e7de9
RB
453 if (data->error < 0)
454 futils__error_cannot_rmdir(path->ptr, "cannot be removed");
deafee7b
RB
455 }
456
ad9a921b 457 else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
331e7de9 458 data->error = futils__error_cannot_rmdir(path->ptr, "still present");
555aa453 459
331e7de9
RB
460 return data->error;
461}
462
463static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
464{
7e5c8a5b
RB
465 futils__rmdir_data *data = opaque;
466 int error;
467
468 if (git_buf_len(path) <= data->baselen)
469 return GIT_ITEROVER;
331e7de9 470
7e5c8a5b 471 error = p_rmdir(git_buf_cstr(path));
331e7de9
RB
472
473 if (error) {
474 int en = errno;
475
476 if (en == ENOENT || en == ENOTDIR) {
477 giterr_clear();
478 error = 0;
479 } else if (en == ENOTEMPTY || en == EEXIST) {
480 giterr_clear();
481 error = GIT_ITEROVER;
482 } else {
7e5c8a5b 483 futils__error_cannot_rmdir(git_buf_cstr(path), NULL);
331e7de9
RB
484 }
485 }
486
487 return error;
42b3a460
MS
488}
489
0d64bef9 490int git_futils_rmdir_r(
331e7de9 491 const char *path, const char *base, uint32_t flags)
42b3a460 492{
97769280 493 int error;
0d64bef9 494 git_buf fullpath = GIT_BUF_INIT;
331e7de9 495 futils__rmdir_data data;
0d64bef9
RB
496
497 /* build path and find "root" where we should start calling mkdir */
498 if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
499 return -1;
500
7e5c8a5b
RB
501 data.base = base ? base : "";
502 data.baselen = base ? strlen(base) : 0;
503 data.flags = flags;
504 data.error = 0;
331e7de9
RB
505
506 error = futils__rmdir_recurs_foreach(&data, &fullpath);
507
508 /* remove now-empty parents if requested */
509 if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0) {
510 error = git_path_walk_up(
511 &fullpath, base, futils__rmdir_empty_parent, &data);
512
513 if (error == GIT_ITEROVER)
514 error = 0;
515 }
0d64bef9
RB
516
517 git_buf_free(&fullpath);
97769280 518
97769280 519 return error;
42b3a460
MS
520}
521
32a4e3b7
SS
522int git_futils_find_system_file(git_buf *path, const char *filename)
523{
524#ifdef GIT_WIN32
525 // try to find git.exe/git.cmd on path
dee18b82 526 if (!win32_find_system_file_using_path(path, filename))
32a4e3b7
SS
527 return 0;
528
529 // try to find msysgit installation path using registry
dee18b82 530 if (!win32_find_system_file_using_registry(path, filename))
32a4e3b7 531 return 0;
349fb6d7 532#else
cb8a7961
VM
533 if (git_buf_joinpath(path, "/etc", filename) < 0)
534 return -1;
73b51450 535
1a481123 536 if (git_path_exists(path->ptr) == true)
deafee7b 537 return 0;
32a4e3b7 538#endif
73b51450
RB
539
540 git_buf_clear(path);
0b956819 541 giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename);
349fb6d7 542 return GIT_ENOTFOUND;
349fb6d7 543}
73b51450 544
349fb6d7
VM
545int git_futils_find_global_file(git_buf *path, const char *filename)
546{
73b51450 547#ifdef GIT_WIN32
349fb6d7 548 struct win32_path root;
0d422ec9
RB
549 static const wchar_t *tmpls[4] = {
550 L"%HOME%\\",
551 L"%HOMEDRIVE%%HOMEPATH%\\",
552 L"%USERPROFILE%\\",
553 NULL,
554 };
555 const wchar_t **tmpl;
556
557 for (tmpl = tmpls; *tmpl != NULL; tmpl++) {
558 /* try to expand environment variable, skipping if not set */
559 if (win32_expand_path(&root, *tmpl) != 0 || root.path[0] == L'%')
560 continue;
561
562 /* try to look up file under path */
563 if (!win32_find_file(path, &root, filename))
aed8f8a1 564 return 0;
349fb6d7
VM
565 }
566
0d422ec9
RB
567 giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
568 git_buf_clear(path);
29ef309e 569
0d422ec9 570 return GIT_ENOTFOUND;
73b51450 571#else
0d422ec9
RB
572 const char *home = getenv("HOME");
573
349fb6d7
VM
574 if (home == NULL) {
575 giterr_set(GITERR_OS, "Global file lookup failed. "
576 "Cannot locate the user's home directory");
18217e7e 577 return GIT_ENOTFOUND;
349fb6d7
VM
578 }
579
580 if (git_buf_joinpath(path, home, filename) < 0)
581 return -1;
582
583 if (git_path_exists(path->ptr) == false) {
0b956819 584 giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
349fb6d7
VM
585 git_buf_clear(path);
586 return GIT_ENOTFOUND;
587 }
588
589 return 0;
73b51450
RB
590#endif
591}
8651c10f
BS
592
593int git_futils_fake_symlink(const char *old, const char *new)
594{
595 int retcode = GIT_ERROR;
596 int fd = git_futils_creat_withpath(new, 0755, 0644);
597 if (fd >= 0) {
598 retcode = p_write(fd, old, strlen(old));
599 p_close(fd);
600 }
601 return retcode;
602}
ca1b6e54 603
85bd1746 604static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done)
ca1b6e54
RB
605{
606 int error = 0;
607 char buffer[4096];
608 ssize_t len = 0;
609
610 while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0)
611 /* p_write() does not have the same semantics as write(). It loops
612 * internally and will return 0 when it has completed writing.
613 */
614 error = p_write(ofd, buffer, len);
615
616 if (len < 0) {
617 giterr_set(GITERR_OS, "Read error while copying file");
618 error = (int)len;
619 }
620
85bd1746 621 if (close_fd_when_done) {
ca1b6e54
RB
622 p_close(ifd);
623 p_close(ofd);
624 }
625
626 return error;
627}
628
85bd1746 629int git_futils_cp(const char *from, const char *to, mode_t filemode)
ca1b6e54
RB
630{
631 int ifd, ofd;
632
ca1b6e54
RB
633 if ((ifd = git_futils_open_ro(from)) < 0)
634 return ifd;
635
636 if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
637 if (errno == ENOENT || errno == ENOTDIR)
638 ofd = GIT_ENOTFOUND;
639 giterr_set(GITERR_OS, "Failed to open '%s' for writing", to);
640 p_close(ifd);
641 return ofd;
642 }
643
85bd1746 644 return cp_by_fd(ifd, ofd, true);
ca1b6e54
RB
645}
646
85bd1746 647static int cp_link(const char *from, const char *to, size_t link_size)
ca1b6e54
RB
648{
649 int error = 0;
650 ssize_t read_len;
85bd1746 651 char *link_data = git__malloc(link_size + 1);
ca1b6e54
RB
652 GITERR_CHECK_ALLOC(link_data);
653
85bd1746
RB
654 read_len = p_readlink(from, link_data, link_size);
655 if (read_len != (ssize_t)link_size) {
ca1b6e54
RB
656 giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from);
657 error = -1;
658 }
659 else {
660 link_data[read_len] = '\0';
661
662 if (p_symlink(link_data, to) < 0) {
663 giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'",
664 link_data, to);
665 error = -1;
666 }
667 }
668
669 git__free(link_data);
670 return error;
671}
672
673typedef struct {
674 const char *to_root;
675 git_buf to;
676 ssize_t from_prefix;
677 uint32_t flags;
678 uint32_t mkdir_flags;
679 mode_t dirmode;
680} cp_r_info;
681
682static int _cp_r_callback(void *ref, git_buf *from)
683{
684 cp_r_info *info = ref;
685 struct stat from_st, to_st;
686 bool exists = false;
687
688 if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 &&
689 from->ptr[git_path_basename_offset(from)] == '.')
690 return 0;
691
692 if (git_buf_joinpath(
693 &info->to, info->to_root, from->ptr + info->from_prefix) < 0)
694 return -1;
695
696 if (p_lstat(info->to.ptr, &to_st) < 0) {
2eb4edf5 697 if (errno != ENOENT && errno != ENOTDIR) {
ca1b6e54
RB
698 giterr_set(GITERR_OS,
699 "Could not access %s while copying files", info->to.ptr);
700 return -1;
701 }
702 } else
703 exists = true;
704
705 if (git_path_lstat(from->ptr, &from_st) < 0)
706 return -1;
707
708 if (S_ISDIR(from_st.st_mode)) {
709 int error = 0;
710 mode_t oldmode = info->dirmode;
711
712 /* if we are not chmod'ing, then overwrite dirmode */
713 if ((info->flags & GIT_CPDIR_CHMOD) == 0)
714 info->dirmode = from_st.st_mode;
715
716 /* make directory now if CREATE_EMPTY_DIRS is requested and needed */
717 if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0)
718 error = git_futils_mkdir(
719 info->to.ptr, NULL, info->dirmode, info->mkdir_flags);
720
721 /* recurse onto target directory */
722 if (!exists || S_ISDIR(to_st.st_mode))
723 error = git_path_direach(from, _cp_r_callback, info);
724
725 if (oldmode != 0)
726 info->dirmode = oldmode;
727
728 return error;
729 }
730
731 if (exists) {
732 if ((info->flags & GIT_CPDIR_OVERWRITE) == 0)
733 return 0;
734
735 if (p_unlink(info->to.ptr) < 0) {
736 giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'",
737 info->to.ptr);
738 return -1;
739 }
740 }
741
742 /* Done if this isn't a regular file or a symlink */
743 if (!S_ISREG(from_st.st_mode) &&
744 (!S_ISLNK(from_st.st_mode) ||
745 (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0))
746 return 0;
747
748 /* Make container directory on demand if needed */
749 if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
750 git_futils_mkdir(
751 info->to.ptr, NULL, info->dirmode, info->mkdir_flags) < 0)
752 return -1;
753
754 /* make symlink or regular file */
755 if (S_ISLNK(from_st.st_mode))
85bd1746 756 return cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size);
ca1b6e54 757 else
85bd1746 758 return git_futils_cp(from->ptr, info->to.ptr, from_st.st_mode);
ca1b6e54
RB
759}
760
761int git_futils_cp_r(
762 const char *from,
763 const char *to,
764 uint32_t flags,
765 mode_t dirmode)
766{
767 int error;
768 git_buf path = GIT_BUF_INIT;
769 cp_r_info info;
770
771 if (git_buf_sets(&path, from) < 0)
772 return -1;
773
774 info.to_root = to;
775 info.flags = flags;
776 info.dirmode = dirmode;
777 info.from_prefix = path.size;
778 git_buf_init(&info.to, 0);
779
780 /* precalculate mkdir flags */
781 if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) {
782 info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST;
783 if ((flags & GIT_CPDIR_CHMOD) != 0)
784 info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH;
785 } else {
786 info.mkdir_flags =
787 ((flags & GIT_CPDIR_CHMOD) != 0) ? GIT_MKDIR_CHMOD : 0;
788 }
789
790 error = _cp_r_callback(&info, &path);
791
792 git_buf_free(&path);
07c06f7a 793 git_buf_free(&info.to);
ca1b6e54
RB
794
795 return error;
796}
744cc03e 797
c1f61af6
VM
798int git_futils_filestamp_check(
799 git_futils_filestamp *stamp, const char *path)
744cc03e
RB
800{
801 struct stat st;
802
c8b511f3
RB
803 /* if the stamp is NULL, then always reload */
804 if (stamp == NULL)
744cc03e
RB
805 return 1;
806
807 if (p_stat(path, &st) < 0)
808 return GIT_ENOTFOUND;
809
c8b511f3
RB
810 if (stamp->mtime == (git_time_t)st.st_mtime &&
811 stamp->size == (git_off_t)st.st_size &&
812 stamp->ino == (unsigned int)st.st_ino)
744cc03e
RB
813 return 0;
814
c8b511f3
RB
815 stamp->mtime = (git_time_t)st.st_mtime;
816 stamp->size = (git_off_t)st.st_size;
817 stamp->ino = (unsigned int)st.st_ino;
744cc03e
RB
818
819 return 1;
820}
821
c1f61af6
VM
822void git_futils_filestamp_set(
823 git_futils_filestamp *target, const git_futils_filestamp *source)
c8b511f3
RB
824{
825 assert(target);
826
827 if (source)
828 memcpy(target, source, sizeof(*target));
829 else
830 memset(target, 0, sizeof(*target));
831}