dtagfs/source/filesystem.d

125 lines
2.2 KiB
D
Raw Normal View History

2016-10-16 01:03:31 +02:00
module dtagfs.filesystem;
2016-10-16 02:49:56 +02:00
import std.algorithm;
import std.range;
import std.file;
import std.conv;
2016-10-16 04:03:39 +02:00
import std.path;
import std.array;
2016-10-16 02:49:56 +02:00
2016-10-16 01:03:31 +02:00
import dfuse.fuse;
import dtagfs.tagprovider;
class FileSystem : Operations
{
2016-10-16 02:49:56 +02:00
private string _source;
private TagProvider[] _tagProviders;
private string[][string] _tagCache;
2016-10-16 01:03:31 +02:00
this(string source, TagProvider[] tagProviders)
{
2016-10-16 02:49:56 +02:00
_source = source;
_tagProviders = tagProviders;
cacheTags();
}
@property
TagProvider primaryTagProvider()
{
return _tagProviders[0];
}
void cacheTags()
{
foreach(tagProvider; _tagProviders.filter!(a => a.cacheReads))
{
foreach(file; dirEntries(_source, SpanMode.breadth).filter!(a => a.isFile))
{
_tagCache[file] ~= tagProvider.getTags(file);
}
}
}
override void getattr(const(char)[] path, ref stat_t stat)
{
2016-10-16 04:03:39 +02:00
if(path == "/" || isTag(path.baseName))
2016-10-16 02:49:56 +02:00
{
2016-10-16 04:03:39 +02:00
stat.st_mode = S_IFDIR | octal!700;
2016-10-16 02:49:56 +02:00
stat.st_size = 0;
return;
}
2016-10-16 04:03:39 +02:00
else if(isFile(path.baseName))
{
stat.st_mode = S_IFREG | octal!700;
stat.st_size = 42;
return;
}
2016-10-16 02:49:56 +02:00
throw new FuseException(errno.ENOENT);
}
2016-10-16 01:03:31 +02:00
2016-10-16 04:03:39 +02:00
bool isTag(const(char)[] name)
{
return _tagCache.values.any!(a => a.canFind(name));
}
bool isFile(const(char)[] name)
{
return _tagCache.keys.any!(a => a.baseName == name);
}
string[] getTags(const(char)[] path)
{
if(path == "/")
{
return _tagCache.byValue()
.joiner
.array
.sort()
.uniq
.array;
}
else
{
auto tags = pathSplitter(path).array[1..$];
return _tagCache.byKeyValue()
.filter!(a => tags.all!(b => a.value.canFind(b)))
.map!(a => a.value)
.joiner
2016-10-16 04:54:44 +02:00
.filter!(a => !tags.canFind(a))
2016-10-16 04:03:39 +02:00
.array
.sort()
.uniq
.array;
}
}
string[] getFiles(const(char)[] path)
{
if(path == "/")
{
return _tagCache.keys.map!(a => a.baseName).array;
}
else
{
auto tags = pathSplitter(path).array[1..$];
return _tagCache.byKeyValue()
.filter!(a => tags.all!(b => a.value.canFind(b)))
.map!(a => a.key.baseName)
.array;
}
}
2016-10-16 02:49:56 +02:00
override string[] readdir(const(char)[] path)
{
2016-10-16 04:03:39 +02:00
//TODO: Don't return tags if only one file (or files with exactly the same set of tags) files?
return getTags(path) ~ getFiles(path);
2016-10-16 01:03:31 +02:00
}
}