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