fileops.c 19.4 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7
#include "common.h"
8
#include "fileops.h"
9
#include "global.h"
10
#include <ctype.h>
11 12 13
#if GIT_WIN32
#include "win32/findfile.h"
#endif
14

15
int git_futils_mkpath2file(const char *file_path, const mode_t mode)
16
{
17
	return git_futils_mkdir(
18 19
		file_path, NULL, mode,
		GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
20 21
}

22
int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode)
Vicent Marti committed
23 24
{
	int fd;
25 26 27
	mode_t mask;

	p_umask(mask = p_umask(0));
Vicent Marti committed
28

29 30 31 32
	git_buf_sets(path_out, filename);
	git_buf_puts(path_out, "_git2_XXXXXX");

	if (git_buf_oom(path_out))
33
		return -1;
Vicent Marti committed
34

35 36
	if ((fd = p_mkstemp(path_out->ptr)) < 0) {
		giterr_set(GITERR_OS,
37
			"Failed to create temporary file '%s'", path_out->ptr);
38 39
		return -1;
	}
Vicent Marti committed
40

41 42 43 44 45 46
	if (p_chmod(path_out->ptr, (mode & ~mask))) {
		giterr_set(GITERR_OS,
			"Failed to set permissions on file '%s'", path_out->ptr);
		return -1;
	}

47
	return fd;
Vicent Marti committed
48 49
}

50
int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode)
51
{
52
	int fd;
53

54 55 56 57 58
	if (git_futils_mkpath2file(path, dirmode) < 0)
		return -1;

	fd = p_creat(path, mode);
	if (fd < 0) {
59
		giterr_set(GITERR_OS, "Failed to create file '%s'", path);
60 61 62 63
		return -1;
	}

	return fd;
64 65
}

66
int git_futils_creat_locked(const char *path, const mode_t mode)
67
{
68
	int fd = p_open(path, O_WRONLY | O_CREAT | O_TRUNC |
69
		O_EXCL | O_BINARY | O_CLOEXEC, mode);
70

71
	if (fd < 0) {
72
		giterr_set(GITERR_OS, "Failed to create locked file '%s'", path);
73
		return errno == EEXIST ? GIT_ELOCKED : -1;
74 75 76
	}

	return fd;
77 78
}

79
int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode)
80
{
81 82
	if (git_futils_mkpath2file(path, dirmode) < 0)
		return -1;
83

Vicent Marti committed
84
	return git_futils_creat_locked(path, mode);
85 86
}

87 88 89
int git_futils_open_ro(const char *path)
{
	int fd = p_open(path, O_RDONLY);
90 91
	if (fd < 0)
		return git_path_set_error(errno, path, "open");
92 93 94
	return fd;
}

Vicent Marti committed
95
git_off_t git_futils_filesize(git_file fd)
96
{
97
	struct stat sb;
98 99 100 101 102

	if (p_fstat(fd, &sb)) {
		giterr_set(GITERR_OS, "Failed to stat file descriptor");
		return -1;
	}
103

104 105
	return sb.st_size;
}
106

107 108 109
mode_t git_futils_canonical_mode(mode_t raw_mode)
{
	if (S_ISREG(raw_mode))
110
		return S_IFREG | GIT_PERMS_CANONICAL(raw_mode);
111 112 113 114
	else if (S_ISLNK(raw_mode))
		return S_IFLNK;
	else if (S_ISGITLINK(raw_mode))
		return S_IFGITLINK;
115 116
	else if (S_ISDIR(raw_mode))
		return S_IFDIR;
117 118 119 120
	else
		return 0;
}

121 122
int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
{
123
	ssize_t read_size = 0;
124 125 126 127 128 129

	git_buf_clear(buf);

	if (git_buf_grow(buf, len + 1) < 0)
		return -1;

130 131
	/* p_read loops internally to read len bytes */
	read_size = p_read(fd, buf->ptr, len);
132

Vicent Marti committed
133
	if (read_size != (ssize_t)len) {
134
		giterr_set(GITERR_OS, "Failed to read descriptor");
135
		git_buf_free(buf);
136
		return -1;
137 138
	}

139 140 141
	buf->ptr[read_size] = '\0';
	buf->size = read_size;

142 143 144 145
	return 0;
}

int git_futils_readbuffer_updated(
146
	git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated)
