voile.fs

ファイルシステムヘルパー
static pure @safe string[] splitPath(string path);
パスを分解してパンくずリストにする
Parameters:
string path 変換したい絶対/相対パス
static pure @safe string joinPath(string[] path, string delimiter = dirSeparator);
パンくずリストをPosixパスに再構築する
Parameters:
string[] path 変換したい絶対/相対パス
string delimiter パスの区切り文字を指定する
static pure @safe string joinWindowsPath(string[] path);
パンくずリストをWindowsパスに再構築する
Parameters:
string[] path 変換したい絶対/相対パス
static pure @safe string joinPosixPath(string[] path);
パンくずリストをPosixパスに再構築する
Parameters:
string[] path 変換したい絶対/相対パス
string posixPath(string path);
パンをPosixパスに変換する(/を使うように変換)
Parameters:
string path 変換したい絶対/相対パス
string windowsPath(string path);
パンをWindowsパスに変換する(\を使うように変換)
Parameters:
string path 変換したい絶対/相対パス
alias joinNativePath = joinPosixPath;
alias nativePath = posixPath;
In Posix
struct FileSystem;
ファイルシステムの操作に関するヘルパ
Examples: コピー禁止とムーブ、インスタンス削除の例
import std.algorithm: move;
string[] msgDestroyed;
{
	FileSystem fs1 = FileSystem(".");
	FileSystem fs2;
	static assert(!__traits(compiles, fs2 = fs1));
	static assert(__traits(compiles, fs2 = fs1.move()));
	fs1.onDestroyed ~= (string x) { msgDestroyed ~= "fs1"; };
	fs2.onDestroyed ~= (string x) { msgDestroyed ~= "fs2"; };
}
assert(msgDestroyed == ["fs1"]);
string workDir;
Handler!(void delegate(string target, uint retrycnt)) onCreating;
作成前に呼ばれる。作成しない場合は例外を投げる
Handler!(void delegate(string target)) onCreated;
作成後に呼ばれる。
Handler!(bool delegate(string target, Exception e)) onCreateFailed;
作成に失敗したら呼ばれる。処理を継続を継続するならtrueを返す。
Handler!(void delegate(string target, uint retrycnt)) onCopying;
コピー前に呼ばれる。コピーしない場合は例外を投げる。
Handler!(void delegate(string src, string target)) onCopied;
コピー後に呼ばれる。
Handler!(bool delegate(string src, string target, Exception e)) onCopyFailed;
コピー失敗したら呼ばれる。処理を継続を継続するならtrueを返す。
Handler!(void delegate(string target, uint retrycnt)) onRemoving;
削除前に呼ばれる。削除しない場合は例外を投げる。
Handler!(void delegate(string target)) onRemoved;
削除後に呼ばれる。
Handler!(bool delegate(string target, Exception e)) onRemoveFailed;
削除に失敗したら呼ばれる。処理を継続を継続するならtrueを返す。
Handler!(void delegate(string target)) onDestroyed;
インスタンスが破棄される際に呼ばれる
void onPostMove();
ムーブの後に呼ばれる
const @safe string absolutePath();
const @safe string absolutePath(string target);
const @safe string absolutePath(string[] targetPath);
const @safe string absolutePath(string target, string base);
const @safe string absolutePath(string[] targetPath, string base);
絶対パスに変換する
Parameters:
string target 変換したい相対パス(何も指定しないとworkDirの絶対パスが返る)
string[] targetPath 変換したい相対パスのパンくずリスト
string base 基準となるパス(このパスの基準はworkDir)
const @safe string actualPath(string path = ".");
実際のパス名に修正する
const @safe string relativePath(string target);
const @safe string relativePath(string[] targetPath);
const @safe string relativePath(string target, string base);
const @safe string relativePath(string[] targetPath, string base);
相対パスに変換する
Parameters:
string target 変換したい絶対/相対パス
string[] targetPath 変換したい相対パスのパンくずリスト
string base 基準となるパス(このパスの基準はworkDir)
const @safe string[] buildSplittedPath(string path);
const @safe string[] buildSplittedPath(string[] paths);
const @safe string[] buildSplittedPath();
パスをパンくず表現に変換
Parameters:
string path 変換したい絶対/相対パス
string[] paths 変換したい相対パスのパンくずリスト
const @safe string buildPosixPath(string path);
const @safe string buildPosixPath(string[] paths);
const @safe string buildPosixPath();
パスをPosix表現に変換
Parameters:
string path 変換したい絶対/相対パス
string[] paths 変換したい相対パスのパンくずリスト
Examples: ditto
auto fs = FileSystem("ut\\test");
assert(fs.buildPosixPath() == "ut/test");
auto posixPath = fs.buildPosixPath("path\\to/file");
assert(posixPath == "ut/test/path/to/file");

