2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 #include "git2/common.h"
11 #include "git2/object.h"
12 #include "git2/repository.h"
13 #include "git2/odb_backend.h"
19 const void *git_blob_rawcontent(const git_blob
*blob
)
23 return blob
->data
.raw
.data
;
25 return git_odb_object_data(blob
->data
.odb
);
28 git_object_size_t
git_blob_rawsize(const git_blob
*blob
)
32 return blob
->data
.raw
.size
;
34 return (git_object_size_t
)git_odb_object_size(blob
->data
.odb
);
37 int git_blob__getbuf(git_buf
*buffer
, git_blob
*blob
)
39 git_object_size_t size
= git_blob_rawsize(blob
);
41 GIT_ERROR_CHECK_BLOBSIZE(size
);
42 return git_buf_set(buffer
, git_blob_rawcontent(blob
), (size_t)size
);
45 void git_blob__free(void *_blob
)
47 git_blob
*blob
= (git_blob
*) _blob
;
49 git_odb_object_free(blob
->data
.odb
);
53 int git_blob__parse_raw(void *_blob
, const char *data
, size_t size
)
55 git_blob
*blob
= (git_blob
*) _blob
;
58 blob
->data
.raw
.data
= data
;
59 blob
->data
.raw
.size
= size
;
63 int git_blob__parse(void *_blob
, git_odb_object
*odb_obj
)
65 git_blob
*blob
= (git_blob
*) _blob
;
67 git_cached_obj_incref((git_cached_obj
*)odb_obj
);
69 blob
->data
.odb
= odb_obj
;
73 int git_blob_create_from_buffer(
74 git_oid
*id
, git_repository
*repo
, const void *buffer
, size_t len
)
78 git_odb_stream
*stream
;
82 if ((error
= git_repository_odb__weakptr(&odb
, repo
)) < 0 ||
83 (error
= git_odb_open_wstream(&stream
, odb
, len
, GIT_OBJECT_BLOB
)) < 0)
86 if ((error
= git_odb_stream_write(stream
, buffer
, len
)) == 0)
87 error
= git_odb_stream_finalize_write(id
, stream
);
89 git_odb_stream_free(stream
);
93 static int write_file_stream(
94 git_oid
*id
, git_odb
*odb
, const char *path
, git_object_size_t file_size
)
97 char buffer
[FILEIO_BUFSIZE
];
98 git_odb_stream
*stream
= NULL
;
99 ssize_t read_len
= -1;
100 git_object_size_t written
= 0;
102 if ((error
= git_odb_open_wstream(
103 &stream
, odb
, file_size
, GIT_OBJECT_BLOB
)) < 0)
106 if ((fd
= git_futils_open_ro(path
)) < 0) {
107 git_odb_stream_free(stream
);
111 while (!error
&& (read_len
= p_read(fd
, buffer
, sizeof(buffer
))) > 0) {
112 error
= git_odb_stream_write(stream
, buffer
, read_len
);
118 if (written
!= file_size
|| read_len
< 0) {
119 git_error_set(GIT_ERROR_OS
, "failed to read file into stream");
124 error
= git_odb_stream_finalize_write(id
, stream
);
126 git_odb_stream_free(stream
);
130 static int write_file_filtered(
132 git_object_size_t
*size
,
134 const char *full_path
,
138 git_buf tgt
= GIT_BUF_INIT
;
140 error
= git_filter_list_apply_to_file(&tgt
, fl
, NULL
, full_path
);
142 /* Write the file to disk if it was properly filtered */
146 error
= git_odb_write(id
, odb
, tgt
.ptr
, tgt
.size
, GIT_OBJECT_BLOB
);
149 git_buf_dispose(&tgt
);
153 static int write_symlink(
154 git_oid
*id
, git_odb
*odb
, const char *path
, size_t link_size
)
160 link_data
= git__malloc(link_size
);
161 GIT_ERROR_CHECK_ALLOC(link_data
);
163 read_len
= p_readlink(path
, link_data
, link_size
);
164 if (read_len
!= (ssize_t
)link_size
) {
165 git_error_set(GIT_ERROR_OS
, "failed to create blob: cannot read symlink '%s'", path
);
166 git__free(link_data
);
170 error
= git_odb_write(id
, odb
, (void *)link_data
, link_size
, GIT_OBJECT_BLOB
);
171 git__free(link_data
);
175 int git_blob__create_from_paths(
178 git_repository
*repo
,
179 const char *content_path
,
180 const char *hint_path
,
182 bool try_load_filters
)
187 git_object_size_t size
;
189 git_buf path
= GIT_BUF_INIT
;
191 assert(hint_path
|| !try_load_filters
);
194 if (git_repository__ensure_not_bare(repo
, "create blob from file") < 0)
195 return GIT_EBAREREPO
;
197 if (git_buf_joinpath(
198 &path
, git_repository_workdir(repo
), hint_path
) < 0)
201 content_path
= path
.ptr
;
204 if ((error
= git_path_lstat(content_path
, &st
)) < 0 ||
205 (error
= git_repository_odb(&odb
, repo
)) < 0)
208 if (S_ISDIR(st
.st_mode
)) {
209 git_error_set(GIT_ERROR_ODB
, "cannot create blob from '%s': it is a directory", content_path
);
210 error
= GIT_EDIRECTORY
;
215 memcpy(out_st
, &st
, sizeof(st
));
218 mode
= hint_mode
? hint_mode
: st
.st_mode
;
221 error
= write_symlink(id
, odb
, content_path
, (size_t)size
);
223 git_filter_list
*fl
= NULL
;
225 if (try_load_filters
)
226 /* Load the filters for writing this file to the ODB */
227 error
= git_filter_list_load(
228 &fl
, repo
, NULL
, hint_path
,
229 GIT_FILTER_TO_ODB
, GIT_FILTER_DEFAULT
);
232 /* well, that didn't work */;
234 /* No filters need to be applied to the document: we can stream
235 * directly from disk */
236 error
= write_file_stream(id
, odb
, content_path
, size
);
238 /* We need to apply one or more filters */
239 error
= write_file_filtered(id
, &size
, odb
, content_path
, fl
);
241 git_filter_list_free(fl
);
245 * TODO: eventually support streaming filtered files, for files
246 * which are bigger than a given threshold. This is not a priority
247 * because applying a filter in streaming mode changes the final
248 * size of the blob, and without knowing its final size, the blob
249 * cannot be written in stream mode to the ODB.
251 * The plan is to do streaming writes to a tempfile on disk and then
252 * opening streaming that file to the ODB, using
253 * `write_file_stream`.
255 * CAREFULLY DESIGNED APIS YO
261 git_buf_dispose(&path
);
266 int git_blob_create_from_workdir(
267 git_oid
*id
, git_repository
*repo
, const char *path
)
269 return git_blob__create_from_paths(id
, NULL
, repo
, NULL
, path
, 0, true);
272 int git_blob_create_from_disk(
273 git_oid
*id
, git_repository
*repo
, const char *path
)
276 git_buf full_path
= GIT_BUF_INIT
;
277 const char *workdir
, *hintpath
;
279 if ((error
= git_path_prettify(&full_path
, path
, NULL
)) < 0) {
280 git_buf_dispose(&full_path
);
284 hintpath
= git_buf_cstr(&full_path
);
285 workdir
= git_repository_workdir(repo
);
287 if (workdir
&& !git__prefixcmp(hintpath
, workdir
))
288 hintpath
+= strlen(workdir
);
290 error
= git_blob__create_from_paths(
291 id
, NULL
, repo
, git_buf_cstr(&full_path
), hintpath
, 0, true);
293 git_buf_dispose(&full_path
);
298 git_writestream parent
;
300 git_repository
*repo
;
304 static int blob_writestream_close(git_writestream
*_stream
)
306 blob_writestream
*stream
= (blob_writestream
*) _stream
;
308 git_filebuf_cleanup(&stream
->fbuf
);
312 static void blob_writestream_free(git_writestream
*_stream
)
314 blob_writestream
*stream
= (blob_writestream
*) _stream
;
316 git_filebuf_cleanup(&stream
->fbuf
);
317 git__free(stream
->hintpath
);
321 static int blob_writestream_write(git_writestream
*_stream
, const char *buffer
, size_t len
)
323 blob_writestream
*stream
= (blob_writestream
*) _stream
;
325 return git_filebuf_write(&stream
->fbuf
, buffer
, len
);
328 int git_blob_create_from_stream(git_writestream
**out
, git_repository
*repo
, const char *hintpath
)
331 git_buf path
= GIT_BUF_INIT
;
332 blob_writestream
*stream
;
336 stream
= git__calloc(1, sizeof(blob_writestream
));
337 GIT_ERROR_CHECK_ALLOC(stream
);
340 stream
->hintpath
= git__strdup(hintpath
);
341 GIT_ERROR_CHECK_ALLOC(stream
->hintpath
);
345 stream
->parent
.write
= blob_writestream_write
;
346 stream
->parent
.close
= blob_writestream_close
;
347 stream
->parent
.free
= blob_writestream_free
;
349 if ((error
= git_repository_item_path(&path
, repo
, GIT_REPOSITORY_ITEM_OBJECTS
)) < 0
350 || (error
= git_buf_joinpath(&path
, path
.ptr
, "streamed")) < 0)
353 if ((error
= git_filebuf_open_withsize(&stream
->fbuf
, git_buf_cstr(&path
), GIT_FILEBUF_TEMPORARY
,
354 0666, 2 * 1024 * 1024)) < 0)
357 *out
= (git_writestream
*) stream
;
361 blob_writestream_free((git_writestream
*) stream
);
363 git_buf_dispose(&path
);
367 int git_blob_create_from_stream_commit(git_oid
*out
, git_writestream
*_stream
)
370 blob_writestream
*stream
= (blob_writestream
*) _stream
;
373 * We can make this more officient by avoiding writing to
374 * disk, but for now let's re-use the helper functions we
377 if ((error
= git_filebuf_flush(&stream
->fbuf
)) < 0)
380 error
= git_blob__create_from_paths(out
, NULL
, stream
->repo
, stream
->fbuf
.path_lock
,
381 stream
->hintpath
, 0, !!stream
->hintpath
);
384 blob_writestream_free(_stream
);
389 int git_blob_is_binary(const git_blob
*blob
)
391 git_buf content
= GIT_BUF_INIT
;
392 git_object_size_t size
;
396 size
= git_blob_rawsize(blob
);
398 git_buf_attach_notowned(&content
, git_blob_rawcontent(blob
),
399 (size_t)min(size
, GIT_FILTER_BYTES_TO_CHECK_NUL
));
400 return git_buf_text_is_binary(&content
);
407 git_blob_filter_options
*given_opts
)
410 git_filter_list
*fl
= NULL
;
411 git_blob_filter_options opts
= GIT_BLOB_FILTER_OPTIONS_INIT
;
412 git_filter_flag_t flags
= GIT_FILTER_DEFAULT
;
414 assert(blob
&& path
&& out
);
416 git_buf_sanitize(out
);
418 GIT_ERROR_CHECK_VERSION(
419 given_opts
, GIT_BLOB_FILTER_OPTIONS_VERSION
, "git_blob_filter_options");
421 if (given_opts
!= NULL
)
422 memcpy(&opts
, given_opts
, sizeof(git_blob_filter_options
));
424 if ((opts
.flags
& GIT_BLOB_FILTER_CHECK_FOR_BINARY
) != 0 &&
425 git_blob_is_binary(blob
))
428 if ((opts
.flags
& GIT_BLOB_FILTER_NO_SYSTEM_ATTRIBUTES
) != 0)
429 flags
|= GIT_FILTER_NO_SYSTEM_ATTRIBUTES
;
431 if ((opts
.flags
& GIT_BLOB_FILTER_ATTTRIBUTES_FROM_HEAD
) != 0)
432 flags
|= GIT_FILTER_ATTRIBUTES_FROM_HEAD
;
434 if (!(error
= git_filter_list_load(
435 &fl
, git_blob_owner(blob
), blob
, path
,
436 GIT_FILTER_TO_WORKTREE
, flags
))) {
438 error
= git_filter_list_apply_to_blob(out
, fl
, blob
);
440 git_filter_list_free(fl
);
446 /* Deprecated functions */
448 int git_blob_create_frombuffer(
449 git_oid
*id
, git_repository
*repo
, const void *buffer
, size_t len
)
451 return git_blob_create_from_buffer(id
, repo
, buffer
, len
);
454 int git_blob_create_fromworkdir(git_oid
*id
, git_repository
*repo
, const char *relative_path
)
456 return git_blob_create_from_workdir(id
, repo
, relative_path
);
459 int git_blob_create_fromdisk(git_oid
*id
, git_repository
*repo
, const char *path
)
461 return git_blob_create_from_disk(id
, repo
, path
);
464 int git_blob_create_fromstream(
465 git_writestream
**out
,
466 git_repository
*repo
,
467 const char *hintpath
)
469 return git_blob_create_from_stream(out
, repo
, hintpath
);
472 int git_blob_create_fromstream_commit(
474 git_writestream
*stream
)
476 return git_blob_create_from_stream_commit(out
, stream
);
479 int git_blob_filtered_content(
483 int check_for_binary_data
)
485 git_blob_filter_options opts
= GIT_BLOB_FILTER_OPTIONS_INIT
;
487 if (check_for_binary_data
)
488 opts
.flags
|= GIT_BLOB_FILTER_CHECK_FOR_BINARY
;
490 opts
.flags
&= ~GIT_BLOB_FILTER_CHECK_FOR_BINARY
;
492 return git_blob_filter(out
, blob
, path
, &opts
);