147 148
{
	git_file fd;
149
	struct stat st;
150
	bool changed = false;
151

152
	assert(buf && path && *path);
153

154 155
	if (updated != NULL)
		*updated = 0;
156

157 158
	if (p_stat(path, &st) < 0)
		return git_path_set_error(errno, path, "stat");
159

160 161 162 163 164 165 166

	if (S_ISDIR(st.st_mode)) {
		giterr_set(GITERR_INVALID, "requested file is a directory");
		return GIT_ENOTFOUND;
	}

	if (!git__is_sizet(st.st_size+1)) {
167 168
		giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path);
		return -1;
169
	}
170 171

	/*
172 173
	 * If we were given a time and/or a size, we only want to read the file
	 * if it has been modified.
174
	 */
175 176 177 178 179 180 181 182
	if (size && *size != (size_t)st.st_size)
		changed = true;
	if (mtime && *mtime != st.st_mtime)
		changed = true;
	if (!size && !mtime)
		changed = true;

	if (!changed) {
183 184
		return 0;
	}
185 186 187

	if (mtime != NULL)
		*mtime = st.st_mtime;
188 189
	if (size != NULL)
		*size = (size_t)st.st_size;
190

191 192 193
	if ((fd = git_futils_open_ro(path)) < 0)
		return fd;

194
	if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) {
195 196
		p_close(fd);
		return -1;
197 198
	}

Vicent Marti committed
199
	p_close(fd);
200

201 202 203
	if (updated != NULL)
		*updated = 1;

204
	return 0;
205 206
}