auto absPosixPath = fs.buildPosixPath("/path\\to/file");
assert(absPosixPath == "/path/to/file");
const @safe string buildWindowsPath(string path);
const @safe string buildWindowsPath(string[] paths);
const @safe string buildWindowsPath();
パスをWindows表現に変換
Parameters:
string path 変換したい絶対/相対パス
string[] paths 変換したい相対パスのパンくずリスト
Examples: ditto
auto fs = FileSystem("ut/test");
assert(fs.buildWindowsPath() == "ut\\test");
auto posixPath = fs.buildWindowsPath("path\\to/file");
assert(posixPath == "ut\\test\\path\\to\\file");
const @safe string[] buildNormalizedSplittedPath(string path);
const @safe string[] buildNormalizedSplittedPath(string[] paths);
const @safe string[] buildNormalizedSplittedPath();
パスを正規化して分解してパンくずリストにする
Parameters:
string path 変換したい絶対/相対パス
string[] paths 変換したい相対パスのパンくずリスト
string buildNormalizedPosixPath(string path);
string buildNormalizedPosixPath(string[] paths);
string buildNormalizedPosixPath();
パスを正規化してPosix表現に変換
Parameters:
string path 変換したい絶対/相対パス
string[] paths 変換したい相対パスのパンくずリスト
Examples: ditto
auto fs = FileSystem("ut\\test");
assert(fs.buildNormalizedPosixPath() == "ut/test");
auto posixPath = fs.buildNormalizedPosixPath("../path\\to/file");
assert(posixPath == "ut/path/to/file");
string buildNormalizedWindowsPath(string path);
string buildNormalizedWindowsPath(string[] paths);
string buildNormalizedWindowsPath();
パスを正規化してWindows表現に変換
Parameters:
string path 変換したい絶対/相対パス
string[] paths 変換したい相対パスのパンくずリスト
Examples: ditto
auto fs = FileSystem("ut\\./test");
assert(fs.buildNormalizedWindowsPath() == "ut\\test");
auto posixPath = fs.buildNormalizedWindowsPath("../path\\to/file");
assert(posixPath == "ut\\path\\to\\file");
alias buildNativePath = buildPosixPath;
alias buildNormalizedNativePath = buildNormalizedPosixPath;
In Posix
const @safe bool exists(string target = ".");
パスが存在するか確認する
Parameters:
string target パス
const @safe bool isFile(string target);
パスがファイルかどうか確認する
Parameters:
string target パス
const @safe bool isDir(string target);
パスがファイルかどうか確認する
Parameters:
string target パス
@safe void makeDir(string target, bool force = true, uint retrycnt = 5);
ディレクトリを作成する
Parameters:
string target パス
bool force 強制的に作成
uint retrycnt リトライする回数
auto entries(SpanMode mode, bool followSymlink = true);
auto entries(string pattern, SpanMode mode = SpanMode.shallow, bool followSymlink = true);
auto entries(RE)(RE pattern, SpanMode mode = SpanMode.shallow, bool followSymlink = true)
if(is(typeof(std.regex.matchFirst("", pattern))));
エントリー一覧
Examples:
auto fs = createDisposableDir("ut");
fs.writeText("a/b/test1.txt", "Test");
fs.writeText("a/c/test2.txt", "Test");
fs.writeText("a/b/test3.txt", "Test");
string[] files;
foreach (de; fs.entries(SpanMode.depth))
{
	files ~= de.name;
	assert(de.name.isAbsolute);
}
assert(files.length == 6);
import std.algorithm: sort;
files.sort();
assert(fs.relativePath(files[0]).splitPath() == ["a"]);
assert(fs.relativePath(files[1]).splitPath() == ["a", "b"]);
assert(fs.relativePath(files[2]).splitPath() == ["a", "b", "test1.txt"]);
assert(fs.relativePath(files[3]).splitPath() == ["a", "b", "test3.txt"]);
assert(fs.relativePath(files[4]).splitPath() == ["a", "c"]);
assert(fs.relativePath(files[5]).splitPath() == ["a", "c", "test2.txt"]);

