Line data Source code
1 : /* Copyright (C) 2026 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/common/file_loader.h" 31 : #include "lib/file/file_system.h" // FileInfo 32 : #include "lib/file/vfs/vfs_path.h" 33 : #include "lib/os_path.h" 34 : #include "lib/status.h" 35 : 36 : #include <cstdint> 37 : #include <ctime> 38 : #include <memory> 39 : 40 : class Path; 41 : 42 : // rationale: this module doesn't build a directory tree of the entries 43 : // within an archive. that task is left to the VFS; here, we are only 44 : // concerned with enumerating all archive entries. 45 : 46 : namespace ERR 47 : { 48 : const Status ARCHIVE_UNKNOWN_FORMAT = -110400; 49 : const Status ARCHIVE_UNKNOWN_METHOD = -110401; 50 : } 51 : 52 1 : struct IArchiveFile : public IFileLoader 53 : { 54 : }; 55 : 56 : typedef std::shared_ptr<IArchiveFile> PIArchiveFile; 57 : 58 1 : struct IArchiveReader 59 : { 60 : virtual ~IArchiveReader(); 61 : 62 : /** 63 : * called for each archive entry. 64 : * @param pathname full pathname of entry; only valid during the callback. 65 : **/ 66 : typedef void (*ArchiveEntryCallback)(const VfsPath& pathname, const CFileInfo& fileInfo, PIArchiveFile archiveFile, uintptr_t cbData); 67 : virtual Status ReadEntries(ArchiveEntryCallback cb, uintptr_t cbData) = 0; 68 : }; 69 : 70 : typedef std::shared_ptr<IArchiveReader> PIArchiveReader; 71 : 72 : // note: when creating an archive, any existing file with the given pathname 73 : // will be overwritten. 74 : 75 : // rationale: don't support partial adding, i.e. updating archive with 76 : // only one file. this would require overwriting parts of the Zip archive, 77 : // which is annoying and slow. also, archives are usually built in 78 : // seek-optimal order, which would break if we start inserting files. 79 : // while testing, loose files can be used, so there's no loss. 80 : 81 0 : struct IArchiveWriter 82 : { 83 : /** 84 : * write out the archive to disk; only hereafter is it valid. 85 : **/ 86 : virtual ~IArchiveWriter(); 87 : 88 : /** 89 : * add a file to the archive. 90 : * 91 : * rationale: passing in a filename instead of the compressed file 92 : * contents makes for better encapsulation because callers don't need 93 : * to know about the codec. one disadvantage is that loading the file 94 : * contents can no longer take advantage of the VFS cache nor previously 95 : * archived versions. however, the archive builder usually adds files 96 : * precisely because they aren't in archives, and the cache would 97 : * thrash anyway, so this is deemed acceptable. 98 : * 99 : * @param pathname the actual file to add 100 : * @param pathnameInArchive the name to store in the archive 101 : **/ 102 : virtual Status AddFile(const OsPath& pathname, const Path& pathnameInArchive) = 0; 103 : 104 : /** 105 : * add a file to the archive, when it is already in memory and not on disk. 106 : * 107 : * @param data the uncompressed file contents to add 108 : * @param size the length of data 109 : * @param mtime the last-modified-time to be stored in the archive 110 : * @param pathnameInArchive the name to store in the archive 111 : **/ 112 : virtual Status AddMemory(const std::uint8_t* data, size_t size, time_t mtime, 113 : const OsPath& pathnameInArchive) = 0; 114 : }; 115 : 116 : typedef std::shared_ptr<IArchiveWriter> PIArchiveWriter; 117 : 118 : #endif // #ifndef INCLUDED_ARCHIVE