| 108 | } |
| 109 | |
| 110 | std::optional<std::filesystem::path> Archive::extract(std::filesystem::path module, std::filesystem::path prefix){ |
| 111 | std::optional<std::filesystem::path> output_path = (prefix / module).lexically_normal(); |
| 112 | // Open file |
| 113 | std::ifstream input(path, std::ios::binary | std::ios::in); |
| 114 | if(!check_magic_version(input)){ |
| 115 | throw Exception::Exception("incorrect magic or version"); |
| 116 | } |
| 117 | input.seekg(sizeof(uint64_t), std::ios::cur); // Skip paths length |
| 118 | // Read path |
| 119 | uint32_t path_count; |
| 120 | uint64_t address = 0; |
| 121 | input.read((char*)&path_count, sizeof(uint32_t)); |
| 122 | for(uint32_t index = 0; index < path_count; ++index){ |
| 123 | uint32_t name_length; |
| 124 | input.read((char*)&name_length, sizeof(uint32_t)); |
| 125 | std::string name(name_length, '\0'); |
| 126 | input.read(name.data(), name_length); |
| 127 | if(name == module){ |
| 128 | input.read((char*)&address, sizeof(uint64_t)); |
| 129 | break; |
| 130 | }else{ |
| 131 | input.seekg(sizeof(uint64_t), std::ios::cur); |
| 132 | } |
| 133 | } |
| 134 | // Read content |
| 135 | if(address != 0){ |
| 136 | // Read module size |
| 137 | input.seekg(address, std::ios::beg); |
| 138 | uint64_t module_size = 0; |
| 139 | input.read((char*)&module_size, sizeof(uint64_t)); |
| 140 | // Create parent path |
| 141 | std::filesystem::create_directories(output_path.value().parent_path()); |
| 142 | // Extract content |
| 143 | std::ofstream output(output_path.value(), std::ios::binary | std::ios::out); |
| 144 | char buf[1024]; |
| 145 | while(module_size > 0){ |
| 146 | input.read(buf, std::min(module_size, (uint64_t)1024)); |
| 147 | std::streamsize extracted = input.gcount(); |
| 148 | output.write(buf, extracted); |
| 149 | module_size -= extracted; |
| 150 | } |
| 151 | // Close output |
| 152 | output.close(); |
| 153 | }else{ |
| 154 | output_path.reset(); |
| 155 | } |
| 156 | // Close input |
| 157 | input.close(); |
| 158 | return output_path; |
| 159 | } |
| 160 | |
| 161 | std::vector<std::filesystem::path> Archive::list(std::filesystem::path prefix){ |
| 162 | std::vector<std::filesystem::path> result; |