line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
559
|
|
|
|
|
|
static int verify_last_error(git_filebuf *file) |
24
|
|
|
|
|
|
|
{ |
25
|
559
|
|
|
|
|
|
switch (file->last_error) { |
26
|
|
|
|
|
|
|
case BUFERR_WRITE: |
27
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to write out file"); |
28
|
0
|
|
|
|
|
|
return -1; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
case BUFERR_MEM: |
31
|
0
|
|
|
|
|
|
git_error_set_oom(); |
32
|
0
|
|
|
|
|
|
return -1; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
default: |
40
|
559
|
|
|
|
|
|
return 0; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
316
|
|
|
|
|
|
static int lock_file(git_filebuf *file, int flags, mode_t mode) |
45
|
|
|
|
|
|
|
{ |
46
|
316
|
50
|
|
|
|
|
if (git_path_exists(file->path_lock) == true) { |
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
|
316
|
100
|
|
|
|
|
if (flags & GIT_FILEBUF_CREATE_LEADING_DIRS) { |
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
|
172
|
|
|
|
|
|
file->fd = git_futils_creat_locked(file->path_lock, mode); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
316
|
50
|
|
|
|
|
if (file->fd < 0) |
62
|
0
|
|
|
|
|
|
return file->fd; |
63
|
|
|
|
|
|
|
|
64
|
316
|
|
|
|
|
|
file->fd_is_open = true; |
65
|
|
|
|
|
|
|
|
66
|
316
|
100
|
|
|
|
|
if ((flags & GIT_FILEBUF_APPEND) && git_path_exists(file->path_original) == true) { |
|
|
50
|
|
|
|
|
|
67
|
|
|
|
|
|
|
git_file source; |
68
|
|
|
|
|
|
|
char buffer[FILEIO_BUFSIZE]; |
69
|
|
|
|
|
|
|
ssize_t read_bytes; |
70
|
3
|
|
|
|
|
|
int error = 0; |
71
|
|
|
|
|
|
|
|
72
|
3
|
|
|
|
|
|
source = p_open(file->path_original, O_RDONLY); |
73
|
3
|
50
|
|
|
|
|
if (source < 0) { |
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
|
100
|
|
|
|
|
while ((read_bytes = p_read(source, buffer, sizeof(buffer))) > 0) { |
81
|
3
|
50
|
|
|
|
|
if ((error = p_write(file->fd, buffer, read_bytes)) < 0) |
82
|
0
|
|
|
|
|
|
break; |
83
|
3
|
50
|
|
|
|
|
if (file->compute_digest) |
84
|
0
|
|
|
|
|
|
git_hash_update(&file->digest, buffer, read_bytes); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
3
|
|
|
|
|
|
p_close(source); |
88
|
|
|
|
|
|
|
|
89
|
3
|
50
|
|
|
|
|
if (read_bytes < 0) { |
90
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to read file '%s'", file->path_original); |
91
|
0
|
|
|
|
|
|
return -1; |
92
|
3
|
50
|
|
|
|
|
} else if (error < 0) { |
93
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to write file '%s'", file->path_lock); |
94
|
3
|
|
|
|
|
|
return -1; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
316
|
|
|
|
|
|
return 0; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
689
|
|
|
|
|
|
void git_filebuf_cleanup(git_filebuf *file) |
102
|
|
|
|
|
|
|
{ |
103
|
689
|
100
|
|
|
|
|
if (file->fd_is_open && file->fd >= 0) |
|
|
50
|
|
|
|
|
|
104
|
36
|
|
|
|
|
|
p_close(file->fd); |
105
|
|
|
|
|
|
|
|
106
|
689
|
100
|
|
|
|
|
if (file->created_lock && !file->did_rename && file->path_lock && git_path_exists(file->path_lock)) |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
107
|
36
|
|
|
|
|
|
p_unlink(file->path_lock); |
108
|
|
|
|
|
|
|
|
109
|
689
|
100
|
|
|
|
|
if (file->compute_digest) { |
110
|
50
|
|
|
|
|
|
git_hash_ctx_cleanup(&file->digest); |
111
|
50
|
|
|
|
|
|
file->compute_digest = 0; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
689
|
100
|
|
|
|
|
if (file->buffer) |
115
|
490
|
|
|
|
|
|
git__free(file->buffer); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
/* use the presence of z_buf to decide if we need to deflateEnd */ |
118
|
689
|
100
|
|
|
|
|
if (file->z_buf) { |
119
|
177
|
|
|
|
|
|
git__free(file->z_buf); |
120
|
177
|
|
|
|
|
|
deflateEnd(&file->zs); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
689
|
100
|
|
|
|
|
if (file->path_original) |
124
|
473
|
|
|
|
|
|
git__free(file->path_original); |
125
|
689
|
100
|
|
|
|
|
if (file->path_lock) |
126
|
493
|
|
|
|
|
|
git__free(file->path_lock); |
127
|
|
|
|
|
|
|
|
128
|
689
|
|
|
|
|
|
memset(file, 0x0, sizeof(git_filebuf)); |
129
|
689
|
|
|
|
|
|
file->fd = -1; |
130
|
689
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
559
|
|
|
|
|
|
GIT_INLINE(int) flush_buffer(git_filebuf *file) |
133
|
|
|
|
|
|
|
{ |
134
|
559
|
|
|
|
|
|
int result = file->write(file, file->buffer, file->buf_pos); |
135
|
559
|
|
|
|
|
|
file->buf_pos = 0; |
136
|
559
|
|
|
|
|
|
return result; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
int git_filebuf_flush(git_filebuf *file) |
140
|
|
|
|
|
|
|
{ |
141
|
0
|
|
|
|
|
|
return flush_buffer(file); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
405
|
|
|
|
|
|
static int write_normal(git_filebuf *file, void *source, size_t len) |
145
|
|
|
|
|
|
|
{ |
146
|
405
|
100
|
|
|
|
|
if (len > 0) { |
147
|
401
|
50
|
|
|
|
|
if (p_write(file->fd, (void *)source, len) < 0) { |
148
|
0
|
|
|
|
|
|
file->last_error = BUFERR_WRITE; |
149
|
0
|
|
|
|
|
|
return -1; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
401
|
100
|
|
|
|
|
if (file->compute_digest) |
153
|
151
|
|
|
|
|
|
git_hash_update(&file->digest, source, len); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
405
|
|
|
|
|
|
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
|
50
|
|
|
|
|
if (len > 0 || file->flush_mode == Z_FINISH) { |
|
|
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
|
50
|
|
|
|
|
if (deflate(zs, file->flush_mode) == Z_STREAM_ERROR) { |
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
|
50
|
|
|
|
|
if (p_write(file->fd, file->z_buf, have) < 0) { |
181
|
0
|
|
|
|
|
|
file->last_error = BUFERR_WRITE; |
182
|
0
|
|
|
|
|
|
return -1; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
157
|
50
|
|
|
|
|
} while (zs->avail_out == 0); |
186
|
|
|
|
|
|
|
|
187
|
157
|
50
|
|
|
|
|
assert(zs->avail_in == 0); |
188
|
|
|
|
|
|
|
|
189
|
157
|
50
|
|
|
|
|
if (file->compute_digest) |
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
|
316
|
|
|
|
|
|
static int resolve_symlink(git_buf *out, const char *path) |
199
|
|
|
|
|
|
|
{ |
200
|
|
|
|
|
|
|
int i, error, root; |
201
|
|
|
|
|
|
|
ssize_t ret; |
202
|
|
|
|
|
|
|
struct stat st; |
203
|
316
|
|
|
|
|
|
git_buf curpath = GIT_BUF_INIT, target = GIT_BUF_INIT; |
204
|
|
|
|
|
|
|
|
205
|
316
|
50
|
|
|
|
|
if ((error = git_buf_grow(&target, GIT_PATH_MAX + 1)) < 0 || |
|
|
50
|
|
|
|
|
|
206
|
|
|
|
|
|
|
(error = git_buf_puts(&curpath, path)) < 0) |
207
|
0
|
|
|
|
|
|
return error; |
208
|
|
|
|
|
|
|
|
209
|
316
|
50
|
|
|
|
|
for (i = 0; i < MAX_SYMLINK_DEPTH; i++) { |
210
|
316
|
|
|
|
|
|
error = p_lstat(curpath.ptr, &st); |
211
|
316
|
100
|
|
|
|
|
if (error < 0 && errno == ENOENT) { |
|
|
50
|
|
|
|
|
|
212
|
75
|
|
|
|
|
|
error = git_buf_puts(out, curpath.ptr); |
213
|
75
|
|
|
|
|
|
goto cleanup; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
241
|
50
|
|
|
|
|
if (error < 0) { |
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
|
50
|
|
|
|
|
if (!S_ISLNK(st.st_mode)) { |
223
|
241
|
|
|
|
|
|
error = git_buf_puts(out, curpath.ptr); |
224
|
241
|
|
|
|
|
|
goto cleanup; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
0
|
|
|
|
|
|
ret = p_readlink(curpath.ptr, target.ptr, GIT_PATH_MAX); |
228
|
0
|
0
|
|
|
|
|
if (ret < 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
|
0
|
|
|
|
|
if (ret == GIT_PATH_MAX) { |
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_path_root(target.ptr); |
245
|
0
|
0
|
|
|
|
|
if (root >= 0) { |
246
|
0
|
0
|
|
|
|
|
if ((error = git_buf_sets(&curpath, target.ptr)) < 0) |
247
|
0
|
|
|
|
|
|
goto cleanup; |
248
|
|
|
|
|
|
|
} else { |
249
|
0
|
|
|
|
|
|
git_buf dir = GIT_BUF_INIT; |
250
|
|
|
|
|
|
|
|
251
|
0
|
0
|
|
|
|
|
if ((error = git_path_dirname_r(&dir, curpath.ptr)) < 0) |
252
|
0
|
|
|
|
|
|
goto cleanup; |
253
|
|
|
|
|
|
|
|
254
|
0
|
|
|
|
|
|
git_buf_swap(&curpath, &dir); |
255
|
0
|
|
|
|
|
|
git_buf_dispose(&dir); |
256
|
|
|
|
|
|
|
|
257
|
0
|
0
|
|
|
|
|
if ((error = git_path_apply_relative(&curpath, target.ptr)) < 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
|
|
|
|
|
|
|
cleanup: |
266
|
316
|
|
|
|
|
|
git_buf_dispose(&curpath); |
267
|
316
|
|
|
|
|
|
git_buf_dispose(&target); |
268
|
316
|
|
|
|
|
|
return error; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
493
|
|
|
|
|
|
int git_filebuf_open(git_filebuf *file, const char *path, int flags, mode_t mode) |
272
|
|
|
|
|
|
|
{ |
273
|
493
|
|
|
|
|
|
return git_filebuf_open_withsize(file, path, flags, mode, WRITE_BUFFER_SIZE); |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
|
276
|
493
|
|
|
|
|
|
int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mode_t mode, size_t size) |
277
|
|
|
|
|
|
|
{ |
278
|
493
|
|
|
|
|
|
int compression, error = -1; |
279
|
|
|
|
|
|
|
size_t path_len, alloc_len; |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
/* opening an already open buffer is a programming error; |
282
|
|
|
|
|
|
|
* assert that this never happens instead of returning |
283
|
|
|
|
|
|
|
* an error code */ |
284
|
493
|
50
|
|
|
|
|
assert(file && path && file->buffer == NULL); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
285
|
|
|
|
|
|
|
|
286
|
493
|
|
|
|
|
|
memset(file, 0x0, sizeof(git_filebuf)); |
287
|
|
|
|
|
|
|
|
288
|
493
|
100
|
|
|
|
|
if (flags & GIT_FILEBUF_DO_NOT_BUFFER) |
289
|
3
|
|
|
|
|
|
file->do_not_buffer = true; |
290
|
|
|
|
|
|
|
|
291
|
493
|
50
|
|
|
|
|
if (flags & GIT_FILEBUF_FSYNC) |
292
|
0
|
|
|
|
|
|
file->do_fsync = true; |
293
|
|
|
|
|
|
|
|
294
|
493
|
|
|
|
|
|
file->buf_size = size; |
295
|
493
|
|
|
|
|
|
file->buf_pos = 0; |
296
|
493
|
|
|
|
|
|
file->fd = -1; |
297
|
493
|
|
|
|
|
|
file->last_error = BUFERR_OK; |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
/* Allocate the main cache buffer */ |
300
|
493
|
100
|
|
|
|
|
if (!file->do_not_buffer) { |
301
|
490
|
|
|
|
|
|
file->buffer = git__malloc(file->buf_size); |
302
|
490
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(file->buffer); |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
/* If we are hashing on-write, allocate a new hash context */ |
306
|
493
|
100
|
|
|
|
|
if (flags & GIT_FILEBUF_HASH_CONTENTS) { |
307
|
152
|
|
|
|
|
|
file->compute_digest = 1; |
308
|
|
|
|
|
|
|
|
309
|
152
|
50
|
|
|
|
|
if (git_hash_ctx_init(&file->digest) < 0) |
310
|
0
|
|
|
|
|
|
goto cleanup; |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
|
313
|
493
|
|
|
|
|
|
compression = flags >> GIT_FILEBUF_DEFLATE_SHIFT; |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
/* If we are deflating on-write, */ |
316
|
493
|
100
|
|
|
|
|
if (compression != 0) { |
317
|
|
|
|
|
|
|
/* Initialize the ZLib stream */ |
318
|
177
|
50
|
|
|
|
|
if (deflateInit(&file->zs, compression) != Z_OK) { |
319
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_ZLIB, "failed to initialize zlib"); |
320
|
0
|
|
|
|
|
|
goto cleanup; |
321
|
|
|
|
|
|
|
} |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
/* Allocate the Zlib cache buffer */ |
324
|
177
|
|
|
|
|
|
file->z_buf = git__malloc(file->buf_size); |
325
|
177
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(file->z_buf); |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
/* Never flush */ |
328
|
177
|
|
|
|
|
|
file->flush_mode = Z_NO_FLUSH; |
329
|
177
|
|
|
|
|
|
file->write = &write_deflate; |
330
|
|
|
|
|
|
|
} else { |
331
|
316
|
|
|
|
|
|
file->write = &write_normal; |
332
|
|
|
|
|
|
|
} |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
/* If we are writing to a temp file */ |
335
|
493
|
100
|
|
|
|
|
if (flags & GIT_FILEBUF_TEMPORARY) { |
336
|
177
|
|
|
|
|
|
git_buf tmp_path = GIT_BUF_INIT; |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
/* Open the file as temporary for locking */ |
339
|
177
|
|
|
|
|
|
file->fd = git_futils_mktmp(&tmp_path, path, mode); |
340
|
|
|
|
|
|
|
|
341
|
177
|
50
|
|
|
|
|
if (file->fd < 0) { |
342
|
0
|
|
|
|
|
|
git_buf_dispose(&tmp_path); |
343
|
0
|
|
|
|
|
|
goto cleanup; |
344
|
|
|
|
|
|
|
} |
345
|
177
|
|
|
|
|
|
file->fd_is_open = true; |
346
|
177
|
|
|
|
|
|
file->created_lock = true; |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
/* No original path */ |
349
|
177
|
|
|
|
|
|
file->path_original = NULL; |
350
|
177
|
|
|
|
|
|
file->path_lock = git_buf_detach(&tmp_path); |
351
|
177
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(file->path_lock); |
352
|
|
|
|
|
|
|
} else { |
353
|
316
|
|
|
|
|
|
git_buf resolved_path = GIT_BUF_INIT; |
354
|
|
|
|
|
|
|
|
355
|
316
|
50
|
|
|
|
|
if ((error = resolve_symlink(&resolved_path, path)) < 0) |
356
|
0
|
|
|
|
|
|
goto cleanup; |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
/* Save the original path of the file */ |
359
|
316
|
|
|
|
|
|
path_len = resolved_path.size; |
360
|
316
|
|
|
|
|
|
file->path_original = git_buf_detach(&resolved_path); |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
/* create the locking path by appending ".lock" to the original */ |
363
|
316
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, path_len, GIT_FILELOCK_EXTLENGTH); |
|
|
50
|
|
|
|
|
|
364
|
316
|
|
|
|
|
|
file->path_lock = git__malloc(alloc_len); |
365
|
316
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(file->path_lock); |
366
|
|
|
|
|
|
|
|
367
|
316
|
|
|
|
|
|
memcpy(file->path_lock, file->path_original, path_len); |
368
|
316
|
|
|
|
|
|
memcpy(file->path_lock + path_len, GIT_FILELOCK_EXTENSION, GIT_FILELOCK_EXTLENGTH); |
369
|
|
|
|
|
|
|
|
370
|
316
|
50
|
|
|
|
|
if (git_path_isdir(file->path_original)) { |
371
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_FILESYSTEM, "path '%s' is a directory", file->path_original); |
372
|
0
|
|
|
|
|
|
error = GIT_EDIRECTORY; |
373
|
0
|
|
|
|
|
|
goto cleanup; |
374
|
|
|
|
|
|
|
} |
375
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
/* open the file for locking */ |
377
|
316
|
50
|
|
|
|
|
if ((error = lock_file(file, flags, mode)) < 0) |
378
|
0
|
|
|
|
|
|
goto cleanup; |
379
|
|
|
|
|
|
|
|
380
|
316
|
|
|
|
|
|
file->created_lock = true; |
381
|
|
|
|
|
|
|
} |
382
|
|
|
|
|
|
|
|
383
|
493
|
|
|
|
|
|
return 0; |
384
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
cleanup: |
386
|
0
|
|
|
|
|
|
git_filebuf_cleanup(file); |
387
|
493
|
|
|
|
|
|
return error; |
388
|
|
|
|
|
|
|
} |
389
|
|
|
|
|
|
|
|
390
|
102
|
|
|
|
|
|
int git_filebuf_hash(git_oid *oid, git_filebuf *file) |
391
|
|
|
|
|
|
|
{ |
392
|
102
|
50
|
|
|
|
|
assert(oid && file && file->compute_digest); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
393
|
|
|
|
|
|
|
|
394
|
102
|
|
|
|
|
|
flush_buffer(file); |
395
|
|
|
|
|
|
|
|
396
|
102
|
50
|
|
|
|
|
if (verify_last_error(file) < 0) |
397
|
0
|
|
|
|
|
|
return -1; |
398
|
|
|
|
|
|
|
|
399
|
102
|
|
|
|
|
|
git_hash_final(oid, &file->digest); |
400
|
102
|
|
|
|
|
|
git_hash_ctx_cleanup(&file->digest); |
401
|
102
|
|
|
|
|
|
file->compute_digest = 0; |
402
|
|
|
|
|
|
|
|
403
|
102
|
|
|
|
|
|
return 0; |
404
|
|
|
|
|
|
|
} |
405
|
|
|
|
|
|
|
|
406
|
162
|
|
|
|
|
|
int git_filebuf_commit_at(git_filebuf *file, const char *path) |
407
|
|
|
|
|
|
|
{ |
408
|
162
|
|
|
|
|
|
git__free(file->path_original); |
409
|
162
|
|
|
|
|
|
file->path_original = git__strdup(path); |
410
|
162
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(file->path_original); |
411
|
|
|
|
|
|
|
|
412
|
162
|
|
|
|
|
|
return git_filebuf_commit(file); |
413
|
|
|
|
|
|
|
} |
414
|
|
|
|
|
|
|
|
415
|
457
|
|
|
|
|
|
int git_filebuf_commit(git_filebuf *file) |
416
|
|
|
|
|
|
|
{ |
417
|
|
|
|
|
|
|
/* temporary files cannot be committed */ |
418
|
457
|
50
|
|
|
|
|
assert(file && file->path_original); |
|
|
50
|
|
|
|
|
|
419
|
|
|
|
|
|
|
|
420
|
457
|
|
|
|
|
|
file->flush_mode = Z_FINISH; |
421
|
457
|
|
|
|
|
|
flush_buffer(file); |
422
|
|
|
|
|
|
|
|
423
|
457
|
50
|
|
|
|
|
if (verify_last_error(file) < 0) |
424
|
0
|
|
|
|
|
|
goto on_error; |
425
|
|
|
|
|
|
|
|
426
|
457
|
|
|
|
|
|
file->fd_is_open = false; |
427
|
|
|
|
|
|
|
|
428
|
457
|
50
|
|
|
|
|
if (file->do_fsync && p_fsync(file->fd) < 0) { |
|
|
0
|
|
|
|
|
|
429
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to fsync '%s'", file->path_lock); |
430
|
0
|
|
|
|
|
|
goto on_error; |
431
|
|
|
|
|
|
|
} |
432
|
|
|
|
|
|
|
|
433
|
457
|
50
|
|
|
|
|
if (p_close(file->fd) < 0) { |
434
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to close file at '%s'", file->path_lock); |
435
|
0
|
|
|
|
|
|
goto on_error; |
436
|
|
|
|
|
|
|
} |
437
|
|
|
|
|
|
|
|
438
|
457
|
|
|
|
|
|
file->fd = -1; |
439
|
|
|
|
|
|
|
|
440
|
457
|
50
|
|
|
|
|
if (p_rename(file->path_lock, file->path_original) < 0) { |
441
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to rename lockfile to '%s'", file->path_original); |
442
|
0
|
|
|
|
|
|
goto on_error; |
443
|
|
|
|
|
|
|
} |
444
|
|
|
|
|
|
|
|
445
|
457
|
50
|
|
|
|
|
if (file->do_fsync && git_futils_fsync_parent(file->path_original) < 0) |
|
|
0
|
|
|
|
|
|
446
|
0
|
|
|
|
|
|
goto on_error; |
447
|
|
|
|
|
|
|
|
448
|
457
|
|
|
|
|
|
file->did_rename = true; |
449
|
|
|
|
|
|
|
|
450
|
457
|
|
|
|
|
|
git_filebuf_cleanup(file); |
451
|
457
|
|
|
|
|
|
return 0; |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
on_error: |
454
|
0
|
|
|
|
|
|
git_filebuf_cleanup(file); |
455
|
0
|
|
|
|
|
|
return -1; |
456
|
|
|
|
|
|
|
} |
457
|
|
|
|
|
|
|
|
458
|
2240
|
|
|
|
|
|
GIT_INLINE(void) add_to_cache(git_filebuf *file, const void *buf, size_t len) |
459
|
|
|
|
|
|
|
{ |
460
|
2240
|
|
|
|
|
|
memcpy(file->buffer + file->buf_pos, buf, len); |
461
|
2240
|
|
|
|
|
|
file->buf_pos += len; |
462
|
2240
|
|
|
|
|
|
} |
463
|
|
|
|
|
|
|
|
464
|
2243
|
|
|
|
|
|
int git_filebuf_write(git_filebuf *file, const void *buff, size_t len) |
465
|
|
|
|
|
|
|
{ |
466
|
2243
|
|
|
|
|
|
const unsigned char *buf = buff; |
467
|
|
|
|
|
|
|
|
468
|
2243
|
50
|
|
|
|
|
ENSURE_BUF_OK(file); |
469
|
|
|
|
|
|
|
|
470
|
2243
|
100
|
|
|
|
|
if (file->do_not_buffer) |
471
|
3
|
|
|
|
|
|
return file->write(file, (void *)buff, len); |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
for (;;) { |
474
|
2240
|
|
|
|
|
|
size_t space_left = file->buf_size - file->buf_pos; |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
/* cache if it's small */ |
477
|
2240
|
50
|
|
|
|
|
if (space_left > len) { |
478
|
2240
|
|
|
|
|
|
add_to_cache(file, buf, len); |
479
|
2240
|
|
|
|
|
|
return 0; |
480
|
|
|
|
|
|
|
} |
481
|
|
|
|
|
|
|
|
482
|
0
|
|
|
|
|
|
add_to_cache(file, buf, space_left); |
483
|
0
|
0
|
|
|
|
|
if (flush_buffer(file) < 0) |
484
|
0
|
|
|
|
|
|
return -1; |
485
|
|
|
|
|
|
|
|
486
|
0
|
|
|
|
|
|
len -= space_left; |
487
|
0
|
|
|
|
|
|
buf += space_left; |
488
|
0
|
|
|
|
|
|
} |
489
|
|
|
|
|
|
|
} |
490
|
|
|
|
|
|
|
|
491
|
206
|
|
|
|
|
|
int git_filebuf_reserve(git_filebuf *file, void **buffer, size_t len) |
492
|
|
|
|
|
|
|
{ |
493
|
206
|
|
|
|
|
|
size_t space_left = file->buf_size - file->buf_pos; |
494
|
|
|
|
|
|
|
|
495
|
206
|
|
|
|
|
|
*buffer = NULL; |
496
|
|
|
|
|
|
|
|
497
|
206
|
50
|
|
|
|
|
ENSURE_BUF_OK(file); |
498
|
|
|
|
|
|
|
|
499
|
206
|
50
|
|
|
|
|
if (len > file->buf_size) { |
500
|
0
|
|
|
|
|
|
file->last_error = BUFERR_MEM; |
501
|
0
|
|
|
|
|
|
return -1; |
502
|
|
|
|
|
|
|
} |
503
|
|
|
|
|
|
|
|
504
|
206
|
50
|
|
|
|
|
if (space_left <= len) { |
505
|
0
|
0
|
|
|
|
|
if (flush_buffer(file) < 0) |
506
|
0
|
|
|
|
|
|
return -1; |
507
|
|
|
|
|
|
|
} |
508
|
|
|
|
|
|
|
|
509
|
206
|
|
|
|
|
|
*buffer = (file->buffer + file->buf_pos); |
510
|
206
|
|
|
|
|
|
file->buf_pos += len; |
511
|
|
|
|
|
|
|
|
512
|
206
|
|
|
|
|
|
return 0; |
513
|
|
|
|
|
|
|
} |
514
|
|
|
|
|
|
|
|
515
|
147
|
|
|
|
|
|
int git_filebuf_printf(git_filebuf *file, const char *format, ...) |
516
|
|
|
|
|
|
|
{ |
517
|
|
|
|
|
|
|
va_list arglist; |
518
|
|
|
|
|
|
|
size_t space_left, len, alloclen; |
519
|
|
|
|
|
|
|
int written, res; |
520
|
|
|
|
|
|
|
char *tmp_buffer; |
521
|
|
|
|
|
|
|
|
522
|
147
|
50
|
|
|
|
|
ENSURE_BUF_OK(file); |
523
|
|
|
|
|
|
|
|
524
|
147
|
|
|
|
|
|
space_left = file->buf_size - file->buf_pos; |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
do { |
527
|
147
|
|
|
|
|
|
va_start(arglist, format); |
528
|
147
|
|
|
|
|
|
written = p_vsnprintf((char *)file->buffer + file->buf_pos, space_left, format, arglist); |
529
|
147
|
|
|
|
|
|
va_end(arglist); |
530
|
|
|
|
|
|
|
|
531
|
147
|
50
|
|
|
|
|
if (written < 0) { |
532
|
0
|
|
|
|
|
|
file->last_error = BUFERR_MEM; |
533
|
0
|
|
|
|
|
|
return -1; |
534
|
|
|
|
|
|
|
} |
535
|
|
|
|
|
|
|
|
536
|
147
|
|
|
|
|
|
len = written; |
537
|
147
|
50
|
|
|
|
|
if (len + 1 <= space_left) { |
538
|
147
|
|
|
|
|
|
file->buf_pos += len; |
539
|
147
|
|
|
|
|
|
return 0; |
540
|
|
|
|
|
|
|
} |
541
|
|
|
|
|
|
|
|
542
|
0
|
0
|
|
|
|
|
if (flush_buffer(file) < 0) |
543
|
0
|
|
|
|
|
|
return -1; |
544
|
|
|
|
|
|
|
|
545
|
0
|
|
|
|
|
|
space_left = file->buf_size - file->buf_pos; |
546
|
|
|
|
|
|
|
|
547
|
0
|
0
|
|
|
|
|
} while (len + 1 <= space_left); |
548
|
|
|
|
|
|
|
|
549
|
0
|
0
|
|
|
|
|
if (GIT_ADD_SIZET_OVERFLOW(&alloclen, len, 1) || |
|
|
0
|
|
|
|
|
|
550
|
0
|
|
|
|
|
|
!(tmp_buffer = git__malloc(alloclen))) { |
551
|
0
|
|
|
|
|
|
file->last_error = BUFERR_MEM; |
552
|
0
|
|
|
|
|
|
return -1; |
553
|
|
|
|
|
|
|
} |
554
|
|
|
|
|
|
|
|
555
|
0
|
|
|
|
|
|
va_start(arglist, format); |
556
|
0
|
|
|
|
|
|
written = p_vsnprintf(tmp_buffer, len + 1, format, arglist); |
557
|
0
|
|
|
|
|
|
va_end(arglist); |
558
|
|
|
|
|
|
|
|
559
|
0
|
0
|
|
|
|
|
if (written < 0) { |
560
|
0
|
|
|
|
|
|
git__free(tmp_buffer); |
561
|
0
|
|
|
|
|
|
file->last_error = BUFERR_MEM; |
562
|
0
|
|
|
|
|
|
return -1; |
563
|
|
|
|
|
|
|
} |
564
|
|
|
|
|
|
|
|
565
|
0
|
|
|
|
|
|
res = git_filebuf_write(file, tmp_buffer, len); |
566
|
0
|
|
|
|
|
|
git__free(tmp_buffer); |
567
|
|
|
|
|
|
|
|
568
|
147
|
|
|
|
|
|
return res; |
569
|
|
|
|
|
|
|
} |
570
|
|
|
|
|
|
|
|
571
|
0
|
|
|
|
|
|
int git_filebuf_stats(time_t *mtime, size_t *size, git_filebuf *file) |
572
|
|
|
|
|
|
|
{ |
573
|
|
|
|
|
|
|
int res; |
574
|
|
|
|
|
|
|
struct stat st; |
575
|
|
|
|
|
|
|
|
576
|
0
|
0
|
|
|
|
|
if (file->fd_is_open) |
577
|
0
|
|
|
|
|
|
res = p_fstat(file->fd, &st); |
578
|
|
|
|
|
|
|
else |
579
|
0
|
|
|
|
|
|
res = p_stat(file->path_original, &st); |
580
|
|
|
|
|
|
|
|
581
|
0
|
0
|
|
|
|
|
if (res < 0) { |
582
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "could not get stat info for '%s'", |
583
|
|
|
|
|
|
|
file->path_original); |
584
|
0
|
|
|
|
|
|
return res; |
585
|
|
|
|
|
|
|
} |
586
|
|
|
|
|
|
|
|
587
|
0
|
0
|
|
|
|
|
if (mtime) |
588
|
0
|
|
|
|
|
|
*mtime = st.st_mtime; |
589
|
0
|
0
|
|
|
|
|
if (size) |
590
|
0
|
|
|
|
|
|
*size = (size_t)st.st_size; |
591
|
|
|
|
|
|
|
|
592
|
0
|
|
|
|
|
|
return 0; |
593
|
|
|
|
|
|
|
} |