| 1 |
|
| /* |
| 2 |
|
| * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 |
|
| * |
| 4 |
|
| * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 |
|
| * a Linking Exception. For full terms see the included COPYING file. |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #include "filebuf.h" |
| 9 |
|
| |
| 10 |
|
| #include "futils.h" |
| 11 |
|
| |
| 12 |
|
| static const size_t WRITE_BUFFER_SIZE = (4096 * 2); |
| 13 |
|
| |
| 14 |
|
| enum buferr_t { |
| 15 |
|
| BUFERR_OK = 0, |
| 16 |
|
| BUFERR_WRITE, |
| 17 |
|
| BUFERR_ZLIB, |
| 18 |
|
| BUFERR_MEM |
| 19 |
|
| }; |
| 20 |
|
| |
| 21 |
|
| #define ENSURE_BUF_OK(buf) if ((buf)->last_error != BUFERR_OK) { return -1; } |
| 22 |
|
| |
| 23 |
560 |
| static int verify_last_error(git_filebuf *file) |
| 24 |
|
| { |
| 25 |
560 |
| switch (file->last_error) { |
| 26 |
0 |
| case BUFERR_WRITE: |
| 27 |
0 |
| git_error_set(GIT_ERROR_OS, "failed to write out file"); |
| 28 |
0 |
| return -1; |
| 29 |
|
| |
| 30 |
0 |
| case BUFERR_MEM: |
| 31 |
0 |
| git_error_set_oom(); |
| 32 |
0 |
| return -1; |
| 33 |
|
| |
| 34 |
0 |
| case BUFERR_ZLIB: |
| 35 |
0 |
| git_error_set(GIT_ERROR_ZLIB, |
| 36 |
|
| "Buffer error when writing out ZLib data"); |
| 37 |
0 |
| return -1; |
| 38 |
|
| |
| 39 |
560 |
| default: |
| 40 |
560 |
| return 0; |
| 41 |
|
| } |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
317 |
| static int lock_file(git_filebuf *file, int flags, mode_t mode) |
| 45 |
|
| { |
| 46 |
317 |
▶ | if (git_fs_path_exists(file->path_lock) == true) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_fs_path_exists(file->path_lock) == true) { |
0 |
317 |
317 |
|
| 47 |
0 |
| git_error_clear(); /* actual OS error code just confuses */ |
| 48 |
0 |
| git_error_set(GIT_ERROR_OS, |
| 49 |
|
| "failed to lock file '%s' for writing", file->path_lock); |
| 50 |
0 |
| return GIT_ELOCKED; |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
|
| /* create path to the file buffer is required */ |
| 54 |
317 |
▶ | if (flags & GIT_FILEBUF_CREATE_LEADING_DIRS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags & GIT_FILEBUF_CREATE_LEADING_DIRS) { |
144 |
173 |
317 |
|
| 55 |
|
| /* XXX: Should dirmode here be configurable? Or is 0777 always fine? */ |
| 56 |
144 |
| file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, mode); |
| 57 |
|
| } else { |
| 58 |
173 |
| file->fd = git_futils_creat_locked(file->path_lock, mode); |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
317 |
▶ | if (file->fd < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->fd < 0) |
0 |
317 |
317 |
|
| 62 |
0 |
| return file->fd; |
| 63 |
|
| |
| 64 |
317 |
| file->fd_is_open = true; |
| 65 |
|
| |
| 66 |
317 |
▶ | if ((flags & GIT_FILEBUF_APPEND) && git_fs_path_exists(file->path_original) == true) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((flags & GIT_FILEBUF_APPEND) && git_fs_path_exists(file->path_original) == true) { |
3 |
314 |
317 |
| if ((flags & GIT_FILEBUF_APPEND) && git_fs_path_exists(file->path_original) == true) { |
3 |
0 |
3 |
|
| 67 |
|
| git_file source; |
| 68 |
|
| char buffer[GIT_BUFSIZE_FILEIO]; |
| 69 |
|
| ssize_t read_bytes; |
| 70 |
3 |
| int error = 0; |
| 71 |
|
| |
| 72 |
3 |
| source = p_open(file->path_original, O_RDONLY); |
| 73 |
3 |
▶ | if (source < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (source < 0) { |
0 |
3 |
3 |
|
| 74 |
0 |
| git_error_set(GIT_ERROR_OS, |
| 75 |
|
| "failed to open file '%s' for reading", |
| 76 |
|
| file->path_original); |
| 77 |
0 |
| return -1; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
6 |
▶ | while ((read_bytes = p_read(source, buffer, sizeof(buffer))) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((read_bytes = p_read(source, buffer, sizeof(buffer))) > 0) { |
3 |
3 |
6 |
|
| 81 |
3 |
▶ | if ((error = p_write(file->fd, buffer, read_bytes)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = p_write(file->fd, buffer, read_bytes)) < 0) |
0 |
3 |
3 |
|
| 82 |
0 |
| break; |
| 83 |
3 |
▶ | if (file->compute_digest) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->compute_digest) |
0 |
3 |
3 |
|
| 84 |
0 |
| git_hash_update(&file->digest, buffer, read_bytes); |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
3 |
| p_close(source); |
| 88 |
|
| |
| 89 |
3 |
▶ | if (read_bytes < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (read_bytes < 0) { |
0 |
3 |
3 |
|
| 90 |
0 |
| git_error_set(GIT_ERROR_OS, "failed to read file '%s'", file->path_original); |
| 91 |
0 |
| return -1; |
| 92 |
3 |
▶ | } else if (error < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (error < 0) { |
0 |
3 |
3 |
|
| 93 |
0 |
| git_error_set(GIT_ERROR_OS, "failed to write file '%s'", file->path_lock); |
| 94 |
0 |
| return -1; |
| 95 |
|
| } |
| 96 |
|
| } |
| 97 |
|
| |
| 98 |
317 |
| return 0; |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
691 |
| void git_filebuf_cleanup(git_filebuf *file) |
| 102 |
|
| { |
| 103 |
691 |
▶ | if (file->fd_is_open && file->fd >= 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (file->fd_is_open && file->fd >= 0) |
36 |
655 |
691 |
| if (file->fd_is_open && file->fd >= 0) |
36 |
0 |
36 |
|
| 104 |
36 |
| p_close(file->fd); |
| 105 |
|
| |
| 106 |
691 |
▶ | if (file->created_lock && !file->did_rename && file->path_lock && git_fs_path_exists(file->path_lock)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (file->created_lock && !file->did_rename && file->path_lock && git_fs_path_exists(file->path_lock)) |
494 |
197 |
691 |
| if (file->created_lock && !file->did_rename && file->path_lock && git_fs_path_exists(file->path_lock)) |
36 |
458 |
494 |
| if (file->created_lock && !file->did_rename && file->path_lock && git_fs_path_exists(file->path_lock)) |
36 |
0 |
36 |
| if (file->created_lock && !file->did_rename && file->path_lock && git_fs_path_exists(file->path_lock)) |
36 |
0 |
36 |
|
| 107 |
36 |
| p_unlink(file->path_lock); |
| 108 |
|
| |
| 109 |
691 |
▶ | if (file->compute_digest) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->compute_digest) { |
51 |
640 |
691 |
|
| 110 |
51 |
| git_hash_ctx_cleanup(&file->digest); |
| 111 |
51 |
| file->compute_digest = 0; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
691 |
▶ | if (file->buffer) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->buffer) |
491 |
200 |
691 |
|
| 115 |
491 |
| git__free(file->buffer); |
| 116 |
|
| |
| 117 |
|
| /* use the presence of z_buf to decide if we need to deflateEnd */ |
| 118 |
691 |
▶ | if (file->z_buf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->z_buf) { |
177 |
514 |
691 |
|
| 119 |
177 |
| git__free(file->z_buf); |
| 120 |
177 |
| deflateEnd(&file->zs); |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
691 |
▶ | if (file->path_original) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->path_original) |
474 |
217 |
691 |
|
| 124 |
474 |
| git__free(file->path_original); |
| 125 |
691 |
▶ | if (file->path_lock) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->path_lock) |
494 |
197 |
691 |
|
| 126 |
494 |
| git__free(file->path_lock); |
| 127 |
|
| |
| 128 |
691 |
| memset(file, 0x0, sizeof(git_filebuf)); |
| 129 |
691 |
| file->fd = -1; |
| 130 |
691 |
| } |
| 131 |
|
| |
| 132 |
560 |
| GIT_INLINE(int) flush_buffer(git_filebuf *file) |
| 133 |
|
| { |
| 134 |
560 |
| int result = file->write(file, file->buffer, file->buf_pos); |
| 135 |
560 |
| file->buf_pos = 0; |
| 136 |
560 |
| return result; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
0 |
| int git_filebuf_flush(git_filebuf *file) |
| 140 |
|
| { |
| 141 |
0 |
| return flush_buffer(file); |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
406 |
| static int write_normal(git_filebuf *file, void *source, size_t len) |
| 145 |
|
| { |
| 146 |
406 |
▶ | if (len > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > 0) { |
402 |
4 |
406 |
|
| 147 |
402 |
▶ | if (p_write(file->fd, (void *)source, len) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p_write(file->fd, (void *)source, len) < 0) { |
0 |
402 |
402 |
|
| 148 |
0 |
| file->last_error = BUFERR_WRITE; |
| 149 |
0 |
| return -1; |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
402 |
▶ | if (file->compute_digest) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->compute_digest) |
152 |
250 |
402 |
|
| 153 |
152 |
| git_hash_update(&file->digest, source, len); |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
406 |
| return 0; |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
157 |
| static int write_deflate(git_filebuf *file, void *source, size_t len) |
| 160 |
|
| { |
| 161 |
157 |
| z_stream *zs = &file->zs; |
| 162 |
|
| |
| 163 |
157 |
▶ | if (len > 0 || file->flush_mode == Z_FINISH) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (len > 0 || file->flush_mode == Z_FINISH) { |
0 |
157 |
157 |
| if (len > 0 || file->flush_mode == Z_FINISH) { |
0 |
0 |
0 |
|
| 164 |
157 |
| zs->next_in = source; |
| 165 |
157 |
| zs->avail_in = (uInt)len; |
| 166 |
|
| |
| 167 |
|
| do { |
| 168 |
|
| size_t have; |
| 169 |
|
| |
| 170 |
157 |
| zs->next_out = file->z_buf; |
| 171 |
157 |
| zs->avail_out = (uInt)file->buf_size; |
| 172 |
|
| |
| 173 |
157 |
▶ | if (deflate(zs, file->flush_mode) == Z_STREAM_ERROR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (deflate(zs, file->flush_mode) == Z_STREAM_ERROR) { |
0 |
157 |
157 |
|
| 174 |
0 |
| file->last_error = BUFERR_ZLIB; |
| 175 |
0 |
| return -1; |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
157 |
| have = file->buf_size - (size_t)zs->avail_out; |
| 179 |
|
| |
| 180 |
157 |
▶ | if (p_write(file->fd, file->z_buf, have) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p_write(file->fd, file->z_buf, have) < 0) { |
0 |
157 |
157 |
|
| 181 |
0 |
| file->last_error = BUFERR_WRITE; |
| 182 |
0 |
| return -1; |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
157 |
▶ | } while (zs->avail_out == 0); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (zs->avail_out == 0); |
0 |
157 |
157 |
|
| 186 |
|
| |
| 187 |
157 |
▶ | GIT_ASSERT(zs->avail_in == 0); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT(zs->avail_in == 0); |
0 |
157 |
157 |
|
| 188 |
|
| |
| 189 |
157 |
▶ | if (file->compute_digest) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->compute_digest) |
0 |
157 |
157 |
|
| 190 |
0 |
| git_hash_update(&file->digest, source, len); |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
157 |
| return 0; |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
|
| #define MAX_SYMLINK_DEPTH 5 |
| 197 |
|
| |
| 198 |
317 |
| static int resolve_symlink(git_str *out, const char *path) |
| 199 |
|
| { |
| 200 |
|
| int i, error, root; |
| 201 |
|
| ssize_t ret; |
| 202 |
|
| struct stat st; |
| 203 |
317 |
| git_str curpath = GIT_STR_INIT, target = GIT_STR_INIT; |
| 204 |
|
| |
| 205 |
634 |
| if ((error = git_str_grow(&target, GIT_PATH_MAX + 1)) < 0 || |
| 206 |
317 |
| (error = git_str_puts(&curpath, path)) < 0) |
| 207 |
0 |
| return error; |
| 208 |
|
| |
| 209 |
317 |
▶ | for (i = 0; i < MAX_SYMLINK_DEPTH; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < MAX_SYMLINK_DEPTH; i++) { |
317 |
0 |
317 |
|
| 210 |
317 |
| error = p_lstat(curpath.ptr, &st); |
| 211 |
317 |
▶ | if (error < 0 && errno == ENOENT) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (error < 0 && errno == ENOENT) { |
76 |
241 |
317 |
| if (error < 0 && errno == ENOENT) { |
76 |
0 |
76 |
|
| 212 |
76 |
| error = git_str_puts(out, curpath.ptr); |
| 213 |
76 |
| goto cleanup; |
| 214 |
|
| } |
| 215 |
|
| |
| 216 |
241 |
▶ | if (error < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (error < 0) { |
0 |
241 |
241 |
|
| 217 |
0 |
| git_error_set(GIT_ERROR_OS, "failed to stat '%s'", curpath.ptr); |
| 218 |
0 |
| error = -1; |
| 219 |
0 |
| goto cleanup; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
241 |
▶ | if (!S_ISLNK(st.st_mode)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!S_ISLNK(st.st_mode)) { |
241 |
0 |
241 |
|
| 223 |
241 |
| error = git_str_puts(out, curpath.ptr); |
| 224 |
241 |
| goto cleanup; |
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
0 |
| ret = p_readlink(curpath.ptr, target.ptr, GIT_PATH_MAX); |
| 228 |
0 |
▶ | if (ret < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret < 0) { |
0 |
0 |
0 |
|
| 229 |
0 |
| git_error_set(GIT_ERROR_OS, "failed to read symlink '%s'", curpath.ptr); |
| 230 |
0 |
| error = -1; |
| 231 |
0 |
| goto cleanup; |
| 232 |
|
| } |
| 233 |
|
| |
| 234 |
0 |
▶ | if (ret == GIT_PATH_MAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == GIT_PATH_MAX) { |
0 |
0 |
0 |
|
| 235 |
0 |
| git_error_set(GIT_ERROR_INVALID, "symlink target too long"); |
| 236 |
0 |
| error = -1; |
| 237 |
0 |
| goto cleanup; |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
|
| /* readlink(2) won't NUL-terminate for us */ |
| 241 |
0 |
| target.ptr[ret] = '\0'; |
| 242 |
0 |
| target.size = ret; |
| 243 |
|
| |
| 244 |
0 |
| root = git_fs_path_root(target.ptr); |
| 245 |
0 |
▶ | if (root >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (root >= 0) { |
0 |
0 |
0 |
|
| 246 |
0 |
▶ | if ((error = git_str_sets(&curpath, target.ptr)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_str_sets(&curpath, target.ptr)) < 0) |
0 |
0 |
0 |
|
| 247 |
0 |
| goto cleanup; |
| 248 |
|
| } else { |
| 249 |
0 |
| git_str dir = GIT_STR_INIT; |
| 250 |
|
| |
| 251 |
0 |
▶ | if ((error = git_fs_path_dirname_r(&dir, curpath.ptr)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_fs_path_dirname_r(&dir, curpath.ptr)) < 0) |
0 |
0 |
0 |
|
| 252 |
0 |
| goto cleanup; |
| 253 |
|
| |
| 254 |
0 |
| git_str_swap(&curpath, &dir); |
| 255 |
0 |
| git_str_dispose(&dir); |
| 256 |
|
| |
| 257 |
0 |
▶ | if ((error = git_fs_path_apply_relative(&curpath, target.ptr)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_fs_path_apply_relative(&curpath, target.ptr)) < 0) |
0 |
0 |
0 |
|
| 258 |
0 |
| goto cleanup; |
| 259 |
|
| } |
| 260 |
|
| } |
| 261 |
|
| |
| 262 |
0 |
| git_error_set(GIT_ERROR_INVALID, "maximum symlink depth reached"); |
| 263 |
0 |
| error = -1; |
| 264 |
|
| |
| 265 |
317 |
| cleanup: |
| 266 |
317 |
| git_str_dispose(&curpath); |
| 267 |
317 |
| git_str_dispose(&target); |
| 268 |
317 |
| return error; |
| 269 |
|
| } |
| 270 |
|
| |
| 271 |
494 |
| int git_filebuf_open(git_filebuf *file, const char *path, int flags, mode_t mode) |
| 272 |
|
| { |
| 273 |
494 |
| return git_filebuf_open_withsize(file, path, flags, mode, WRITE_BUFFER_SIZE); |
| 274 |
|
| } |
| 275 |
|
| |
| 276 |
494 |
| int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mode_t mode, size_t size) |
| 277 |
|
| { |
| 278 |
494 |
| int compression, error = -1; |
| 279 |
|
| size_t path_len, alloc_len; |
| 280 |
|
| |
| 281 |
494 |
▶ | GIT_ASSERT_ARG(file); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(file); |
0 |
494 |
494 |
|
| 282 |
494 |
▶ | GIT_ASSERT_ARG(path); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(path); |
0 |
494 |
494 |
|
| 283 |
494 |
▶ | GIT_ASSERT(file->buffer == NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT(file->buffer == NULL); |
0 |
494 |
494 |
|
| 284 |
|
| |
| 285 |
494 |
| memset(file, 0x0, sizeof(git_filebuf)); |
| 286 |
|
| |
| 287 |
494 |
▶ | if (flags & GIT_FILEBUF_DO_NOT_BUFFER) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags & GIT_FILEBUF_DO_NOT_BUFFER) |
3 |
491 |
494 |
|
| 288 |
3 |
| file->do_not_buffer = true; |
| 289 |
|
| |
| 290 |
494 |
▶ | if (flags & GIT_FILEBUF_FSYNC) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags & GIT_FILEBUF_FSYNC) |
0 |
494 |
494 |
|
| 291 |
0 |
| file->do_fsync = true; |
| 292 |
|
| |
| 293 |
494 |
| file->buf_size = size; |
| 294 |
494 |
| file->buf_pos = 0; |
| 295 |
494 |
| file->fd = -1; |
| 296 |
494 |
| file->last_error = BUFERR_OK; |
| 297 |
|
| |
| 298 |
|
| /* Allocate the main cache buffer */ |
| 299 |
494 |
▶ | if (!file->do_not_buffer) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!file->do_not_buffer) { |
491 |
3 |
494 |
|
| 300 |
491 |
| file->buffer = git__malloc(file->buf_size); |
| 301 |
491 |
▶ | GIT_ERROR_CHECK_ALLOC(file->buffer); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(file->buffer); |
0 |
491 |
491 |
|
| 302 |
|
| } |
| 303 |
|
| |
| 304 |
|
| /* If we are hashing on-write, allocate a new hash context */ |
| 305 |
494 |
▶ | if (flags & GIT_FILEBUF_HASH_CONTENTS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags & GIT_FILEBUF_HASH_CONTENTS) { |
153 |
341 |
494 |
|
| 306 |
153 |
| file->compute_digest = 1; |
| 307 |
|
| |
| 308 |
153 |
▶ | if (git_hash_ctx_init(&file->digest, GIT_HASH_ALGORITHM_SHA1) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_hash_ctx_init(&file->digest, GIT_HASH_ALGORITHM_SHA1) < 0) |
0 |
153 |
153 |
|
| 309 |
0 |
| goto cleanup; |
| 310 |
|
| } |
| 311 |
|
| |
| 312 |
494 |
| compression = flags >> GIT_FILEBUF_DEFLATE_SHIFT; |
| 313 |
|
| |
| 314 |
|
| /* If we are deflating on-write, */ |
| 315 |
494 |
▶ | if (compression != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (compression != 0) { |
177 |
317 |
494 |
|
| 316 |
|
| /* Initialize the ZLib stream */ |
| 317 |
177 |
▶ | if (deflateInit(&file->zs, compression) != Z_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (deflateInit(&file->zs, compression) != Z_OK) { |
0 |
177 |
177 |
|
| 318 |
0 |
| git_error_set(GIT_ERROR_ZLIB, "failed to initialize zlib"); |
| 319 |
0 |
| goto cleanup; |
| 320 |
|
| } |
| 321 |
|
| |
| 322 |
|
| /* Allocate the Zlib cache buffer */ |
| 323 |
177 |
| file->z_buf = git__malloc(file->buf_size); |
| 324 |
177 |
▶ | GIT_ERROR_CHECK_ALLOC(file->z_buf); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(file->z_buf); |
0 |
177 |
177 |
|
| 325 |
|
| |
| 326 |
|
| /* Never flush */ |
| 327 |
177 |
| file->flush_mode = Z_NO_FLUSH; |
| 328 |
177 |
| file->write = &write_deflate; |
| 329 |
|
| } else { |
| 330 |
317 |
| file->write = &write_normal; |
| 331 |
|
| } |
| 332 |
|
| |
| 333 |
|
| /* If we are writing to a temp file */ |
| 334 |
494 |
▶ | if (flags & GIT_FILEBUF_TEMPORARY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags & GIT_FILEBUF_TEMPORARY) { |
177 |
317 |
494 |
|
| 335 |
177 |
| git_str tmp_path = GIT_STR_INIT; |
| 336 |
|
| |
| 337 |
|
| /* Open the file as temporary for locking */ |
| 338 |
177 |
| file->fd = git_futils_mktmp(&tmp_path, path, mode); |
| 339 |
|
| |
| 340 |
177 |
▶ | if (file->fd < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->fd < 0) { |
0 |
177 |
177 |
|
| 341 |
0 |
| git_str_dispose(&tmp_path); |
| 342 |
0 |
| goto cleanup; |
| 343 |
|
| } |
| 344 |
177 |
| file->fd_is_open = true; |
| 345 |
177 |
| file->created_lock = true; |
| 346 |
|
| |
| 347 |
|
| /* No original path */ |
| 348 |
177 |
| file->path_original = NULL; |
| 349 |
177 |
| file->path_lock = git_str_detach(&tmp_path); |
| 350 |
177 |
▶ | GIT_ERROR_CHECK_ALLOC(file->path_lock); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(file->path_lock); |
0 |
177 |
177 |
|
| 351 |
|
| } else { |
| 352 |
317 |
| git_str resolved_path = GIT_STR_INIT; |
| 353 |
|
| |
| 354 |
317 |
▶ | if ((error = resolve_symlink(&resolved_path, path)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = resolve_symlink(&resolved_path, path)) < 0) |
0 |
317 |
317 |
|
| 355 |
0 |
| goto cleanup; |
| 356 |
|
| |
| 357 |
|
| /* Save the original path of the file */ |
| 358 |
317 |
| path_len = resolved_path.size; |
| 359 |
317 |
| file->path_original = git_str_detach(&resolved_path); |
| 360 |
|
| |
| 361 |
|
| /* create the locking path by appending ".lock" to the original */ |
| 362 |
317 |
▶ | GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, path_len, GIT_FILELOCK_EXTLENGTH); |
Branch: 2 branches
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, path_len, GIT_FILELOCK_EXTLENGTH); |
0 |
317 |
317 |
| GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, path_len, GIT_FILELOCK_EXTLENGTH); |
0 |
317 |
317 |
|
| 363 |
317 |
| file->path_lock = git__malloc(alloc_len); |
| 364 |
317 |
▶ | GIT_ERROR_CHECK_ALLOC(file->path_lock); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(file->path_lock); |
0 |
317 |
317 |
|
| 365 |
|
| |
| 366 |
317 |
| memcpy(file->path_lock, file->path_original, path_len); |
| 367 |
317 |
| memcpy(file->path_lock + path_len, GIT_FILELOCK_EXTENSION, GIT_FILELOCK_EXTLENGTH); |
| 368 |
|
| |
| 369 |
317 |
▶ | if (git_fs_path_isdir(file->path_original)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_fs_path_isdir(file->path_original)) { |
0 |
317 |
317 |
|
| 370 |
0 |
| git_error_set(GIT_ERROR_FILESYSTEM, "path '%s' is a directory", file->path_original); |
| 371 |
0 |
| error = GIT_EDIRECTORY; |
| 372 |
0 |
| goto cleanup; |
| 373 |
|
| } |
| 374 |
|
| |
| 375 |
|
| /* open the file for locking */ |
| 376 |
317 |
▶ | if ((error = lock_file(file, flags, mode)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = lock_file(file, flags, mode)) < 0) |
0 |
317 |
317 |
|
| 377 |
0 |
| goto cleanup; |
| 378 |
|
| |
| 379 |
317 |
| file->created_lock = true; |
| 380 |
|
| } |
| 381 |
|
| |
| 382 |
494 |
| return 0; |
| 383 |
|
| |
| 384 |
0 |
| cleanup: |
| 385 |
0 |
| git_filebuf_cleanup(file); |
| 386 |
0 |
| return error; |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
102 |
| int git_filebuf_hash(unsigned char *out, git_filebuf *file) |
| 390 |
|
| { |
| 391 |
102 |
▶ | GIT_ASSERT_ARG(out); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(out); |
0 |
102 |
102 |
|
| 392 |
102 |
▶ | GIT_ASSERT_ARG(file); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(file); |
0 |
102 |
102 |
|
| 393 |
102 |
▶ | GIT_ASSERT_ARG(file->compute_digest); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(file->compute_digest); |
0 |
102 |
102 |
|
| 394 |
|
| |
| 395 |
102 |
| flush_buffer(file); |
| 396 |
|
| |
| 397 |
102 |
▶ | if (verify_last_error(file) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (verify_last_error(file) < 0) |
0 |
102 |
102 |
|
| 398 |
0 |
| return -1; |
| 399 |
|
| |
| 400 |
102 |
| git_hash_final(out, &file->digest); |
| 401 |
102 |
| git_hash_ctx_cleanup(&file->digest); |
| 402 |
102 |
| file->compute_digest = 0; |
| 403 |
|
| |
| 404 |
102 |
| return 0; |
| 405 |
|
| } |
| 406 |
|
| |
| 407 |
162 |
| int git_filebuf_commit_at(git_filebuf *file, const char *path) |
| 408 |
|
| { |
| 409 |
162 |
| git__free(file->path_original); |
| 410 |
162 |
| file->path_original = git__strdup(path); |
| 411 |
162 |
▶ | GIT_ERROR_CHECK_ALLOC(file->path_original); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(file->path_original); |
0 |
162 |
162 |
|
| 412 |
|
| |
| 413 |
162 |
| return git_filebuf_commit(file); |
| 414 |
|
| } |
| 415 |
|
| |
| 416 |
458 |
| int git_filebuf_commit(git_filebuf *file) |
| 417 |
|
| { |
| 418 |
|
| /* temporary files cannot be committed */ |
| 419 |
458 |
▶ | GIT_ASSERT_ARG(file); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT_ARG(file); |
0 |
458 |
458 |
|
| 420 |
458 |
▶ | GIT_ASSERT(file->path_original); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ASSERT(file->path_original); |
0 |
458 |
458 |
|
| 421 |
|
| |
| 422 |
458 |
| file->flush_mode = Z_FINISH; |
| 423 |
458 |
| flush_buffer(file); |
| 424 |
|
| |
| 425 |
458 |
▶ | if (verify_last_error(file) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (verify_last_error(file) < 0) |
0 |
458 |
458 |
|
| 426 |
0 |
| goto on_error; |
| 427 |
|
| |
| 428 |
458 |
| file->fd_is_open = false; |
| 429 |
|
| |
| 430 |
458 |
▶ | if (file->do_fsync && p_fsync(file->fd) < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (file->do_fsync && p_fsync(file->fd) < 0) { |
0 |
458 |
458 |
| if (file->do_fsync && p_fsync(file->fd) < 0) { |
0 |
0 |
0 |
|
| 431 |
0 |
| git_error_set(GIT_ERROR_OS, "failed to fsync '%s'", file->path_lock); |
| 432 |
0 |
| goto on_error; |
| 433 |
|
| } |
| 434 |
|
| |
| 435 |
458 |
▶ | if (p_close(file->fd) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p_close(file->fd) < 0) { |
0 |
458 |
458 |
|
| 436 |
0 |
| git_error_set(GIT_ERROR_OS, "failed to close file at '%s'", file->path_lock); |
| 437 |
0 |
| goto on_error; |
| 438 |
|
| } |
| 439 |
|
| |
| 440 |
458 |
| file->fd = -1; |
| 441 |
|
| |
| 442 |
458 |
▶ | if (p_rename(file->path_lock, file->path_original) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p_rename(file->path_lock, file->path_original) < 0) { |
0 |
458 |
458 |
|
| 443 |
0 |
| git_error_set(GIT_ERROR_OS, "failed to rename lockfile to '%s'", file->path_original); |
| 444 |
0 |
| goto on_error; |
| 445 |
|
| } |
| 446 |
|
| |
| 447 |
458 |
▶ | if (file->do_fsync && git_futils_fsync_parent(file->path_original) < 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (file->do_fsync && git_futils_fsync_parent(file->path_original) < 0) |
0 |
458 |
458 |
| if (file->do_fsync && git_futils_fsync_parent(file->path_original) < 0) |
0 |
0 |
0 |
|
| 448 |
0 |
| goto on_error; |
| 449 |
|
| |
| 450 |
458 |
| file->did_rename = true; |
| 451 |
|
| |
| 452 |
458 |
| git_filebuf_cleanup(file); |
| 453 |
458 |
| return 0; |
| 454 |
|
| |
| 455 |
0 |
| on_error: |
| 456 |
0 |
| git_filebuf_cleanup(file); |
| 457 |
0 |
| return -1; |
| 458 |
|
| } |
| 459 |
|
| |
| 460 |
2241 |
| GIT_INLINE(void) add_to_cache(git_filebuf *file, const void *buf, size_t len) |
| 461 |
|
| { |
| 462 |
2241 |
| memcpy(file->buffer + file->buf_pos, buf, len); |
| 463 |
2241 |
| file->buf_pos += len; |
| 464 |
2241 |
| } |
| 465 |
|
| |
| 466 |
2244 |
| int git_filebuf_write(git_filebuf *file, const void *buff, size_t len) |
| 467 |
|
| { |
| 468 |
2244 |
| const unsigned char *buf = buff; |
| 469 |
|
| |
| 470 |
2244 |
▶ | ENSURE_BUF_OK(file); |
Branch: 1 branch
| Branch | True | False | Total |
| ENSURE_BUF_OK(file); |
0 |
2244 |
2244 |
|
| 471 |
|
| |
| 472 |
2244 |
▶ | if (file->do_not_buffer) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->do_not_buffer) |
2241 |
3 |
2244 |
|
| 473 |
3 |
| return file->write(file, (void *)buff, len); |
| 474 |
|
| |
| 475 |
0 |
| for (;;) { |
| 476 |
2241 |
| size_t space_left = file->buf_size - file->buf_pos; |
| 477 |
|
| |
| 478 |
|
| /* cache if it's small */ |
| 479 |
2241 |
▶ | if (space_left > len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (space_left > len) { |
2241 |
0 |
2241 |
|
| 480 |
2241 |
| add_to_cache(file, buf, len); |
| 481 |
2241 |
| return 0; |
| 482 |
|
| } |
| 483 |
|
| |
| 484 |
0 |
| add_to_cache(file, buf, space_left); |
| 485 |
0 |
▶ | if (flush_buffer(file) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flush_buffer(file) < 0) |
0 |
0 |
0 |
|
| 486 |
0 |
| return -1; |
| 487 |
|
| |
| 488 |
0 |
| len -= space_left; |
| 489 |
0 |
| buf += space_left; |
| 490 |
|
| } |
| 491 |
|
| } |
| 492 |
|
| |
| 493 |
206 |
| int git_filebuf_reserve(git_filebuf *file, void **buffer, size_t len) |
| 494 |
|
| { |
| 495 |
206 |
| size_t space_left = file->buf_size - file->buf_pos; |
| 496 |
|
| |
| 497 |
206 |
| *buffer = NULL; |
| 498 |
|
| |
| 499 |
206 |
▶ | ENSURE_BUF_OK(file); |
Branch: 1 branch
| Branch | True | False | Total |
| ENSURE_BUF_OK(file); |
0 |
206 |
206 |
|
| 500 |
|
| |
| 501 |
206 |
▶ | if (len > file->buf_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > file->buf_size) { |
0 |
206 |
206 |
|
| 502 |
0 |
| file->last_error = BUFERR_MEM; |
| 503 |
0 |
| return -1; |
| 504 |
|
| } |
| 505 |
|
| |
| 506 |
206 |
▶ | if (space_left <= len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (space_left <= len) { |
0 |
206 |
206 |
|
| 507 |
0 |
▶ | if (flush_buffer(file) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flush_buffer(file) < 0) |
0 |
0 |
0 |
|
| 508 |
0 |
| return -1; |
| 509 |
|
| } |
| 510 |
|
| |
| 511 |
206 |
| *buffer = (file->buffer + file->buf_pos); |
| 512 |
206 |
| file->buf_pos += len; |
| 513 |
|
| |
| 514 |
206 |
| return 0; |
| 515 |
|
| } |
| 516 |
|
| |
| 517 |
147 |
| int git_filebuf_printf(git_filebuf *file, const char *format, ...) |
| 518 |
|
| { |
| 519 |
|
| va_list arglist; |
| 520 |
|
| size_t space_left, len, alloclen; |
| 521 |
|
| int written, res; |
| 522 |
|
| char *tmp_buffer; |
| 523 |
|
| |
| 524 |
147 |
▶ | ENSURE_BUF_OK(file); |
Branch: 1 branch
| Branch | True | False | Total |
| ENSURE_BUF_OK(file); |
0 |
147 |
147 |
|
| 525 |
|
| |
| 526 |
147 |
| space_left = file->buf_size - file->buf_pos; |
| 527 |
|
| |
| 528 |
|
| do { |
| 529 |
147 |
| va_start(arglist, format); |
| 530 |
147 |
| written = p_vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist); |
| 531 |
147 |
| va_end(arglist); |
| 532 |
|
| |
| 533 |
147 |
▶ | if (written < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (written < 0) { |
0 |
147 |
147 |
|
| 534 |
0 |
| file->last_error = BUFERR_MEM; |
| 535 |
0 |
| return -1; |
| 536 |
|
| } |
| 537 |
|
| |
| 538 |
147 |
| len = written; |
| 539 |
147 |
▶ | if (len + 1 <= space_left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len + 1 <= space_left) { |
147 |
0 |
147 |
|
| 540 |
147 |
| file->buf_pos += len; |
| 541 |
147 |
| return 0; |
| 542 |
|
| } |
| 543 |
|
| |
| 544 |
0 |
▶ | if (flush_buffer(file) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flush_buffer(file) < 0) |
0 |
0 |
0 |
|
| 545 |
0 |
| return -1; |
| 546 |
|
| |
| 547 |
0 |
| space_left = file->buf_size - file->buf_pos; |
| 548 |
|
| |
| 549 |
0 |
▶ | } while (len + 1 <= space_left); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (len + 1 <= space_left); |
0 |
0 |
0 |
|
| 550 |
|
| |
| 551 |
0 |
▶ | if (GIT_ADD_SIZET_OVERFLOW(&alloclen, len, 1) || |
Branch: 2 branches
| Branch | True | False | Total |
| if (GIT_ADD_SIZET_OVERFLOW(&alloclen, len, 1) || |
0 |
0 |
0 |
| if (GIT_ADD_SIZET_OVERFLOW(&alloclen, len, 1) || |
0 |
0 |
0 |
|
| 552 |
0 |
| !(tmp_buffer = git__malloc(alloclen))) { |
| 553 |
0 |
| file->last_error = BUFERR_MEM; |
| 554 |
0 |
| return -1; |
| 555 |
|
| } |
| 556 |
|
| |
| 557 |
0 |
| va_start(arglist, format); |
| 558 |
0 |
| written = p_vsnprintf(tmp_buffer, len + 1, format, arglist); |
| 559 |
0 |
| va_end(arglist); |
| 560 |
|
| |
| 561 |
0 |
▶ | if (written < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (written < 0) { |
0 |
0 |
0 |
|
| 562 |
0 |
| git__free(tmp_buffer); |
| 563 |
0 |
| file->last_error = BUFERR_MEM; |
| 564 |
0 |
| return -1; |
| 565 |
|
| } |
| 566 |
|
| |
| 567 |
0 |
| res = git_filebuf_write(file, tmp_buffer, len); |
| 568 |
0 |
| git__free(tmp_buffer); |
| 569 |
|
| |
| 570 |
0 |
| return res; |
| 571 |
|
| } |
| 572 |
|
| |
| 573 |
0 |
| int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file) |
| 574 |
|
| { |
| 575 |
|
| int res; |
| 576 |
|
| struct stat st; |
| 577 |
|
| |
| 578 |
0 |
▶ | if (file->fd_is_open) |
Branch: 1 branch
| Branch | True | False | Total |
| if (file->fd_is_open) |
0 |
0 |
0 |
|
| 579 |
0 |
| res = p_fstat(file->fd, &st); |
| 580 |
|
| else |
| 581 |
0 |
| res = p_stat(file->path_original, &st); |
| 582 |
|
| |
| 583 |
0 |
▶ | if (res < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (res < 0) { |
0 |
0 |
0 |
|
| 584 |
0 |
| git_error_set(GIT_ERROR_OS, "could not get stat info for '%s'", |
| 585 |
|
| file->path_original); |
| 586 |
0 |
| return res; |
| 587 |
|
| } |
| 588 |
|
| |
| 589 |
0 |
▶ | if (mtime) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mtime) |
0 |
0 |
0 |
|
| 590 |
0 |
| *mtime = st.st_mtime; |
| 591 |
0 |
▶ | if (size) |
Branch: 1 branch
| Branch | True | False | Total |
| if (size) |
0 |
0 |
0 |
|
| 592 |
0 |
| *size = (size_t)st.st_size; |
| 593 |
|
| |
| 594 |
0 |
| return 0; |
| 595 |
|
| } |