15 const std::string& path) {
16 std::error_code error;
17 if (!std::filesystem::is_regular_file(path, error) || error) {
18 return absl::InvalidArgumentError(
"Choose a regular room JSON file.");
20 std::ifstream file(path, std::ios::binary);
22 return absl::PermissionDeniedError(
"Cannot open room JSON file.");
27 file.read(json.data(),
static_cast<std::streamsize
>(json.size()));
28 json.resize(
static_cast<size_t>(file.gcount()));
30 return absl::DataLossError(
"Failed to read room JSON file.");
33 return absl::ResourceExhaustedError(
34 "Room JSON exceeds the 1 MiB document limit.");
41 const std::string& json) {
43 return absl::UnimplementedError(
"Use Copy Room JSON in the browser build.");
46 std::string extension = std::filesystem::path(path).extension().string();
48 extension.begin(), extension.end(), extension.begin(),
49 [](
unsigned char c) { return static_cast<char>(std::tolower(c)); });
50 if (extension !=
".json") {
51 return absl::InvalidArgumentError(
52 "Save room templates with a .json extension.");
54 std::error_code error;
55 const auto status = std::filesystem::symlink_status(path, error);
56 if (error && error != std::errc::no_such_file_or_directory) {
57 return absl::PermissionDeniedError(
"Cannot inspect room JSON destination.");
59 const bool exists = std::filesystem::exists(status);
61 if (!std::filesystem::is_regular_file(status)) {
62 return absl::InvalidArgumentError(
63 "Cannot replace a directory or symlink.");
66 return absl::FailedPreconditionError(
67 "The destination is not a valid room template. Choose another file.");