Coverage Report

Created: 2025-06-09 06:06

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