files = null;
foreach (de; fs.entries(regex(r"test\d.txt"), SpanMode.depth))
{
	files ~= de.name;
	assert(de.name.isAbsolute);
}
assert(files.length == 3);
files.sort();
assert(fs.relativePath(files[0]).splitPath() == ["a", "b", "test1.txt"]);
assert(fs.relativePath(files[1]).splitPath() == ["a", "b", "test3.txt"]);
assert(fs.relativePath(files[2]).splitPath() == ["a", "c", "test2.txt"]);
void writeText(string filename, in char[] text);
テキストファイルを書き出す
string readText(string filename);
テキストファイルを読み込む
void writeBinary(string filename, in ubyte[] binary);
バイナリファイルを書き出す
immutable(ubyte)[] readBinary(string filename);
バイナリファイルを読み込む
void writeJson(T)(string filename, in T data, JSONOptions options = JSONOptions.none);
JSONファイルを書き出す
T readJson(T)(string filename);
JSONファイルを書き出す
Examples:
auto fs = createDisposableDir("ut");
fs.writeJson!uint("a/b/test.json", 10);
assert(fs.readJson!uint("a/b/test.json") == 10);
Examples: ditto
import voile.json;
auto fs = createDisposableDir("ut");
static struct A
{
	int a = 123;
	JSONValue json() const @property
	{
		JSONValue v;
		v.setValue("a", a);
		return v;
	}
	void json(JSONValue v) @property
	{
		assert(v.type == JSONType.object);
		a = v.getValue("a", 123);
	}
}
fs.writeJson("a/b/test.json", A(10));
assert(fs.readJson!A("a/b/test.json") == A(10));
File createFile(string filename);
ファイルを新しく作成する
File openFile(string filename, string attr = "r+");
ファイルを開く
ファイルがなければ新しく作成して開く
const bool clearReadonly(string target);
@safe bool removeFiles(string target, bool force = true, uint retrycnt = 5);
bool removeFiles(string targetDir, string blobFilter, bool force = true, uint retrycnt = 5);
bool copyFile(string srcFile, string targetFile, bool force = true, uint retrycnt = 5);
ファイルをコピーする
bool copyFiles(string src, string target, bool force = true, uint retrycnt = 5);
Examples:
auto fs = createDisposableDir("ut");
assert(!fs.isFile("a/b/c.txt"));
fs.writeText("a/b/c.txt", "aaaaa");
fs.copyFiles("a/b/c.txt", "a/c/c.txt");
assert(fs.isFile("a/c/c.txt"));
Examples:
auto fs = createDisposableDir("ut");
assert(!fs.isFile("a/b/c.txt"));
fs.writeText("a/b/c.txt", "aaaaa");
fs.copyFiles("a/b", "a/c");
assert(fs.isFile("a/c/c.txt"));
bool copyFiles(string srcDir, string blobFilter, string targetDir, bool force = true, uint retrycnt = 5);
Examples:
auto fs = createDisposableDir("ut");
assert(!fs.isFile("a/b/c.txt"));
assert(!fs.isFile("a/b/d.dat"));
fs.writeText("a/b/c.txt", "aaaaa");
fs.writeBinary("a/b/d.dat", [1,2,3]);
fs.copyFiles("a/b", "*.txt", "a/c");
assert(fs.isFile("a/c/c.txt"));
assert(!fs.isFile("a/c/d.dat"));
bool mirrorFiles(string srcDir, string dstDir, bool force = true, uint retrycnt = 5);
ファイルをミラーリングする
bool moveFiles(string src, string dst, bool force = true, bool retrycnt = true);
ファイルを移動する
void symlink(in char[] target, in char[] link);
シンボリックリンクを作成する
string readLink(in char[] link);
シンボリックリンクの実パスを得る
void setTimeStamp(string target, SysTime accessTime, SysTime modificationTime);
void setTimeStamp(string target, SysTime modificationTime);
void setTimeStamp(string target, DateTime accessTime, DateTime modificationTime);
void setTimeStamp(string target, DateTime modificationTime);
void getTimeStamp(string target, out SysTime accessTime, out SysTime modificationTime);
void getTimeStamp(string target, out SysTime modificationTime);
void getTimeStamp(string target, out DateTime accessTime, out DateTime modificationTime);
void getTimeStamp(string target, out DateTime modificationTime);
SysTime getTimeStamp(string target);
タイムスタンプを変更/取得する
const @trusted string searchPath(in char[] executable, in string[] additional = null);
パスを検索する
auto spawnProcess(string[] args, string[string] env = null, std.process.Config cfg = std.process.Config.none);
auto spawnProcess(string[] args, File fin, File fout, File ferr, string[string] env = null, std.process.Config cfg = std.process.Config.suppressConsole);
auto spawnProcess(string[] args, Pipe pin, Pipe pout, Pipe perr, string[string] env = null, std.process.Config cfg = std.process.Config.suppressConsole);
auto pipeProcess(string[] args, string[string] env = null, std.process.Config cfg = std.process.Config.suppressConsole);
auto execute(string[] args, string[string] env = null, std.process.Config cfg = std.process.Config.suppressConsole);
プロセスを実行する
Examples:
import std.string;
auto fs = createDisposableDir("ut");
auto pipeo = pipe();
version (Windows)
{
	auto cmd = ["cmd", "/C", "echo xxx"];
}
else
{
	auto cmd = ["bash", "-c", "echo xxx"];
}
auto pid = fs.spawnProcess(cmd, Pipe.init, pipeo, pipeo, ["Path": fs.absolutePath]);
pid.wait();
auto xxx = pipeo.readEnd().readln;
assert(xxx.chomp == "xxx");
Examples:
import std.string;
auto fs = createDisposableDir("ut");
auto pipeo = pipe();
version (Windows)
{
	auto cmd = ["cmd", "/C", "echo xxx"];
}
else
{
	auto cmd = ["bash", "-c", "echo xxx"];
}
auto pipe = fs.pipeProcess(cmd, ["Path": fs.absolutePath]);
pipe.pid.wait();
auto xxx = pipe.stdout().readln;
assert(xxx.chomp == "xxx");
Examples:
import std.string;
auto fs = createDisposableDir("ut");
auto tout = fs.createFile("test.txt");
version (Windows)
{
	auto cmd = ["cmd", "/C", "echo xxx"];
}
else
{
	auto cmd = ["bash", "-c", "echo xxx"];
}
auto pid = fs.spawnProcess(cmd, File.init, tout, File.init, ["Path": fs.absolutePath]);
pid.wait();
tout.close();
auto result = fs.readText("test.txt");
assert(result.chomp == "xxx");
Examples:
import std.string;
auto fs = createDisposableDir("ut");
version (Windows)
{
	auto cmd = ["cmd", "/C", "echo xxx"];
}
else
{
	auto cmd = ["bash", "-c", "echo xxx"];
}
auto result = fs.execute(cmd, ["Path": fs.absolutePath]);
assert(result.output.chomp == "xxx");
@safe FileSystem createTempDir(string basePath = tempDir, string prefix = "voile-", uint retrycnt = 5);
一時ディレクトリを作成し、作成したディレクトリのFileSystemを返す
@safe FileSystem createDisposableDir(string basePath = tempDir, string prefix = "voile-", uint retrycnt = 5);
使い捨ての一時ディレクトリを作成
作成したディレクトリのFileSystemを返す。返されたFileSystemは、インスタンスの破棄時に削除される。
Examples:
string dir;
{
	auto fs = createDisposableDir("ut");
	dir = fs.workDir;
}
assert(!dir.exists);