takane
Validators for ArtifactDB file formats
Loading...
Searching...
No Matches
rds_file.hpp
Go to the documentation of this file.
1#ifndef TAKANE_RDS_FILE_HPP
2#define TAKANE_RDS_FILE_HPP
3
4#include "utils_files.hpp"
5#include "ritsuko/ritsuko.hpp"
6
7#include <filesystem>
8#include <stdexcept>
9#include <string>
10
16namespace takane {
17
22namespace rds_file {
23
32inline void validate(const std::filesystem::path& path, const ObjectMetadata& metadata, Options& options) {
33 const auto& rdsmap = internal_json::extract_typed_object_from_metadata(metadata.other, "rds_file");
34
35 const std::string& vstring = internal_json::extract_string_from_typed_object(rdsmap, "version", "rds_file");
36 auto version = ritsuko::parse_version_string(vstring.c_str(), vstring.size(), /* skip_patch = */ true);
37 if (version.major != 1) {
38 throw std::runtime_error("unsupported version string '" + vstring + "'");
39 }
40
41 auto fpath = path / "file.rds";
42
43 // Check magic numbers.
44 internal_files::check_gzip_signature(fpath);
45
46 const char* expected = "X\n";
47 size_t expected_len = 2;
48
49 auto reader = internal_other::open_reader<byteme::GzipFileReader>(fpath, expected_len);
50 byteme::PerByte<> pb(&reader);
51 bool okay = pb.valid();
52
53 for (size_t i = 0; i < expected_len; ++i) {
54 if (!okay) {
55 throw std::runtime_error("incomplete RDS file signature for '" + fpath.string() + "'");
56 }
57 if (pb.get() != expected[i]) {
58 throw std::runtime_error("incorrect RDS file signature for '" + fpath.string() + "'");
59 }
60 okay = pb.advance();
61 }
62
63 if (options.rds_file_strict_check) {
64 options.rds_file_strict_check(path, metadata, options);
65 }
66}
67
68}
69
70}
71
72#endif
void validate(const std::filesystem::path &path, const ObjectMetadata &metadata, Options &options)
Definition rds_file.hpp:32
takane validation functions.
Definition _derived_from.hpp:15
Object metadata, including the type and other fields.
Definition utils_public.hpp:26
std::unordered_map< std::string, std::shared_ptr< millijson::Base > > other
Definition utils_public.hpp:35
Validation options.
Definition utils_public.hpp:94
std::function< void(const std::filesystem::path &, const ObjectMetadata &, Options &)> rds_file_strict_check
Definition utils_public.hpp:229