]> git.proxmox.com Git - libgit2.git/blame - src/util/futils.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / src / util / futils.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 */
eae0bfdc 7
22a2d3d5 8#include "futils.h"
eae0bfdc 9
c25aa7cd 10#include "runtime.h"
500ec543 11#include "strmap.h"
c25aa7cd 12#include "hash.h"
e579e0f7
MB
13#include "rand.h"
14
2e29957a 15#include <ctype.h>
997579be
SS
16#if GIT_WIN32
17#include "win32/findfile.h"
18#endif
ec250c6e 19
e579e0f7
MB
20#define GIT_FILEMODE_DEFAULT 0100666
21
ce8cd006 22int git_futils_mkpath2file(const char *file_path, const mode_t mode)
55ffebe3 23{
ca1b6e54 24 return git_futils_mkdir(
ac2fba0e 25 file_path, mode,
331e7de9 26 GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
55ffebe3
VM
27}
28
e579e0f7 29int git_futils_mktmp(git_str *path_out, const char *filename, mode_t mode)
72a3fe42 30{
e579e0f7
MB
31 const int open_flags = O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC;
32 unsigned int tries = 32;
72a3fe42
VM
33 int fd;
34
e579e0f7
MB
35 while (tries--) {
36 uint64_t rand = git_rand_next();
97769280 37
e579e0f7
MB
38 git_str_sets(path_out, filename);
39 git_str_puts(path_out, "_git2_");
40 git_str_encode_hexstr(path_out, (void *)&rand, sizeof(uint64_t));
72a3fe42 41
e579e0f7
MB
42 if (git_str_oom(path_out))
43 return -1;
72a3fe42 44
e579e0f7
MB
45 /* Note that we open with O_CREAT | O_EXCL */
46 if ((fd = p_open(path_out->ptr, open_flags, mode)) >= 0)
47 return fd;
1d3a8aeb
ET
48 }
49
e579e0f7
MB
50 git_error_set(GIT_ERROR_OS,
51 "failed to create temporary file '%s'", path_out->ptr);
52 git_str_dispose(path_out);
53 return -1;
72a3fe42
VM
54}
55
ce8cd006 56int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode)
7dd8a9f7 57{
1a481123 58 int fd;
55ffebe3 59
1a481123
VM
60 if (git_futils_mkpath2file(path, dirmode) < 0)
61 return -1;
62
63 fd = p_creat(path, mode);
64 if (fd < 0) {
ac3d33df 65 git_error_set(GIT_ERROR_OS, "failed to create file '%s'", path);
1a481123
VM
66 return -1;
67 }
68
69 return fd;
55ffebe3
VM
70}
71
33127043 72int git_futils_creat_locked(const char *path, const mode_t mode)
1549cba9 73{
4a26915d
ET
74 int fd = p_open(path, O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC,
75 mode);
09719c50 76
1a481123 77 if (fd < 0) {
f94825c1 78 int error = errno;
ac3d33df 79 git_error_set(GIT_ERROR_OS, "failed to create locked file '%s'", path);
f94825c1
CMN
80 switch (error) {
81 case EEXIST:
82 return GIT_ELOCKED;
83 case ENOENT:
84 return GIT_ENOTFOUND;
85 default:
86 return -1;
87 }
1a481123
VM
88 }
89
90 return fd;
1549cba9
RG
91}
92
ce8cd006 93int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode)
1549cba9 94{
1a481123
VM
95 if (git_futils_mkpath2file(path, dirmode) < 0)
96 return -1;
1549cba9 97
f79026b4 98 return git_futils_creat_locked(path, mode);
ec250c6e
AE
99}
100
deafee7b
RB
101int git_futils_open_ro(const char *path)
102{
103 int fd = p_open(path, O_RDONLY);
14997dc5 104 if (fd < 0)
e579e0f7 105 return git_fs_path_set_error(errno, path, "open");
deafee7b
RB
106 return fd;
107}
108
eae0bfdc
PP
109int git_futils_truncate(const char *path, int mode)
110{
111 int fd = p_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, mode);
112 if (fd < 0)
e579e0f7 113 return git_fs_path_set_error(errno, path, "open");
eae0bfdc
PP
114
115 close(fd);
116 return 0;
117}
118
22a2d3d5 119int git_futils_filesize(uint64_t *out, git_file fd)
ec250c6e 120{
7dd8a9f7 121 struct stat sb;
deafee7b
RB
122
123 if (p_fstat(fd, &sb)) {
ac3d33df 124 git_error_set(GIT_ERROR_OS, "failed to stat file descriptor");
deafee7b
RB
125 return -1;
126 }
5ad739e8 127
22a2d3d5
UG
128 if (sb.st_size < 0) {
129 git_error_set(GIT_ERROR_INVALID, "invalid file size");
130 return -1;
131 }
132
133 *out = sb.st_size;
134 return 0;
ec250c6e 135}
4188d28f 136
b6c93aef
RB
137mode_t git_futils_canonical_mode(mode_t raw_mode)
138{
139 if (S_ISREG(raw_mode))
f240acce 140 return S_IFREG | GIT_PERMS_CANONICAL(raw_mode);
b6c93aef
RB
141 else if (S_ISLNK(raw_mode))
142 return S_IFLNK;
b6c93aef
RB
143 else if (S_ISGITLINK(raw_mode))
144 return S_IFGITLINK;
7c7ff7d1
RB
145 else if (S_ISDIR(raw_mode))
146 return S_IFDIR;
b6c93aef
RB
147 else
148 return 0;
149}
150
e579e0f7 151int git_futils_readbuffer_fd(git_str *buf, git_file fd, size_t len)
60b9d3fc 152{
de590550 153 ssize_t read_size = 0;
f1453c59 154 size_t alloc_len;
60b9d3fc 155
e579e0f7 156 git_str_clear(buf);
60b9d3fc 157
8d534b47 158 if (!git__is_ssizet(len)) {
ac3d33df 159 git_error_set(GIT_ERROR_INVALID, "read too large");
8d534b47
ET
160 return -1;
161 }
162
ac3d33df 163 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, len, 1);
e579e0f7 164 if (git_str_grow(buf, alloc_len) < 0)
60b9d3fc
RB
165 return -1;
166
1f35e89d
RB
167 /* p_read loops internally to read len bytes */
168 read_size = p_read(fd, buf->ptr, len);
60b9d3fc 169
ad5611d8 170 if (read_size < 0) {
ac3d33df 171 git_error_set(GIT_ERROR_OS, "failed to read descriptor");
e579e0f7 172 git_str_dispose(buf);
1f35e89d 173 return -1;
60b9d3fc
RB
174 }
175
ad5611d8
TR
176 if ((size_t)read_size != len) {
177 git_error_set(GIT_ERROR_FILESYSTEM, "could not read (expected %" PRIuZ " bytes, read %" PRIuZ ")", len, (size_t)read_size);
178 git_str_dispose(buf);
179 return -1;
180 }
181
1f35e89d
RB
182 buf->ptr[read_size] = '\0';
183 buf->size = read_size;
184
60b9d3fc
RB
185 return 0;
186}
187
ad5611d8
TR
188int git_futils_readbuffer_fd_full(git_str *buf, git_file fd)
189{
190 static size_t blocksize = 10240;
191 size_t alloc_len = 0, total_size = 0;
192 ssize_t read_size = 0;
193
194 git_str_clear(buf);
195
196 while (true) {
197 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, blocksize);
198
199 if (git_str_grow(buf, alloc_len) < 0)
200 return -1;
201
202 /* p_read loops internally to read blocksize bytes */
203 read_size = p_read(fd, buf->ptr, blocksize);
204
205 if (read_size < 0) {
206 git_error_set(GIT_ERROR_OS, "failed to read descriptor");
207 git_str_dispose(buf);
208 return -1;
209 }
210
211 total_size += read_size;
212
213 if ((size_t)read_size < blocksize) {
214 break;
215 }
216 }
217
218 buf->ptr[total_size] = '\0';
219 buf->size = total_size;
220
221 return 0;
222}
223
60b9d3fc 224int git_futils_readbuffer_updated(
e579e0f7
MB
225 git_str *out,
226 const char *path,
227 unsigned char checksum[GIT_HASH_SHA1_SIZE],
228 int *updated)
75d58430 229{
eb597799 230 int error;
75d58430 231 git_file fd;
c3da9f06 232 struct stat st;
e579e0f7
MB
233 git_str buf = GIT_STR_INIT;
234 unsigned char checksum_new[GIT_HASH_SHA1_SIZE];
75d58430 235
c25aa7cd
PP
236 GIT_ASSERT_ARG(out);
237 GIT_ASSERT_ARG(path && *path);
75d58430 238
c3da9f06
CMN
239 if (updated != NULL)
240 *updated = 0;
75d58430 241
14997dc5 242 if (p_stat(path, &st) < 0)
e579e0f7 243 return git_fs_path_set_error(errno, path, "stat");
c3da9f06 244
7c9f5bec
CMN
245
246 if (S_ISDIR(st.st_mode)) {
ac3d33df 247 git_error_set(GIT_ERROR_INVALID, "requested file is a directory");
7c9f5bec
CMN
248 return GIT_ENOTFOUND;
249 }
250
251 if (!git__is_sizet(st.st_size+1)) {
ac3d33df 252 git_error_set(GIT_ERROR_OS, "invalid regular file stat for '%s'", path);
1a481123 253 return -1;
13224ea4 254 }
c3da9f06 255
9ccdb211
BR
256 if ((fd = git_futils_open_ro(path)) < 0)
257 return fd;
258
3547b122 259 if (git_futils_readbuffer_fd(&buf, fd, (size_t)st.st_size) < 0) {
e1de726c
RB
260 p_close(fd);
261 return -1;
75d58430
RJ
262 }
263
f79026b4 264 p_close(fd);
75d58430 265
11c8e756 266 if (checksum) {
e579e0f7
MB
267 if ((error = git_hash_buf(checksum_new, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0) {
268 git_str_dispose(&buf);
11c8e756
ET
269 return error;
270 }
eb597799 271
11c8e756
ET
272 /*
273 * If we were given a checksum, we only want to use it if it's different
274 */
e579e0f7
MB
275 if (!memcmp(checksum, checksum_new, GIT_HASH_SHA1_SIZE)) {
276 git_str_dispose(&buf);
11c8e756
ET
277 if (updated)
278 *updated = 0;
eb597799 279
11c8e756
ET
280 return 0;
281 }
282
e579e0f7 283 memcpy(checksum, checksum_new, GIT_HASH_SHA1_SIZE);
eb597799
CMN
284 }
285
286 /*
287 * If we're here, the file did change, or the user didn't have an old version
288 */
c3da9f06
CMN
289 if (updated != NULL)
290 *updated = 1;
291
e579e0f7
MB
292 git_str_swap(out, &buf);
293 git_str_dispose(&buf);
3547b122 294
13224ea4 295 return 0;
c3da9f06
CMN
296}
297
e579e0f7 298int git_futils_readbuffer(git_str *buf, const char *path)
97769280 299{
eb597799 300 return git_futils_readbuffer_updated(buf, path, NULL, NULL);
97769280
RB
301}
302
4742148d 303int git_futils_writebuffer(
e579e0f7 304 const git_str *buf, const char *path, int flags, mode_t mode)
4742148d 305{
5312621b 306 int fd, do_fsync = 0, error = 0;
4742148d 307
5a747e0c 308 if (!flags)
4742148d 309 flags = O_CREAT | O_TRUNC | O_WRONLY;
5a747e0c 310
5312621b
ET
311 if ((flags & O_FSYNC) != 0)
312 do_fsync = 1;
313
314 flags &= ~O_FSYNC;
4742148d 315
4742148d 316 if (!mode)
e579e0f7 317 mode = GIT_FILEMODE_DEFAULT;
4742148d
RB
318
319 if ((fd = p_open(path, flags, mode)) < 0) {
ac3d33df 320 git_error_set(GIT_ERROR_OS, "could not open '%s' for writing", path);
4742148d
RB
321 return fd;
322 }
323
e579e0f7 324 if ((error = p_write(fd, git_str_cstr(buf), git_str_len(buf))) < 0) {
ac3d33df 325 git_error_set(GIT_ERROR_OS, "could not write to '%s'", path);
4742148d 326 (void)p_close(fd);
f5254d78 327 return error;
4742148d
RB
328 }
329
5312621b 330 if (do_fsync && (error = p_fsync(fd)) < 0) {
ac3d33df 331 git_error_set(GIT_ERROR_OS, "could not fsync '%s'", path);
5312621b
ET
332 p_close(fd);
333 return error;
334 }
335
1229e1c4 336 if ((error = p_close(fd)) < 0) {
ac3d33df 337 git_error_set(GIT_ERROR_OS, "error while closing '%s'", path);
1229e1c4
ET
338 return error;
339 }
340
341 if (do_fsync && (flags & O_CREAT))
342 error = git_futils_fsync_parent(path);
4742148d
RB
343
344 return error;
345}
346
ce8cd006 347int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
19a30a3f 348{
deafee7b
RB
349 if (git_futils_mkpath2file(to, dirmode) < 0)
350 return -1;
19a30a3f 351
deafee7b 352 if (p_rename(from, to) < 0) {
ac3d33df 353 git_error_set(GIT_ERROR_OS, "failed to rename '%s' to '%s'", from, to);
deafee7b
RB
354 return -1;
355 }
356
357 return 0;
19a30a3f
VM
358}
359
22a2d3d5 360int git_futils_mmap_ro(git_map *out, git_file fd, off64_t begin, size_t len)
20e7f426 361{
f79026b4 362 return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
20e7f426
SP
363}
364
74fa4bfa
RB
365int git_futils_mmap_ro_file(git_map *out, const char *path)
366{
0d0fa7c3 367 git_file fd = git_futils_open_ro(path);
22a2d3d5 368 uint64_t len;
da9abdd6 369 int result;
0d0fa7c3
RB
370
371 if (fd < 0)
372 return fd;
373
22a2d3d5 374 if ((result = git_futils_filesize(&len, fd)) < 0)
38b6e700 375 goto out;
9daba9f4 376
deafee7b 377 if (!git__is_sizet(len)) {
ac3d33df 378 git_error_set(GIT_ERROR_OS, "file `%s` too large to mmap", path);
38b6e700
PS
379 result = -1;
380 goto out;
deafee7b 381 }
0d0fa7c3 382
da9abdd6 383 result = git_futils_mmap_ro(out, fd, 0, (size_t)len);
38b6e700 384out:
74fa4bfa
RB
385 p_close(fd);
386 return result;
387}
388
f79026b4 389void git_futils_mmap_free(git_map *out)
20e7f426 390{
f79026b4 391 p_munmap(out);
20e7f426
SP
392}
393
81aaf370
ET
394GIT_INLINE(int) mkdir_validate_dir(
395 const char *path,
e74340b0
ET
396 struct stat *st,
397 mode_t mode,
398 uint32_t flags,
81aaf370 399 struct git_futils_mkdir_options *opts)
e74340b0 400{
81aaf370
ET
401 /* with exclusive create, existing dir is an error */
402 if ((flags & GIT_MKDIR_EXCL) != 0) {
ac3d33df 403 git_error_set(GIT_ERROR_FILESYSTEM,
909d5494 404 "failed to make directory '%s': directory exists", path);
81aaf370
ET
405 return GIT_EEXISTS;
406 }
407
e74340b0
ET
408 if ((S_ISREG(st->st_mode) && (flags & GIT_MKDIR_REMOVE_FILES)) ||
409 (S_ISLNK(st->st_mode) && (flags & GIT_MKDIR_REMOVE_SYMLINKS))) {
81aaf370 410 if (p_unlink(path) < 0) {
ac3d33df 411 git_error_set(GIT_ERROR_OS, "failed to remove %s '%s'",
81aaf370 412 S_ISLNK(st->st_mode) ? "symlink" : "file", path);
e74340b0
ET
413 return GIT_EEXISTS;
414 }
415
81aaf370 416 opts->perfdata.mkdir_calls++;
e74340b0 417
81aaf370 418 if (p_mkdir(path, mode) < 0) {
ac3d33df 419 git_error_set(GIT_ERROR_OS, "failed to make directory '%s'", path);
e74340b0
ET
420 return GIT_EEXISTS;
421 }
422 }
423
424 else if (S_ISLNK(st->st_mode)) {
425 /* Re-stat the target, make sure it's a directory */
81aaf370 426 opts->perfdata.stat_calls++;
e74340b0 427
81aaf370 428 if (p_stat(path, st) < 0) {
ac3d33df 429 git_error_set(GIT_ERROR_OS, "failed to make directory '%s'", path);
e74340b0
ET
430 return GIT_EEXISTS;
431 }
432 }
433
434 else if (!S_ISDIR(st->st_mode)) {
ac3d33df 435 git_error_set(GIT_ERROR_FILESYSTEM,
909d5494 436 "failed to make directory '%s': directory exists", path);
e74340b0
ET
437 return GIT_EEXISTS;
438 }
439
440 return 0;
441}
442
81aaf370
ET
443GIT_INLINE(int) mkdir_validate_mode(
444 const char *path,
445 struct stat *st,
446 bool terminal_path,
447 mode_t mode,
448 uint32_t flags,
449 struct git_futils_mkdir_options *opts)
450{
451 if (((terminal_path && (flags & GIT_MKDIR_CHMOD) != 0) ||
452 (flags & GIT_MKDIR_CHMOD_PATH) != 0) && st->st_mode != mode) {
453
454 opts->perfdata.chmod_calls++;
455
456 if (p_chmod(path, mode) < 0) {
ac3d33df 457 git_error_set(GIT_ERROR_OS, "failed to set permissions on '%s'", path);
81aaf370
ET
458 return -1;
459 }
460 }
461
462 return 0;
463}
28cdb315 464
e24c60db 465GIT_INLINE(int) mkdir_canonicalize(
e579e0f7 466 git_str *path,
e24c60db 467 uint32_t flags)
1a5204a7 468{
e24c60db 469 ssize_t root_len;
ac2fba0e 470
e24c60db 471 if (path->size == 0) {
ac3d33df 472 git_error_set(GIT_ERROR_OS, "attempt to create empty path");
ca1b6e54 473 return -1;
97769280 474 }
f0b2bfe5 475
9cb5b0f7 476 /* Trim trailing slashes (except the root) */
e579e0f7 477 if ((root_len = git_fs_path_root(path->ptr)) < 0)
9cb5b0f7
ET
478 root_len = 0;
479 else
480 root_len++;
481
e24c60db
ET
482 while (path->size > (size_t)root_len && path->ptr[path->size - 1] == '/')
483 path->ptr[--path->size] = '\0';
412de9a6 484
ca1b6e54 485 /* if we are not supposed to made the last element, truncate it */
3c42e4ef 486 if ((flags & GIT_MKDIR_SKIP_LAST2) != 0) {
e579e0f7 487 git_fs_path_dirname_r(path, path->ptr);
3c42e4ef
RB
488 flags |= GIT_MKDIR_SKIP_LAST;
489 }
9cb5b0f7 490 if ((flags & GIT_MKDIR_SKIP_LAST) != 0) {
e579e0f7 491 git_fs_path_dirname_r(path, path->ptr);
9cb5b0f7 492 }
ca1b6e54 493
9cb5b0f7 494 /* We were either given the root path (or trimmed it to
e24c60db
ET
495 * the root), we don't have anything to do.
496 */
497 if (path->size <= (size_t)root_len)
e579e0f7 498 git_str_clear(path);
e24c60db
ET
499
500 return 0;
501}
502
503int git_futils_mkdir(
504 const char *path,
505 mode_t mode,
506 uint32_t flags)
507{
e579e0f7 508 git_str make_path = GIT_STR_INIT, parent_path = GIT_STR_INIT;
e24c60db
ET
509 const char *relative;
510 struct git_futils_mkdir_options opts = { 0 };
511 struct stat st;
512 size_t depth = 0;
9ce2e7b3 513 int len = 0, root_len, error;
e24c60db 514
e579e0f7 515 if ((error = git_str_puts(&make_path, path)) < 0 ||
e24c60db 516 (error = mkdir_canonicalize(&make_path, flags)) < 0 ||
e579e0f7 517 (error = git_str_puts(&parent_path, make_path.ptr)) < 0 ||
e24c60db
ET
518 make_path.size == 0)
519 goto done;
520
e579e0f7 521 root_len = git_fs_path_root(make_path.ptr);
9ce2e7b3 522
e24c60db
ET
523 /* find the first parent directory that exists. this will be used
524 * as the base to dirname_relative.
9cb5b0f7 525 */
e24c60db
ET
526 for (relative = make_path.ptr; parent_path.size; ) {
527 error = p_lstat(parent_path.ptr, &st);
528
529 if (error == 0) {
530 break;
531 } else if (errno != ENOENT) {
ac3d33df 532 git_error_set(GIT_ERROR_OS, "failed to stat '%s'", parent_path.ptr);
22a2d3d5 533 error = -1;
e24c60db
ET
534 goto done;
535 }
536
537 depth++;
538
539 /* examine the parent of the current path */
e579e0f7 540 if ((len = git_fs_path_dirname_r(&parent_path, parent_path.ptr)) < 0) {
e24c60db
ET
541 error = len;
542 goto done;
543 }
544
c25aa7cd 545 GIT_ASSERT(len);
e24c60db 546
6147f643
PP
547 /*
548 * We've walked all the given path's parents and it's either relative
549 * (the parent is simply '.') or rooted (the length is less than or
550 * equal to length of the root path). The path may be less than the
551 * root path length on Windows, where `C:` == `C:/`.
e24c60db 552 */
22a2d3d5
UG
553 if ((len == 1 && parent_path.ptr[0] == '.') ||
554 (len == 1 && parent_path.ptr[0] == '/') ||
555 len <= root_len) {
e24c60db
ET
556 relative = make_path.ptr;
557 break;
558 }
559
560 relative = make_path.ptr + len + 1;
561
562 /* not recursive? just make this directory relative to its parent. */
563 if ((flags & GIT_MKDIR_PATH) == 0)
564 break;
565 }
566
567 /* we found an item at the location we're trying to create,
568 * validate it.
569 */
570 if (depth == 0) {
81aaf370 571 error = mkdir_validate_dir(make_path.ptr, &st, mode, flags, &opts);
e24c60db 572
81aaf370
ET
573 if (!error)
574 error = mkdir_validate_mode(
575 make_path.ptr, &st, true, mode, flags, &opts);
e24c60db 576
f7e56150
RB
577 goto done;
578 }
579
e24c60db
ET
580 /* we already took `SKIP_LAST` and `SKIP_LAST2` into account when
581 * canonicalizing `make_path`.
582 */
583 flags &= ~(GIT_MKDIR_SKIP_LAST2 | GIT_MKDIR_SKIP_LAST);
584
585 error = git_futils_mkdir_relative(relative,
586 parent_path.size ? parent_path.ptr : NULL, mode, flags, &opts);
587
588done:
e579e0f7
MB
589 git_str_dispose(&make_path);
590 git_str_dispose(&parent_path);
e24c60db
ET
591 return error;
592}
593
594int git_futils_mkdir_r(const char *path, const mode_t mode)
595{
596 return git_futils_mkdir(path, mode, GIT_MKDIR_PATH);
597}
598
599int git_futils_mkdir_relative(
600 const char *relative_path,
601 const char *base,
602 mode_t mode,
603 uint32_t flags,
604 struct git_futils_mkdir_options *opts)
605{
e579e0f7 606 git_str make_path = GIT_STR_INIT;
e24c60db
ET
607 ssize_t root = 0, min_root_len;
608 char lastch = '/', *tail;
609 struct stat st;
610 struct git_futils_mkdir_options empty_opts = {0};
611 int error;
612
613 if (!opts)
614 opts = &empty_opts;
615
616 /* build path and find "root" where we should start calling mkdir */
e579e0f7 617 if (git_fs_path_join_unrooted(&make_path, relative_path, base, &root) < 0)
e24c60db
ET
618 return -1;
619
620 if ((error = mkdir_canonicalize(&make_path, flags)) < 0 ||
621 make_path.size == 0)
622 goto done;
623
ca1b6e54
RB
624 /* if we are not supposed to make the whole path, reset root */
625 if ((flags & GIT_MKDIR_PATH) == 0)
e579e0f7 626 root = git_str_rfind(&make_path, '/');
ca1b6e54 627
f7e56150 628 /* advance root past drive name or network mount prefix */
e579e0f7 629 min_root_len = git_fs_path_root(make_path.ptr);
f7e56150
RB
630 if (root < min_root_len)
631 root = min_root_len;
2da72fb2 632 while (root >= 0 && make_path.ptr[root] == '/')
f7e56150
RB
633 ++root;
634
ca1b6e54 635 /* clip root to make_path length */
f7e56150
RB
636 if (root > (ssize_t)make_path.size)
637 root = (ssize_t)make_path.size; /* i.e. NUL byte of string */
0e26202c
RB
638 if (root < 0)
639 root = 0;
ca1b6e54 640
f7e56150
RB
641 /* walk down tail of path making each directory */
642 for (tail = &make_path.ptr[root]; *tail; *tail = lastch) {
d88e6e9b 643 bool mkdir_attempted = false;
999d4405 644
ca1b6e54
RB
645 /* advance tail to include next path component */
646 while (*tail == '/')
647 tail++;
648 while (*tail && *tail != '/')
649 tail++;
650
651 /* truncate path at next component */
652 lastch = *tail;
653 *tail = '\0';
999d4405 654 st.st_mode = 0;
ca1b6e54 655
500ec543
ET
656 if (opts->dir_map && git_strmap_exists(opts->dir_map, make_path.ptr))
657 continue;
658
fe598f09 659 /* See what's going on with this path component */
500ec543 660 opts->perfdata.stat_calls++;
1d50b364 661
d88e6e9b 662retry_lstat:
fe598f09 663 if (p_lstat(make_path.ptr, &st) < 0) {
d88e6e9b 664 if (mkdir_attempted || errno != ENOENT) {
ac3d33df 665 git_error_set(GIT_ERROR_OS, "cannot access component in path '%s'", make_path.ptr);
d88e6e9b 666 error = -1;
fe598f09
ET
667 goto done;
668 }
669
ac3d33df 670 git_error_clear();
d88e6e9b
VM
671 opts->perfdata.mkdir_calls++;
672 mkdir_attempted = true;
673 if (p_mkdir(make_path.ptr, mode) < 0) {
674 if (errno == EEXIST)
675 goto retry_lstat;
ac3d33df 676 git_error_set(GIT_ERROR_OS, "failed to make directory '%s'", make_path.ptr);
d88e6e9b
VM
677 error = -1;
678 goto done;
679 }
fe598f09 680 } else {
81aaf370
ET
681 if ((error = mkdir_validate_dir(
682 make_path.ptr, &st, mode, flags, opts)) < 0)
fe598f09 683 goto done;
d5f25204 684 }
40c44d2f 685
999d4405 686 /* chmod if requested and necessary */
81aaf370
ET
687 if ((error = mkdir_validate_mode(
688 make_path.ptr, &st, (lastch == '\0'), mode, flags, opts)) < 0)
689 goto done;
500ec543
ET
690
691 if (opts->dir_map && opts->pool) {
f1453c59
ET
692 char *cache_path;
693 size_t alloc_size;
694
ac3d33df 695 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, make_path.size, 1);
22a2d3d5 696 cache_path = git_pool_malloc(opts->pool, alloc_size);
ac3d33df 697 GIT_ERROR_CHECK_ALLOC(cache_path);
500ec543
ET
698
699 memcpy(cache_path, make_path.ptr, make_path.size + 1);
700
22a2d3d5 701 if ((error = git_strmap_set(opts->dir_map, cache_path, cache_path)) < 0)
500ec543
ET
702 goto done;
703 }
a993e4fe 704 }
40c44d2f 705
999d4405
RB
706 error = 0;
707
708 /* check that full path really is a directory if requested & needed */
f7e56150 709 if ((flags & GIT_MKDIR_VERIFY_DIR) != 0 &&
1d50b364 710 lastch != '\0') {
500ec543 711 opts->perfdata.stat_calls++;
1d50b364
ET
712
713 if (p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode)) {
ac3d33df 714 git_error_set(GIT_ERROR_OS, "path is not a directory '%s'",
e74340b0 715 make_path.ptr);
1d50b364
ET
716 error = GIT_ENOTFOUND;
717 }
999d4405 718 }
77c3999c 719
999d4405 720done:
e579e0f7 721 git_str_dispose(&make_path);
331e7de9 722 return error;
ca1b6e54 723}
77c3999c 724
331e7de9 725typedef struct {
ad9a921b 726 const char *base;
7e5c8a5b 727 size_t baselen;
331e7de9 728 uint32_t flags;
219d3457 729 int depth;
331e7de9
RB
730} futils__rmdir_data;
731
219d3457
RB
732#define FUTILS_MAX_DEPTH 100
733
331e7de9 734static int futils__error_cannot_rmdir(const char *path, const char *filemsg)
42b3a460 735{
331e7de9 736 if (filemsg)
ac3d33df 737 git_error_set(GIT_ERROR_OS, "could not remove directory '%s': %s",
331e7de9
RB
738 path, filemsg);
739 else
ac3d33df 740 git_error_set(GIT_ERROR_OS, "could not remove directory '%s'", path);
555aa453 741
331e7de9
RB
742 return -1;
743}
deafee7b 744
e579e0f7 745static int futils__rm_first_parent(git_str *path, const char *ceiling)
ad9a921b
RB
746{
747 int error = GIT_ENOTFOUND;
748 struct stat st;
749
750 while (error == GIT_ENOTFOUND) {
e579e0f7 751 git_str_rtruncate_at_char(path, '/');
ad9a921b
RB
752
753 if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0)
754 error = 0;
cccacac5 755 else if (p_lstat_posixly(path->ptr, &st) == 0) {
ad9a921b
RB
756 if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
757 error = p_unlink(path->ptr);
758 else if (!S_ISDIR(st.st_mode))
759 error = -1; /* fail to remove non-regular file */
760 } else if (errno != ENOTDIR)
761 error = -1;
762 }
763
764 if (error)
765 futils__error_cannot_rmdir(path->ptr, "cannot remove parent");
766
767 return error;
768}
769
e579e0f7 770static int futils__rmdir_recurs_foreach(void *opaque, git_str *path)
331e7de9 771{
96869a4e 772 int error = 0;
331e7de9 773 futils__rmdir_data *data = opaque;
14997dc5 774 struct stat st;
555aa453 775
14997dc5
RB
776 if (data->depth > FUTILS_MAX_DEPTH)
777 error = futils__error_cannot_rmdir(
778 path->ptr, "directory nesting too deep");
219d3457 779
14997dc5 780 else if ((error = p_lstat_posixly(path->ptr, &st)) < 0) {
ad9a921b 781 if (errno == ENOENT)
14997dc5 782 error = 0;
ad9a921b
RB
783 else if (errno == ENOTDIR) {
784 /* asked to remove a/b/c/d/e and a/b is a normal file */
785 if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0)
14997dc5 786 error = futils__rm_first_parent(path, data->base);
ad9a921b
RB
787 else
788 futils__error_cannot_rmdir(
789 path->ptr, "parent is not directory");
790 }
791 else
e579e0f7 792 error = git_fs_path_set_error(errno, path->ptr, "rmdir");
ad9a921b
RB
793 }
794
795 else if (S_ISDIR(st.st_mode)) {
219d3457
RB
796 data->depth++;
797
e579e0f7 798 error = git_fs_path_direach(path, 0, futils__rmdir_recurs_foreach, data);
219d3457
RB
799
800 data->depth--;
801
96869a4e 802 if (error < 0)
25e0b157 803 return error;
96869a4e 804
219d3457 805 if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0)
25e0b157 806 return error;
331e7de9 807
14997dc5 808 if ((error = p_rmdir(path->ptr)) < 0) {
331e7de9 809 if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
e09d18ee 810 (errno == ENOTEMPTY || errno == EEXIST || errno == EBUSY))
14997dc5 811 error = 0;
331e7de9 812 else
e579e0f7 813 error = git_fs_path_set_error(errno, path->ptr, "rmdir");
deafee7b 814 }
858dba58
VM
815 }
816
331e7de9 817 else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) {
14997dc5 818 if (p_unlink(path->ptr) < 0)
e579e0f7 819 error = git_fs_path_set_error(errno, path->ptr, "remove");
deafee7b
RB
820 }
821
ad9a921b 822 else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
14997dc5 823 error = futils__error_cannot_rmdir(path->ptr, "still present");
555aa453 824
25e0b157 825 return error;
331e7de9
RB
826}
827
bbb988a5 828static int futils__rmdir_empty_parent(void *opaque, const char *path)
331e7de9 829{
7e5c8a5b 830 futils__rmdir_data *data = opaque;
96869a4e 831 int error = 0;
7e5c8a5b 832
bbb988a5 833 if (strlen(path) <= data->baselen)
25e0b157 834 error = GIT_ITEROVER;
331e7de9 835
bbb988a5 836 else if (p_rmdir(path) < 0) {
331e7de9
RB
837 int en = errno;
838
839 if (en == ENOENT || en == ENOTDIR) {
96869a4e 840 /* do nothing */
4a0df574
ET
841 } else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0 &&
842 en == EBUSY) {
e579e0f7 843 error = git_fs_path_set_error(errno, path, "rmdir");
e09d18ee 844 } else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) {
331e7de9
RB
845 error = GIT_ITEROVER;
846 } else {
e579e0f7 847 error = git_fs_path_set_error(errno, path, "rmdir");
331e7de9
RB
848 }
849 }
850
25e0b157 851 return error;
42b3a460
MS
852}
853
0d64bef9 854int git_futils_rmdir_r(
331e7de9 855 const char *path, const char *base, uint32_t flags)
42b3a460 856{
97769280 857 int error;
e579e0f7 858 git_str fullpath = GIT_STR_INIT;
96869a4e 859 futils__rmdir_data data;
0d64bef9
RB
860
861 /* build path and find "root" where we should start calling mkdir */
e579e0f7 862 if (git_fs_path_join_unrooted(&fullpath, path, base, NULL) < 0)
0d64bef9
RB
863 return -1;
864
96869a4e 865 memset(&data, 0, sizeof(data));
7e5c8a5b
RB
866 data.base = base ? base : "";
867 data.baselen = base ? strlen(base) : 0;
868 data.flags = flags;
331e7de9
RB
869
870 error = futils__rmdir_recurs_foreach(&data, &fullpath);
871
872 /* remove now-empty parents if requested */
96869a4e 873 if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0)
e579e0f7 874 error = git_fs_path_walk_up(
331e7de9
RB
875 &fullpath, base, futils__rmdir_empty_parent, &data);
876
25e0b157 877 if (error == GIT_ITEROVER) {
ac3d33df 878 git_error_clear();
96869a4e 879 error = 0;
25e0b157 880 }
0d64bef9 881
e579e0f7 882 git_str_dispose(&fullpath);
97769280 883
97769280 884 return error;
42b3a460
MS
885}
886
22a2d3d5 887int git_futils_fake_symlink(const char *target, const char *path)
8651c10f
BS
888{
889 int retcode = GIT_ERROR;
22a2d3d5 890 int fd = git_futils_creat_withpath(path, 0755, 0644);
8651c10f 891 if (fd >= 0) {
22a2d3d5 892 retcode = p_write(fd, target, strlen(target));
8651c10f
BS
893 p_close(fd);
894 }
895 return retcode;
896}
ca1b6e54 897
85bd1746 898static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done)
ca1b6e54
RB
899{
900 int error = 0;
ad5611d8 901 char buffer[GIT_BUFSIZE_FILEIO];
ca1b6e54
RB
902 ssize_t len = 0;
903
904 while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0)
905 /* p_write() does not have the same semantics as write(). It loops
906 * internally and will return 0 when it has completed writing.
907 */
908 error = p_write(ofd, buffer, len);
909
910 if (len < 0) {
ac3d33df 911 git_error_set(GIT_ERROR_OS, "read error while copying file");
ca1b6e54
RB
912 error = (int)len;
913 }
914
edef91ee 915 if (error < 0)
ac3d33df 916 git_error_set(GIT_ERROR_OS, "write error while copying file");
edef91ee 917
85bd1746 918 if (close_fd_when_done) {
ca1b6e54
RB
919 p_close(ifd);
920 p_close(ofd);
921 }
922
923 return error;
924}
925
85bd1746 926int git_futils_cp(const char *from, const char *to, mode_t filemode)
ca1b6e54
RB
927{
928 int ifd, ofd;
929
ca1b6e54
RB
930 if ((ifd = git_futils_open_ro(from)) < 0)
931 return ifd;
932
933 if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
ca1b6e54 934 p_close(ifd);
e579e0f7 935 return git_fs_path_set_error(errno, to, "open for writing");
ca1b6e54
RB
936 }
937
85bd1746 938 return cp_by_fd(ifd, ofd, true);
ca1b6e54
RB
939}
940
27051d4e 941int git_futils_touch(const char *path, time_t *when)
8f09a98e
ET
942{
943 struct p_timeval times[2];
8f09a98e
ET
944 int ret;
945
27051d4e
ET
946 times[0].tv_sec = times[1].tv_sec = when ? *when : time(NULL);
947 times[0].tv_usec = times[1].tv_usec = 0;
8f09a98e
ET
948
949 ret = p_utimes(path, times);
950
e579e0f7 951 return (ret < 0) ? git_fs_path_set_error(errno, path, "touch") : 0;
8f09a98e
ET
952}
953
85bd1746 954static int cp_link(const char *from, const char *to, size_t link_size)
ca1b6e54
RB
955{
956 int error = 0;
957 ssize_t read_len;
392702ee 958 char *link_data;
f1453c59 959 size_t alloc_size;
392702ee 960
ac3d33df 961 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, link_size, 1);
f1453c59 962 link_data = git__malloc(alloc_size);
ac3d33df 963 GIT_ERROR_CHECK_ALLOC(link_data);
ca1b6e54 964
85bd1746
RB
965 read_len = p_readlink(from, link_data, link_size);
966 if (read_len != (ssize_t)link_size) {
ac3d33df 967 git_error_set(GIT_ERROR_OS, "failed to read symlink data for '%s'", from);
ca1b6e54
RB
968 error = -1;
969 }
970 else {
971 link_data[read_len] = '\0';
972
973 if (p_symlink(link_data, to) < 0) {
ac3d33df 974 git_error_set(GIT_ERROR_OS, "could not symlink '%s' as '%s'",
ca1b6e54
RB
975 link_data, to);
976 error = -1;
977 }
978 }
979
980 git__free(link_data);
981 return error;
982}
983
984typedef struct {
985 const char *to_root;
e579e0f7 986 git_str to;
ca1b6e54
RB
987 ssize_t from_prefix;
988 uint32_t flags;
989 uint32_t mkdir_flags;
990 mode_t dirmode;
991} cp_r_info;
992
3c42e4ef
RB
993#define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10)
994
e579e0f7 995static int _cp_r_mkdir(cp_r_info *info, git_str *from)
3c42e4ef
RB
996{
997 int error = 0;
998
999 /* create root directory the first time we need to create a directory */
1000 if ((info->flags & GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT) == 0) {
1001 error = git_futils_mkdir(
ac2fba0e 1002 info->to_root, info->dirmode,
18f08264 1003 (info->flags & GIT_CPDIR_CHMOD_DIRS) ? GIT_MKDIR_CHMOD : 0);
3c42e4ef
RB
1004
1005 info->flags |= GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT;
1006 }
1007
1008 /* create directory with root as base to prevent excess chmods */
1009 if (!error)
ac2fba0e 1010 error = git_futils_mkdir_relative(
3c42e4ef 1011 from->ptr + info->from_prefix, info->to_root,
ac2fba0e 1012 info->dirmode, info->mkdir_flags, NULL);
3c42e4ef
RB
1013
1014 return error;
1015}
1016
e579e0f7 1017static int _cp_r_callback(void *ref, git_str *from)
ca1b6e54 1018{
3c42e4ef 1019 int error = 0;
ca1b6e54
RB
1020 cp_r_info *info = ref;
1021 struct stat from_st, to_st;
1022 bool exists = false;
1023
1024 if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 &&
e579e0f7 1025 from->ptr[git_fs_path_basename_offset(from)] == '.')
ca1b6e54
RB
1026 return 0;
1027
e579e0f7 1028 if ((error = git_str_joinpath(
96869a4e 1029 &info->to, info->to_root, from->ptr + info->from_prefix)) < 0)
25e0b157 1030 return error;
ca1b6e54 1031
e579e0f7 1032 if (!(error = git_fs_path_lstat(info->to.ptr, &to_st)))
ca1b6e54 1033 exists = true;
14997dc5 1034 else if (error != GIT_ENOTFOUND)
25e0b157 1035 return error;
14997dc5 1036 else {
ac3d33df 1037 git_error_clear();
14997dc5
RB
1038 error = 0;
1039 }
ca1b6e54 1040
e579e0f7 1041 if ((error = git_fs_path_lstat(from->ptr, &from_st)) < 0)
25e0b157 1042 return error;
ca1b6e54
RB
1043
1044 if (S_ISDIR(from_st.st_mode)) {
ca1b6e54
RB
1045 mode_t oldmode = info->dirmode;
1046
1047 /* if we are not chmod'ing, then overwrite dirmode */
18f08264 1048 if ((info->flags & GIT_CPDIR_CHMOD_DIRS) == 0)
ca1b6e54
RB
1049 info->dirmode = from_st.st_mode;
1050
1051 /* make directory now if CREATE_EMPTY_DIRS is requested and needed */
1052 if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0)
3c42e4ef 1053 error = _cp_r_mkdir(info, from);
ca1b6e54
RB
1054
1055 /* recurse onto target directory */
25e0b157 1056 if (!error && (!exists || S_ISDIR(to_st.st_mode)))
e579e0f7 1057 error = git_fs_path_direach(from, 0, _cp_r_callback, info);
ca1b6e54
RB
1058
1059 if (oldmode != 0)
1060 info->dirmode = oldmode;
1061
25e0b157 1062 return error;
ca1b6e54
RB
1063 }
1064
1065 if (exists) {
1066 if ((info->flags & GIT_CPDIR_OVERWRITE) == 0)
1067 return 0;
1068
1069 if (p_unlink(info->to.ptr) < 0) {
ac3d33df 1070 git_error_set(GIT_ERROR_OS, "cannot overwrite existing file '%s'",
ca1b6e54 1071 info->to.ptr);
25e0b157 1072 return GIT_EEXISTS;
ca1b6e54
RB
1073 }
1074 }
1075
1076 /* Done if this isn't a regular file or a symlink */
1077 if (!S_ISREG(from_st.st_mode) &&
1078 (!S_ISLNK(from_st.st_mode) ||
1079 (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0))
1080 return 0;
1081
1082 /* Make container directory on demand if needed */
1083 if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
3c42e4ef 1084 (error = _cp_r_mkdir(info, from)) < 0)
25e0b157 1085 return error;
ca1b6e54
RB
1086
1087 /* make symlink or regular file */
94f742ba 1088 if (info->flags & GIT_CPDIR_LINK_FILES) {
54738368 1089 if ((error = p_link(from->ptr, info->to.ptr)) < 0)
ac3d33df 1090 git_error_set(GIT_ERROR_OS, "failed to link '%s'", from->ptr);
94f742ba 1091 } else if (S_ISLNK(from_st.st_mode)) {
3c42e4ef 1092 error = cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size);
94f742ba 1093 } else {
3c42e4ef
RB
1094 mode_t usemode = from_st.st_mode;
1095
18f08264 1096 if ((info->flags & GIT_CPDIR_SIMPLE_TO_MODE) != 0)
f240acce 1097 usemode = GIT_PERMS_FOR_WRITE(usemode);
3c42e4ef
RB
1098
1099 error = git_futils_cp(from->ptr, info->to.ptr, usemode);
3c42e4ef
RB
1100 }
1101
25e0b157 1102 return error;
ca1b6e54
RB
1103}
1104
1105int git_futils_cp_r(
1106 const char *from,
1107 const char *to,
1108 uint32_t flags,
1109 mode_t dirmode)
1110{
1111 int error;
e579e0f7 1112 git_str path = GIT_STR_INIT;
ca1b6e54
RB
1113 cp_r_info info;
1114
e579e0f7 1115 if (git_str_joinpath(&path, from, "") < 0) /* ensure trailing slash */
ca1b6e54
RB
1116 return -1;
1117
96869a4e 1118 memset(&info, 0, sizeof(info));
ca1b6e54
RB
1119 info.to_root = to;
1120 info.flags = flags;
1121 info.dirmode = dirmode;
1122 info.from_prefix = path.size;
e579e0f7 1123 git_str_init(&info.to, 0);
ca1b6e54
RB
1124
1125 /* precalculate mkdir flags */
1126 if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) {
3c42e4ef
RB
1127 /* if not creating empty dirs, then use mkdir to create the path on
1128 * demand right before files are copied.
1129 */
ca1b6e54 1130 info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST;
18f08264 1131 if ((flags & GIT_CPDIR_CHMOD_DIRS) != 0)
ca1b6e54
RB
1132 info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH;
1133 } else {
3c42e4ef 1134 /* otherwise, we will do simple mkdir as directories are encountered */
ca1b6e54 1135 info.mkdir_flags =
18f08264 1136 ((flags & GIT_CPDIR_CHMOD_DIRS) != 0) ? GIT_MKDIR_CHMOD : 0;
ca1b6e54
RB
1137 }
1138
1139 error = _cp_r_callback(&info, &path);
1140
e579e0f7
MB
1141 git_str_dispose(&path);
1142 git_str_dispose(&info.to);
ca1b6e54
RB
1143
1144 return error;
1145}
744cc03e 1146
c1f61af6
VM
1147int git_futils_filestamp_check(
1148 git_futils_filestamp *stamp, const char *path)
744cc03e
RB
1149{
1150 struct stat st;
1151
c8b511f3
RB
1152 /* if the stamp is NULL, then always reload */
1153 if (stamp == NULL)
744cc03e
RB
1154 return 1;
1155
7d490872 1156 if (p_stat(path, &st) < 0)
744cc03e
RB
1157 return GIT_ENOTFOUND;
1158
3d6a42d1 1159 if (stamp->mtime.tv_sec == st.st_mtime &&
0226f7dd 1160#if defined(GIT_USE_NSEC)
3d6a42d1 1161 stamp->mtime.tv_nsec == st.st_mtime_nsec &&
0226f7dd 1162#endif
22a2d3d5 1163 stamp->size == (uint64_t)st.st_size &&
c8b511f3 1164 stamp->ino == (unsigned int)st.st_ino)
744cc03e
RB
1165 return 0;
1166
3d6a42d1 1167 stamp->mtime.tv_sec = st.st_mtime;
0226f7dd 1168#if defined(GIT_USE_NSEC)
3d6a42d1 1169 stamp->mtime.tv_nsec = st.st_mtime_nsec;
0226f7dd 1170#endif
22a2d3d5 1171 stamp->size = (uint64_t)st.st_size;
c8b511f3 1172 stamp->ino = (unsigned int)st.st_ino;
744cc03e
RB
1173
1174 return 1;
1175}
1176
c1f61af6
VM
1177void git_futils_filestamp_set(
1178 git_futils_filestamp *target, const git_futils_filestamp *source)
c8b511f3 1179{
c8b511f3
RB
1180 if (source)
1181 memcpy(target, source, sizeof(*target));
1182 else
1183 memset(target, 0, sizeof(*target));
1184}
823c0e9c
RB
1185
1186
1187void git_futils_filestamp_set_from_stat(
1188 git_futils_filestamp *stamp, struct stat *st)
1189{
1190 if (st) {
3d6a42d1
ET
1191 stamp->mtime.tv_sec = st->st_mtime;
1192#if defined(GIT_USE_NSEC)
1193 stamp->mtime.tv_nsec = st->st_mtime_nsec;
1194#else
973a09a4
AR
1195 stamp->mtime.tv_nsec = 0;
1196#endif
22a2d3d5 1197 stamp->size = (uint64_t)st->st_size;
823c0e9c
RB
1198 stamp->ino = (unsigned int)st->st_ino;
1199 } else {
1200 memset(stamp, 0, sizeof(*stamp));
1201 }
1202}
1229e1c4
ET
1203
1204int git_futils_fsync_dir(const char *path)
1205{
3ac05d11
ET
1206#ifdef GIT_WIN32
1207 GIT_UNUSED(path);
1208 return 0;
1209#else
1229e1c4
ET
1210 int fd, error = -1;
1211
1212 if ((fd = p_open(path, O_RDONLY)) < 0) {
ac3d33df 1213 git_error_set(GIT_ERROR_OS, "failed to open directory '%s' for fsync", path);
1229e1c4
ET
1214 return -1;
1215 }
1216
1217 if ((error = p_fsync(fd)) < 0)
ac3d33df 1218 git_error_set(GIT_ERROR_OS, "failed to fsync directory '%s'", path);
1229e1c4
ET
1219
1220 p_close(fd);
1221 return error;
3ac05d11 1222#endif
1229e1c4
ET
1223}
1224
1225int git_futils_fsync_parent(const char *path)
1226{
0c28c72d
PS
1227 char *parent;
1228 int error;
1229
e579e0f7 1230 if ((parent = git_fs_path_dirname(path)) == NULL)
0c28c72d 1231 return -1;
1229e1c4 1232
0c28c72d 1233 error = git_futils_fsync_dir(parent);
1229e1c4
ET
1234 git__free(parent);
1235 return error;
1236}