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