]>
git.proxmox.com Git - libgit2.git/blob - src/blob.c
2 * Copyright (C) 2009-2011 the libgit2 contributors
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.
8 #include "git2/common.h"
9 #include "git2/object.h"
10 #include "git2/repository.h"
15 const void *git_blob_rawcontent(git_blob
*blob
)
18 return blob
->odb_object
->raw
.data
;
21 size_t git_blob_rawsize(git_blob
*blob
)
24 return blob
->odb_object
->raw
.len
;
27 void git_blob__free(git_blob
*blob
)
29 git_odb_object_free(blob
->odb_object
);
33 int git_blob__parse(git_blob
*blob
, git_odb_object
*odb_obj
)
36 git_cached_obj_incref((git_cached_obj
*)odb_obj
);
37 blob
->odb_object
= odb_obj
;
41 int git_blob_create_frombuffer(git_oid
*oid
, git_repository
*repo
, const void *buffer
, size_t len
)
45 git_odb_stream
*stream
;
47 error
= git_repository_odb__weakptr(&odb
, repo
);
48 if (error
< GIT_SUCCESS
)
51 if ((error
= git_odb_open_wstream(&stream
, odb
, len
, GIT_OBJ_BLOB
)) < GIT_SUCCESS
)
52 return git__rethrow(error
, "Failed to create blob");
54 if ((error
= stream
->write(stream
, buffer
, len
)) < GIT_SUCCESS
) {
59 error
= stream
->finalize_write(oid
, stream
);
62 if (error
< GIT_SUCCESS
)
63 return git__rethrow(error
, "Failed to create blob");
68 int git_blob_create_fromfile(git_oid
*oid
, git_repository
*repo
, const char *path
)
70 int error
= GIT_SUCCESS
;
71 git_buf full_path
= GIT_BUF_INIT
;
73 git_odb_stream
*stream
= NULL
;
78 workdir
= git_repository_workdir(repo
);
80 return git__throw(GIT_ENOTFOUND
, "Failed to create blob. (No working directory found)");
82 error
= git_buf_joinpath(&full_path
, workdir
, path
);
83 if (error
< GIT_SUCCESS
)
86 error
= p_lstat(full_path
.ptr
, &st
);
88 error
= git__throw(GIT_EOSERR
, "Failed to stat blob. %s", strerror(errno
));
94 error
= git_repository_odb__weakptr(&odb
, repo
);
95 if (error
< GIT_SUCCESS
)
98 if ((error
= git_odb_open_wstream(&stream
, odb
, (size_t)size
, GIT_OBJ_BLOB
)) < GIT_SUCCESS
)
101 if (S_ISLNK(st
.st_mode
)) {
105 link_data
= git__malloc(size
);
111 read_len
= p_readlink(full_path
.ptr
, link_data
, size
);
113 if (read_len
!= (ssize_t
)size
) {
114 error
= git__throw(GIT_EOSERR
, "Failed to create blob. Can't read symlink");
119 stream
->write(stream
, link_data
, size
);
126 if ((fd
= p_open(full_path
.ptr
, O_RDONLY
)) < 0) {
127 error
= git__throw(GIT_ENOTFOUND
, "Failed to create blob. Could not open '%s'", full_path
.ptr
);
132 ssize_t read_len
= p_read(fd
, buffer
, sizeof(buffer
));
135 error
= git__throw(GIT_EOSERR
, "Failed to create blob. Can't read full file");
140 stream
->write(stream
, buffer
, read_len
);
147 error
= stream
->finalize_write(oid
, stream
);
151 stream
->free(stream
);
153 git_buf_free(&full_path
);