| 190 | #pragma mark ModelPackageImpl |
| 191 | |
| 192 | ModelPackageImpl::ModelPackageImpl(const std::filesystem::path& path, bool createIfNecessary, bool readOnly) |
| 193 | : m_packagePath(path), |
| 194 | m_manifestPath(path / kModelPackageManifestFileName), |
| 195 | m_packageDataDirPath(path / kModelPackageDataDir), |
| 196 | m_manifest(nullptr), |
| 197 | m_readOnly(readOnly) |
| 198 | { |
| 199 | if (std::filesystem::exists(m_packagePath)) { |
| 200 | if (std::filesystem::exists(m_manifestPath)) { |
| 201 | std::ifstream manifestStream(m_manifestPath, std::ios::binary); |
| 202 | m_manifest = std::make_unique<JsonMap>(manifestStream); |
| 203 | manifestStream.close(); |
| 204 | } else { |
| 205 | throw std::runtime_error("A valid manifest does not exist at path: " + m_manifestPath.string()); |
| 206 | } |
| 207 | } |
| 208 | // Create the package structure at specified path |
| 209 | else if (createIfNecessary) { |
| 210 | if (false == create_directory(m_packagePath)) { |
| 211 | throw std::runtime_error("Failed to create model package at path: " + m_packagePath.string()); |
| 212 | } |
| 213 | |
| 214 | if (false == create_directory(m_packageDataDirPath)) { |
| 215 | throw std::runtime_error("Failed to create data directory at path: " + m_packageDataDirPath.string()); |
| 216 | } |
| 217 | |
| 218 | m_manifest = std::make_unique<JsonMap>(); |
| 219 | std::stringstream ss; |
| 220 | ss << kModelPackageFileFormatMajorVersion << "." << kModelPackageFileFormatMinorVersion << "." << kModelPackageFileFormatPatchVersion; |
| 221 | m_manifest->setString(kModelPackageFileFormatVersionKey, ss.str()); |
| 222 | } |
| 223 | // Error out since package does not exist |
| 224 | else { |
| 225 | throw std::runtime_error("Failed to open model package at path: " + m_packagePath.string()); |
| 226 | } |
| 227 | |
| 228 | validate(); |
| 229 | } |
| 230 | |
| 231 | ModelPackageImpl::~ModelPackageImpl() |
| 232 | { |