]> git.proxmox.com Git - libgit2.git/blame - src/fileops.c
Add config read fns with controlled error behavior
[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"
a3aa5f4d 9#include "global.h"
2e29957a 10#include <ctype.h>
997579be
SS
11#if GIT_WIN32
12#include "win32/findfile.h"
13#endif
ec250c6e 14
ce8cd006 15int git_futils_mkpath2file(const char *file_path, const mode_t mode)
55ffebe3 16{
ca1b6e54 17 return git_futils_mkdir(
331e7de9
RB
18 file_path, NULL, mode,
19 GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
55ffebe3
VM
20}
21
1d3a8aeb 22int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode)
72a3fe42
VM
23{
24 int fd;
1d3a8aeb
ET
25 mode_t mask;
26
27 p_umask(mask = p_umask(0));
72a3fe42 28
97769280
RB
29 git_buf_sets(path_out, filename);
30 git_buf_puts(path_out, "_git2_XXXXXX");
31
32 if (git_buf_oom(path_out))
1a481123 33 return -1;
72a3fe42 34
1a481123
VM
35 if ((fd = p_mkstemp(path_out->ptr)) < 0) {
36 giterr_set(GITERR_OS,
ae9e29fd 37 "Failed to create temporary file '%s'", path_out->ptr);
1a481123
VM
38 return -1;
39 }
72a3fe42 40
1d3a8aeb
ET
41 if (p_chmod(path_out->ptr, (mode & ~mask))) {
42 giterr_set(GITERR_OS,
43 "Failed to set permissions on file '%s'", path_out->ptr);
44 return -1;
45 }
46
f978b748 47 return fd;
72a3fe42
VM
48}
49
ce8cd006 50int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode)
7dd8a9f7 51{
1a481123 52 int fd;
55ffebe3 53
1a481123
VM
54 if (git_futils_mkpath2file(path, dirmode) < 0)
55 return -1;
56
57 fd = p_creat(path, mode);
58 if (fd < 0) {
ae9e29fd 59 giterr_set(GITERR_OS, "Failed to create file '%s'", path);
1a481123
VM
60 return -1;
61 }
62
63 return fd;
55ffebe3
VM
64}
65
33127043 66int git_futils_creat_locked(const char *path, const mode_t mode)
1549cba9 67{
13f36ffb 68 int fd = p_open(path, O_WRONLY | O_CREAT | O_TRUNC |
3d3ea4dc 69 O_EXCL | O_BINARY | O_CLOEXEC, mode);
09719c50 70
1a481123 71 if (fd < 0) {
ae9e29fd 72 giterr_set(GITERR_OS, "Failed to create locked file '%s'", path);
3d276874 73 return errno == EEXIST ? GIT_ELOCKED : -1;
1a481123
VM
74 }
75
76 return fd;
1549cba9
RG
77}
78
ce8cd006 79int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode)
1549cba9 80{
1a481123
VM
81 if (git_futils_mkpath2file(path, dirmode) < 0)
82 return -1;
1549cba9 83
f79026b4 84 return git_futils_creat_locked(path, mode);
ec250c6e
AE
85}
86
deafee7b
RB
87int git_futils_open_ro(const char *path)
88{
89 int fd = p_open(path, O_RDONLY);
14997dc5
RB
90 if (fd < 0)
91 return git_path_set_error(errno, path, "open");
deafee7b
RB
92 return fd;
93}
94
f79026b4 95git_off_t git_futils_filesize(git_file fd)
ec250c6e 96{
7dd8a9f7 97 struct stat sb;
deafee7b
RB
98
99 if (p_fstat(fd, &sb)) {
100 giterr_set(GITERR_OS, "Failed to stat file descriptor");
101 return -1;
102 }
5ad739e8 103
ec250c6e
AE
104 return sb.st_size;
105}
4188d28f 106
b6c93aef
RB
107mode_t git_futils_canonical_mode(mode_t raw_mode)
108{
109 if (S_ISREG(raw_mode))
f240acce 110 return S_IFREG | GIT_PERMS_CANONICAL(raw_mode);
b6c93aef
RB
111 else if (S_ISLNK(raw_mode))
112 return S_IFLNK;
b6c93aef
RB
113 else if (S_ISGITLINK(raw_mode))
114 return S_IFGITLINK;
7c7ff7d1
RB
115 else if (S_ISDIR(raw_mode))
116 return S_IFDIR;
b6c93aef
RB
117 else
118 return 0;
119}
120
60b9d3fc
RB
121int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
122{
de590550 123 ssize_t read_size = 0;
60b9d3fc
RB
124
125 git_buf_clear(buf);
126
127 if (git_buf_grow(buf, len + 1) < 0)
128 return -1;
129
1f35e89d
RB
130 /* p_read loops internally to read len bytes */
131 read_size = p_read(fd, buf->ptr, len);
60b9d3fc 132
c859184b 133 if (read_size != (ssize_t)len) {
1f35e89d
RB
134 giterr_set(GITERR_OS, "Failed to read descriptor");
135 return -1;
60b9d3fc
RB
136 }
137
1f35e89d
RB
138 buf->ptr[read_size] = '\0';
139 buf->size = read_size;
140
60b9d3fc
RB
141 return 0;
142}
143
144int git_futils_readbuffer_updated(
744cc03e 145 git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated)
75d58430
RJ
146{
147 git_file fd;
c3da9f06 148 struct stat st;
744cc03e 149 bool changed = false;
75d58430 150
13224ea4 151 assert(buf && path && *path);
75d58430 152
c3da9f06
CMN
153 if (updated != NULL)
154 *updated = 0;
75d58430 155
14997dc5
RB
156 if (p_stat(path, &st) < 0)
157 return git_path_set_error(errno, path, "stat");
c3da9f06 158
7c9f5bec
CMN
159
160 if (S_ISDIR(st.st_mode)) {
161 giterr_set(GITERR_INVALID, "requested file is a directory");
162 return GIT_ENOTFOUND;
163 }
164
165 if (!git__is_sizet(st.st_size+1)) {
1a481123
VM
166 giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path);
167 return -1;
13224ea4 168 }
c3da9f06
CMN
169
170 /*
744cc03e
RB
171 * If we were given a time and/or a size, we only want to read the file
172 * if it has been modified.
c3da9f06 173 */
744cc03e
RB
174 if (size && *size != (size_t)st.st_size)
175 changed = true;
176 if (mtime && *mtime != st.st_mtime)
177 changed = true;
178 if (!size && !mtime)
179 changed = true;
180
181 if (!changed) {
13224ea4
VM
182 return 0;
183 }
c3da9f06
CMN
184
185 if (mtime != NULL)
186 *mtime = st.st_mtime;
744cc03e
RB
187 if (size != NULL)
188 *size = (size_t)st.st_size;
c3da9f06 189
9ccdb211
BR
190 if ((fd = git_futils_open_ro(path)) < 0)
191 return fd;
192
60b9d3fc 193 if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) {
e1de726c
RB
194 p_close(fd);
195 return -1;
75d58430
RJ
196 }
197
f79026b4 198 p_close(fd);
75d58430 199
c3da9f06
CMN
200 if (updated != NULL)
201 *updated = 1;
202
13224ea4 203 return 0;
c3da9f06
CMN
204}
205
13224ea4 206int git_futils_readbuffer(git_buf *buf, const char *path)
97769280 207{
744cc03e 208 return git_futils_readbuffer_updated(buf, path, NULL, NULL, NULL);
97769280
RB
209}
210
4742148d
RB
211int git_futils_writebuffer(
212 const git_buf *buf, const char *path, int flags, mode_t mode)
213{
214 int fd, error = 0;
215
216 if (flags <= 0)
217 flags = O_CREAT | O_TRUNC | O_WRONLY;
218 if (!mode)
219 mode = GIT_FILEMODE_BLOB;
220
221 if ((fd = p_open(path, flags, mode)) < 0) {
222 giterr_set(GITERR_OS, "Could not open '%s' for writing", path);
223 return fd;
224 }
225
226 if ((error = p_write(fd, git_buf_cstr(buf), git_buf_len(buf))) < 0) {
227 giterr_set(GITERR_OS, "Could not write to '%s'", path);
228 (void)p_close(fd);
f5254d78 229 return error;
4742148d
RB
230 }
231
232 if ((error = p_close(fd)) < 0)
233 giterr_set(GITERR_OS, "Error while closing '%s'", path);
234
235 return error;
236}
237
ce8cd006 238int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
19a30a3f 239{
deafee7b
RB
240 if (git_futils_mkpath2file(to, dirmode) < 0)
241 return -1;
19a30a3f 242
deafee7b
RB
243 if (p_rename(from, to) < 0) {
244 giterr_set(GITERR_OS, "Failed to rename '%s' to '%s'", from, to);
245 return -1;
246 }
247
248 return 0;
19a30a3f
VM
249}
250
f79026b4 251int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
20e7f426 252{
f79026b4 253 return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
20e7f426
SP
254}
255
74fa4bfa
RB
256int git_futils_mmap_ro_file(git_map *out, const char *path)
257{
0d0fa7c3
RB
258 git_file fd = git_futils_open_ro(path);
259 git_off_t len;
da9abdd6 260 int result;
0d0fa7c3
RB
261
262 if (fd < 0)
263 return fd;
264
265 len = git_futils_filesize(fd);
deafee7b
RB
266 if (!git__is_sizet(len)) {
267 giterr_set(GITERR_OS, "File `%s` too large to mmap", path);
268 return -1;
269 }
0d0fa7c3 270
da9abdd6 271 result = git_futils_mmap_ro(out, fd, 0, (size_t)len);
74fa4bfa
RB
272 p_close(fd);
273 return result;
274}
275
f79026b4 276void git_futils_mmap_free(git_map *out)
20e7f426 277{
f79026b4 278 p_munmap(out);
20e7f426
SP
279}
280
ca1b6e54
RB
281int git_futils_mkdir(
282 const char *path,
283 const char *base,
284 mode_t mode,
285 uint32_t flags)
1a5204a7 286{
f7e56150 287 int error = -1;
97769280 288 git_buf make_path = GIT_BUF_INIT;
2e1fa15f 289 ssize_t root = 0, min_root_len;
f7e56150 290 char lastch = '/', *tail;
999d4405 291 struct stat st;
dc07184f 292
ca1b6e54
RB
293 /* build path and find "root" where we should start calling mkdir */
294 if (git_path_join_unrooted(&make_path, path, base, &root) < 0)
295 return -1;
dc07184f 296
ca1b6e54
RB
297 if (make_path.size == 0) {
298 giterr_set(GITERR_OS, "Attempt to create empty path");
999d4405 299 goto done;
97769280 300 }
f0b2bfe5 301
ca1b6e54
RB
302 /* remove trailing slashes on path */
303 while (make_path.ptr[make_path.size - 1] == '/') {
304 make_path.size--;
305 make_path.ptr[make_path.size] = '\0';
306 }
412de9a6 307
ca1b6e54 308 /* if we are not supposed to made the last element, truncate it */
3c42e4ef
RB
309 if ((flags & GIT_MKDIR_SKIP_LAST2) != 0) {
310 git_buf_rtruncate_at_char(&make_path, '/');
311 flags |= GIT_MKDIR_SKIP_LAST;
312 }
ca1b6e54
RB
313 if ((flags & GIT_MKDIR_SKIP_LAST) != 0)
314 git_buf_rtruncate_at_char(&make_path, '/');
315
f7e56150
RB
316 /* if nothing left after truncation, then we're done! */
317 if (!make_path.size) {
318 error = 0;
319 goto done;
320 }
321
ca1b6e54
RB
322 /* if we are not supposed to make the whole path, reset root */
323 if ((flags & GIT_MKDIR_PATH) == 0)
324 root = git_buf_rfind(&make_path, '/');
325
f7e56150
RB
326 /* advance root past drive name or network mount prefix */
327 min_root_len = git_path_root(make_path.ptr);
328 if (root < min_root_len)
329 root = min_root_len;
2da72fb2 330 while (root >= 0 && make_path.ptr[root] == '/')
f7e56150
RB
331 ++root;
332
ca1b6e54 333 /* clip root to make_path length */
f7e56150
RB
334 if (root > (ssize_t)make_path.size)
335 root = (ssize_t)make_path.size; /* i.e. NUL byte of string */
0e26202c
RB
336 if (root < 0)
337 root = 0;
ca1b6e54 338
f7e56150
RB
339 /* walk down tail of path making each directory */
340 for (tail = &make_path.ptr[root]; *tail; *tail = lastch) {
999d4405 341
ca1b6e54
RB
342 /* advance tail to include next path component */
343 while (*tail == '/')
344 tail++;
345 while (*tail && *tail != '/')
346 tail++;
347
348 /* truncate path at next component */
349 lastch = *tail;
350 *tail = '\0';
999d4405 351 st.st_mode = 0;
ca1b6e54
RB
352
353 /* make directory */
331e7de9 354 if (p_mkdir(make_path.ptr, mode) < 0) {
c2408a69 355 int tmp_errno = giterr_system_last();
999d4405
RB
356
357 /* ignore error if directory already exists */
022a45e0 358 if (p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode)) {
c2408a69 359 giterr_system_set(tmp_errno);
f7e56150 360 giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path.ptr);
999d4405 361 goto done;
6c72035f
PK
362 }
363
999d4405
RB
364 /* with exclusive create, existing dir is an error */
365 if ((flags & GIT_MKDIR_EXCL) != 0) {
f7e56150 366 giterr_set(GITERR_OS, "Directory already exists '%s'", make_path.ptr);
6c72035f 367 error = GIT_EEXISTS;
999d4405 368 goto done;
331e7de9 369 }
d5f25204 370 }
40c44d2f 371
999d4405 372 /* chmod if requested and necessary */
f7e56150
RB
373 if (((flags & GIT_MKDIR_CHMOD_PATH) != 0 ||
374 (lastch == '\0' && (flags & GIT_MKDIR_CHMOD) != 0)) &&
375 st.st_mode != mode &&
376 (error = p_chmod(make_path.ptr, mode)) < 0) {
377 giterr_set(GITERR_OS, "Failed to set permissions on '%s'", make_path.ptr);
378 goto done;
ca1b6e54 379 }
a993e4fe 380 }
40c44d2f 381
999d4405
RB
382 error = 0;
383
384 /* check that full path really is a directory if requested & needed */
f7e56150
RB
385 if ((flags & GIT_MKDIR_VERIFY_DIR) != 0 &&
386 lastch != '\0' &&
999d4405 387 (p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode))) {
f7e56150 388 giterr_set(GITERR_OS, "Path is not a directory '%s'", make_path.ptr);
999d4405
RB
389 error = GIT_ENOTFOUND;
390 }
77c3999c 391
999d4405 392done:
ca1b6e54 393 git_buf_free(&make_path);
331e7de9 394 return error;
ca1b6e54 395}
77c3999c 396
ca1b6e54
RB
397int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
398{
399 return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH);
170d3f2f 400}
401
331e7de9 402typedef struct {
ad9a921b 403 const char *base;
7e5c8a5b 404 size_t baselen;
331e7de9
RB
405 uint32_t flags;
406 int error;
219d3457 407 int depth;
331e7de9
RB
408} futils__rmdir_data;
409
219d3457
RB
410#define FUTILS_MAX_DEPTH 100
411
331e7de9 412static int futils__error_cannot_rmdir(const char *path, const char *filemsg)
42b3a460 413{
331e7de9
RB
414 if (filemsg)
415 giterr_set(GITERR_OS, "Could not remove directory. File '%s' %s",
416 path, filemsg);
417 else
418 giterr_set(GITERR_OS, "Could not remove directory '%s'", path);
555aa453 419
331e7de9
RB
420 return -1;
421}
deafee7b 422
ad9a921b
RB
423static int futils__rm_first_parent(git_buf *path, const char *ceiling)
424{
425 int error = GIT_ENOTFOUND;
426 struct stat st;
427
428 while (error == GIT_ENOTFOUND) {
429 git_buf_rtruncate_at_char(path, '/');
430
431 if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0)
432 error = 0;
cccacac5 433 else if (p_lstat_posixly(path->ptr, &st) == 0) {
ad9a921b
RB
434 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
435 error = p_unlink(path->ptr);
436 else if (!S_ISDIR(st.st_mode))
437 error = -1; /* fail to remove non-regular file */
438 } else if (errno != ENOTDIR)
439 error = -1;
440 }
441
442 if (error)
443 futils__error_cannot_rmdir(path->ptr, "cannot remove parent");
444
445 return error;
446}
447
331e7de9
RB
448static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
449{
450 futils__rmdir_data *data = opaque;
14997dc5
RB
451 int error = data->error;
452 struct stat st;
555aa453 453
14997dc5
RB
454 if (data->depth > FUTILS_MAX_DEPTH)
455 error = futils__error_cannot_rmdir(
456 path->ptr, "directory nesting too deep");
219d3457 457
14997dc5 458 else if ((error = p_lstat_posixly(path->ptr, &st)) < 0) {
ad9a921b 459 if (errno == ENOENT)
14997dc5 460 error = 0;
ad9a921b
RB
461 else if (errno == ENOTDIR) {
462 /* asked to remove a/b/c/d/e and a/b is a normal file */
463 if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0)
14997dc5 464 error = futils__rm_first_parent(path, data->base);
ad9a921b
RB
465 else
466 futils__error_cannot_rmdir(
467 path->ptr, "parent is not directory");
468 }
469 else
14997dc5 470 error = git_path_set_error(errno, path->ptr, "rmdir");
ad9a921b
RB
471 }
472
473 else if (S_ISDIR(st.st_mode)) {
219d3457
RB
474 data->depth++;
475
14997dc5
RB
476 error = git_path_direach(path, 0, futils__rmdir_recurs_foreach, data);
477 if (error < 0)
478 return (error == GIT_EUSER) ? data->error : error;
219d3457
RB
479
480 data->depth--;
481
482 if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0)
483 return data->error;
331e7de9 484
14997dc5 485 if ((error = p_rmdir(path->ptr)) < 0) {
331e7de9 486 if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
e09d18ee 487 (errno == ENOTEMPTY || errno == EEXIST || errno == EBUSY))
14997dc5 488 error = 0;
331e7de9 489 else
14997dc5 490 error = git_path_set_error(errno, path->ptr, "rmdir");
deafee7b 491 }
858dba58
VM
492 }
493
331e7de9 494 else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) {
14997dc5
RB
495 if (p_unlink(path->ptr) < 0)
496 error = git_path_set_error(errno, path->ptr, "remove");
deafee7b
RB
497 }
498
ad9a921b 499 else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
14997dc5 500 error = futils__error_cannot_rmdir(path->ptr, "still present");
555aa453 501
14997dc5
RB
502 data->error = error;
503 return error;
331e7de9
RB
504}
505
506static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
507{
7e5c8a5b
RB
508 futils__rmdir_data *data = opaque;
509 int error;
510
511 if (git_buf_len(path) <= data->baselen)
512 return GIT_ITEROVER;
331e7de9 513
7e5c8a5b 514 error = p_rmdir(git_buf_cstr(path));
331e7de9
RB
515
516 if (error) {
517 int en = errno;
518
519 if (en == ENOENT || en == ENOTDIR) {
520 giterr_clear();
521 error = 0;
e09d18ee 522 } else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) {
331e7de9
RB
523 giterr_clear();
524 error = GIT_ITEROVER;
525 } else {
14997dc5 526 error = git_path_set_error(errno, git_buf_cstr(path), "rmdir");
331e7de9
RB
527 }
528 }
529
530 return error;
42b3a460
MS
531}
532
0d64bef9 533int git_futils_rmdir_r(
331e7de9 534 const char *path, const char *base, uint32_t flags)
42b3a460 535{
97769280 536 int error;
0d64bef9 537 git_buf fullpath = GIT_BUF_INIT;
219d3457 538 futils__rmdir_data data = { 0 };
0d64bef9
RB
539
540 /* build path and find "root" where we should start calling mkdir */
541 if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
542 return -1;
543
7e5c8a5b
RB
544 data.base = base ? base : "";
545 data.baselen = base ? strlen(base) : 0;
546 data.flags = flags;
331e7de9
RB
547
548 error = futils__rmdir_recurs_foreach(&data, &fullpath);
549
550 /* remove now-empty parents if requested */
551 if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0) {
552 error = git_path_walk_up(
553 &fullpath, base, futils__rmdir_empty_parent, &data);
554
555 if (error == GIT_ITEROVER)
556 error = 0;
557 }
0d64bef9
RB
558
559 git_buf_free(&fullpath);
97769280 560
97769280 561 return error;
42b3a460
MS
562}
563
5540d947 564
41954a49 565static int git_futils_guess_system_dirs(git_buf *out)
32a4e3b7
SS
566{
567#ifdef GIT_WIN32
f84bc388 568 return git_win32__find_system_dirs(out, L"etc\\");
5540d947 569#else
41954a49 570 return git_buf_sets(out, "/etc");
5540d947
RB
571#endif
572}
32a4e3b7 573
41954a49 574static int git_futils_guess_global_dirs(git_buf *out)
5540d947
RB
575{
576#ifdef GIT_WIN32
32460251 577 return git_win32__find_global_dirs(out);
349fb6d7 578#else
41954a49 579 return git_buf_sets(out, getenv("HOME"));
5540d947
RB
580#endif
581}
73b51450 582
41954a49 583static int git_futils_guess_xdg_dirs(git_buf *out)
5540d947
RB
584{
585#ifdef GIT_WIN32
32460251 586 return git_win32__find_xdg_dirs(out);
5540d947 587#else
5540d947
RB
588 const char *env = NULL;
589
590 if ((env = getenv("XDG_CONFIG_HOME")) != NULL)
41954a49 591 return git_buf_joinpath(out, env, "git");
5540d947 592 else if ((env = getenv("HOME")) != NULL)
41954a49 593 return git_buf_joinpath(out, env, ".config/git");
5540d947 594
41954a49
RB
595 git_buf_clear(out);
596 return 0;
32a4e3b7 597#endif
5540d947 598}
73b51450 599
7e8934bb
L
600static int git_futils_guess_template_dirs(git_buf *out)
601{
602#ifdef GIT_WIN32
603 return git_win32__find_system_dirs(out, L"share\\git-core\\templates");
604#else
605 return git_buf_sets(out, "/usr/share/git-core/templates");
606#endif
607}
608
41954a49
RB
609typedef int (*git_futils_dirs_guess_cb)(git_buf *out);
610
611static git_buf git_futils__dirs[GIT_FUTILS_DIR__MAX] =
7e8934bb 612 { GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT };
5540d947 613
5540d947
RB
614static git_futils_dirs_guess_cb git_futils__dir_guess[GIT_FUTILS_DIR__MAX] = {
615 git_futils_guess_system_dirs,
616 git_futils_guess_global_dirs,
617 git_futils_guess_xdg_dirs,
7e8934bb 618 git_futils_guess_template_dirs,
5540d947
RB
619};
620
39b1ad7f 621void git_futils_dirs_global_shutdown(void)
a3aa5f4d
RB
622{
623 int i;
624 for (i = 0; i < GIT_FUTILS_DIR__MAX; ++i)
625 git_buf_free(&git_futils__dirs[i]);
626}
627
0a1c8f55
ET
628int git_futils_dirs_global_init(void)
629{
630 git_futils_dir_t i;
989710d9 631 const git_buf *path;
0a1c8f55
ET
632 int error = 0;
633
989710d9
RB
634 for (i = 0; !error && i < GIT_FUTILS_DIR__MAX; i++)
635 error = git_futils_dirs_get(&path, i);
0a1c8f55 636
a3aa5f4d
RB
637 git__on_shutdown(git_futils_dirs_global_shutdown);
638
0a1c8f55
ET
639 return error;
640}
641
5540d947
RB
642static int git_futils_check_selector(git_futils_dir_t which)
643{
644 if (which < GIT_FUTILS_DIR__MAX)
645 return 0;
646 giterr_set(GITERR_INVALID, "config directory selector out of range");
647 return -1;
349fb6d7 648}
73b51450 649
41954a49 650int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which)
349fb6d7 651{
41954a49 652 assert(out);
349fb6d7 653
5540d947 654 *out = NULL;
29ef309e 655
5540d947 656 GITERR_CHECK_ERROR(git_futils_check_selector(which));
0d422ec9 657
41954a49
RB
658 if (!git_buf_len(&git_futils__dirs[which]))
659 GITERR_CHECK_ERROR(
660 git_futils__dir_guess[which](&git_futils__dirs[which]));
349fb6d7 661
5540d947
RB
662 *out = &git_futils__dirs[which];
663 return 0;
664}
665
41954a49 666int git_futils_dirs_get_str(char *out, size_t outlen, git_futils_dir_t which)
5540d947 667{
41954a49
RB
668 const git_buf *path = NULL;
669
5540d947 670 GITERR_CHECK_ERROR(git_futils_check_selector(which));
41954a49
RB
671 GITERR_CHECK_ERROR(git_futils_dirs_get(&path, which));
672
673 if (!out || path->size >= outlen) {
674 giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path");
675 return GIT_EBUFS;
676 }
5540d947 677
41954a49
RB
678 git_buf_copy_cstr(out, outlen, path);
679 return 0;
680}
5540d947 681
41954a49
RB
682#define PATH_MAGIC "$PATH"
683
684int git_futils_dirs_set(git_futils_dir_t which, const char *search_path)
685{
686 const char *expand_path = NULL;
687 git_buf merge = GIT_BUF_INIT;
688
689 GITERR_CHECK_ERROR(git_futils_check_selector(which));
690
691 if (search_path != NULL)
692 expand_path = strstr(search_path, PATH_MAGIC);
693
694 /* init with default if not yet done and needed (ignoring error) */
695 if ((!search_path || expand_path) &&
696 !git_buf_len(&git_futils__dirs[which]))
5540d947
RB
697 git_futils__dir_guess[which](&git_futils__dirs[which]);
698
41954a49
RB
699 /* if $PATH is not referenced, then just set the path */
700 if (!expand_path)
701 return git_buf_sets(&git_futils__dirs[which], search_path);
702
703 /* otherwise set to join(before $PATH, old value, after $PATH) */
704 if (expand_path > search_path)
705 git_buf_set(&merge, search_path, expand_path - search_path);
706
707 if (git_buf_len(&git_futils__dirs[which]))
708 git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR,
709 merge.ptr, git_futils__dirs[which].ptr);
710
711 expand_path += strlen(PATH_MAGIC);
712 if (*expand_path)
713 git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path);
714
715 git_buf_swap(&git_futils__dirs[which], &merge);
716 git_buf_free(&merge);
717
718 return git_buf_oom(&git_futils__dirs[which]) ? -1 : 0;
719}
720
5540d947
RB
721static int git_futils_find_in_dirlist(
722 git_buf *path, const char *name, git_futils_dir_t which, const char *label)
723{
41954a49
RB
724 size_t len;
725 const char *scan, *next = NULL;
726 const git_buf *syspath;
5540d947 727
41954a49 728 GITERR_CHECK_ERROR(git_futils_dirs_get(&syspath, which));
5540d947 729
41954a49
RB
730 for (scan = git_buf_cstr(syspath); scan; scan = next) {
731 for (next = strchr(scan, GIT_PATH_LIST_SEPARATOR);
732 next && next > scan && next[-1] == '\\';
733 next = strchr(next + 1, GIT_PATH_LIST_SEPARATOR))
734 /* find unescaped separator or end of string */;
735
736 len = next ? (size_t)(next++ - scan) : strlen(scan);
737 if (!len)
0d422ec9
RB
738 continue;
739
41954a49 740 GITERR_CHECK_ERROR(git_buf_set(path, scan, len));
a025907e
L
741 if (name)
742 GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));
349fb6d7 743
5540d947 744 if (git_path_exists(path->ptr))
aed8f8a1 745 return 0;
349fb6d7
VM
746 }
747
0d422ec9 748 git_buf_clear(path);
5540d947 749 giterr_set(GITERR_OS, "The %s file '%s' doesn't exist", label, name);
0d422ec9 750 return GIT_ENOTFOUND;
5540d947 751}
349fb6d7 752
5540d947
RB
753int git_futils_find_system_file(git_buf *path, const char *filename)
754{
755 return git_futils_find_in_dirlist(
756 path, filename, GIT_FUTILS_DIR_SYSTEM, "system");
757}
349fb6d7 758
5540d947
RB
759int git_futils_find_global_file(git_buf *path, const char *filename)
760{
761 return git_futils_find_in_dirlist(
762 path, filename, GIT_FUTILS_DIR_GLOBAL, "global");
763}
349fb6d7 764
5540d947
RB
765int git_futils_find_xdg_file(git_buf *path, const char *filename)
766{
767 return git_futils_find_in_dirlist(
768 path, filename, GIT_FUTILS_DIR_XDG, "global/xdg");
73b51450 769}
8651c10f 770
a025907e
L
771int git_futils_find_template_dir(git_buf *path)
772{
773 return git_futils_find_in_dirlist(
774 path, NULL, GIT_FUTILS_DIR_TEMPLATE, "template");
775}
776
8651c10f
BS
777int git_futils_fake_symlink(const char *old, const char *new)
778{
779 int retcode = GIT_ERROR;
780 int fd = git_futils_creat_withpath(new, 0755, 0644);
781 if (fd >= 0) {
782 retcode = p_write(fd, old, strlen(old));
783 p_close(fd);
784 }
785 return retcode;
786}
ca1b6e54 787
85bd1746 788static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done)
ca1b6e54
RB
789{
790 int error = 0;
791 char buffer[4096];
792 ssize_t len = 0;
793
794 while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0)
795 /* p_write() does not have the same semantics as write(). It loops
796 * internally and will return 0 when it has completed writing.
797 */
798 error = p_write(ofd, buffer, len);
799
800 if (len < 0) {
801 giterr_set(GITERR_OS, "Read error while copying file");
802 error = (int)len;
803 }
804
85bd1746 805 if (close_fd_when_done) {
ca1b6e54
RB
806 p_close(ifd);
807 p_close(ofd);
808 }
809
810 return error;
811}
812
85bd1746 813int git_futils_cp(const char *from, const char *to, mode_t filemode)
ca1b6e54
RB
814{
815 int ifd, ofd;
816
ca1b6e54
RB
817 if ((ifd = git_futils_open_ro(from)) < 0)
818 return ifd;
819
820 if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
ca1b6e54 821 p_close(ifd);
14997dc5 822 return git_path_set_error(errno, to, "open for writing");
ca1b6e54
RB
823 }
824
85bd1746 825 return cp_by_fd(ifd, ofd, true);
ca1b6e54
RB
826}
827
85bd1746 828static int cp_link(const char *from, const char *to, size_t link_size)
ca1b6e54
RB
829{
830 int error = 0;
831 ssize_t read_len;
85bd1746 832 char *link_data = git__malloc(link_size + 1);
ca1b6e54
RB
833 GITERR_CHECK_ALLOC(link_data);
834
85bd1746
RB
835 read_len = p_readlink(from, link_data, link_size);
836 if (read_len != (ssize_t)link_size) {
ca1b6e54
RB
837 giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from);
838 error = -1;
839 }
840 else {
841 link_data[read_len] = '\0';
842
843 if (p_symlink(link_data, to) < 0) {
844 giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'",
845 link_data, to);
846 error = -1;
847 }
848 }
849
850 git__free(link_data);
851 return error;
852}
853
854typedef struct {
855 const char *to_root;
856 git_buf to;
857 ssize_t from_prefix;
858 uint32_t flags;
859 uint32_t mkdir_flags;
860 mode_t dirmode;
d0cd6c42 861 int error;
ca1b6e54
RB
862} cp_r_info;
863
3c42e4ef
RB
864#define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10)
865
866static int _cp_r_mkdir(cp_r_info *info, git_buf *from)
867{
868 int error = 0;
869
870 /* create root directory the first time we need to create a directory */
871 if ((info->flags & GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT) == 0) {
872 error = git_futils_mkdir(
873 info->to_root, NULL, info->dirmode,
18f08264 874 (info->flags & GIT_CPDIR_CHMOD_DIRS) ? GIT_MKDIR_CHMOD : 0);
3c42e4ef
RB
875
876 info->flags |= GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT;
877 }
878
879 /* create directory with root as base to prevent excess chmods */
880 if (!error)
881 error = git_futils_mkdir(
882 from->ptr + info->from_prefix, info->to_root,
883 info->dirmode, info->mkdir_flags);
884
885 return error;
886}
887
ca1b6e54
RB
888static int _cp_r_callback(void *ref, git_buf *from)
889{
3c42e4ef 890 int error = 0;
ca1b6e54
RB
891 cp_r_info *info = ref;
892 struct stat from_st, to_st;
893 bool exists = false;
894
895 if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 &&
896 from->ptr[git_path_basename_offset(from)] == '.')
897 return 0;
898
899 if (git_buf_joinpath(
d0cd6c42 900 &info->to, info->to_root, from->ptr + info->from_prefix) < 0) {
901 error = -1;
902 goto exit;
903 }
ca1b6e54 904
14997dc5 905 if (!(error = git_path_lstat(info->to.ptr, &to_st)))
ca1b6e54 906 exists = true;
14997dc5
RB
907 else if (error != GIT_ENOTFOUND)
908 goto exit;
909 else {
910 giterr_clear();
911 error = 0;
912 }
ca1b6e54 913
3c42e4ef 914 if ((error = git_path_lstat(from->ptr, &from_st)) < 0)
d0cd6c42 915 goto exit;
ca1b6e54
RB
916
917 if (S_ISDIR(from_st.st_mode)) {
ca1b6e54
RB
918 mode_t oldmode = info->dirmode;
919
920 /* if we are not chmod'ing, then overwrite dirmode */
18f08264 921 if ((info->flags & GIT_CPDIR_CHMOD_DIRS) == 0)
ca1b6e54
RB
922 info->dirmode = from_st.st_mode;
923
924 /* make directory now if CREATE_EMPTY_DIRS is requested and needed */
925 if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0)
3c42e4ef 926 error = _cp_r_mkdir(info, from);
ca1b6e54
RB
927
928 /* recurse onto target directory */
219d3457
RB
929 if (!error && (!exists || S_ISDIR(to_st.st_mode))) {
930 error = git_path_direach(from, 0, _cp_r_callback, info);
931
932 if (error == GIT_EUSER)
933 error = info->error;
934 }
ca1b6e54
RB
935
936 if (oldmode != 0)
937 info->dirmode = oldmode;
938
d0cd6c42 939 goto exit;
ca1b6e54
RB
940 }
941
942 if (exists) {
943 if ((info->flags & GIT_CPDIR_OVERWRITE) == 0)
944 return 0;
945
946 if (p_unlink(info->to.ptr) < 0) {
947 giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'",
948 info->to.ptr);
d0cd6c42 949 error = -1;
950 goto exit;
ca1b6e54
RB
951 }
952 }
953
954 /* Done if this isn't a regular file or a symlink */
955 if (!S_ISREG(from_st.st_mode) &&
956 (!S_ISLNK(from_st.st_mode) ||
957 (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0))
958 return 0;
959
960 /* Make container directory on demand if needed */
961 if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
3c42e4ef 962 (error = _cp_r_mkdir(info, from)) < 0)
d0cd6c42 963 goto exit;
ca1b6e54
RB
964
965 /* make symlink or regular file */
966 if (S_ISLNK(from_st.st_mode))
3c42e4ef
RB
967 error = cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size);
968 else {
969 mode_t usemode = from_st.st_mode;
970
18f08264 971 if ((info->flags & GIT_CPDIR_SIMPLE_TO_MODE) != 0)
f240acce 972 usemode = GIT_PERMS_FOR_WRITE(usemode);
3c42e4ef
RB
973
974 error = git_futils_cp(from->ptr, info->to.ptr, usemode);
3c42e4ef
RB
975 }
976
d0cd6c42 977exit:
978 info->error = error;
3c42e4ef 979 return error;
ca1b6e54
RB
980}
981
982int git_futils_cp_r(
983 const char *from,
984 const char *to,
985 uint32_t flags,
986 mode_t dirmode)
987{
988 int error;
989 git_buf path = GIT_BUF_INIT;
990 cp_r_info info;
991
3c42e4ef 992 if (git_buf_joinpath(&path, from, "") < 0) /* ensure trailing slash */
ca1b6e54
RB
993 return -1;
994
995 info.to_root = to;
996 info.flags = flags;
997 info.dirmode = dirmode;
998 info.from_prefix = path.size;
d0cd6c42 999 info.error = 0;
ca1b6e54
RB
1000 git_buf_init(&info.to, 0);
1001
1002 /* precalculate mkdir flags */
1003 if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) {
3c42e4ef
RB
1004 /* if not creating empty dirs, then use mkdir to create the path on
1005 * demand right before files are copied.
1006 */
ca1b6e54 1007 info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST;
18f08264 1008 if ((flags & GIT_CPDIR_CHMOD_DIRS) != 0)
ca1b6e54
RB
1009 info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH;
1010 } else {
3c42e4ef 1011 /* otherwise, we will do simple mkdir as directories are encountered */
ca1b6e54 1012 info.mkdir_flags =
18f08264 1013 ((flags & GIT_CPDIR_CHMOD_DIRS) != 0) ? GIT_MKDIR_CHMOD : 0;
ca1b6e54
RB
1014 }
1015
1016 error = _cp_r_callback(&info, &path);
1017
1018 git_buf_free(&path);
07c06f7a 1019 git_buf_free(&info.to);
ca1b6e54 1020
d0cd6c42 1021 if (error == GIT_EUSER)
1022 error = info.error;
1023
ca1b6e54
RB
1024 return error;
1025}
744cc03e 1026
c1f61af6
VM
1027int git_futils_filestamp_check(
1028 git_futils_filestamp *stamp, const char *path)
744cc03e
RB
1029{
1030 struct stat st;
1031
c8b511f3
RB
1032 /* if the stamp is NULL, then always reload */
1033 if (stamp == NULL)
744cc03e
RB
1034 return 1;
1035
e830c020
RB
1036 if (p_stat(path, &st) < 0) {
1037 giterr_set(GITERR_OS, "Could not stat '%s'", path);
744cc03e 1038 return GIT_ENOTFOUND;
e830c020 1039 }
744cc03e 1040
c8b511f3
RB
1041 if (stamp->mtime == (git_time_t)st.st_mtime &&
1042 stamp->size == (git_off_t)st.st_size &&
1043 stamp->ino == (unsigned int)st.st_ino)
744cc03e
RB
1044 return 0;
1045
c8b511f3
RB
1046 stamp->mtime = (git_time_t)st.st_mtime;
1047 stamp->size = (git_off_t)st.st_size;
1048 stamp->ino = (unsigned int)st.st_ino;
744cc03e
RB
1049
1050 return 1;
1051}
1052
c1f61af6
VM
1053void git_futils_filestamp_set(
1054 git_futils_filestamp *target, const git_futils_filestamp *source)
c8b511f3
RB
1055{
1056 assert(target);
1057
1058 if (source)
1059 memcpy(target, source, sizeof(*target));
1060 else
1061 memset(target, 0, sizeof(*target));
1062}