1 /* 2 Copyright (c) 2017-2018 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 Permission is hereby granted, free of charge, to any person or organization 6 obtaining a copy of the software and accompanying documentation covered by 7 this license (the "Software") to use, reproduce, display, distribute, 8 execute, and transmit the Software, and to prepare derivative works of the 9 Software, and to permit third-parties to whom the Software is furnished to 10 do so, all subject to the following: 11 12 The copyright notices in the Software and this entire statement, including 13 the above license grant, this restriction and the following disclaimer, 14 must be included in all copies of the Software, in whole or in part, and 15 all derivative works of the Software, unless such copies or derivative 16 works are solely in the form of machine-executable object code generated by 17 a source language processor. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 DEALINGS IN THE SOFTWARE. 26 */ 27 28 module dagon.core.vfs; 29 30 import std.string; 31 import std.path; 32 import dlib.core.memory; 33 import dlib.core.stream; 34 import dlib.container.array; 35 import dlib.container.dict; 36 import dlib.filesystem.filesystem; 37 import dlib.filesystem.stdfs; 38 39 class StdDirFileSystem: ReadOnlyFileSystem 40 { 41 StdFileSystem stdfs; 42 string rootDir; 43 44 this(string rootDir) 45 { 46 this.rootDir = rootDir; 47 stdfs = New!StdFileSystem(); 48 } 49 50 bool stat(string filename, out FileStat stat) 51 { 52 string path = format("%s/%s", rootDir, filename); 53 return stdfs.stat(path, stat); 54 } 55 56 InputStream openForInput(string filename) 57 { 58 string path = format("%s/%s", rootDir, filename); 59 return stdfs.openForInput(path); 60 } 61 62 Directory openDir(string dir) 63 { 64 string path = format("%s/%s", rootDir, dir); 65 return stdfs.openDir(path); 66 } 67 68 ~this() 69 { 70 Delete(stdfs); 71 } 72 } 73 74 class VirtualFileSystem: ReadOnlyFileSystem 75 { 76 DynamicArray!ReadOnlyFileSystem mounted; 77 78 this() 79 { 80 } 81 82 void mount(string dir) 83 { 84 StdDirFileSystem fs = New!StdDirFileSystem(dir); 85 mounted.append(fs); 86 } 87 88 void mount(ReadOnlyFileSystem fs) 89 { 90 mounted.append(fs); 91 } 92 93 string containingDir(string filename) 94 { 95 string res; 96 foreach(i, fs; mounted) 97 { 98 if (cast(StdDirFileSystem)fs) 99 { 100 FileStat s; 101 if (fs.stat(filename, s)) 102 { 103 res = (cast(StdDirFileSystem)fs).rootDir; 104 break; 105 } 106 } 107 } 108 return res; 109 } 110 111 bool stat(string filename, out FileStat stat) 112 { 113 bool res = false; 114 foreach(i, fs; mounted) 115 { 116 FileStat s; 117 if (fs.stat(filename, s)) 118 { 119 stat = s; 120 res = true; 121 break; 122 } 123 } 124 125 return res; 126 } 127 128 InputStream openForInput(string filename) 129 { 130 foreach(i, fs; mounted) 131 { 132 FileStat s; 133 if (fs.stat(filename, s)) 134 { 135 return fs.openForInput(filename); 136 } 137 } 138 139 return null; 140 } 141 142 Directory openDir(string path) 143 { 144 // TODO 145 return null; 146 } 147 148 ~this() 149 { 150 foreach(i, fs; mounted) 151 Delete(fs); 152 mounted.free(); 153 } 154 }