summaryrefslogtreecommitdiff
path: root/src/dir.cpp
blob: f502c45f06a39b699fdbd93c854c48f45931eeb0 (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
#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 r, std::string && p) : repo(r), path(std::move(p)), 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 e = Git::TreeEntryByPath(repo->tree, path);
			if (!S_ISDIR(git_tree_entry_filemode(e.get()))) {
				throw NetFS::SystemError(ENOTDIR);
			}
			subTreeCache = Git::TreeLookup(repo->repo, *git_tree_entry_id(e.get()));
		}
		subTreeCacheRootId = *git_tree_id(repo->tree.get());
	}
	return subTreeCache;
}

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

NetFS::NameList
GitFS::Directory::readdir(const ::Ice::Current &)
{
	const auto subTree = getSubtree();
	NetFS::NameList list;
	for (auto idx = git_tree_entrycount(subTree.get()); idx--;) {
		const auto entry = git_tree_entry_byindex(subTree.get(), idx);
		list.push_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 a {};
		a << *entry << *repo->commit;
		if (S_ISREG(git_tree_entry_filemode(entry))) {
			auto blob = Git::BlobLookup(repo->repo, *git_tree_entry_id(entry));
			a << *blob;
		}
		a.gid = repo->gid;
		a.uid = repo->uid;
		list.emplace(git_tree_entry_name(entry), a);
	}
	return list;
}