]> git.proxmox.com Git - libgit2.git/blame - src/fileops.c
Rename xdr to xdg
[libgit2.git] / src / fileops.c
CommitLineData
bb742ede 1/*
5e0de328 2 * Copyright (C) 2009-2012 the libgit2 contributors
bb742ede
VM
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
5ee2fe77 7#include "common.h"
ec250c6e 8#include "fileops.h"
2e29957a 9#include <ctype.h>
ec250c6e 10
ce8cd006 11int git_futils_mkpath2file(const char *file_path, const mode_t mode)
55ffebe3 12{
ca1b6e54
RB
13 return git_futils_mkdir(
14 file_path, NULL, mode, GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST);
55ffebe3
VM
15}
16
97769280 17int git_futils_mktmp(git_buf *path_out, const char *filename)
72a3fe42
VM
18{
19 int fd;
20
97769280
RB
21 git_buf_sets(path_out, filename);
22 git_buf_puts(path_out, "_git2_XXXXXX");
23
24 if (git_buf_oom(path_out))
1a481123 25 return -1;
72a3fe42 26
1a481123
VM
27 if ((fd = p_mkstemp(path_out->ptr)) < 0) {
28 giterr_set(GITERR_OS,
ae9e29fd 29 "Failed to create temporary file '%s'", path_out->ptr);
1a481123
VM
30 return -1;
31 }
72a3fe42 32
f978b748 33 return fd;
72a3fe42
VM
34}
35
ce8cd006 36int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode)
7dd8a9f7 37{
1a481123 38 int fd;
55ffebe3 39
1a481123
VM
40 if (git_futils_mkpath2file(path, dirmode) < 0)
41 return -1;
42
43 fd = p_creat(path, mode);
44 if (fd < 0) {
ae9e29fd 45 giterr_set(GITERR_OS, "Failed to create file '%s'", path);
1a481123
VM
46 return -1;
47 }
48
49 return fd;
55ffebe3
VM
50}
51
33127043 52int git_futils_creat_locked(const char *path, const mode_t mode)
1549cba9 53{
09719c50 54 int fd;
55
56#ifdef GIT_WIN32
6813169a 57 wchar_t buf[GIT_WIN_PATH];
09719c50 58
0f4c6175 59 git__utf8_to_16(buf, GIT_WIN_PATH, path);
09719c50 60 fd = _wopen(buf, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode);
09719c50 61#else
62 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode);
63#endif
64
1a481123 65 if (fd < 0) {
ae9e29fd 66 giterr_set(GITERR_OS, "Failed to create locked file '%s'", path);
1a481123
VM
67 return -1;
68 }
69
70 return fd;
1549cba9
RG
71}
72
ce8cd006 73int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode)
1549cba9 74{
1a481123
VM
75 if (git_futils_mkpath2file(path, dirmode) < 0)
76 return -1;
1549cba9 77
f79026b4 78 return git_futils_creat_locked(path, mode);
ec250c6e
AE
79}
80
deafee7b
RB
81int git_futils_open_ro(const char *path)
82{
83 int fd = p_open(path, O_RDONLY);
84 if (fd < 0) {
d0a920a6 85 if (errno == ENOENT || errno == ENOTDIR)
deafee7b
RB
86 fd = GIT_ENOTFOUND;
87 giterr_set(GITERR_OS, "Failed to open '%s'", path);
88 }
89 return fd;
90}
91
f79026b4 92git_off_t git_futils_filesize(git_file fd)
ec250c6e 93{
7dd8a9f7 94 struct stat sb;
deafee7b
RB
95
96 if (p_fstat(fd, &sb)) {
97 giterr_set(GITERR_OS, "Failed to stat file descriptor");
98 return -1;
99 }
5ad739e8 100
ec250c6e
AE
101 return sb.st_size;
102}
4188d28f 103
b6c93aef
RB
104mode_t git_futils_canonical_mode(mode_t raw_mode)
105{
106 if (S_ISREG(raw_mode))
107 return S_IFREG | GIT_CANONICAL_PERMS(raw_mode);
108 else if (S_ISLNK(raw_mode))
109 return S_IFLNK;
b6c93aef
RB
110 else if (S_ISGITLINK(raw_mode))
111 return S_IFGITLINK;
7c7ff7d1
RB
112 else if (S_ISDIR(raw_mode))
113 return S_IFDIR;
b6c93aef
RB
114 else
115 return 0;
116}
117
60b9d3fc
RB
118int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
119{
1f35e89d 120 ssize_t read_size;
60b9d3fc
RB
121
122 git_buf_clear(buf);
123
124 if (git_buf_grow(buf, len + 1) < 0)
125 return -1;
126
1f35e89d
RB
127 /* p_read loops internally to read len bytes */
128 read_size = p_read(fd, buf->ptr, len);
60b9d3fc 129
c859184b 130 if (read_size != (ssize_t)len) {
1f35e89d
RB
131 giterr_set(GITERR_OS, "Failed to read descriptor");
132 return -1;
60b9d3fc
RB
133 }
134
1f35e89d
RB
135 buf->ptr[read_size] = '\0';
136 buf->size = read_size;
137
60b9d3fc
RB
138 return 0;
139}
140
141int git_futils_readbuffer_updated(
142 git_buf *buf, const char *path, time_t *mtime, int *updated)
75d58430
RJ
143{
144 git_file fd;
c3da9f06 145 struct stat st;
75d58430 146
13224ea4 147 assert(buf && path && *path);
75d58430 148
c3da9f06
CMN
149 if (updated != NULL)
150 *updated = 0;
75d58430 151
ae9e29fd
RB
152 if ((fd = git_futils_open_ro(path)) < 0)
153 return fd;
c3da9f06 154
13224ea4 155 if (p_fstat(fd, &st) < 0 || S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) {
e1de726c 156 p_close(fd);
1a481123
VM
157 giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path);
158 return -1;
13224ea4 159 }
c3da9f06
CMN
160
161 /*
162 * If we were given a time, we only want to read the file if it
163 * has been modified.
164 */
13224ea4 165 if (mtime != NULL && *mtime >= st.st_mtime) {
e1de726c 166 p_close(fd);
13224ea4
VM
167 return 0;
168 }
c3da9f06
CMN
169
170 if (mtime != NULL)
171 *mtime = st.st_mtime;
c3da9f06 172
60b9d3fc 173 if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) {
e1de726c
RB
174 p_close(fd);
175 return -1;
75d58430
RJ
176 }
177
f79026b4 178 p_close(fd);
75d58430 179
c3da9f06
CMN
180 if (updated != NULL)
181 *updated = 1;
182
13224ea4 183 return 0;
c3da9f06
CMN
184}
185
13224ea4 186int git_futils_readbuffer(git_buf *buf, const char *path)
97769280 187{
13224ea4 188 return git_futils_readbuffer_updated(buf, path, NULL, NULL);
97769280
RB
189}
190
ce8cd006 191int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
19a30a3f 192{
deafee7b
RB
193 if (git_futils_mkpath2file(to, dirmode) < 0)
194 return -1;
19a30a3f 195
deafee7b
RB
196 if (p_rename(from, to) < 0) {
197 giterr_set(GITERR_OS, "Failed to rename '%s' to '%s'", from, to);
198 return -1;
199 }
200
201 return 0;
19a30a3f
VM
202}
203
f79026b4 204int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
20e7f426 205{
f79026b4 206 return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
20e7f426
SP
207}
208
74fa4bfa
RB
209int git_futils_mmap_ro_file(git_map *out, const char *path)
210{
0d0fa7c3
RB
211 git_file fd = git_futils_open_ro(path);
212 git_off_t len;
da9abdd6 213 int result;
0d0fa7c3
RB
214
215 if (fd < 0)
216 return fd;
217
218 len = git_futils_filesize(fd);
deafee7b
RB
219 if (!git__is_sizet(len)) {
220 giterr_set(GITERR_OS, "File `%s` too large to mmap", path);
221 return -1;
222 }
0d0fa7c3 223
da9abdd6 224 result = git_futils_mmap_ro(out, fd, 0, (size_t)len);
74fa4bfa
RB
225 p_close(fd);
226 return result;
227}
228
f79026b4 229void git_futils_mmap_free(git_map *out)
20e7f426 230{
f79026b4 231 p_munmap(out);
20e7f426
SP
232}
233
ca1b6e54
RB
234int git_futils_mkdir(
235 const char *path,
236 const char *base,
237 mode_t mode,
238 uint32_t flags)
1a5204a7 239{
97769280 240 git_buf make_path = GIT_BUF_INIT;
ca1b6e54
RB
241 ssize_t root = 0;
242 char lastch, *tail;
dc07184f 243
ca1b6e54
RB
244 /* build path and find "root" where we should start calling mkdir */
245 if (git_path_join_unrooted(&make_path, path, base, &root) < 0)
246 return -1;
dc07184f 247
ca1b6e54
RB
248 if (make_path.size == 0) {
249 giterr_set(GITERR_OS, "Attempt to create empty path");
250 goto fail;
97769280 251 }
f0b2bfe5 252
ca1b6e54
RB
253 /* remove trailing slashes on path */
254 while (make_path.ptr[make_path.size - 1] == '/') {
255 make_path.size--;
256 make_path.ptr[make_path.size] = '\0';
257 }
412de9a6 258
ca1b6e54
RB
259 /* if we are not supposed to made the last element, truncate it */
260 if ((flags & GIT_MKDIR_SKIP_LAST) != 0)
261 git_buf_rtruncate_at_char(&make_path, '/');
262
263 /* if we are not supposed to make the whole path, reset root */
264 if ((flags & GIT_MKDIR_PATH) == 0)
265 root = git_buf_rfind(&make_path, '/');
266
267 /* clip root to make_path length */
268 if (root >= (ssize_t)make_path.size)
269 root = (ssize_t)make_path.size - 1;
0e26202c
RB
270 if (root < 0)
271 root = 0;
ca1b6e54
RB
272
273 tail = & make_path.ptr[root];
274
275 while (*tail) {
276 /* advance tail to include next path component */
277 while (*tail == '/')
278 tail++;
279 while (*tail && *tail != '/')
280 tail++;
281
282 /* truncate path at next component */
283 lastch = *tail;
284 *tail = '\0';
285
286 /* make directory */
287 if (p_mkdir(make_path.ptr, mode) < 0 &&
288 (errno != EEXIST || (flags & GIT_MKDIR_EXCL) != 0))
289 {
290 giterr_set(GITERR_OS, "Failed to make directory '%s'",
291 make_path.ptr);
292 goto fail;
d5f25204 293 }
40c44d2f 294
ca1b6e54
RB
295 /* chmod if requested */
296 if ((flags & GIT_MKDIR_CHMOD_PATH) != 0 ||
297 ((flags & GIT_MKDIR_CHMOD) != 0 && lastch == '\0'))
298 {
299 if (p_chmod(make_path.ptr, mode) < 0) {
300 giterr_set(GITERR_OS, "Failed to set permissions on '%s'",
301 make_path.ptr);
302 goto fail;
303 }
304 }
f0b2bfe5 305
ca1b6e54 306 *tail = lastch;
a993e4fe 307 }
40c44d2f 308
97769280 309 git_buf_free(&make_path);
ca1b6e54 310 return 0;
77c3999c 311
ca1b6e54
RB
312fail:
313 git_buf_free(&make_path);
314 return -1;
315}
77c3999c 316
ca1b6e54
RB
317int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
318{
319 return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH);
170d3f2f 320}
321
97769280 322static int _rmdir_recurs_foreach(void *opaque, git_buf *path)
42b3a460 323{
1a2b8725 324 git_directory_removal_type removal_type = *(git_directory_removal_type *)opaque;
555aa453 325
326 assert(removal_type == GIT_DIRREMOVAL_EMPTY_HIERARCHY
327 || removal_type == GIT_DIRREMOVAL_FILES_AND_DIRS
328 || removal_type == GIT_DIRREMOVAL_ONLY_EMPTY_DIRS);
42b3a460 329
1a481123 330 if (git_path_isdir(path->ptr) == true) {
deafee7b
RB
331 if (git_path_direach(path, _rmdir_recurs_foreach, opaque) < 0)
332 return -1;
333
334 if (p_rmdir(path->ptr) < 0) {
54bdc64a 335 if (removal_type == GIT_DIRREMOVAL_ONLY_EMPTY_DIRS && (errno == ENOTEMPTY || errno == EEXIST))
555aa453 336 return 0;
337
deafee7b
RB
338 giterr_set(GITERR_OS, "Could not remove directory '%s'", path->ptr);
339 return -1;
340 }
42b3a460 341
deafee7b 342 return 0;
858dba58
VM
343 }
344
555aa453 345 if (removal_type == GIT_DIRREMOVAL_FILES_AND_DIRS) {
deafee7b
RB
346 if (p_unlink(path->ptr) < 0) {
347 giterr_set(GITERR_OS, "Could not remove directory. File '%s' cannot be removed", path->ptr);
348 return -1;
349 }
350
351 return 0;
352 }
353
555aa453 354 if (removal_type == GIT_DIRREMOVAL_EMPTY_HIERARCHY) {
355 giterr_set(GITERR_OS, "Could not remove directory. File '%s' still present", path->ptr);
356 return -1;
357 }
358
359 return 0;
42b3a460
MS
360}
361
1a2b8725 362int git_futils_rmdir_r(const char *path, git_directory_removal_type removal_type)
42b3a460 363{
97769280
RB
364 int error;
365 git_buf p = GIT_BUF_INIT;
366
367 error = git_buf_sets(&p, path);
deafee7b 368 if (!error)
555aa453 369 error = _rmdir_recurs_foreach(&removal_type, &p);
97769280
RB
370 git_buf_free(&p);
371 return error;
42b3a460
MS
372}
373
23059130 374#ifdef GIT_WIN32
349fb6d7
VM
375struct win32_path {
376 wchar_t path[MAX_PATH];
73b51450 377 DWORD len;
349fb6d7 378};
73b51450 379
349fb6d7 380static int win32_expand_path(struct win32_path *s_root, const wchar_t *templ)
73b51450 381{
349fb6d7
VM
382 s_root->len = ExpandEnvironmentStringsW(templ, s_root->path, MAX_PATH);
383 return s_root->len ? 0 : -1;
73b51450
RB
384}
385
349fb6d7 386static int win32_find_file(git_buf *path, const struct win32_path *root, const char *filename)
73b51450 387{
0f4c6175 388 size_t len, alloc_len;
349fb6d7 389 wchar_t *file_utf16 = NULL;
6813169a 390 char file_utf8[GIT_PATH_MAX];
73b51450
RB
391
392 if (!root || !filename || (len = strlen(filename)) == 0)
393 return GIT_ENOTFOUND;
394
395 /* allocate space for wchar_t path to file */
0f4c6175
VM
396 alloc_len = root->len + len + 2;
397 file_utf16 = git__calloc(alloc_len, sizeof(wchar_t));
deafee7b 398 GITERR_CHECK_ALLOC(file_utf16);
73b51450
RB
399
400 /* append root + '\\' + filename as wchar_t */
401 memcpy(file_utf16, root->path, root->len * sizeof(wchar_t));
402
403 if (*filename == '/' || *filename == '\\')
404 filename++;
405
0f4c6175 406 git__utf8_to_16(file_utf16 + root->len - 1, alloc_len, filename);
73b51450 407
73b51450
RB
408 /* check access */
409 if (_waccess(file_utf16, F_OK) < 0) {
6813169a
VM
410 git__free(file_utf16);
411 return GIT_ENOTFOUND;
73b51450
RB
412 }
413
6813169a
VM
414 git__utf16_to_8(file_utf8, file_utf16);
415 git_path_mkposix(file_utf8);
416 git_buf_sets(path, file_utf8);
73b51450 417
73b51450 418 git__free(file_utf16);
6813169a 419 return 0;
73b51450 420}
8b3de0b6 421static wchar_t* win32_nextpath(wchar_t *path, wchar_t *buf, size_t buflen)
549ee21a 422{
8b3de0b6 423 wchar_t term, *base = path;
549ee21a 424
8b3de0b6 425 assert(path && buf && buflen);
549ee21a 426
8b3de0b6 427 term = (*path == L'"') ? *path++ : L';';
549ee21a 428
8b3de0b6
SS
429 for (buflen--; *path && *path != term && buflen; buflen--)
430 *buf++ = *path++;
549ee21a 431
8b3de0b6 432 *buf = L'\0'; /* reserved a byte via initial subtract */
549ee21a 433
8b3de0b6
SS
434 while (*path == term || *path == L';')
435 path++;
549ee21a 436
8b3de0b6 437 return (path != base) ? path : NULL;
549ee21a
SS
438}
439
19aa8416 440static int win32_find_system_file_using_path(git_buf *path, const char *filename)
549ee21a 441{
77ddd4cc 442 wchar_t * env = NULL;
549ee21a
SS
443 struct win32_path root;
444
77ddd4cc
SS
445 env = _wgetenv(L"PATH");
446 if (!env)
549ee21a
SS
447 return -1;
448
549ee21a 449 // search in all paths defined in PATH
dee18b82 450 while ((env = win32_nextpath(env, root.path, MAX_PATH - 1)) != NULL && *root.path)
549ee21a
SS
451 {
452 wchar_t * pfin = root.path + wcslen(root.path) - 1; // last char of the current path entry
453
454 // ensure trailing slash
455 if (*pfin != L'/' && *pfin != L'\\')
77ddd4cc 456 wcscpy(++pfin, L"\\"); // we have enough space left, MAX_PATH - 1 is used in nextpath above
549ee21a
SS
457
458 root.len = (DWORD)wcslen(root.path) + 1;
459
460 if (win32_find_file(path, &root, "git.cmd") == 0 || win32_find_file(path, &root, "git.exe") == 0) {
461 // we found the cmd or bin directory of a git installaton
462 if (root.len > 5) {
77ddd4cc 463 wcscpy(root.path + wcslen(root.path) - 4, L"etc\\");
549ee21a 464 if (win32_find_file(path, &root, filename) == 0)
549ee21a 465 return 0;
549ee21a
SS
466 }
467 }
468 }
469
549ee21a
SS
470 return GIT_ENOTFOUND;
471}
73b51450 472
19aa8416 473static int win32_find_system_file_using_registry(git_buf *path, const char *filename)
73b51450 474{
6605f51d
SS
475#ifndef _WIN64
476#define REG_MSYSGIT_INSTALL L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1"
477#else
478#define REG_MSYSGIT_INSTALL L"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1"
479#endif
480
349fb6d7 481 struct win32_path root;
9cde607c 482
6605f51d
SS
483 HKEY hKey;
484 DWORD dwType = REG_SZ;
485 DWORD dwSize = MAX_PATH;
486
487 root.len = 0;
488 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
29ef309e 489 {
6605f51d
SS
490 if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType,(LPBYTE)&root.path, &dwSize) == ERROR_SUCCESS)
491 {
492 // InstallLocation points to the root of the msysgit directory
77ddd4cc 493 if (dwSize + 4 > MAX_PATH) // 4 = wcslen(L"etc\\")
6605f51d
SS
494 {
495 giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory - path too long");
496 return -1;
497 }
77ddd4cc 498 wcscat(root.path, L"etc\\");
6605f51d
SS
499 root.len = (DWORD)wcslen(root.path) + 1;
500 }
501 }
502 RegCloseKey(hKey);
503
504 if (!root.len) {
505 giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory");
349fb6d7
VM
506 return -1;
507 }
508
29ef309e 509 if (win32_find_file(path, &root, filename) < 0) {
0b956819 510 giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename);
29ef309e
RB
511 git_buf_clear(path);
512 return GIT_ENOTFOUND;
513 }
514
349fb6d7 515 return 0;
32a4e3b7
SS
516}
517#endif
349fb6d7 518
32a4e3b7
SS
519int git_futils_find_system_file(git_buf *path, const char *filename)
520{
521#ifdef GIT_WIN32
522 // try to find git.exe/git.cmd on path
dee18b82 523 if (!win32_find_system_file_using_path(path, filename))
32a4e3b7
SS
524 return 0;
525
526 // try to find msysgit installation path using registry
dee18b82 527 if (!win32_find_system_file_using_registry(path, filename))
32a4e3b7 528 return 0;
349fb6d7 529#else
cb8a7961
VM
530 if (git_buf_joinpath(path, "/etc", filename) < 0)
531 return -1;
73b51450 532
1a481123 533 if (git_path_exists(path->ptr) == true)
deafee7b 534 return 0;
32a4e3b7 535#endif
73b51450
RB
536
537 git_buf_clear(path);
0b956819 538 giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename);
349fb6d7 539 return GIT_ENOTFOUND;
349fb6d7 540}
73b51450 541
349fb6d7
VM
542int git_futils_find_global_file(git_buf *path, const char *filename)
543{
aed8f8a1
SS
544 const char *home = getenv("HOME");
545
73b51450 546#ifdef GIT_WIN32
349fb6d7
VM
547 struct win32_path root;
548
aed8f8a1
SS
549 if (home != NULL) {
550 if (git_buf_joinpath(path, home, filename) < 0)
551 return -1;
552
553 if (git_path_exists(path->ptr)) {
554 return 0;
555 }
556 }
557
f2b126c7
SS
558 if (getenv("HOMEPATH") != NULL) {
559 if (win32_expand_path(&root, L"%HOMEDRIVE%%HOMEPATH%\\") < 0 ||
560 root.path[0] == L'%') /* i.e. no expansion happened */
561 {
562 giterr_set(GITERR_OS, "Cannot locate the user's profile directory");
563 return -1;
564 }
565 } else {
566 if (win32_expand_path(&root, L"%USERPROFILE%\\") < 0 ||
567 root.path[0] == L'%') /* i.e. no expansion happened */
568 {
569 giterr_set(GITERR_OS, "Cannot locate the user's profile directory");
570 return -1;
571 }
349fb6d7
VM
572 }
573
29ef309e 574 if (win32_find_file(path, &root, filename) < 0) {
0b956819 575 giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
29ef309e
RB
576 git_buf_clear(path);
577 return GIT_ENOTFOUND;
578 }
579
349fb6d7 580 return 0;
73b51450 581#else
349fb6d7
VM
582 if (home == NULL) {
583 giterr_set(GITERR_OS, "Global file lookup failed. "
584 "Cannot locate the user's home directory");
585 return -1;
586 }
587
588 if (git_buf_joinpath(path, home, filename) < 0)
589 return -1;
590
591 if (git_path_exists(path->ptr) == false) {
0b956819 592 giterr_set(GITERR_OS, "The global file '%s' doesn't exist", filename);
349fb6d7
VM
593 git_buf_clear(path);
594 return GIT_ENOTFOUND;
595 }
596
597 return 0;
73b51450
RB
598#endif
599}
8651c10f
BS
600
601int git_futils_fake_symlink(const char *old, const char *new)
602{
603 int retcode = GIT_ERROR;
604 int fd = git_futils_creat_withpath(new, 0755, 0644);
605 if (fd >= 0) {
606 retcode = p_write(fd, old, strlen(old));
607 p_close(fd);
608 }
609 return retcode;
610}
ca1b6e54 611
85bd1746 612static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done)
ca1b6e54
RB
613{
614 int error = 0;
615 char buffer[4096];
616 ssize_t len = 0;
617
618 while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0)
619 /* p_write() does not have the same semantics as write(). It loops
620 * internally and will return 0 when it has completed writing.
621 */
622 error = p_write(ofd, buffer, len);
623
624 if (len < 0) {
625 giterr_set(GITERR_OS, "Read error while copying file");
626 error = (int)len;
627 }
628
85bd1746 629 if (close_fd_when_done) {
ca1b6e54
RB
630 p_close(ifd);
631 p_close(ofd);
632 }
633
634 return error;
635}
636
85bd1746 637int git_futils_cp(const char *from, const char *to, mode_t filemode)
ca1b6e54
RB
638{
639 int ifd, ofd;
640
ca1b6e54
RB
641 if ((ifd = git_futils_open_ro(from)) < 0)
642 return ifd;
643
644 if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
645 if (errno == ENOENT || errno == ENOTDIR)
646 ofd = GIT_ENOTFOUND;
647 giterr_set(GITERR_OS, "Failed to open '%s' for writing", to);
648 p_close(ifd);
649 return ofd;
650 }
651
85bd1746 652 return cp_by_fd(ifd, ofd, true);
ca1b6e54
RB
653}
654
85bd1746 655static int cp_link(const char *from, const char *to, size_t link_size)
ca1b6e54
RB
656{
657 int error = 0;
658 ssize_t read_len;
85bd1746 659 char *link_data = git__malloc(link_size + 1);
ca1b6e54
RB
660 GITERR_CHECK_ALLOC(link_data);
661
85bd1746
RB
662 read_len = p_readlink(from, link_data, link_size);
663 if (read_len != (ssize_t)link_size) {
ca1b6e54
RB
664 giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from);
665 error = -1;
666 }
667 else {
668 link_data[read_len] = '\0';
669
670 if (p_symlink(link_data, to) < 0) {
671 giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'",
672 link_data, to);
673 error = -1;
674 }
675 }
676
677 git__free(link_data);
678 return error;
679}
680
681typedef struct {
682 const char *to_root;
683 git_buf to;
684 ssize_t from_prefix;
685 uint32_t flags;
686 uint32_t mkdir_flags;
687 mode_t dirmode;
688} cp_r_info;
689
690static int _cp_r_callback(void *ref, git_buf *from)
691{
692 cp_r_info *info = ref;
693 struct stat from_st, to_st;
694 bool exists = false;
695
696 if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 &&
697 from->ptr[git_path_basename_offset(from)] == '.')
698 return 0;
699
700 if (git_buf_joinpath(
701 &info->to, info->to_root, from->ptr + info->from_prefix) < 0)
702 return -1;
703
704 if (p_lstat(info->to.ptr, &to_st) < 0) {
2eb4edf5 705 if (errno != ENOENT && errno != ENOTDIR) {
ca1b6e54
RB
706 giterr_set(GITERR_OS,
707 "Could not access %s while copying files", info->to.ptr);
708 return -1;
709 }
710 } else
711 exists = true;
712
713 if (git_path_lstat(from->ptr, &from_st) < 0)
714 return -1;
715
716 if (S_ISDIR(from_st.st_mode)) {
717 int error = 0;
718 mode_t oldmode = info->dirmode;
719
720 /* if we are not chmod'ing, then overwrite dirmode */
721 if ((info->flags & GIT_CPDIR_CHMOD) == 0)
722 info->dirmode = from_st.st_mode;
723
724 /* make directory now if CREATE_EMPTY_DIRS is requested and needed */
725 if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0)
726 error = git_futils_mkdir(
727 info->to.ptr, NULL, info->dirmode, info->mkdir_flags);
728
729 /* recurse onto target directory */
730 if (!exists || S_ISDIR(to_st.st_mode))
731 error = git_path_direach(from, _cp_r_callback, info);
732
733 if (oldmode != 0)
734 info->dirmode = oldmode;
735
736 return error;
737 }
738
739 if (exists) {
740 if ((info->flags & GIT_CPDIR_OVERWRITE) == 0)
741 return 0;
742
743 if (p_unlink(info->to.ptr) < 0) {
744 giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'",
745 info->to.ptr);
746 return -1;
747 }
748 }
749
750 /* Done if this isn't a regular file or a symlink */
751 if (!S_ISREG(from_st.st_mode) &&
752 (!S_ISLNK(from_st.st_mode) ||
753 (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0))
754 return 0;
755
756 /* Make container directory on demand if needed */
757 if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
758 git_futils_mkdir(
759 info->to.ptr, NULL, info->dirmode, info->mkdir_flags) < 0)
760 return -1;
761
762 /* make symlink or regular file */
763 if (S_ISLNK(from_st.st_mode))
85bd1746 764 return cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size);
ca1b6e54 765 else
85bd1746 766 return git_futils_cp(from->ptr, info->to.ptr, from_st.st_mode);
ca1b6e54
RB
767}
768
769int git_futils_cp_r(
770 const char *from,
771 const char *to,
772 uint32_t flags,
773 mode_t dirmode)
774{
775 int error;
776 git_buf path = GIT_BUF_INIT;
777 cp_r_info info;
778
779 if (git_buf_sets(&path, from) < 0)
780 return -1;
781
782 info.to_root = to;
783 info.flags = flags;
784 info.dirmode = dirmode;
785 info.from_prefix = path.size;
786 git_buf_init(&info.to, 0);
787
788 /* precalculate mkdir flags */
789 if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) {
790 info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST;
791 if ((flags & GIT_CPDIR_CHMOD) != 0)
792 info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH;
793 } else {
794 info.mkdir_flags =
795 ((flags & GIT_CPDIR_CHMOD) != 0) ? GIT_MKDIR_CHMOD : 0;
796 }
797
798 error = _cp_r_callback(&info, &path);
799
800 git_buf_free(&path);
07c06f7a 801 git_buf_free(&info.to);
ca1b6e54
RB
802
803 return error;
804}