207
int git_futils_readbuffer(git_buf *buf, const char *path)
208
{
209
	return git_futils_readbuffer_updated(buf, path, NULL, NULL, NULL);
210 211
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
int git_futils_writebuffer(
	const git_buf *buf,	const char *path, int flags, mode_t mode)
{
	int fd, error = 0;

	if (flags <= 0)
		flags = O_CREAT | O_TRUNC | O_WRONLY;
	if (!mode)
		mode = GIT_FILEMODE_BLOB;

	if ((fd = p_open(path, flags, mode)) < 0) {
		giterr_set(GITERR_OS, "Could not open '%s' for writing", path);
		return fd;
	}

	if ((error = p_write(fd, git_buf_cstr(buf), git_buf_len(buf))) < 0) {
		giterr_set(GITERR_OS, "Could not write to '%s'", path);
		(void)p_close(fd);
230
		return error;
231 232 233 234 235 236 237 238
	}

	if ((error = p_close(fd)) < 0)
		giterr_set(GITERR_OS, "Error while closing '%s'", path);

	return error;
}

239
int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
240
{
241 242
	if (git_futils_mkpath2file(to, dirmode) < 0)
		return -1;
243

244 245 246 247 248 249
	if (p_rename(from, to) < 0) {
		giterr_set(GITERR_OS, "Failed to rename '%s' to '%s'", from, to);
		return -1;
	}

	return 0;
250 251
}

Vicent Marti committed
252
int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
253
{
Vicent Marti committed
254
	return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
255 256
}

257 258
int git_futils_mmap_ro_file(git_map *out, const char *path)
{
259 260
	git_file fd = git_futils_open_ro(path);
	git_off_t len;
261
	int result;
262 263 264 265 266

	if (fd < 0)
		return fd;

	len = git_futils_filesize(fd);
267 268 269 270
	if (!git__is_sizet(len)) {
		giterr_set(GITERR_OS, "File `%s` too large to mmap", path);
		return -1;
	}
271

272
	result = git_futils_mmap_ro(out, fd, 0, (size_t)len);
273 274 275 276
	p_close(fd);
	return result;
}

Vicent Marti committed
277
void git_futils_mmap_free(git_map *out)
278
{
Vicent Marti committed
279
	p_munmap(out);
280 281
}

282 283 284 285 286
int git_futils_mkdir(
	const char *path,
	const char *base,
	mode_t mode,
	uint32_t flags)
287
{
288
	int error = -1;
289
	git_buf make_path = GIT_BUF_INIT;
Vicent Marti committed
290
	ssize_t root = 0, min_root_len;
291
	char lastch = '/', *tail;
292
	struct stat st;
293

294 295 296
	/* build path and find "root" where we should start calling mkdir */
	if (git_path_join_unrooted(&make_path, path, base, &root) < 0)
		return -1;
297

298 299
	if (make_path.size == 0) {
		giterr_set(GITERR_OS, "Attempt to create empty path");
300
		goto done;
301
	}
302

303 304 305 306 307
	/* remove trailing slashes on path */
	while (make_path.ptr[make_path.size - 1] == '/') {
		make_path.size--;
		make_path.ptr[make_path.size] = '\0';
	}
308

309
	/* if we are not supposed to made the last element, truncate it */
310 311 312 313
	if ((flags & GIT_MKDIR_SKIP_LAST2) != 0) {
		git_buf_rtruncate_at_char(&make_path, '/');
		flags |= GIT_MKDIR_SKIP_LAST;
	}
314 315 316
	if ((flags & GIT_MKDIR_SKIP_LAST) != 0)
		git_buf_rtruncate_at_char(&make_path, '/');

317 318 319 320 321 322
	/* if nothing left after truncation, then we're done! */
	if (!make_path.size) {
		error = 0;
		goto done;
	}

323 324 325 326
	/* if we are not supposed to make the whole path, reset root */
	if ((flags & GIT_MKDIR_PATH) == 0)
		root = git_buf_rfind(&make_path, '/');

327 328 329 330
	/* advance root past drive name or network mount prefix */
	min_root_len = git_path_root(make_path.ptr);
	if (root < min_root_len)
		root = min_root_len;
yorah committed
331
	while (root >= 0 && make_path.ptr[root] == '/')
332 333
		++root;

334
	/* clip root to make_path length */
335 336
	if (root > (ssize_t)make_path.size)
		root = (ssize_t)make_path.size; /* i.e. NUL byte of string */
337 338
	if (root < 0)
		root = 0;
339

340 341
	/* walk down tail of path making each directory */
	for (tail = &make_path.ptr[root]; *tail; *tail = lastch) {
342

343 344 345 346 347 348 349 350 351
		/* advance tail to include next path component */
		while (*tail == '/')
			tail++;
		while (*tail && *tail != '/')
			tail++;

		/* truncate path at next component */
		lastch = *tail;
		*tail = '\0';
352
		st.st_mode = 0;
353 354

		/* make directory */
355
		if (p_mkdir(make_path.ptr, mode) < 0) {
356
			int tmp_errno = giterr_system_last();
357

358 359 360
			/* ignore error if not at end or if directory already exists */
			if (lastch == '\0' &&
				(p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode))) {
361
				giterr_system_set(tmp_errno);
362
				giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path.ptr);
363
				goto done;
364 365
			}

366 367
			/* with exclusive create, existing dir is an error */
			if ((flags & GIT_MKDIR_EXCL) != 0) {
368
				giterr_set(GITERR_OS, "Directory already exists '%s'", make_path.ptr);
369
				error = GIT_EEXISTS;
370
				goto done;
371
			}
372
		}
373

374
		/* chmod if requested and necessary */
375 376 377
		if (((flags & GIT_MKDIR_CHMOD_PATH) != 0 ||
			 (lastch == '\0' && (flags & GIT_MKDIR_CHMOD) != 0)) &&
			st.st_mode != mode &&
378 379
			(error = p_chmod(make_path.ptr, mode)) < 0 &&
			lastch == '\0') {
380 381
			giterr_set(GITERR_OS, "Failed to set permissions on '%s'", make_path.ptr);
			goto done;
382
		}
383
	}
384

385 386 387
	error = 0;

	/* check that full path really is a directory if requested & needed */
388 389
	if ((flags & GIT_MKDIR_VERIFY_DIR) != 0 &&
		lastch != '\0' &&
390
		(p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode))) {
391
		giterr_set(GITERR_OS, "Path is not a directory '%s'", make_path.ptr);
392 393
		error = GIT_ENOTFOUND;
	}
394

395
done:
396
	git_buf_free(&make_path);
397
	return error;
398
}
399

400 401 402
int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
{
	return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH);
403 404
}

405
typedef struct {
406
	const char *base;
407
	size_t baselen;
408
	uint32_t flags;
409
	int depth;
410 411
} futils__rmdir_data;

412 413
#define FUTILS_MAX_DEPTH 100

414
static int futils__error_cannot_rmdir(const char *path, const char *filemsg)
415
{
416 417 418 419 420
	if (filemsg)
		giterr_set(GITERR_OS, "Could not remove directory. File '%s' %s",
				   path, filemsg);
	else
		giterr_set(GITERR_OS, "Could not remove directory '%s'", path);
421

422 423
	return -1;
}
424

