Line data Source code
1 : /* Copyright (C) 2021 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : /*
24 : * interface for reading from and creating archives.
25 : */
26 :
27 : #ifndef INCLUDED_ARCHIVE
28 : #define INCLUDED_ARCHIVE
29 :
30 : #include "lib/file/file_system.h" // FileInfo
31 : #include "lib/file/common/file_loader.h"
32 : #include "lib/file/vfs/vfs_path.h"
33 :
34 : // rationale: this module doesn't build a directory tree of the entries
35 : // within an archive. that task is left to the VFS; here, we are only
36 : // concerned with enumerating all archive entries.
37 :
38 : namespace ERR
39 : {
40 : const Status ARCHIVE_UNKNOWN_FORMAT = -110400;
41 : const Status ARCHIVE_UNKNOWN_METHOD = -110401;
42 : }
43 :
44 2 : struct IArchiveFile : public IFileLoader
45 : {
46 : };
47 :
48 : typedef std::shared_ptr<IArchiveFile> PIArchiveFile;
49 :
50 1 : struct IArchiveReader
51 : {
52 : virtual ~IArchiveReader();
53 :
54 : /**
55 : * called for each archive entry.
56 : * @param pathname full pathname of entry; only valid during the callback.
57 : **/
58 : typedef void (*ArchiveEntryCallback)(const VfsPath& pathname, const CFileInfo& fileInfo, PIArchiveFile archiveFile, uintptr_t cbData);
59 : virtual Status ReadEntries(ArchiveEntryCallback cb, uintptr_t cbData) = 0;
60 : };
61 :
62 : typedef std::shared_ptr<IArchiveReader> PIArchiveReader;
63 :
64 : // note: when creating an archive, any existing file with the given pathname
65 : // will be overwritten.
66 :
67 : // rationale: don't support partial adding, i.e. updating archive with
68 : // only one file. this would require overwriting parts of the Zip archive,
69 : // which is annoying and slow. also, archives are usually built in
70 : // seek-optimal order, which would break if we start inserting files.
71 : // while testing, loose files can be used, so there's no loss.
72 :
73 0 : struct IArchiveWriter
74 : {
75 : /**
76 : * write out the archive to disk; only hereafter is it valid.
77 : **/
78 : virtual ~IArchiveWriter();
79 :
80 : /**
81 : * add a file to the archive.
82 : *
83 : * rationale: passing in a filename instead of the compressed file
84 : * contents makes for better encapsulation because callers don't need
85 : * to know about the codec. one disadvantage is that loading the file
86 : * contents can no longer take advantage of the VFS cache nor previously
87 : * archived versions. however, the archive builder usually adds files
88 : * precisely because they aren't in archives, and the cache would
89 : * thrash anyway, so this is deemed acceptable.
90 : *
91 : * @param pathname the actual file to add
92 : * @param pathnameInArchive the name to store in the archive
93 : **/
94 : virtual Status AddFile(const OsPath& pathname, const Path& pathnameInArchive) = 0;
95 :
96 : /**
97 : * add a file to the archive, when it is already in memory and not on disk.
98 : *
99 : * @param data the uncompressed file contents to add
100 : * @param size the length of data
101 : * @param mtime the last-modified-time to be stored in the archive
102 : * @param pathnameInArchive the name to store in the archive
103 : **/
104 : virtual Status AddMemory(const u8* data, size_t size, time_t mtime, const OsPath& pathnameInArchive) = 0;
105 : };
106 :
107 : typedef std::shared_ptr<IArchiveWriter> PIArchiveWriter;
108 :
109 : #endif // #ifndef INCLUDED_ARCHIVE
|