dtagfs/source/filesystem.d

205 lines
4.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 05:00:19 +02:00
import std.string;
import std.stdio;
import std.typecons;
2016-10-16 02:49:56 +02:00
2016-10-16 05:26:45 +02:00
import core.sys.posix.unistd;
2016-11-08 02:33:52 +01:00
import core.stdc.string;
import core.stdc.errno;
2016-10-16 05:26:45 +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 stat_t _sourceStat;
2016-10-16 02:49:56 +02:00
private string[][string] _tagCache;
private string[][string] _dirCache;
2016-10-16 02:49:56 +02:00
private string[] _tagList;
2016-11-08 02:33:52 +01:00
private string[string] _fileLinkCache;
2016-10-24 02:27:58 +02:00
2016-11-14 05:23:58 +01:00
private bool _noCommon;
enum exclusionChar = '!';
2016-11-14 05:23:58 +01:00
this(string source, TagProvider[] tagProviders, bool noCommon)
2016-10-16 01:03:31 +02:00
{
2016-10-16 02:49:56 +02:00
_source = source;
_tagProviders = tagProviders;
2016-11-14 05:23:58 +01:00
_noCommon = noCommon;
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))
{
2016-11-08 05:16:24 +01:00
auto tags = tagProvider.getTags(file);
_tagCache[file] ~= tags.map!(tag => tag.replace("/", "")).array; //replace '/' (directory separator) with full-width solidus
2016-11-08 02:33:52 +01:00
_fileLinkCache[file.baseName] = file;
2016-10-16 02:49:56 +02:00
}
}
_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
//excluded tags are also valid directories
if(path == "/" || isTag(path.baseName.stripLeft(exclusionChar)))
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))
{
stat.st_mode = S_IFLNK | octal!777;
stat.st_nlink = 1;
2016-10-16 04:03:39 +02:00
}
2016-10-24 02:27:58 +02:00
else
{
throw new FuseException(ENOENT);
2016-10-24 02:27:58 +02:00
}
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)
{
2016-11-08 02:33:52 +01:00
return (name in _fileLinkCache) !is null;
2016-10-16 04:03:39 +02:00
}
2016-10-16 05:00:19 +02:00
string findFile(const(char)[] name)
{
if(!isFile(name))
{
return null;
}
2016-11-08 02:33:52 +01:00
return _fileLinkCache[name];
2016-10-16 05:00:19 +02:00
}
2016-11-29 19:36:01 +01:00
Tuple!(bool, "exclude", string, "name")[] pathToTagTuples(const(char)[] path)
{
//map the path to a range of tuples with an exclude (bool) and a name (string) field for each tag in the path
return pathSplitter(path)
.array[1..$]
.map!(tag => tuple!("exclude", "name")(tag[0] == exclusionChar, cast(immutable)tag.stripLeft(exclusionChar)))
.array;
}
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
{
2016-11-29 19:36:01 +01:00
auto tags = pathToTagTuples(path);
2016-10-16 04:03:39 +02:00
2016-11-29 19:36:01 +01:00
//filter pairs with tags that (should not be excluded && can be found) || (should be excluded && can't be found)
auto filePairs = _tagCache.byKeyValue().filter!(a => tags.all!(b => !b.exclude == a.value.canFind(b.name)));
2016-11-14 05:23:58 +01:00
return filePairs.map!(a => a.value)
2016-10-16 04:03:39 +02:00
.joiner
2016-11-29 19:36:01 +01:00
.filter!(a => !tags.map!(tag => tag.name).canFind(a)) //don't match tags that are already in the path
2016-11-14 05:23:58 +01:00
.filter!(a => !_noCommon || !filePairs.all!(f => f.value.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
{
2016-11-29 19:36:01 +01:00
auto tags = pathToTagTuples(path);
2016-10-16 04:03:39 +02:00
//filter files with tags that (should not be excluded && can be found) || (should be excluded && can't be found)
2016-10-16 04:03:39 +02:00
return _tagCache.byKeyValue()
.filter!(a => tags.all!(b => !b.exclude == a.value.canFind(b.name)))
2016-10-16 04:03:39 +02:00
.map!(a => a.key.baseName)
.array;
}
}
override ulong readlink(const(char)[] path, ubyte[] buf)
{
auto realPath = findFile(path.baseName);
if(realPath is null)
{
throw new FuseException(ENOENT);
}
2016-11-08 02:33:52 +01:00
strncpy(cast(char*)buf, cast(char*)realPath.toStringz, realPath.length);
2016-11-08 02:33:52 +01:00
return realPath.length;
}
2016-11-08 03:17:21 +01:00
override bool access(const(char)[] path, int mode)
2016-11-08 04:35:22 +01:00
{
2016-11-08 03:17:21 +01:00
//TODO: Check if this should always be true
return true;
2016-11-08 04:35:22 +01:00
}
2016-11-08 03:17:21 +01:00
2016-10-16 02:49:56 +02:00
override string[] readdir(const(char)[] path)
{
if(path in _dirCache)
{
return _dirCache[path];
}
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
}
}