425 426 427 428 429 430 431 432 433 434
static int futils__rm_first_parent(git_buf *path, const char *ceiling)
{
	int error = GIT_ENOTFOUND;
	struct stat st;

	while (error == GIT_ENOTFOUND) {
		git_buf_rtruncate_at_char(path, '/');

		if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0)
			error = 0;
435
		else if (p_lstat_posixly(path->ptr, &st) == 0) {
436 437 438 439 440 441 442 443 444 445 446 447 448 449
			if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
				error = p_unlink(path->ptr);
			else if (!S_ISDIR(st.st_mode))
				error = -1; /* fail to remove non-regular file */
		} else if (errno != ENOTDIR)
			error = -1;
	}

	if (error)
		futils__error_cannot_rmdir(path->ptr, "cannot remove parent");

	return error;
}

450 451
static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
{
452
	int error = 0;
453
	futils__rmdir_data *data = opaque;
454
	struct stat st;
455

456 457 458
	if (data->depth > FUTILS_MAX_DEPTH)
		error = futils__error_cannot_rmdir(
			path->ptr, "directory nesting too deep");
459

460
	else if ((error = p_lstat_posixly(path->ptr, &st)) < 0) {
461
		if (errno == ENOENT)
462
			error = 0;
463 464 465
		else if (errno == ENOTDIR) {
			/* asked to remove a/b/c/d/e and a/b is a normal file */
			if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0)
466
				error = futils__rm_first_parent(path, data->base);
467 468 469 470 471
			else
				futils__error_cannot_rmdir(
					path->ptr, "parent is not directory");
		}
		else
472
			error = git_path_set_error(errno, path->ptr, "rmdir");
473 474 475
	}

	else if (S_ISDIR(st.st_mode)) {
476 477
		data->depth++;

478
		error = git_path_direach(path, 0, futils__rmdir_recurs_foreach, data);
479 480 481

		data->depth--;

482
		if (error < 0)
483
			return error;
484

485
		if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0)
486
			return error;
487

488
		if ((error = p_rmdir(path->ptr)) < 0) {
489
			if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
490
				(errno == ENOTEMPTY || errno == EEXIST || errno == EBUSY))
491
				error = 0;
492
			else
493
				error = git_path_set_error(errno, path->ptr, "rmdir");
494
		}
495 496
	}

497
	else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) {
498 499
		if (p_unlink(path->ptr) < 0)
			error = git_path_set_error(errno, path->ptr, "remove");
500 501
	}

502
	else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
503
		error = futils__error_cannot_rmdir(path->ptr, "still present");
504

505
	return error;
506 507 508 509
}

static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
{
510
	futils__rmdir_data *data = opaque;
511
	int error = 0;
512 513

	if (git_buf_len(path) <= data->baselen)
514
		error = GIT_ITEROVER;
515

516
	else if (p_rmdir(git_buf_cstr(path)) < 0) {
517 518 519
		int en = errno;

		if (en == ENOENT || en == ENOTDIR) {
520
			/* do nothing */
521
		} else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) {
522 523
			error = GIT_ITEROVER;
		} else {
524
			error = git_path_set_error(errno, git_buf_cstr(path), "rmdir");
525 526 527
		}
	}

528
	return error;
529 530
}

531
int git_futils_rmdir_r(
532
	const char *path, const char *base, uint32_t flags)
533
{
534
	int error;
535
	git_buf fullpath = GIT_BUF_INIT;
536
	futils__rmdir_data data;
537 538 539 540 541

	/* build path and find "root" where we should start calling mkdir */
	if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
		return -1;

542
	memset(&data, 0, sizeof(data));
543 544 545
	data.base    = base ? base : "";
	data.baselen = base ? strlen(base) : 0;
	data.flags   = flags;
546 547 548 549

	error = futils__rmdir_recurs_foreach(&data, &fullpath);

	/* remove now-empty parents if requested */
550
	if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0)
551 552 553
		error = git_path_walk_up(
			&fullpath, base, futils__rmdir_empty_parent, &data);

554 555
	if (error == GIT_ITEROVER) {
		giterr_clear();
556
		error = 0;
557
	}
558 559

	git_buf_free(&fullpath);
560 561

	return error;
562 563
}

564 565 566 567 568 569 570 571 572 573
int git_futils_fake_symlink(const char *old, const char *new)
{
	int retcode = GIT_ERROR;
	int fd = git_futils_creat_withpath(new, 0755, 0644);
	if (fd >= 0) {
		retcode = p_write(fd, old, strlen(old));
		p_close(fd);
	}
	return retcode;
}
574

