dtagfs/source/filesystem.d

187 lines
3.3 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 05:00:19 +02:00
import std.string;
import std.stdio;
2016-10-16 02:49:56 +02:00
2016-10-16 05:26:45 +02:00
import core.sys.posix.unistd;
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 stat_t _sourceStat;
2016-10-16 02:49:56 +02:00
private string[][string] _tagCache;
private stat_t[string] _statCache;
private string[][string] _dirCache;
2016-10-16 02:49:56 +02:00
private string[] _tagList;
2016-10-24 02:27:58 +02:00
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;
2016-10-24 02:27:58 +02:00
lstat(toStringz(source), &_sourceStat);
2016-10-16 05:26:45 +02:00
2016-10-16 02:49:56 +02:00
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);
}
}
_tagList = _tagCache.byValue()
.joiner
.array
.sort()
.uniq
.array;
2016-10-16 02:49:56 +02:00
}
override void getattr(const(char)[] path, ref stat_t stat)
{
2016-10-24 02:27:58 +02:00
stat.st_uid = _sourceStat.st_uid;
stat.st_gid = _sourceStat.st_gid;
2016-10-16 05:26:45 +02:00
2016-10-16 04:03:39 +02:00
if(path == "/" || isTag(path.baseName))
2016-10-16 02:49:56 +02:00
{
2016-10-24 02:27:58 +02:00
stat.st_mode = _sourceStat.st_mode;
2016-10-16 02:49:56 +02:00
}
2016-10-16 04:03:39 +02:00
else if(isFile(path.baseName))
{
if(path.baseName in _statCache)
{
stat = _statCache[path.baseName];
}
else
{
auto file = findFile(path.baseName);
lstat(toStringz(file), &stat);
_statCache[path.baseName] = stat;
}
2016-10-16 04:03:39 +02:00
}
2016-10-24 02:27:58 +02:00
else
{
throw new FuseException(errno.ENOENT);
}
2016-10-16 02:49:56 +02:00
}
2016-10-16 01:03:31 +02:00
2016-10-16 04:03:39 +02:00
bool isTag(const(char)[] name)
{
return _tagList.canFind(name);
2016-10-16 04:03:39 +02:00
}
bool isFile(const(char)[] name)
{
return _tagCache.keys.any!(a => a.baseName == name);
}
2016-10-16 05:00:19 +02:00
string findFile(const(char)[] name)
{
if(!isFile(name))
{
return null;
}
return _tagCache.keys.filter!(a => indexOf(a, name) != -1).array[0];
}
2016-10-16 04:03:39 +02:00
string[] getTags(const(char)[] path)
{
if(path == "/")
{
return _tagList;
2016-10-16 04:03:39 +02:00
}
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)
{
if(path in _dirCache)
{
return _dirCache[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?
2016-10-16 05:00:19 +02:00
if (path == "/")
{
return _dirCache[path] = getTags(path) ~ getFiles(path);
2016-10-16 05:00:19 +02:00
}
else
{
return _dirCache[path] = getTags(path) ~ getFiles(path) ~ [".", ".."];
2016-10-16 05:00:19 +02:00
}
2016-10-16 01:03:31 +02:00
}
2016-10-16 05:00:19 +02:00
override ulong read(const(char)[] path, ubyte[] buf, ulong offset)
{
auto realPath = findFile(path.baseName);
2016-10-24 02:27:58 +02:00
if(realPath is null)
2016-10-16 05:00:19 +02:00
{
throw new FuseException(errno.ENOENT);
}
auto file = File(realPath, "r");
file.seek(offset);
auto bytesRead = file.rawRead(buf).length;
file.close();
return bytesRead;
}
2016-10-16 01:03:31 +02:00
}