]>
Commit | Line | Data |
---|---|---|
bb742ede | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
bb742ede VM |
3 | * |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
5ee2fe77 | 7 | #include "common.h" |
ec250c6e | 8 | #include "fileops.h" |
a3aa5f4d | 9 | #include "global.h" |
500ec543 | 10 | #include "strmap.h" |
2e29957a | 11 | #include <ctype.h> |
997579be SS |
12 | #if GIT_WIN32 |
13 | #include "win32/findfile.h" | |
14 | #endif | |
ec250c6e | 15 | |
500ec543 ET |
16 | GIT__USE_STRMAP; |
17 | ||
ce8cd006 | 18 | int git_futils_mkpath2file(const char *file_path, const mode_t mode) |
55ffebe3 | 19 | { |
ca1b6e54 | 20 | return git_futils_mkdir( |
331e7de9 RB |
21 | file_path, NULL, mode, |
22 | GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR); | |
55ffebe3 VM |
23 | } |
24 | ||
1d3a8aeb | 25 | int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode) |
72a3fe42 VM |
26 | { |
27 | int fd; | |
1d3a8aeb ET |
28 | mode_t mask; |
29 | ||
30 | p_umask(mask = p_umask(0)); | |
72a3fe42 | 31 | |
97769280 RB |
32 | git_buf_sets(path_out, filename); |
33 | git_buf_puts(path_out, "_git2_XXXXXX"); | |
34 | ||
35 | if (git_buf_oom(path_out)) | |
1a481123 | 36 | return -1; |
72a3fe42 | 37 | |
1a481123 VM |
38 | if ((fd = p_mkstemp(path_out->ptr)) < 0) { |
39 | giterr_set(GITERR_OS, | |
ae9e29fd | 40 | "Failed to create temporary file '%s'", path_out->ptr); |
1a481123 VM |
41 | return -1; |
42 | } | |
72a3fe42 | 43 | |
1d3a8aeb ET |
44 | if (p_chmod(path_out->ptr, (mode & ~mask))) { |
45 | giterr_set(GITERR_OS, | |
46 | "Failed to set permissions on file '%s'", path_out->ptr); | |
47 | return -1; | |
48 | } | |
49 | ||
f978b748 | 50 | return fd; |
72a3fe42 VM |
51 | } |
52 | ||
ce8cd006 | 53 | int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode) |
7dd8a9f7 | 54 | { |
1a481123 | 55 | int fd; |
55ffebe3 | 56 | |
1a481123 VM |
57 | if (git_futils_mkpath2file(path, dirmode) < 0) |
58 | return -1; | |
59 | ||
60 | fd = p_creat(path, mode); | |
61 | if (fd < 0) { | |
ae9e29fd | 62 | giterr_set(GITERR_OS, "Failed to create file '%s'", path); |
1a481123 VM |
63 | return -1; |
64 | } | |
65 | ||
66 | return fd; | |
55ffebe3 VM |
67 | } |
68 | ||
33127043 | 69 | int git_futils_creat_locked(const char *path, const mode_t mode) |
1549cba9 | 70 | { |
13f36ffb | 71 | int fd = p_open(path, O_WRONLY | O_CREAT | O_TRUNC | |
3d3ea4dc | 72 | O_EXCL | O_BINARY | O_CLOEXEC, mode); |
09719c50 | 73 | |
1a481123 | 74 | if (fd < 0) { |
ae9e29fd | 75 | giterr_set(GITERR_OS, "Failed to create locked file '%s'", path); |
3d276874 | 76 | return errno == EEXIST ? GIT_ELOCKED : -1; |
1a481123 VM |
77 | } |
78 | ||
79 | return fd; | |
1549cba9 RG |
80 | } |
81 | ||
ce8cd006 | 82 | int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode) |
1549cba9 | 83 | { |
1a481123 VM |
84 | if (git_futils_mkpath2file(path, dirmode) < 0) |
85 | return -1; | |
1549cba9 | 86 | |
f79026b4 | 87 | return git_futils_creat_locked(path, mode); |
ec250c6e AE |
88 | } |
89 | ||
deafee7b RB |
90 | int git_futils_open_ro(const char *path) |
91 | { | |
92 | int fd = p_open(path, O_RDONLY); | |
14997dc5 RB |
93 | if (fd < 0) |
94 | return git_path_set_error(errno, path, "open"); | |
deafee7b RB |
95 | return fd; |
96 | } | |
97 | ||
f79026b4 | 98 | git_off_t git_futils_filesize(git_file fd) |
ec250c6e | 99 | { |
7dd8a9f7 | 100 | struct stat sb; |
deafee7b RB |
101 | |
102 | if (p_fstat(fd, &sb)) { | |
103 | giterr_set(GITERR_OS, "Failed to stat file descriptor"); | |
104 | return -1; | |
105 | } | |
5ad739e8 | 106 | |
ec250c6e AE |
107 | return sb.st_size; |
108 | } | |
4188d28f | 109 | |
b6c93aef RB |
110 | mode_t git_futils_canonical_mode(mode_t raw_mode) |
111 | { | |
112 | if (S_ISREG(raw_mode)) | |
f240acce | 113 | return S_IFREG | GIT_PERMS_CANONICAL(raw_mode); |
b6c93aef RB |
114 | else if (S_ISLNK(raw_mode)) |
115 | return S_IFLNK; | |
b6c93aef RB |
116 | else if (S_ISGITLINK(raw_mode)) |
117 | return S_IFGITLINK; | |
7c7ff7d1 RB |
118 | else if (S_ISDIR(raw_mode)) |
119 | return S_IFDIR; | |
b6c93aef RB |
120 | else |
121 | return 0; | |
122 | } | |
123 | ||
60b9d3fc RB |
124 | int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len) |
125 | { | |
de590550 | 126 | ssize_t read_size = 0; |
60b9d3fc RB |
127 | |
128 | git_buf_clear(buf); | |
129 | ||
392702ee | 130 | GITERR_CHECK_ALLOC_ADD(len, 1); |
60b9d3fc RB |
131 | if (git_buf_grow(buf, len + 1) < 0) |
132 | return -1; | |
133 | ||
1f35e89d RB |
134 | /* p_read loops internally to read len bytes */ |
135 | read_size = p_read(fd, buf->ptr, len); | |
60b9d3fc | 136 | |
c859184b | 137 | if (read_size != (ssize_t)len) { |
1f35e89d | 138 | giterr_set(GITERR_OS, "Failed to read descriptor"); |
823c0e9c | 139 | git_buf_free(buf); |
1f35e89d | 140 | return -1; |
60b9d3fc RB |
141 | } |
142 | ||
1f35e89d RB |
143 | buf->ptr[read_size] = '\0'; |
144 | buf->size = read_size; | |
145 | ||
60b9d3fc RB |
146 | return 0; |
147 | } | |
148 | ||
149 | int git_futils_readbuffer_updated( | |
744cc03e | 150 | git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated) |
75d58430 RJ |
151 | { |
152 | git_file fd; | |
c3da9f06 | 153 | struct stat st; |
744cc03e | 154 | bool changed = false; |
75d58430 | 155 | |
13224ea4 | 156 | assert(buf && path && *path); |
75d58430 | 157 | |
c3da9f06 CMN |
158 | if (updated != NULL) |
159 | *updated = 0; | |
75d58430 | 160 | |
14997dc5 RB |
161 | if (p_stat(path, &st) < 0) |
162 | return git_path_set_error(errno, path, "stat"); | |
c3da9f06 | 163 | |
7c9f5bec CMN |
164 | |
165 | if (S_ISDIR(st.st_mode)) { | |
166 | giterr_set(GITERR_INVALID, "requested file is a directory"); | |
167 | return GIT_ENOTFOUND; | |
168 | } | |
169 | ||
170 | if (!git__is_sizet(st.st_size+1)) { | |
1a481123 VM |
171 | giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path); |
172 | return -1; | |
13224ea4 | 173 | } |
c3da9f06 CMN |
174 | |
175 | /* | |
744cc03e RB |
176 | * If we were given a time and/or a size, we only want to read the file |
177 | * if it has been modified. | |
c3da9f06 | 178 | */ |
744cc03e RB |
179 | if (size && *size != (size_t)st.st_size) |
180 | changed = true; | |
181 | if (mtime && *mtime != st.st_mtime) | |
182 | changed = true; | |
183 | if (!size && !mtime) | |
184 | changed = true; | |
185 | ||
186 | if (!changed) { | |
13224ea4 VM |
187 | return 0; |
188 | } | |
c3da9f06 CMN |
189 | |
190 | if (mtime != NULL) | |
191 | *mtime = st.st_mtime; | |
744cc03e RB |
192 | if (size != NULL) |
193 | *size = (size_t)st.st_size; | |
c3da9f06 | 194 | |
9ccdb211 BR |
195 | if ((fd = git_futils_open_ro(path)) < 0) |
196 | return fd; | |
197 | ||
60b9d3fc | 198 | if (git_futils_readbuffer_fd(buf, fd, (size_t)st.st_size) < 0) { |
e1de726c RB |
199 | p_close(fd); |
200 | return -1; | |
75d58430 RJ |
201 | } |
202 | ||
f79026b4 | 203 | p_close(fd); |
75d58430 | 204 | |
c3da9f06 CMN |
205 | if (updated != NULL) |
206 | *updated = 1; | |
207 | ||
13224ea4 | 208 | return 0; |
c3da9f06 CMN |
209 | } |
210 | ||
13224ea4 | 211 | int git_futils_readbuffer(git_buf *buf, const char *path) |
97769280 | 212 | { |
744cc03e | 213 | return git_futils_readbuffer_updated(buf, path, NULL, NULL, NULL); |
97769280 RB |
214 | } |
215 | ||
4742148d RB |
216 | int git_futils_writebuffer( |
217 | const git_buf *buf, const char *path, int flags, mode_t mode) | |
218 | { | |
219 | int fd, error = 0; | |
220 | ||
221 | if (flags <= 0) | |
222 | flags = O_CREAT | O_TRUNC | O_WRONLY; | |
223 | if (!mode) | |
224 | mode = GIT_FILEMODE_BLOB; | |
225 | ||
226 | if ((fd = p_open(path, flags, mode)) < 0) { | |
227 | giterr_set(GITERR_OS, "Could not open '%s' for writing", path); | |
228 | return fd; | |
229 | } | |
230 | ||
231 | if ((error = p_write(fd, git_buf_cstr(buf), git_buf_len(buf))) < 0) { | |
232 | giterr_set(GITERR_OS, "Could not write to '%s'", path); | |
233 | (void)p_close(fd); | |
f5254d78 | 234 | return error; |
4742148d RB |
235 | } |
236 | ||
237 | if ((error = p_close(fd)) < 0) | |
238 | giterr_set(GITERR_OS, "Error while closing '%s'", path); | |
239 | ||
240 | return error; | |
241 | } | |
242 | ||
ce8cd006 | 243 | int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode) |
19a30a3f | 244 | { |
deafee7b RB |
245 | if (git_futils_mkpath2file(to, dirmode) < 0) |
246 | return -1; | |
19a30a3f | 247 | |
deafee7b RB |
248 | if (p_rename(from, to) < 0) { |
249 | giterr_set(GITERR_OS, "Failed to rename '%s' to '%s'", from, to); | |
250 | return -1; | |
251 | } | |
252 | ||
253 | return 0; | |
19a30a3f VM |
254 | } |
255 | ||
f79026b4 | 256 | int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len) |
20e7f426 | 257 | { |
f79026b4 | 258 | return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin); |
20e7f426 SP |
259 | } |
260 | ||
74fa4bfa RB |
261 | int git_futils_mmap_ro_file(git_map *out, const char *path) |
262 | { | |
0d0fa7c3 RB |
263 | git_file fd = git_futils_open_ro(path); |
264 | git_off_t len; | |
da9abdd6 | 265 | int result; |
0d0fa7c3 RB |
266 | |
267 | if (fd < 0) | |
268 | return fd; | |
269 | ||
270 | len = git_futils_filesize(fd); | |
deafee7b RB |
271 | if (!git__is_sizet(len)) { |
272 | giterr_set(GITERR_OS, "File `%s` too large to mmap", path); | |
273 | return -1; | |
274 | } | |
0d0fa7c3 | 275 | |
da9abdd6 | 276 | result = git_futils_mmap_ro(out, fd, 0, (size_t)len); |
74fa4bfa RB |
277 | p_close(fd); |
278 | return result; | |
279 | } | |
280 | ||
f79026b4 | 281 | void git_futils_mmap_free(git_map *out) |
20e7f426 | 282 | { |
f79026b4 | 283 | p_munmap(out); |
20e7f426 SP |
284 | } |
285 | ||
e74340b0 ET |
286 | GIT_INLINE(int) validate_existing( |
287 | const char *make_path, | |
288 | struct stat *st, | |
289 | mode_t mode, | |
290 | uint32_t flags, | |
291 | struct git_futils_mkdir_perfdata *perfdata) | |
292 | { | |
293 | if ((S_ISREG(st->st_mode) && (flags & GIT_MKDIR_REMOVE_FILES)) || | |
294 | (S_ISLNK(st->st_mode) && (flags & GIT_MKDIR_REMOVE_SYMLINKS))) { | |
295 | if (p_unlink(make_path) < 0) { | |
296 | giterr_set(GITERR_OS, "Failed to remove %s '%s'", | |
297 | S_ISLNK(st->st_mode) ? "symlink" : "file", make_path); | |
298 | return GIT_EEXISTS; | |
299 | } | |
300 | ||
301 | perfdata->mkdir_calls++; | |
302 | ||
303 | if (p_mkdir(make_path, mode) < 0) { | |
304 | giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path); | |
305 | return GIT_EEXISTS; | |
306 | } | |
307 | } | |
308 | ||
309 | else if (S_ISLNK(st->st_mode)) { | |
310 | /* Re-stat the target, make sure it's a directory */ | |
311 | perfdata->stat_calls++; | |
312 | ||
313 | if (p_stat(make_path, st) < 0) { | |
314 | giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path); | |
315 | return GIT_EEXISTS; | |
316 | } | |
317 | } | |
318 | ||
319 | else if (!S_ISDIR(st->st_mode)) { | |
320 | giterr_set(GITERR_FILESYSTEM, | |
321 | "Failed to make directory '%s': directory exists", make_path); | |
322 | return GIT_EEXISTS; | |
323 | } | |
324 | ||
325 | return 0; | |
326 | } | |
327 | ||
500ec543 | 328 | int git_futils_mkdir_ext( |
ca1b6e54 RB |
329 | const char *path, |
330 | const char *base, | |
331 | mode_t mode, | |
1d50b364 | 332 | uint32_t flags, |
500ec543 | 333 | struct git_futils_mkdir_options *opts) |
1a5204a7 | 334 | { |
f7e56150 | 335 | int error = -1; |
97769280 | 336 | git_buf make_path = GIT_BUF_INIT; |
9cb5b0f7 | 337 | ssize_t root = 0, min_root_len, root_len; |
f7e56150 | 338 | char lastch = '/', *tail; |
999d4405 | 339 | struct stat st; |
dc07184f | 340 | |
ca1b6e54 RB |
341 | /* build path and find "root" where we should start calling mkdir */ |
342 | if (git_path_join_unrooted(&make_path, path, base, &root) < 0) | |
343 | return -1; | |
dc07184f | 344 | |
ca1b6e54 RB |
345 | if (make_path.size == 0) { |
346 | giterr_set(GITERR_OS, "Attempt to create empty path"); | |
999d4405 | 347 | goto done; |
97769280 | 348 | } |
f0b2bfe5 | 349 | |
9cb5b0f7 ET |
350 | /* Trim trailing slashes (except the root) */ |
351 | if ((root_len = git_path_root(make_path.ptr)) < 0) | |
352 | root_len = 0; | |
353 | else | |
354 | root_len++; | |
355 | ||
356 | while (make_path.size > (size_t)root_len && | |
357 | make_path.ptr[make_path.size - 1] == '/') | |
358 | make_path.ptr[--make_path.size] = '\0'; | |
412de9a6 | 359 | |
ca1b6e54 | 360 | /* if we are not supposed to made the last element, truncate it */ |
3c42e4ef | 361 | if ((flags & GIT_MKDIR_SKIP_LAST2) != 0) { |
9cb5b0f7 | 362 | git_path_dirname_r(&make_path, make_path.ptr); |
3c42e4ef RB |
363 | flags |= GIT_MKDIR_SKIP_LAST; |
364 | } | |
9cb5b0f7 ET |
365 | if ((flags & GIT_MKDIR_SKIP_LAST) != 0) { |
366 | git_path_dirname_r(&make_path, make_path.ptr); | |
367 | } | |
ca1b6e54 | 368 | |
9cb5b0f7 ET |
369 | /* We were either given the root path (or trimmed it to |
370 | * the root), we don't have anything to do. | |
371 | */ | |
372 | if (make_path.size <= (size_t)root_len) { | |
f7e56150 RB |
373 | error = 0; |
374 | goto done; | |
375 | } | |
376 | ||
ca1b6e54 RB |
377 | /* if we are not supposed to make the whole path, reset root */ |
378 | if ((flags & GIT_MKDIR_PATH) == 0) | |
379 | root = git_buf_rfind(&make_path, '/'); | |
380 | ||
f7e56150 RB |
381 | /* advance root past drive name or network mount prefix */ |
382 | min_root_len = git_path_root(make_path.ptr); | |
383 | if (root < min_root_len) | |
384 | root = min_root_len; | |
2da72fb2 | 385 | while (root >= 0 && make_path.ptr[root] == '/') |
f7e56150 RB |
386 | ++root; |
387 | ||
ca1b6e54 | 388 | /* clip root to make_path length */ |
f7e56150 RB |
389 | if (root > (ssize_t)make_path.size) |
390 | root = (ssize_t)make_path.size; /* i.e. NUL byte of string */ | |
0e26202c RB |
391 | if (root < 0) |
392 | root = 0; | |
ca1b6e54 | 393 | |
f7e56150 RB |
394 | /* walk down tail of path making each directory */ |
395 | for (tail = &make_path.ptr[root]; *tail; *tail = lastch) { | |
999d4405 | 396 | |
ca1b6e54 RB |
397 | /* advance tail to include next path component */ |
398 | while (*tail == '/') | |
399 | tail++; | |
400 | while (*tail && *tail != '/') | |
401 | tail++; | |
402 | ||
403 | /* truncate path at next component */ | |
404 | lastch = *tail; | |
405 | *tail = '\0'; | |
999d4405 | 406 | st.st_mode = 0; |
ca1b6e54 | 407 | |
500ec543 ET |
408 | if (opts->dir_map && git_strmap_exists(opts->dir_map, make_path.ptr)) |
409 | continue; | |
410 | ||
fe598f09 | 411 | /* See what's going on with this path component */ |
500ec543 | 412 | opts->perfdata.stat_calls++; |
1d50b364 | 413 | |
fe598f09 | 414 | if (p_lstat(make_path.ptr, &st) < 0) { |
500ec543 | 415 | opts->perfdata.mkdir_calls++; |
999d4405 | 416 | |
fe598f09 ET |
417 | if (errno != ENOENT || p_mkdir(make_path.ptr, mode) < 0) { |
418 | giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path.ptr); | |
419 | error = GIT_EEXISTS; | |
420 | goto done; | |
421 | } | |
422 | ||
423 | giterr_clear(); | |
424 | } else { | |
425 | /* with exclusive create, existing dir is an error */ | |
426 | if ((flags & GIT_MKDIR_EXCL) != 0) { | |
427 | giterr_set(GITERR_FILESYSTEM, "Failed to make directory '%s': directory exists", make_path.ptr); | |
428 | error = GIT_EEXISTS; | |
429 | goto done; | |
430 | } | |
431 | ||
e74340b0 | 432 | if ((error = validate_existing( |
500ec543 | 433 | make_path.ptr, &st, mode, flags, &opts->perfdata)) < 0) |
1d50b364 | 434 | goto done; |
d5f25204 | 435 | } |
40c44d2f | 436 | |
999d4405 | 437 | /* chmod if requested and necessary */ |
f7e56150 RB |
438 | if (((flags & GIT_MKDIR_CHMOD_PATH) != 0 || |
439 | (lastch == '\0' && (flags & GIT_MKDIR_CHMOD) != 0)) && | |
1d50b364 ET |
440 | st.st_mode != mode) { |
441 | ||
500ec543 | 442 | opts->perfdata.chmod_calls++; |
1d50b364 ET |
443 | |
444 | if ((error = p_chmod(make_path.ptr, mode)) < 0 && | |
445 | lastch == '\0') { | |
e74340b0 ET |
446 | giterr_set(GITERR_OS, "Failed to set permissions on '%s'", |
447 | make_path.ptr); | |
1d50b364 ET |
448 | goto done; |
449 | } | |
ca1b6e54 | 450 | } |
500ec543 ET |
451 | |
452 | if (opts->dir_map && opts->pool) { | |
453 | char *cache_path = git_pool_malloc(opts->pool, make_path.size + 1); | |
454 | GITERR_CHECK_ALLOC(cache_path); | |
455 | ||
456 | memcpy(cache_path, make_path.ptr, make_path.size + 1); | |
457 | ||
458 | git_strmap_insert(opts->dir_map, cache_path, cache_path, error); | |
459 | if (error < 0) | |
460 | goto done; | |
461 | } | |
a993e4fe | 462 | } |
40c44d2f | 463 | |
999d4405 RB |
464 | error = 0; |
465 | ||
466 | /* check that full path really is a directory if requested & needed */ | |
f7e56150 | 467 | if ((flags & GIT_MKDIR_VERIFY_DIR) != 0 && |
1d50b364 | 468 | lastch != '\0') { |
500ec543 | 469 | opts->perfdata.stat_calls++; |
1d50b364 ET |
470 | |
471 | if (p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode)) { | |
e74340b0 ET |
472 | giterr_set(GITERR_OS, "Path is not a directory '%s'", |
473 | make_path.ptr); | |
1d50b364 ET |
474 | error = GIT_ENOTFOUND; |
475 | } | |
999d4405 | 476 | } |
77c3999c | 477 | |
999d4405 | 478 | done: |
ca1b6e54 | 479 | git_buf_free(&make_path); |
331e7de9 | 480 | return error; |
ca1b6e54 | 481 | } |
77c3999c | 482 | |
1d50b364 ET |
483 | int git_futils_mkdir( |
484 | const char *path, | |
485 | const char *base, | |
486 | mode_t mode, | |
487 | uint32_t flags) | |
488 | { | |
500ec543 ET |
489 | struct git_futils_mkdir_options options = {0}; |
490 | return git_futils_mkdir_ext(path, base, mode, flags, &options); | |
1d50b364 ET |
491 | } |
492 | ||
ca1b6e54 RB |
493 | int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode) |
494 | { | |
495 | return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH); | |
170d3f2f | 496 | } |
497 | ||
331e7de9 | 498 | typedef struct { |
ad9a921b | 499 | const char *base; |
7e5c8a5b | 500 | size_t baselen; |
331e7de9 | 501 | uint32_t flags; |
219d3457 | 502 | int depth; |
331e7de9 RB |
503 | } futils__rmdir_data; |
504 | ||
219d3457 RB |
505 | #define FUTILS_MAX_DEPTH 100 |
506 | ||
331e7de9 | 507 | static int futils__error_cannot_rmdir(const char *path, const char *filemsg) |
42b3a460 | 508 | { |
331e7de9 RB |
509 | if (filemsg) |
510 | giterr_set(GITERR_OS, "Could not remove directory. File '%s' %s", | |
511 | path, filemsg); | |
512 | else | |
513 | giterr_set(GITERR_OS, "Could not remove directory '%s'", path); | |
555aa453 | 514 | |
331e7de9 RB |
515 | return -1; |
516 | } | |
deafee7b | 517 | |
ad9a921b RB |
518 | static int futils__rm_first_parent(git_buf *path, const char *ceiling) |
519 | { | |
520 | int error = GIT_ENOTFOUND; | |
521 | struct stat st; | |
522 | ||
523 | while (error == GIT_ENOTFOUND) { | |
524 | git_buf_rtruncate_at_char(path, '/'); | |
525 | ||
526 | if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0) | |
527 | error = 0; | |
cccacac5 | 528 | else if (p_lstat_posixly(path->ptr, &st) == 0) { |
ad9a921b RB |
529 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) |
530 | error = p_unlink(path->ptr); | |
531 | else if (!S_ISDIR(st.st_mode)) | |
532 | error = -1; /* fail to remove non-regular file */ | |
533 | } else if (errno != ENOTDIR) | |
534 | error = -1; | |
535 | } | |
536 | ||
537 | if (error) | |
538 | futils__error_cannot_rmdir(path->ptr, "cannot remove parent"); | |
539 | ||
540 | return error; | |
541 | } | |
542 | ||
331e7de9 RB |
543 | static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path) |
544 | { | |
96869a4e | 545 | int error = 0; |
331e7de9 | 546 | futils__rmdir_data *data = opaque; |
14997dc5 | 547 | struct stat st; |
555aa453 | 548 | |
14997dc5 RB |
549 | if (data->depth > FUTILS_MAX_DEPTH) |
550 | error = futils__error_cannot_rmdir( | |
551 | path->ptr, "directory nesting too deep"); | |
219d3457 | 552 | |
14997dc5 | 553 | else if ((error = p_lstat_posixly(path->ptr, &st)) < 0) { |
ad9a921b | 554 | if (errno == ENOENT) |
14997dc5 | 555 | error = 0; |
ad9a921b RB |
556 | else if (errno == ENOTDIR) { |
557 | /* asked to remove a/b/c/d/e and a/b is a normal file */ | |
558 | if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0) | |
14997dc5 | 559 | error = futils__rm_first_parent(path, data->base); |
ad9a921b RB |
560 | else |
561 | futils__error_cannot_rmdir( | |
562 | path->ptr, "parent is not directory"); | |
563 | } | |
564 | else | |
14997dc5 | 565 | error = git_path_set_error(errno, path->ptr, "rmdir"); |
ad9a921b RB |
566 | } |
567 | ||
568 | else if (S_ISDIR(st.st_mode)) { | |
219d3457 RB |
569 | data->depth++; |
570 | ||
14997dc5 | 571 | error = git_path_direach(path, 0, futils__rmdir_recurs_foreach, data); |
219d3457 RB |
572 | |
573 | data->depth--; | |
574 | ||
96869a4e | 575 | if (error < 0) |
25e0b157 | 576 | return error; |
96869a4e | 577 | |
219d3457 | 578 | if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0) |
25e0b157 | 579 | return error; |
331e7de9 | 580 | |
14997dc5 | 581 | if ((error = p_rmdir(path->ptr)) < 0) { |
331e7de9 | 582 | if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 && |
e09d18ee | 583 | (errno == ENOTEMPTY || errno == EEXIST || errno == EBUSY)) |
14997dc5 | 584 | error = 0; |
331e7de9 | 585 | else |
14997dc5 | 586 | error = git_path_set_error(errno, path->ptr, "rmdir"); |
deafee7b | 587 | } |
858dba58 VM |
588 | } |
589 | ||
331e7de9 | 590 | else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) { |
14997dc5 RB |
591 | if (p_unlink(path->ptr) < 0) |
592 | error = git_path_set_error(errno, path->ptr, "remove"); | |
deafee7b RB |
593 | } |
594 | ||
ad9a921b | 595 | else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0) |
14997dc5 | 596 | error = futils__error_cannot_rmdir(path->ptr, "still present"); |
555aa453 | 597 | |
25e0b157 | 598 | return error; |
331e7de9 RB |
599 | } |
600 | ||
bbb988a5 | 601 | static int futils__rmdir_empty_parent(void *opaque, const char *path) |
331e7de9 | 602 | { |
7e5c8a5b | 603 | futils__rmdir_data *data = opaque; |
96869a4e | 604 | int error = 0; |
7e5c8a5b | 605 | |
bbb988a5 | 606 | if (strlen(path) <= data->baselen) |
25e0b157 | 607 | error = GIT_ITEROVER; |
331e7de9 | 608 | |
bbb988a5 | 609 | else if (p_rmdir(path) < 0) { |
331e7de9 RB |
610 | int en = errno; |
611 | ||
612 | if (en == ENOENT || en == ENOTDIR) { | |
96869a4e | 613 | /* do nothing */ |
e09d18ee | 614 | } else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) { |
331e7de9 RB |
615 | error = GIT_ITEROVER; |
616 | } else { | |
bbb988a5 | 617 | error = git_path_set_error(errno, path, "rmdir"); |
331e7de9 RB |
618 | } |
619 | } | |
620 | ||
25e0b157 | 621 | return error; |
42b3a460 MS |
622 | } |
623 | ||
0d64bef9 | 624 | int git_futils_rmdir_r( |
331e7de9 | 625 | const char *path, const char *base, uint32_t flags) |
42b3a460 | 626 | { |
97769280 | 627 | int error; |
0d64bef9 | 628 | git_buf fullpath = GIT_BUF_INIT; |
96869a4e | 629 | futils__rmdir_data data; |
0d64bef9 RB |
630 | |
631 | /* build path and find "root" where we should start calling mkdir */ | |
632 | if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0) | |
633 | return -1; | |
634 | ||
96869a4e | 635 | memset(&data, 0, sizeof(data)); |
7e5c8a5b RB |
636 | data.base = base ? base : ""; |
637 | data.baselen = base ? strlen(base) : 0; | |
638 | data.flags = flags; | |
331e7de9 RB |
639 | |
640 | error = futils__rmdir_recurs_foreach(&data, &fullpath); | |
641 | ||
642 | /* remove now-empty parents if requested */ | |
96869a4e | 643 | if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0) |
331e7de9 RB |
644 | error = git_path_walk_up( |
645 | &fullpath, base, futils__rmdir_empty_parent, &data); | |
646 | ||
25e0b157 RB |
647 | if (error == GIT_ITEROVER) { |
648 | giterr_clear(); | |
96869a4e | 649 | error = 0; |
25e0b157 | 650 | } |
0d64bef9 RB |
651 | |
652 | git_buf_free(&fullpath); | |
97769280 | 653 | |
97769280 | 654 | return error; |
42b3a460 MS |
655 | } |
656 | ||
8651c10f BS |
657 | int git_futils_fake_symlink(const char *old, const char *new) |
658 | { | |
659 | int retcode = GIT_ERROR; | |
660 | int fd = git_futils_creat_withpath(new, 0755, 0644); | |
661 | if (fd >= 0) { | |
662 | retcode = p_write(fd, old, strlen(old)); | |
663 | p_close(fd); | |
664 | } | |
665 | return retcode; | |
666 | } | |
ca1b6e54 | 667 | |
85bd1746 | 668 | static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done) |
ca1b6e54 RB |
669 | { |
670 | int error = 0; | |
671 | char buffer[4096]; | |
672 | ssize_t len = 0; | |
673 | ||
674 | while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0) | |
675 | /* p_write() does not have the same semantics as write(). It loops | |
676 | * internally and will return 0 when it has completed writing. | |
677 | */ | |
678 | error = p_write(ofd, buffer, len); | |
679 | ||
680 | if (len < 0) { | |
681 | giterr_set(GITERR_OS, "Read error while copying file"); | |
682 | error = (int)len; | |
683 | } | |
684 | ||
85bd1746 | 685 | if (close_fd_when_done) { |
ca1b6e54 RB |
686 | p_close(ifd); |
687 | p_close(ofd); | |
688 | } | |
689 | ||
690 | return error; | |
691 | } | |
692 | ||
85bd1746 | 693 | int git_futils_cp(const char *from, const char *to, mode_t filemode) |
ca1b6e54 RB |
694 | { |
695 | int ifd, ofd; | |
696 | ||
ca1b6e54 RB |
697 | if ((ifd = git_futils_open_ro(from)) < 0) |
698 | return ifd; | |
699 | ||
700 | if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) { | |
ca1b6e54 | 701 | p_close(ifd); |
14997dc5 | 702 | return git_path_set_error(errno, to, "open for writing"); |
ca1b6e54 RB |
703 | } |
704 | ||
85bd1746 | 705 | return cp_by_fd(ifd, ofd, true); |
ca1b6e54 RB |
706 | } |
707 | ||
85bd1746 | 708 | static int cp_link(const char *from, const char *to, size_t link_size) |
ca1b6e54 RB |
709 | { |
710 | int error = 0; | |
711 | ssize_t read_len; | |
392702ee ET |
712 | char *link_data; |
713 | ||
714 | GITERR_CHECK_ALLOC_ADD(link_size, 1); | |
715 | link_data = git__malloc(link_size + 1); | |
ca1b6e54 RB |
716 | GITERR_CHECK_ALLOC(link_data); |
717 | ||
85bd1746 RB |
718 | read_len = p_readlink(from, link_data, link_size); |
719 | if (read_len != (ssize_t)link_size) { | |
ca1b6e54 RB |
720 | giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from); |
721 | error = -1; | |
722 | } | |
723 | else { | |
724 | link_data[read_len] = '\0'; | |
725 | ||
726 | if (p_symlink(link_data, to) < 0) { | |
727 | giterr_set(GITERR_OS, "Could not symlink '%s' as '%s'", | |
728 | link_data, to); | |
729 | error = -1; | |
730 | } | |
731 | } | |
732 | ||
733 | git__free(link_data); | |
734 | return error; | |
735 | } | |
736 | ||
737 | typedef struct { | |
738 | const char *to_root; | |
739 | git_buf to; | |
740 | ssize_t from_prefix; | |
741 | uint32_t flags; | |
742 | uint32_t mkdir_flags; | |
743 | mode_t dirmode; | |
744 | } cp_r_info; | |
745 | ||
3c42e4ef RB |
746 | #define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10) |
747 | ||
748 | static int _cp_r_mkdir(cp_r_info *info, git_buf *from) | |
749 | { | |
750 | int error = 0; | |
751 | ||
752 | /* create root directory the first time we need to create a directory */ | |
753 | if ((info->flags & GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT) == 0) { | |
754 | error = git_futils_mkdir( | |
755 | info->to_root, NULL, info->dirmode, | |
18f08264 | 756 | (info->flags & GIT_CPDIR_CHMOD_DIRS) ? GIT_MKDIR_CHMOD : 0); |
3c42e4ef RB |
757 | |
758 | info->flags |= GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT; | |
759 | } | |
760 | ||
761 | /* create directory with root as base to prevent excess chmods */ | |
762 | if (!error) | |
763 | error = git_futils_mkdir( | |
764 | from->ptr + info->from_prefix, info->to_root, | |
765 | info->dirmode, info->mkdir_flags); | |
766 | ||
767 | return error; | |
768 | } | |
769 | ||
ca1b6e54 RB |
770 | static int _cp_r_callback(void *ref, git_buf *from) |
771 | { | |
3c42e4ef | 772 | int error = 0; |
ca1b6e54 RB |
773 | cp_r_info *info = ref; |
774 | struct stat from_st, to_st; | |
775 | bool exists = false; | |
776 | ||
777 | if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 && | |
778 | from->ptr[git_path_basename_offset(from)] == '.') | |
779 | return 0; | |
780 | ||
96869a4e RB |
781 | if ((error = git_buf_joinpath( |
782 | &info->to, info->to_root, from->ptr + info->from_prefix)) < 0) | |
25e0b157 | 783 | return error; |
ca1b6e54 | 784 | |
14997dc5 | 785 | if (!(error = git_path_lstat(info->to.ptr, &to_st))) |
ca1b6e54 | 786 | exists = true; |
14997dc5 | 787 | else if (error != GIT_ENOTFOUND) |
25e0b157 | 788 | return error; |
14997dc5 RB |
789 | else { |
790 | giterr_clear(); | |
791 | error = 0; | |
792 | } | |
ca1b6e54 | 793 | |
3c42e4ef | 794 | if ((error = git_path_lstat(from->ptr, &from_st)) < 0) |
25e0b157 | 795 | return error; |
ca1b6e54 RB |
796 | |
797 | if (S_ISDIR(from_st.st_mode)) { | |
ca1b6e54 RB |
798 | mode_t oldmode = info->dirmode; |
799 | ||
800 | /* if we are not chmod'ing, then overwrite dirmode */ | |
18f08264 | 801 | if ((info->flags & GIT_CPDIR_CHMOD_DIRS) == 0) |
ca1b6e54 RB |
802 | info->dirmode = from_st.st_mode; |
803 | ||
804 | /* make directory now if CREATE_EMPTY_DIRS is requested and needed */ | |
805 | if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0) | |
3c42e4ef | 806 | error = _cp_r_mkdir(info, from); |
ca1b6e54 RB |
807 | |
808 | /* recurse onto target directory */ | |
25e0b157 | 809 | if (!error && (!exists || S_ISDIR(to_st.st_mode))) |
219d3457 | 810 | error = git_path_direach(from, 0, _cp_r_callback, info); |
ca1b6e54 RB |
811 | |
812 | if (oldmode != 0) | |
813 | info->dirmode = oldmode; | |
814 | ||
25e0b157 | 815 | return error; |
ca1b6e54 RB |
816 | } |
817 | ||
818 | if (exists) { | |
819 | if ((info->flags & GIT_CPDIR_OVERWRITE) == 0) | |
820 | return 0; | |
821 | ||
822 | if (p_unlink(info->to.ptr) < 0) { | |
823 | giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'", | |
824 | info->to.ptr); | |
25e0b157 | 825 | return GIT_EEXISTS; |
ca1b6e54 RB |
826 | } |
827 | } | |
828 | ||
829 | /* Done if this isn't a regular file or a symlink */ | |
830 | if (!S_ISREG(from_st.st_mode) && | |
831 | (!S_ISLNK(from_st.st_mode) || | |
832 | (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0)) | |
833 | return 0; | |
834 | ||
835 | /* Make container directory on demand if needed */ | |
836 | if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 && | |
3c42e4ef | 837 | (error = _cp_r_mkdir(info, from)) < 0) |
25e0b157 | 838 | return error; |
ca1b6e54 RB |
839 | |
840 | /* make symlink or regular file */ | |
94f742ba CMN |
841 | if (info->flags & GIT_CPDIR_LINK_FILES) { |
842 | error = p_link(from->ptr, info->to.ptr); | |
843 | } else if (S_ISLNK(from_st.st_mode)) { | |
3c42e4ef | 844 | error = cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size); |
94f742ba | 845 | } else { |
3c42e4ef RB |
846 | mode_t usemode = from_st.st_mode; |
847 | ||
18f08264 | 848 | if ((info->flags & GIT_CPDIR_SIMPLE_TO_MODE) != 0) |
f240acce | 849 | usemode = GIT_PERMS_FOR_WRITE(usemode); |
3c42e4ef RB |
850 | |
851 | error = git_futils_cp(from->ptr, info->to.ptr, usemode); | |
3c42e4ef RB |
852 | } |
853 | ||
25e0b157 | 854 | return error; |
ca1b6e54 RB |
855 | } |
856 | ||
857 | int git_futils_cp_r( | |
858 | const char *from, | |
859 | const char *to, | |
860 | uint32_t flags, | |
861 | mode_t dirmode) | |
862 | { | |
863 | int error; | |
864 | git_buf path = GIT_BUF_INIT; | |
865 | cp_r_info info; | |
866 | ||
3c42e4ef | 867 | if (git_buf_joinpath(&path, from, "") < 0) /* ensure trailing slash */ |
ca1b6e54 RB |
868 | return -1; |
869 | ||
96869a4e | 870 | memset(&info, 0, sizeof(info)); |
ca1b6e54 RB |
871 | info.to_root = to; |
872 | info.flags = flags; | |
873 | info.dirmode = dirmode; | |
874 | info.from_prefix = path.size; | |
875 | git_buf_init(&info.to, 0); | |
876 | ||
877 | /* precalculate mkdir flags */ | |
878 | if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) { | |
3c42e4ef RB |
879 | /* if not creating empty dirs, then use mkdir to create the path on |
880 | * demand right before files are copied. | |
881 | */ | |
ca1b6e54 | 882 | info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST; |
18f08264 | 883 | if ((flags & GIT_CPDIR_CHMOD_DIRS) != 0) |
ca1b6e54 RB |
884 | info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH; |
885 | } else { | |
3c42e4ef | 886 | /* otherwise, we will do simple mkdir as directories are encountered */ |
ca1b6e54 | 887 | info.mkdir_flags = |
18f08264 | 888 | ((flags & GIT_CPDIR_CHMOD_DIRS) != 0) ? GIT_MKDIR_CHMOD : 0; |
ca1b6e54 RB |
889 | } |
890 | ||
891 | error = _cp_r_callback(&info, &path); | |
892 | ||
893 | git_buf_free(&path); | |
07c06f7a | 894 | git_buf_free(&info.to); |
ca1b6e54 RB |
895 | |
896 | return error; | |
897 | } | |
744cc03e | 898 | |
c1f61af6 VM |
899 | int git_futils_filestamp_check( |
900 | git_futils_filestamp *stamp, const char *path) | |
744cc03e RB |
901 | { |
902 | struct stat st; | |
903 | ||
c8b511f3 RB |
904 | /* if the stamp is NULL, then always reload */ |
905 | if (stamp == NULL) | |
744cc03e RB |
906 | return 1; |
907 | ||
7d490872 | 908 | if (p_stat(path, &st) < 0) |
744cc03e RB |
909 | return GIT_ENOTFOUND; |
910 | ||
c8b511f3 RB |
911 | if (stamp->mtime == (git_time_t)st.st_mtime && |
912 | stamp->size == (git_off_t)st.st_size && | |
913 | stamp->ino == (unsigned int)st.st_ino) | |
744cc03e RB |
914 | return 0; |
915 | ||
c8b511f3 RB |
916 | stamp->mtime = (git_time_t)st.st_mtime; |
917 | stamp->size = (git_off_t)st.st_size; | |
918 | stamp->ino = (unsigned int)st.st_ino; | |
744cc03e RB |
919 | |
920 | return 1; | |
921 | } | |
922 | ||
c1f61af6 VM |
923 | void git_futils_filestamp_set( |
924 | git_futils_filestamp *target, const git_futils_filestamp *source) | |
c8b511f3 RB |
925 | { |
926 | assert(target); | |
927 | ||
928 | if (source) | |
929 | memcpy(target, source, sizeof(*target)); | |
930 | else | |
931 | memset(target, 0, sizeof(*target)); | |
932 | } | |
823c0e9c RB |
933 | |
934 | ||
935 | void git_futils_filestamp_set_from_stat( | |
936 | git_futils_filestamp *stamp, struct stat *st) | |
937 | { | |
938 | if (st) { | |
939 | stamp->mtime = (git_time_t)st->st_mtime; | |
940 | stamp->size = (git_off_t)st->st_size; | |
941 | stamp->ino = (unsigned int)st->st_ino; | |
942 | } else { | |
943 | memset(stamp, 0, sizeof(*stamp)); | |
944 | } | |
945 | } |