575
static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done)
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
{
	int error = 0;
	char buffer[4096];
	ssize_t len = 0;

	while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0)
		/* p_write() does not have the same semantics as write().  It loops
		 * internally and will return 0 when it has completed writing.
		 */
		error = p_write(ofd, buffer, len);

	if (len < 0) {
		giterr_set(GITERR_OS, "Read error while copying file");
		error = (int)len;
	}

592
	if (close_fd_when_done) {
593 594 595 596 597 598 599
		p_close(ifd);
		p_close(ofd);
	}

	return error;
}

600
int git_futils_cp(const char *from, const char *to, mode_t filemode)
601 602 603 604 605 606 607 608
{
	int ifd, ofd;

	if ((ifd = git_futils_open_ro(from)) < 0)
		return ifd;

	if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
		p_close(ifd);
609
		return git_path_set_error(errno, to, "open for writing");
610 611
	}

612
	return cp_by_fd(ifd, ofd, true);
613 614
}

615
static int cp_link(const char *from, const char *to, size_t link_size)
616 617 618
{
	int error = 0;
	ssize_t read_len;
619
	char *link_data = git__malloc(link_size + 1);
620 621
	GITERR_CHECK_ALLOC(link_data);

622 623
	read_len = p_readlink(from, link_data, link_size);
	if (read_len != (ssize_t)link_size) {
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
		giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from);
		error = -1;
	}
	else {
		link_data[read_len] = '\0';

		if (p_symlink(link_data, to) < 0) {
			giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'",
				link_data, to);
			error = -1;
		}
	}

	git__free(link_data);
	return error;
}

typedef struct {
	const char *to_root;
	git_buf to;
	ssize_t from_prefix;
	uint32_t flags;
	uint32_t mkdir_flags;
	mode_t dirmode;
} cp_r_info;

650 651 652 653 654 655 656 657 658 659
#define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10)

static int _cp_r_mkdir(cp_r_info *info, git_buf *from)
{
	int error = 0;

	/* create root directory the first time we need to create a directory */
	if ((info->flags & GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT) == 0) {
		error = git_futils_mkdir(
			info->to_root, NULL, info->dirmode,
660
			(info->flags & GIT_CPDIR_CHMOD_DIRS) ? GIT_MKDIR_CHMOD : 0);
661 662 663 664 665 666 667 668 669 670 671 672 673

		info->flags |= GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT;
	}

	/* create directory with root as base to prevent excess chmods */
	if (!error)
		error = git_futils_mkdir(
			from->ptr + info->from_prefix, info->to_root,
			info->dirmode, info->mkdir_flags);

	return error;
}

674 675
static int _cp_r_callback(void *ref, git_buf *from)
{
676
	int error = 0;
677 678 679 680 681 682 683 684
	cp_r_info *info = ref;
	struct stat from_st, to_st;
	bool exists = false;

	if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 &&
		from->ptr[git_path_basename_offset(from)] == '.')
		return 0;

685 686
	if ((error = git_buf_joinpath(
			&info->to, info->to_root, from->ptr + info->from_prefix)) < 0)
687
		return error;
688

689
	if (!(error = git_path_lstat(info->to.ptr, &to_st)))
690
		exists = true;
691
	else if (error != GIT_ENOTFOUND)
692
		return error;
693 694 695 696
	else {
		giterr_clear();
		error = 0;
	}
697

698
	if ((error = git_path_lstat(from->ptr, &from_st)) < 0)
699
		return error;
700 701 702 703 704

	if (S_ISDIR(from_st.st_mode)) {
		mode_t oldmode = info->dirmode;

		/* if we are not chmod'ing, then overwrite dirmode */
705
		if ((info->flags & GIT_CPDIR_CHMOD_DIRS) == 0)
706 707 708 709
			info->dirmode = from_st.st_mode;

		/* make directory now if CREATE_EMPTY_DIRS is requested and needed */
		if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0)
710
			error = _cp_r_mkdir(info, from);
711 712

		/* recurse onto target directory */
713
		if (!error && (!exists || S_ISDIR(to_st.st_mode)))
714
			error = git_path_direach(from, 0, _cp_r_callback, info);
715 716 717 718

		if (oldmode != 0)
			info->dirmode = oldmode;

719
		return error;
