summaryrefslogtreecommitdiff
path: root/src/dir.cpp
blob: b2de7804ffc07b9a888e998433b783495fd34502 (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 "dir.h"
#include "git.h"
#include "repo.h"
#include <Ice/Current.h>
#include <Ice/ObjectAdapter.h>
#include <cerrno>
#include <exceptions.h>
#include <git2.h>
#include <memory>
#include <string>
#include <sys/stat.h>
#include <types.h>
#include <utility>

GitFS::Directory::Directory(Repo * const repo, std::string path) :
	repo(repo), path(std::move(path)), subTreeCacheRootId({})
{
	getSubtree();
}

GitFS::Git::TreePtr
GitFS::Directory::getSubtree() const
{
	repo->update();
	if (!git_oid_equal(&subTreeCacheRootId, git_tree_id(repo->tree.get()))) {
		if (path == "/") {
			subTreeCache = repo->tree;
		}
		else {
			auto entry = Git::treeEntryByPath(repo->tree, path);
			if (!S_ISDIR(git_tree_entry_filemode(entry.get()))) {
				throw NetFS::SystemError(ENOTDIR);
			}
			subTreeCache = Git::treeLookup(repo->repo, *git_tree_entry_id(entry.get()));
		}
		subTreeCacheRootId = *git_tree_id(repo->tree.get());
	}
	return subTreeCache;
}

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

NetFS::NameList
GitFS::Directory::readdir(const ::Ice::Current &)
{
	const auto subTree = getSubtree();
	NetFS::NameList list;
	auto idx = git_tree_entrycount(subTree.get());
	list.reserve(idx);
	while (idx--) {
		const auto entry = git_tree_entry_byindex(subTree.get(), idx);
		list.emplace_back(git_tree_entry_name(entry));
	}
	return list;
}

NetFS::DirectoryContents
GitFS::Directory::listdir(const ::Ice::Current &)
{
	const auto subTree = getSubtree();
	NetFS::DirectoryContents list;
	for (auto idx = git_tree_entrycount(subTree.get()); idx--;) {
		const auto entry = git_tree_entry_byindex(subTree.get(), idx);
		NetFS::Attr attr {};
		attr << *entry << *repo->commit;
		if (S_ISREG(git_tree_entry_filemode(entry))) {
			auto blob = Git::blobLookup(repo->repo, *git_tree_entry_id(entry));
			attr << *blob;
		}
		attr.gid = repo->gid;
		attr.uid = repo->uid;
		list.emplace(git_tree_entry_name(entry), std::move(attr));
	}
	return list;
}