1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #ifndef REALM_OS_SYNC_FILE_HPP
20 #define REALM_OS_SYNC_FILE_HPP
24 #include "sync/sync_user.hpp"
26 #include <realm/util/optional.hpp>
32 enum class FilePathType {
36 // FIXME: Does it make sense to use realm::StringData arguments for these functions instead of std::string?
38 /// Given a string, turn it into a percent-encoded string.
39 std::string make_percent_encoded_string(const std::string& raw_string);
41 /// Given a percent-encoded string, turn it into the original (non-encoded) string.
42 std::string make_raw_string(const std::string& percent_encoded_string);
44 /// Given a file path and a path component, return a new path created by appending the component to the path.
45 std::string file_path_by_appending_component(const std::string& path,
46 const std::string& component,
47 FilePathType path_type=FilePathType::File);
49 /// Given a file path and an extension, append the extension to the path.
50 std::string file_path_by_appending_extension(const std::string& path, const std::string& extension);
52 /// Create a timestamped `mktemp`-compatible template string using the current local time.
53 std::string create_timestamped_template(const std::string& prefix, int wildcard_count=8);
55 /// Reserve a unique file name based on a base directory path and a `mktemp`-compatible template string.
56 /// Returns the path of the file.
57 std::string reserve_unique_file_name(const std::string& path, const std::string& template_string);
61 class SyncFileManager {
63 SyncFileManager(std::string base_path) : m_base_path(std::move(base_path)) { }
65 /// Return the user directory for a given user, creating it if it does not already exist.
66 std::string user_directory(const std::string& local_identity,
67 util::Optional<SyncUserIdentifier> user_info=none) const;
69 /// Remove the user directory for a given user.
70 void remove_user_directory(const std::string& local_identity) const; // throws
72 /// Rename a user directory. Returns true if a directory at `old_name` existed
73 /// and was successfully renamed to `new_name`. Returns false if no directory
74 /// exists at `old_name`.
75 bool try_rename_user_directory(const std::string& old_name, const std::string& new_name) const;
77 /// Return the path for a given Realm, creating the user directory if it does not already exist.
78 std::string path(const std::string&, const std::string&,
79 util::Optional<SyncUserIdentifier> user_info=none) const;
81 /// Remove the Realm at a given path for a given user. Returns `true` if the remove operation fully succeeds.
82 bool remove_realm(const std::string& local_identity, const std::string& raw_realm_path) const;
84 /// Remove the Realm whose primary Realm file is located at `absolute_path`. Returns `true` if the remove
85 /// operation fully succeeds.
86 bool remove_realm(const std::string& absolute_path) const;
88 /// Copy the Realm file at the location `old_path` to the location of `new_path`.
89 bool copy_realm_file(const std::string& old_path, const std::string& new_path) const;
91 /// Return the path for the metadata Realm files.
92 std::string metadata_path() const;
94 /// Remove the metadata Realm.
95 bool remove_metadata_realm() const;
97 const std::string& base_path() const
102 std::string recovery_directory_path() const
104 return get_special_directory(c_recovery_directory);
108 std::string m_base_path;
110 static constexpr const char c_sync_directory[] = "realm-object-server";
111 static constexpr const char c_utility_directory[] = "io.realm.object-server-utility";
112 static constexpr const char c_recovery_directory[] = "io.realm.object-server-recovered-realms";
113 static constexpr const char c_metadata_directory[] = "metadata";
114 static constexpr const char c_metadata_realm[] = "sync_metadata.realm";
115 static constexpr const char c_user_info_file[] = "__user_info";
117 std::string get_special_directory(std::string directory_name) const;
119 std::string get_utility_directory() const
121 return get_special_directory(c_utility_directory);
124 std::string get_base_sync_directory() const;
129 #endif // REALM_OS_SYNC_FILE_HPP