1 /*******************************************************************************
2 
3     An implementation of `Filesystem` using vibe.d functions
4 
5 *******************************************************************************/
6 
7 module dub.internal.io.realfs;
8 
9 public import dub.internal.io.filesystem;
10 
11 /// Ditto
12 public final class RealFS : Filesystem {
13     static import dub.internal.vibecompat.core.file;
14     static import std.file;
15 
16     ///
17     private NativePath path_;
18 
19     ///
20     public this (NativePath cwd = NativePath(std.file.getcwd()))
21         scope @safe pure nothrow @nogc
22     {
23         this.path_ = cwd;
24     }
25 
26     public override NativePath getcwd () const scope
27     {
28         return this.path_;
29     }
30 
31     ///
32     public override void chdir (in NativePath path) scope
33     {
34         std.file.chdir(path.toNativeString());
35     }
36 
37     ///
38     protected override bool existsDirectory (in NativePath path) const scope
39 	{
40 		return dub.internal.vibecompat.core.file.existsDirectory(path);
41 	}
42 
43 	/// Ditto
44 	protected override void mkdir (in NativePath path) scope
45 	{
46 		dub.internal.vibecompat.core.file.ensureDirectory(path);
47 	}
48 
49 	/// Ditto
50 	protected override bool existsFile (in NativePath path) const scope
51 	{
52 		return dub.internal.vibecompat.core.file.existsFile(path);
53 	}
54 
55 	/// Ditto
56 	protected override void writeFile (in NativePath path, const(ubyte)[] data)
57         scope
58 	{
59 		return dub.internal.vibecompat.core.file.writeFile(path, data);
60 	}
61 
62     /// Reads a file, returns the content as `ubyte[]`
63     public override ubyte[] readFile (in NativePath path) const scope
64     {
65         return cast(ubyte[]) std.file.read(path.toNativeString());
66     }
67 
68 	/// Ditto
69 	protected override string readText (in NativePath path) const scope
70 	{
71 		return dub.internal.vibecompat.core.file.readText(path);
72 	}
73 
74 	/// Ditto
75 	protected override IterateDirDg iterateDirectory (in NativePath path) scope
76 	{
77 		return dub.internal.vibecompat.core.file.iterateDirectory(path);
78 	}
79 
80 	/// Ditto
81 	protected override void removeFile (in NativePath path, bool force = false) scope
82 	{
83 		return std.file.remove(path.toNativeString());
84 	}
85 
86     ///
87     public override void removeDir (in NativePath path, bool force = false)
88     {
89         const str = path.toNativeString();
90         if (!std.file.exists(str)) return;
91         if (force)
92             std.file.rmdirRecurse(str);
93         else
94             std.file.rmdir(str);
95     }
96 
97 	/// Ditto
98 	protected override void setTimes (in NativePath path, in SysTime accessTime,
99 		in SysTime modificationTime)
100 	{
101 		std.file.setTimes(
102 			path.toNativeString(), accessTime, modificationTime);
103 	}
104 
105 	/// Ditto
106 	protected override void setAttributes (in NativePath path, uint attributes)
107 	{
108 		std.file.setAttributes(path.toNativeString(), attributes);
109 	}
110 }