From 1036d056466e66567859e386e22dcdab07e3e48c Mon Sep 17 00:00:00 2001 From: Andrei Alexandrescu Date: Fri, 16 May 2008 21:07:50 +0000 Subject: [PATCH] Changed Boolean function signatures (e.g. exists) to return bool instead of int. Got rid of some gotos. Added the readText, lastModified, mkdirRecurse, and rmdirRecurse functions. --- std/file.d | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/std/file.d b/std/file.d index 7870ee23d..47ffff32d 100644 --- a/std/file.d +++ b/std/file.d @@ -940,7 +940,7 @@ void[] read(string name) * */ -S readText(S)(in string name) +S readText(S = string)(in string name) { auto result = cast(S) read(name); std.utf.validate(result); @@ -1199,6 +1199,17 @@ void mkdir(string pathname) cenforce(std.c.linux.linux.mkdir(toStringz(pathname), 0777) == 0, pathname); } +/**************************************************** + * Make directory and all parent directories as needed. + */ + +void mkdirRecurse(string pathname) +{ + invariant left = dirname(pathname); + exists(left) || mkdirRecurse(left); + mkdir(pathname); +} + /**************************************************** * Remove directory. */ @@ -1208,6 +1219,31 @@ void rmdir(string pathname) cenforce(std.c.linux.linux.rmdir(toStringz(pathname)) == 0, pathname); } +/**************************************************** +Remove directory and all of its content and subdirectories, +recursively. + */ + +void rmdirRecurse(string pathname) +{ + // all children, recursively depth-first + foreach (DirEntry e; dirEntries(pathname, SpanMode.depth)) + { + e.isdir ? rmdir(e.name) : remove(e.name); + } + // the dir itself + rmdir(pathname); +} + +unittest +{ + auto d = "/tmp/deleteme/a/b/c/d/e/f/g"; + enforce(collectException(mkdir(d))); + mkdirRecurse(d); + rmdirRecurse("/tmp/deleteme"); + enforce(!exists("/tmp/deleteme")); +} + /**************************************************** * Get current directory. */