summaryrefslogtreecommitdiff
path: root/src/blob.cpp
blob: 285cd0bdc10af627b7bbc649cf2de2244fd48991 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "blob.h"
#include "repo.h"
#include <Ice/Current.h>
#include <Ice/ObjectAdapter.h>
#include <cerrno>
#include <exceptions.h>
#include <file.h>
#include <numeric.h>
#include <sys/stat.h>
#include <types.h>

GitFS::Blob::Blob(const Repo * const repo, const std::string & path) :
	repo(repo), entry(Git::treeEntryByPath(repo->tree, path)), blob(getBlob()),
	blobContent(static_cast<const ::Ice::Byte *>(git_blob_rawcontent(blob.get())), git_blob_rawsize(blob.get()))
{
}

GitFS::Git::BlobPtr
GitFS::Blob::getBlob() const
{
	const auto mode = git_tree_entry_filemode(entry.get());

	if (S_ISDIR(mode)) {
		throw NetFS::SystemError(EISDIR);
	}
	if (S_ISLNK(mode)) {
		throw NetFS::SystemError(ELOOP);
	}

	return Git::blobLookup(repo->repo, *git_tree_entry_id(entry.get()));
}

void
GitFS::Blob::close(const ::Ice::Current & ice)
{
	ice.adapter->remove(ice.id);
}

NetFS::Attr
GitFS::Blob::fgetattr(const ::Ice::Current &)
{
	NetFS::Attr attr;
	attr << *blob << *entry << *repo->commit;
	attr.gid = repo->gid;
	attr.uid = repo->uid;
	return attr;
}

NetFS::Buffer
GitFS::Blob::read(long long int offsetSized, long long int sizeLong, const ::Ice::Current &)
{
	const size_t offset = safe {offsetSized};
	if (offset > blobContent.size()) {
		return {};
	}
	const size_t size = safe {sizeLong};
	const auto len = std::min(blobContent.size() - offset, size);
	const auto range = blobContent.subspan(offset, len);
	return {range.begin(), range.end()};
}

void
GitFS::Blob::ftruncate(long long int, const ::Ice::Current &)
{
	throw NetFS::SystemError(EROFS);
}

void
GitFS::Blob::write(
		long long int, long long int, std::pair<const Ice::Byte *, const Ice::Byte *>, const ::Ice::Current &)
{
	throw NetFS::SystemError(EROFS);
}

long long int
GitFS::Blob::copyrange(FilePrxPtr, long long int, long long int, long long int, int, const Ice::Current &)
{
	throw NetFS::SystemError(EROFS);
}