blob.c 2.84 KB
Newer Older
1
/*
Vicent Marti committed
2
 * Copyright (C) 2009-2011 the libgit2 contributors
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7
 */

8 9 10
#include "git2/common.h"
#include "git2/object.h"
#include "git2/repository.h"
11 12 13 14

#include "common.h"
#include "blob.h"

15
const void *git_blob_rawcontent(git_blob *blob)
16
{
17
	assert(blob);
Vicent Marti committed
18
	return blob->odb_object->raw.data;
19 20
}

21
size_t git_blob_rawsize(git_blob *blob)
22 23
{
	assert(blob);
Vicent Marti committed
24
	return blob->odb_object->raw.len;
25 26 27 28
}

void git_blob__free(git_blob *blob)
{
Vicent Marti committed
29
	git_odb_object_close(blob->odb_object);
30
	git__free(blob);
31 32
}

Vicent Marti committed
33
int git_blob__parse(git_blob *blob, git_odb_object *odb_obj)
34 35
{
	assert(blob);
Vicent Marti committed
36 37
	git_cached_obj_incref((git_cached_obj *)odb_obj);
	blob->odb_object = odb_obj;
38 39 40
	return GIT_SUCCESS;
}

Vicent Marti committed
41
int git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *buffer, size_t len)
42
{
Vicent Marti committed
43 44
	int error;
	git_odb_stream *stream;
45

Vicent Marti committed
46
	if ((error = git_odb_open_wstream(&stream, repo->db, len, GIT_OBJ_BLOB)) < GIT_SUCCESS)
Vicent Marti committed
47
		return git__rethrow(error, "Failed to create blob");
48

49 50 51 52
	if ((error = stream->write(stream, buffer, len)) < GIT_SUCCESS) {
		stream->free(stream);
		return error;
	}
53

Vicent Marti committed
54 55
	error = stream->finalize_write(oid, stream);
	stream->free(stream);
56

57 58
	if (error < GIT_SUCCESS)
		return git__rethrow(error, "Failed to create blob");
Vicent Marti committed
59 60

	return GIT_SUCCESS;
61 62
}

63
int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *path)
64
{
65 66
	int error, islnk;
	int fd = 0;
Vicent Marti committed
67 68 69 70
	char full_path[GIT_PATH_MAX];
	char buffer[2048];
	git_off_t size;
	git_odb_stream *stream;
71 72
	struct stat st;

Vicent Marti committed
73
	if (repo->path_workdir == NULL)
Vicent Marti committed
74
		return git__throw(GIT_ENOTFOUND, "Failed to create blob. (No working directory found)");
75

Vicent Marti committed
76
	git_path_join(full_path, repo->path_workdir, path);
77

Vicent Marti committed
78
	error = p_lstat(full_path, &st);
79 80 81 82 83
	if (error < 0) {
		return git__throw(GIT_EOSERR, "Failed to stat blob. %s", strerror(errno));
	}

	islnk = S_ISLNK(st.st_mode);
84
	size = st.st_size;
85

86
	if (!islnk)
Vicent Marti committed
87
		if ((fd = p_open(full_path, O_RDONLY)) < 0)
88
			return git__throw(GIT_ENOTFOUND, "Failed to create blob. Could not open '%s'", full_path);
89

Vicent Marti committed
90
	if ((error = git_odb_open_wstream(&stream, repo->db, (size_t)size, GIT_OBJ_BLOB)) < GIT_SUCCESS) {
91
		if (!islnk)
Vicent Marti committed
92
			p_close(fd);
93
		return git__rethrow(error, "Failed to create blob");
Vicent Marti committed
94
	}
95

Vicent Marti committed
96 97
	while (size > 0) {
		ssize_t read_len;
98

99
		if (!islnk)
Vicent Marti committed
100
			read_len = p_read(fd, buffer, sizeof(buffer));
101
		else
Vicent Marti committed
102
			read_len = p_readlink(full_path, buffer, sizeof(buffer));
103

Vicent Marti committed
104
		if (read_len < 0) {
105
			if (!islnk)
Vicent Marti committed
106
				p_close(fd);
Vicent Marti committed
107
			stream->free(stream);
108
			return git__throw(GIT_EOSERR, "Failed to create blob. Can't read full file");
Vicent Marti committed
109 110 111 112 113 114 115 116
		}

		stream->write(stream, buffer, read_len);
		size -= read_len;
	}

	error = stream->finalize_write(oid, stream);
	stream->free(stream);
117
	if (!islnk)
Vicent Marti committed
118
		p_close(fd);
Vicent Marti committed
119

120
	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to create blob");
121 122
}