720 721 722 723 724 725 726 727 728
	}

	if (exists) {
		if ((info->flags & GIT_CPDIR_OVERWRITE) == 0)
			return 0;

		if (p_unlink(info->to.ptr) < 0) {
			giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'",
				info->to.ptr);
729
			return GIT_EEXISTS;
730 731 732 733 734 735 736 737 738 739 740
		}
	}

	/* Done if this isn't a regular file or a symlink */
	if (!S_ISREG(from_st.st_mode) &&
		(!S_ISLNK(from_st.st_mode) ||
		 (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0))
		return 0;

	/* Make container directory on demand if needed */
	if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
741
		(error = _cp_r_mkdir(info, from)) < 0)
742
		return error;
743 744

	/* make symlink or regular file */
745 746 747
	if (info->flags & GIT_CPDIR_LINK_FILES) {
		error = p_link(from->ptr, info->to.ptr);
	} else if (S_ISLNK(from_st.st_mode)) {
748
		error = cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size);
749
	} else {
750 751
		mode_t usemode = from_st.st_mode;

752
		if ((info->flags & GIT_CPDIR_SIMPLE_TO_MODE) != 0)
753
			usemode = GIT_PERMS_FOR_WRITE(usemode);
754 755 756 757

		error = git_futils_cp(from->ptr, info->to.ptr, usemode);
	}

758
	return error;
759 760 761 762 763 764 765 766 767 768 769 770
}

int git_futils_cp_r(
	const char *from,
	const char *to,
	uint32_t flags,
	mode_t dirmode)
{
	int error;
	git_buf path = GIT_BUF_INIT;
	cp_r_info info;

771
	if (git_buf_joinpath(&path, from, "") < 0) /* ensure trailing slash */
772 773
		return -1;

774
	memset(&info, 0, sizeof(info));
775 776 777 778 779 780 781 782
	info.to_root = to;
	info.flags   = flags;
	info.dirmode = dirmode;
	info.from_prefix = path.size;
	git_buf_init(&info.to, 0);

	/* precalculate mkdir flags */
	if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) {
783 784 785
		/* if not creating empty dirs, then use mkdir to create the path on
		 * demand right before files are copied.
		 */
786
		info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST;
787
		if ((flags & GIT_CPDIR_CHMOD_DIRS) != 0)
788 789
			info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH;
	} else {
790
		/* otherwise, we will do simple mkdir as directories are encountered */
791
		info.mkdir_flags =
792
			((flags & GIT_CPDIR_CHMOD_DIRS) != 0) ? GIT_MKDIR_CHMOD : 0;
793 794 795 796 797
	}

	error = _cp_r_callback(&info, &path);

	git_buf_free(&path);
Russell Belfer committed
798
	git_buf_free(&info.to);
799 800 801

	return error;
}
802

Vicent Marti committed
803 804
int git_futils_filestamp_check(
	git_futils_filestamp *stamp, const char *path)
805 806 807
{
	struct stat st;

808 809
	/* if the stamp is NULL, then always reload */
	if (stamp == NULL)
810 811
		return 1;

812
	if (p_stat(path, &st) < 0)
813 814
		return GIT_ENOTFOUND;

815 816 817
	if (stamp->mtime == (git_time_t)st.st_mtime &&
		stamp->size  == (git_off_t)st.st_size   &&
		stamp->ino   == (unsigned int)st.st_ino)
818 819
		return 0;

820 821 822
	stamp->mtime = (git_time_t)st.st_mtime;
	stamp->size  = (git_off_t)st.st_size;
	stamp->ino   = (unsigned int)st.st_ino;
823 824 825 826

	return 1;
}

Vicent Marti committed
827 828
void git_futils_filestamp_set(
	git_futils_filestamp *target, const git_futils_filestamp *source)
829 830 831 832 833 834 835 836
{
	assert(target);

	if (source)
		memcpy(target, source, sizeof(*target));
	else
		memset(target, 0, sizeof(*target));
}
837 838 839 840 841 842 843 844 845 846 847 848 849


void git_futils_filestamp_set_from_stat(
	git_futils_filestamp *stamp, struct stat *st)
{
	if (st) {
		stamp->mtime = (git_time_t)st->st_mtime;
		stamp->size  = (git_off_t)st->st_size;
		stamp->ino   = (unsigned int)st->st_ino;
	} else {
		memset(stamp, 0, sizeof(*stamp));
	}
}