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