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.resource.boxfs;
29 
30 import std.stdio;
31 import std.datetime;
32 import std.algorithm;
33 
34 import dlib.core.memory;
35 import dlib.core.stream;
36 import dlib.filesystem.filesystem;
37 import dlib.container.dict;
38 import dlib.container.array;
39 import dlib.text.utils;
40 
41 struct BoxEntry
42 {
43     ulong offset;
44     ulong size;
45 }
46 
47 class UnmanagedArrayStream: ArrayStream
48 {
49     ubyte[] buffer;
50 
51     this(ubyte[] data)
52     {
53         super(data, data.length);
54         buffer = data;
55     }
56 
57     ~this()
58     {
59         Delete(buffer);
60     }
61 }
62 
63 class BoxFileSystem: ReadOnlyFileSystem
64 {
65     InputStream boxStrm;
66     string rootDir = "";
67     Dict!(BoxEntry, string) files;
68     DynamicArray!string filenames;
69     bool deleteStream = false;
70     
71     this(ReadOnlyFileSystem fs, string filename, string rootDir = "")
72     {        
73         this(fs.openForInput(filename), true, rootDir);
74     }
75 
76     this(InputStream istrm, bool deleteStream = false, string rootDir = "")
77     {
78         this.deleteStream = deleteStream;
79         this.rootDir = rootDir;
80         this.boxStrm = istrm;
81 
82         ubyte[4] magic;
83         boxStrm.fillArray(magic);
84         assert(magic == "BOXF");
85 
86         ulong numFiles;
87         boxStrm.readLE(&numFiles);
88 
89         files = New!(Dict!(BoxEntry, string));
90 
91         string rootDirWithSeparator;
92         if (rootDir.length)
93             rootDirWithSeparator = catStr(rootDir, "/");
94 
95         foreach(i; 0..numFiles)
96         {
97             uint filenameSize;
98             boxStrm.readLE(&filenameSize);
99             ubyte[] filenameBytes = New!(ubyte[])(filenameSize);
100             boxStrm.fillArray(filenameBytes);
101             string filename = cast(string)filenameBytes;
102             ulong offset, size;
103             boxStrm.readLE(&offset);
104             boxStrm.readLE(&size);
105 
106             if (rootDirWithSeparator.length)
107             {
108                 if (filename.startsWith(rootDirWithSeparator))
109                 {
110                     string newFilename = filename[rootDirWithSeparator.length..$];
111                     filenames.append(filename);
112                     files[newFilename] = BoxEntry(offset, size);
113                 }
114                 else
115                     Delete(filenameBytes);
116             }
117             else
118             {
119                 filenames.append(filename);
120                 files[filename] = BoxEntry(offset, size);
121             }
122         }
123 
124         if (rootDirWithSeparator.length)
125             Delete(rootDirWithSeparator);
126     }
127 
128     bool stat(string filename, out FileStat stat)
129     {
130         if (filename in files)
131         {
132             stat.isFile = true;
133             stat.isDirectory = false;
134             stat.sizeInBytes = files[filename].size;
135             stat.creationTimestamp = SysTime.init;
136             stat.modificationTimestamp = SysTime.init;
137 
138             return true;
139         }
140         else
141             return false;
142     }
143 
144     InputStream openForInput(string filename)
145     {
146         if (filename in files)
147         {
148             BoxEntry file = files[filename];
149             ubyte[] buffer = New!(ubyte[])(cast(size_t)file.size);
150             boxStrm.position = file.offset;
151             boxStrm.fillArray(buffer);
152             return New!UnmanagedArrayStream(buffer);
153         }
154         else
155             return null;
156     }
157 
158     Directory openDir(string dir)
159     {
160         // TODO
161         return null;
162     }
163 
164     ~this()
165     {
166         foreach(f; filenames)
167             Delete(f);
168         filenames.free();
169         Delete(files);
170         if (deleteStream)
171             Delete(boxStrm);
172     }
173 }
174