• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / 2031

12 Feb 2024 07:38PM UTC coverage: 91.844% (+0.006%) from 91.838%
2031

push

Evergreen

web-flow
Make `SyncSession::get_file_ident()` public (#7203)

93014 of 171494 branches covered (54.24%)

23 of 28 new or added lines in 4 files covered. (82.14%)

51 existing lines in 12 files now uncovered.

235322 of 256218 relevant lines covered (91.84%)

6255148.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.88
/src/realm/object-store/c_api/sync.cpp
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2021 Realm Inc.
4
//
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
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
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 utilied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
#include <realm/sync/config.hpp>
20
#include <realm/sync/client.hpp>
21
#include <realm/sync/protocol.hpp>
22
#include <realm/sync/network/websocket.hpp>
23
#include <realm/object-store/c_api/conversion.hpp>
24
#include <realm/object-store/sync/sync_manager.hpp>
25
#include <realm/object-store/sync/sync_session.hpp>
26
#include <realm/object-store/sync/async_open_task.hpp>
27
#include <realm/util/basic_system_errors.hpp>
28

29
#include "types.hpp"
30
#include "util.hpp"
31

32

33
realm_async_open_task_progress_notification_token::~realm_async_open_task_progress_notification_token()
34
{
×
35
    task->unregister_download_progress_notifier(token);
×
36
}
×
37

38
realm_sync_session_connection_state_notification_token::~realm_sync_session_connection_state_notification_token()
39
{
×
40
    session->unregister_connection_change_callback(token);
×
41
}
×
42

43
realm_sync_user_subscription_token::~realm_sync_user_subscription_token()
44
{
2✔
45
    user->unsubscribe(token);
2✔
46
}
2✔
47

48
namespace realm::c_api {
49

50
static_assert(realm_sync_client_metadata_mode_e(SyncClientConfig::MetadataMode::NoEncryption) ==
51
              RLM_SYNC_CLIENT_METADATA_MODE_PLAINTEXT);
52
static_assert(realm_sync_client_metadata_mode_e(SyncClientConfig::MetadataMode::Encryption) ==
53
              RLM_SYNC_CLIENT_METADATA_MODE_ENCRYPTED);
54
static_assert(realm_sync_client_metadata_mode_e(SyncClientConfig::MetadataMode::NoMetadata) ==
55
              RLM_SYNC_CLIENT_METADATA_MODE_DISABLED);
56

57
static_assert(realm_sync_client_reconnect_mode_e(ReconnectMode::normal) == RLM_SYNC_CLIENT_RECONNECT_MODE_NORMAL);
58
static_assert(realm_sync_client_reconnect_mode_e(ReconnectMode::testing) == RLM_SYNC_CLIENT_RECONNECT_MODE_TESTING);
59

60
static_assert(realm_sync_session_resync_mode_e(ClientResyncMode::Manual) == RLM_SYNC_SESSION_RESYNC_MODE_MANUAL);
61
static_assert(realm_sync_session_resync_mode_e(ClientResyncMode::DiscardLocal) ==
62
              RLM_SYNC_SESSION_RESYNC_MODE_DISCARD_LOCAL);
63
static_assert(realm_sync_session_resync_mode_e(ClientResyncMode::Recover) == RLM_SYNC_SESSION_RESYNC_MODE_RECOVER);
64
static_assert(realm_sync_session_resync_mode_e(ClientResyncMode::RecoverOrDiscard) ==
65
              RLM_SYNC_SESSION_RESYNC_MODE_RECOVER_OR_DISCARD);
66

67
static_assert(realm_sync_session_stop_policy_e(SyncSessionStopPolicy::Immediately) ==
68
              RLM_SYNC_SESSION_STOP_POLICY_IMMEDIATELY);
69
static_assert(realm_sync_session_stop_policy_e(SyncSessionStopPolicy::LiveIndefinitely) ==
70
              RLM_SYNC_SESSION_STOP_POLICY_LIVE_INDEFINITELY);
71
static_assert(realm_sync_session_stop_policy_e(SyncSessionStopPolicy::AfterChangesUploaded) ==
72
              RLM_SYNC_SESSION_STOP_POLICY_AFTER_CHANGES_UPLOADED);
73

74
static_assert(realm_sync_session_state_e(SyncSession::State::Active) == RLM_SYNC_SESSION_STATE_ACTIVE);
75
static_assert(realm_sync_session_state_e(SyncSession::State::Dying) == RLM_SYNC_SESSION_STATE_DYING);
76
static_assert(realm_sync_session_state_e(SyncSession::State::Inactive) == RLM_SYNC_SESSION_STATE_INACTIVE);
77
static_assert(realm_sync_session_state_e(SyncSession::State::WaitingForAccessToken) ==
78
              RLM_SYNC_SESSION_STATE_WAITING_FOR_ACCESS_TOKEN);
79
static_assert(realm_sync_session_state_e(SyncSession::State::Paused) == RLM_SYNC_SESSION_STATE_PAUSED);
80

81
static_assert(realm_sync_connection_state_e(SyncSession::ConnectionState::Disconnected) ==
82
              RLM_SYNC_CONNECTION_STATE_DISCONNECTED);
83
static_assert(realm_sync_connection_state_e(SyncSession::ConnectionState::Connecting) ==
84
              RLM_SYNC_CONNECTION_STATE_CONNECTING);
85
static_assert(realm_sync_connection_state_e(SyncSession::ConnectionState::Connected) ==
86
              RLM_SYNC_CONNECTION_STATE_CONNECTED);
87

88
static_assert(realm_sync_progress_direction_e(SyncSession::ProgressDirection::upload) ==
89
              RLM_SYNC_PROGRESS_DIRECTION_UPLOAD);
90
static_assert(realm_sync_progress_direction_e(SyncSession::ProgressDirection::download) ==
91
              RLM_SYNC_PROGRESS_DIRECTION_DOWNLOAD);
92

93

94
namespace {
95
using namespace realm::sync;
96
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::NoAction) == RLM_SYNC_ERROR_ACTION_NO_ACTION);
97
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::ProtocolViolation) ==
98
              RLM_SYNC_ERROR_ACTION_PROTOCOL_VIOLATION);
99
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::ApplicationBug) ==
100
              RLM_SYNC_ERROR_ACTION_APPLICATION_BUG);
101
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::Warning) == RLM_SYNC_ERROR_ACTION_WARNING);
102
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::Transient) == RLM_SYNC_ERROR_ACTION_TRANSIENT);
103
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::DeleteRealm) ==
104
              RLM_SYNC_ERROR_ACTION_DELETE_REALM);
105
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::ClientReset) ==
106
              RLM_SYNC_ERROR_ACTION_CLIENT_RESET);
107
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::ClientResetNoRecovery) ==
108
              RLM_SYNC_ERROR_ACTION_CLIENT_RESET_NO_RECOVERY);
109
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::MigrateToFLX) ==
110
              RLM_SYNC_ERROR_ACTION_MIGRATE_TO_FLX);
111
static_assert(realm_sync_error_action_e(ProtocolErrorInfo::Action::RevertToPBS) ==
112
              RLM_SYNC_ERROR_ACTION_REVERT_TO_PBS);
113

114
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Pending) ==
115
              RLM_SYNC_SUBSCRIPTION_PENDING);
116
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Bootstrapping) ==
117
              RLM_SYNC_SUBSCRIPTION_BOOTSTRAPPING);
118
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::AwaitingMark) ==
119
              RLM_SYNC_SUBSCRIPTION_AWAITING_MARK);
120
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Complete) ==
121
              RLM_SYNC_SUBSCRIPTION_COMPLETE);
122
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Error) == RLM_SYNC_SUBSCRIPTION_ERROR);
123
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Superseded) ==
124
              RLM_SYNC_SUBSCRIPTION_SUPERSEDED);
125
static_assert(realm_flx_sync_subscription_set_state_e(SubscriptionSet::State::Uncommitted) ==
126
              RLM_SYNC_SUBSCRIPTION_UNCOMMITTED);
127

128
} // namespace
129

130

131
static Query add_ordering_to_realm_query(Query realm_query, const DescriptorOrdering& ordering)
132
{
28✔
133
    auto ordering_copy = util::make_bind<DescriptorOrdering>();
28✔
134
    *ordering_copy = ordering;
28✔
135
    realm_query.set_ordering(ordering_copy);
28✔
136
    return realm_query;
28✔
137
}
28✔
138

139
RLM_API realm_sync_client_config_t* realm_sync_client_config_new(void) noexcept
140
{
6✔
141
    return new realm_sync_client_config_t;
6✔
142
}
6✔
143

144
RLM_API void realm_sync_client_config_set_base_file_path(realm_sync_client_config_t* config,
145
                                                         const char* path) noexcept
146
{
2✔
147
    config->base_file_path = path;
2✔
148
}
2✔
149

150
RLM_API void realm_sync_client_config_set_metadata_mode(realm_sync_client_config_t* config,
151
                                                        realm_sync_client_metadata_mode_e mode) noexcept
152
{
2✔
153
    config->metadata_mode = SyncClientConfig::MetadataMode(mode);
2✔
154
}
2✔
155

156
RLM_API void realm_sync_client_config_set_metadata_encryption_key(realm_sync_client_config_t* config,
157
                                                                  const uint8_t key[64]) noexcept
158
{
×
159
    config->custom_encryption_key = std::vector<char>(key, key + 64);
×
160
}
×
161

162
RLM_API void realm_sync_client_config_set_reconnect_mode(realm_sync_client_config_t* config,
163
                                                         realm_sync_client_reconnect_mode_e mode) noexcept
164
{
×
165
    config->reconnect_mode = ReconnectMode(mode);
×
166
}
×
167
RLM_API void realm_sync_client_config_set_multiplex_sessions(realm_sync_client_config_t* config,
168
                                                             bool multiplex) noexcept
169
{
×
170
    config->multiplex_sessions = multiplex;
×
171
}
×
172

173
RLM_API void realm_sync_client_config_set_user_agent_binding_info(realm_sync_client_config_t* config,
174
                                                                  const char* info) noexcept
175
{
×
176
    config->user_agent_binding_info = info;
×
177
}
×
178

179
RLM_API void realm_sync_client_config_set_user_agent_application_info(realm_sync_client_config_t* config,
180
                                                                      const char* info) noexcept
181
{
×
182
    config->user_agent_application_info = info;
×
183
}
×
184

185
RLM_API void realm_sync_client_config_set_connect_timeout(realm_sync_client_config_t* config,
186
                                                          uint64_t timeout) noexcept
187
{
×
188
    config->timeouts.connect_timeout = timeout;
×
189
}
×
190

191
RLM_API void realm_sync_client_config_set_connection_linger_time(realm_sync_client_config_t* config,
192
                                                                 uint64_t time) noexcept
193
{
×
194
    config->timeouts.connection_linger_time = time;
×
195
}
×
196

197
RLM_API void realm_sync_client_config_set_ping_keepalive_period(realm_sync_client_config_t* config,
198
                                                                uint64_t period) noexcept
199
{
×
200
    config->timeouts.ping_keepalive_period = period;
×
201
}
×
202

203
RLM_API void realm_sync_client_config_set_pong_keepalive_timeout(realm_sync_client_config_t* config,
204
                                                                 uint64_t timeout) noexcept
205
{
×
206
    config->timeouts.pong_keepalive_timeout = timeout;
×
207
}
×
208

209
RLM_API void realm_sync_client_config_set_fast_reconnect_limit(realm_sync_client_config_t* config,
210
                                                               uint64_t limit) noexcept
211
{
×
212
    config->timeouts.fast_reconnect_limit = limit;
×
213
}
×
214

215
/// Register an app local callback handler for bindings interested in registering callbacks before/after
216
/// the ObjectStore thread runs for this app. This only works for the default socket provider implementation.
217
/// IMPORTANT: If a function is supplied that handles the exception, it must call abort() or cause the
218
/// application to crash since the SyncClient will be in a bad state if this occurs and will not be able to
219
/// shut down properly.
220
/// @param config pointer to sync client config created by realm_sync_client_config_new()
221
/// @param on_thread_create callback invoked when the object store thread is created
222
/// @param on_thread_destroy callback invoked when the object store thread is destroyed
223
/// @param on_error callback invoked to signal to the listener that some error has occurred.
224
/// @param user_data pointer to user defined data that is provided to each of the callback functions
225
/// @param free_userdata callback invoked when the user_data is to be freed
226
RLM_API void realm_sync_client_config_set_default_binding_thread_observer(
227
    realm_sync_client_config_t* config, realm_on_object_store_thread_callback_t on_thread_create,
228
    realm_on_object_store_thread_callback_t on_thread_destroy, realm_on_object_store_error_callback_t on_error,
229
    realm_userdata_t user_data, realm_free_userdata_func_t free_userdata)
230
{
4✔
231
    config->default_socket_provider_thread_observer = std::make_shared<CBindingThreadObserver>(
4✔
232
        on_thread_create, on_thread_destroy, on_error, user_data, free_userdata);
4✔
233
}
4✔
234

235
RLM_API void realm_config_set_sync_config(realm_config_t* config, realm_sync_config_t* sync_config)
236
{
4✔
237
    config->sync_config = std::make_shared<SyncConfig>(*sync_config);
4✔
238
}
4✔
239

240
RLM_API realm_sync_config_t* realm_sync_config_new(const realm_user_t* user, const char* partition_value) noexcept
241
{
4✔
242
    return new realm_sync_config_t(*user, partition_value);
4✔
243
}
4✔
244

245
RLM_API realm_sync_config_t* realm_flx_sync_config_new(const realm_user_t* user) noexcept
246
{
×
247
    return new realm_sync_config(*user, realm::SyncConfig::FLXSyncEnabled{});
×
248
}
×
249

250
RLM_API void realm_sync_config_set_session_stop_policy(realm_sync_config_t* config,
251
                                                       realm_sync_session_stop_policy_e policy) noexcept
252
{
×
253
    config->stop_policy = SyncSessionStopPolicy(policy);
×
254
}
×
255

256
RLM_API void realm_sync_config_set_error_handler(realm_sync_config_t* config, realm_sync_error_handler_func_t handler,
257
                                                 realm_userdata_t userdata,
258
                                                 realm_free_userdata_func_t userdata_free) noexcept
259
{
12✔
260
    auto cb = [handler, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](
12✔
261
                  std::shared_ptr<SyncSession> session, SyncError error) {
12✔
262
        auto c_error = realm_sync_error_t();
12✔
263

6✔
264
        std::string error_code_message;
12✔
265
        c_error.status = to_capi(error.status);
12✔
266
        c_error.is_fatal = error.is_fatal;
12✔
267
        c_error.is_unrecognized_by_client = error.is_unrecognized_by_client;
12✔
268
        c_error.is_client_reset_requested = error.is_client_reset_requested();
12✔
269
        c_error.server_requests_action = static_cast<realm_sync_error_action_e>(error.server_requests_action);
12✔
270
        c_error.c_original_file_path_key = error.c_original_file_path_key;
12✔
271
        c_error.c_recovery_file_path_key = error.c_recovery_file_path_key;
12✔
272
        c_error.user_code_error = ErrorStorage::get_thread_local()->get_and_clear_user_code_error();
12✔
273

6✔
274
        std::vector<realm_sync_error_user_info_t> c_user_info;
12✔
275
        c_user_info.reserve(error.user_info.size());
12✔
276
        for (auto& info : error.user_info) {
16✔
277
            c_user_info.push_back({info.first.c_str(), info.second.c_str()});
16✔
278
        }
16✔
279

6✔
280
        c_error.user_info_map = c_user_info.data();
12✔
281
        c_error.user_info_length = c_user_info.size();
12✔
282

6✔
283
        std::vector<realm_sync_error_compensating_write_info_t> c_compensating_writes;
12✔
284
        for (const auto& compensating_write : error.compensating_writes_info) {
8✔
285
            c_compensating_writes.push_back({compensating_write.reason.c_str(),
4✔
286
                                             compensating_write.object_name.c_str(),
4✔
287
                                             to_capi(compensating_write.primary_key)});
4✔
288
        }
4✔
289
        c_error.compensating_writes = c_compensating_writes.data();
12✔
290
        c_error.compensating_writes_length = c_compensating_writes.size();
12✔
291

6✔
292
        realm_sync_session_t c_session(session);
12✔
293
        handler(userdata.get(), &c_session, std::move(c_error));
12✔
294
    };
12✔
295
    config->error_handler = std::move(cb);
12✔
296
}
12✔
297

298
RLM_API void realm_sync_config_set_client_validate_ssl(realm_sync_config_t* config, bool validate) noexcept
299
{
×
300
    config->client_validate_ssl = validate;
×
301
}
×
302

303
RLM_API void realm_sync_config_set_ssl_trust_certificate_path(realm_sync_config_t* config, const char* path) noexcept
304
{
×
305
    config->ssl_trust_certificate_path = std::string(path);
×
306
}
×
307

308
RLM_API void realm_sync_config_set_ssl_verify_callback(realm_sync_config_t* config,
309
                                                       realm_sync_ssl_verify_func_t callback,
310
                                                       realm_userdata_t userdata,
311
                                                       realm_free_userdata_func_t userdata_free) noexcept
312
{
×
313
    auto cb = [callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](
×
314
                  const std::string& server_address, SyncConfig::ProxyConfig::port_type server_port,
×
315
                  const char* pem_data, size_t pem_size, int preverify_ok, int depth) {
×
316
        return callback(userdata.get(), server_address.c_str(), server_port, pem_data, pem_size, preverify_ok, depth);
×
317
    };
×
318

319
    config->ssl_verify_callback = std::move(cb);
×
320
}
×
321

322
RLM_API void realm_sync_config_set_cancel_waits_on_nonfatal_error(realm_sync_config_t* config, bool cancel) noexcept
323
{
×
324
    config->cancel_waits_on_nonfatal_error = cancel;
×
325
}
×
326

327
RLM_API void realm_sync_config_set_authorization_header_name(realm_sync_config_t* config, const char* name) noexcept
328
{
×
329
    config->authorization_header_name = std::string(name);
×
330
}
×
331

332
RLM_API void realm_sync_config_set_custom_http_header(realm_sync_config_t* config, const char* name,
333
                                                      const char* value) noexcept
334
{
×
335
    config->custom_http_headers[name] = value;
×
336
}
×
337

338
RLM_API void realm_sync_config_set_recovery_directory_path(realm_sync_config_t* config, const char* path) noexcept
339
{
×
340
    config->recovery_directory = std::string(path);
×
341
}
×
342

343
RLM_API void realm_sync_config_set_resync_mode(realm_sync_config_t* config,
344
                                               realm_sync_session_resync_mode_e mode) noexcept
345
{
10✔
346
    config->client_resync_mode = ClientResyncMode(mode);
10✔
347
}
10✔
348

349
RLM_API realm_object_id_t realm_sync_subscription_id(const realm_flx_sync_subscription_t* subscription) noexcept
350
{
2✔
351
    REALM_ASSERT(subscription != nullptr);
2✔
352
    return to_capi(subscription->id);
2✔
353
}
2✔
354

355
RLM_API realm_string_t realm_sync_subscription_name(const realm_flx_sync_subscription_t* subscription) noexcept
356
{
2✔
357
    REALM_ASSERT(subscription != nullptr);
2✔
358
    return to_capi(subscription->name);
2✔
359
}
2✔
360

361
RLM_API realm_string_t
362
realm_sync_subscription_object_class_name(const realm_flx_sync_subscription_t* subscription) noexcept
363
{
×
364
    REALM_ASSERT(subscription != nullptr);
×
365
    return to_capi(subscription->object_class_name);
×
366
}
×
367

368
RLM_API realm_string_t
369
realm_sync_subscription_query_string(const realm_flx_sync_subscription_t* subscription) noexcept
370
{
×
371
    REALM_ASSERT(subscription != nullptr);
×
372
    return to_capi(subscription->query_string);
×
373
}
×
374

375
RLM_API realm_timestamp_t
376
realm_sync_subscription_created_at(const realm_flx_sync_subscription_t* subscription) noexcept
377
{
2✔
378
    REALM_ASSERT(subscription != nullptr);
2✔
379
    return to_capi(subscription->created_at);
2✔
380
}
2✔
381

382
RLM_API realm_timestamp_t
383
realm_sync_subscription_updated_at(const realm_flx_sync_subscription_t* subscription) noexcept
384
{
2✔
385
    REALM_ASSERT(subscription != nullptr);
2✔
386
    return to_capi(subscription->updated_at);
2✔
387
}
2✔
388

389
RLM_API void realm_sync_config_set_before_client_reset_handler(realm_sync_config_t* config,
390
                                                               realm_sync_before_client_reset_func_t callback,
391
                                                               realm_userdata_t userdata,
392
                                                               realm_free_userdata_func_t userdata_free) noexcept
393
{
8✔
394
    auto cb = [callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](SharedRealm before_realm) {
8✔
395
        realm_t r1{before_realm};
8✔
396
        if (!callback(userdata.get(), &r1)) {
8✔
397
            throw CallbackFailed{};
4✔
398
        }
4✔
399
    };
8✔
400
    config->notify_before_client_reset = std::move(cb);
8✔
401
}
8✔
402

403
RLM_API void realm_sync_config_set_after_client_reset_handler(realm_sync_config_t* config,
404
                                                              realm_sync_after_client_reset_func_t callback,
405
                                                              realm_userdata_t userdata,
406
                                                              realm_free_userdata_func_t userdata_free) noexcept
407
{
6✔
408
    auto cb = [callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](
6✔
409
                  SharedRealm before_realm, ThreadSafeReference after_realm, bool did_recover) {
5✔
410
        realm_t r1{before_realm};
4✔
411
        auto tsr = realm_t::thread_safe_reference(std::move(after_realm));
4✔
412
        if (!callback(userdata.get(), &r1, &tsr, did_recover)) {
4✔
413
            throw CallbackFailed{};
2✔
414
        }
2✔
415
    };
4✔
416
    config->notify_after_client_reset = std::move(cb);
6✔
417
}
6✔
418

419
RLM_API void realm_sync_config_set_initial_subscription_handler(
420
    realm_sync_config_t* config, realm_async_open_task_init_subscription_func_t callback, bool rerun_on_open,
421
    realm_userdata_t userdata, realm_free_userdata_func_t userdata_free)
422
{
4✔
423
    auto cb = [callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](SharedRealm realm) {
2✔
424
        realm_t r{realm};
×
425
        callback(&r, userdata.get());
×
426
    };
×
427
    config->subscription_initializer = std::move(cb);
4✔
428
    config->rerun_init_subscription_on_open = rerun_on_open;
4✔
429
}
4✔
430

431
RLM_API realm_flx_sync_subscription_set_t* realm_sync_get_latest_subscription_set(const realm_t* realm)
432
{
22✔
433
    REALM_ASSERT(realm != nullptr);
22✔
434
    return wrap_err([&]() {
22✔
435
        return new realm_flx_sync_subscription_set_t((*realm)->get_latest_subscription_set());
22✔
436
    });
22✔
437
}
22✔
438

439
RLM_API realm_flx_sync_subscription_set_t* realm_sync_get_active_subscription_set(const realm_t* realm)
440
{
×
441
    REALM_ASSERT(realm != nullptr);
×
442
    return wrap_err([&]() {
×
443
        return new realm_flx_sync_subscription_set_t((*realm)->get_active_subscription_set());
×
444
    });
×
445
}
×
446

447
RLM_API realm_flx_sync_subscription_set_state_e
448
realm_sync_on_subscription_set_state_change_wait(const realm_flx_sync_subscription_set_t* subscription_set,
449
                                                 realm_flx_sync_subscription_set_state_e notify_when) noexcept
450
{
14✔
451
    REALM_ASSERT(subscription_set != nullptr);
14✔
452
    SubscriptionSet::State state =
14✔
453
        subscription_set->get_state_change_notification(static_cast<SubscriptionSet::State>(notify_when)).get();
14✔
454
    return static_cast<realm_flx_sync_subscription_set_state_e>(state);
14✔
455
}
14✔
456

457
RLM_API bool
458
realm_sync_on_subscription_set_state_change_async(const realm_flx_sync_subscription_set_t* subscription_set,
459
                                                  realm_flx_sync_subscription_set_state_e notify_when,
460
                                                  realm_sync_on_subscription_state_changed_t callback,
461
                                                  realm_userdata_t userdata, realm_free_userdata_func_t userdata_free)
462
{
2✔
463
    REALM_ASSERT(subscription_set != nullptr && callback != nullptr);
2✔
464
    return wrap_err([&]() {
2✔
465
        auto future_state =
2✔
466
            subscription_set->get_state_change_notification(static_cast<SubscriptionSet::State>(notify_when));
2✔
467
        std::move(future_state)
2✔
468
            .get_async([callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](
2✔
469
                           const StatusWith<SubscriptionSet::State>& state) -> void {
2✔
470
                if (state.is_ok())
2✔
471
                    callback(userdata.get(), static_cast<realm_flx_sync_subscription_set_state_e>(state.get_value()));
2✔
472
                else
×
473
                    callback(userdata.get(), realm_flx_sync_subscription_set_state_e::RLM_SYNC_SUBSCRIPTION_ERROR);
×
474
            });
2✔
475
        return true;
2✔
476
    });
2✔
477
}
2✔
478

479
RLM_API int64_t
480
realm_sync_subscription_set_version(const realm_flx_sync_subscription_set_t* subscription_set) noexcept
481
{
2✔
482
    REALM_ASSERT(subscription_set != nullptr);
2✔
483
    return subscription_set->version();
2✔
484
}
2✔
485

486
RLM_API realm_flx_sync_subscription_set_state_e
487
realm_sync_subscription_set_state(const realm_flx_sync_subscription_set_t* subscription_set) noexcept
488
{
×
489
    REALM_ASSERT(subscription_set != nullptr);
×
490
    return static_cast<realm_flx_sync_subscription_set_state_e>(subscription_set->state());
×
491
}
×
492

493
RLM_API const char*
494
realm_sync_subscription_set_error_str(const realm_flx_sync_subscription_set_t* subscription_set) noexcept
495
{
×
496
    REALM_ASSERT(subscription_set != nullptr);
×
497
    return subscription_set->error_str().data();
×
498
}
×
499

500
RLM_API size_t realm_sync_subscription_set_size(const realm_flx_sync_subscription_set_t* subscription_set) noexcept
501
{
2✔
502
    REALM_ASSERT(subscription_set != nullptr);
2✔
503
    return subscription_set->size();
2✔
504
}
2✔
505

506
RLM_API realm_flx_sync_subscription_t*
507
realm_sync_find_subscription_by_name(const realm_flx_sync_subscription_set_t* subscription_set,
508
                                     const char* name) noexcept
509
{
4✔
510
    REALM_ASSERT(subscription_set != nullptr);
4✔
511
    auto ptr = subscription_set->find(name);
4✔
512
    if (!ptr)
4✔
513
        return nullptr;
2✔
514
    return new realm_flx_sync_subscription_t(*ptr);
2✔
515
}
2✔
516

517
RLM_API realm_flx_sync_subscription_t*
518
realm_sync_find_subscription_by_results(const realm_flx_sync_subscription_set_t* subscription_set,
519
                                        realm_results_t* results) noexcept
520
{
4✔
521
    REALM_ASSERT(subscription_set != nullptr);
4✔
522
    auto realm_query = add_ordering_to_realm_query(results->get_query(), results->get_ordering());
4✔
523
    auto ptr = subscription_set->find(realm_query);
4✔
524
    if (!ptr)
4✔
525
        return nullptr;
×
526
    return new realm_flx_sync_subscription_t{*ptr};
4✔
527
}
4✔
528

529
RLM_API realm_flx_sync_subscription_t*
530
realm_sync_subscription_at(const realm_flx_sync_subscription_set_t* subscription_set, size_t index)
531
{
×
532
    REALM_ASSERT(subscription_set != nullptr && index < subscription_set->size());
×
533
    try {
×
534
        return new realm_flx_sync_subscription_t{subscription_set->at(index)};
×
535
    }
×
536
    catch (...) {
×
537
        return nullptr;
×
538
    }
×
539
}
×
540

541
RLM_API realm_flx_sync_subscription_t*
542
realm_sync_find_subscription_by_query(const realm_flx_sync_subscription_set_t* subscription_set,
543
                                      realm_query_t* query) noexcept
544
{
2✔
545
    REALM_ASSERT(subscription_set != nullptr);
2✔
546
    auto realm_query = add_ordering_to_realm_query(query->get_query(), query->get_ordering());
2✔
547
    auto ptr = subscription_set->find(realm_query);
2✔
548
    if (!ptr)
2✔
549
        return nullptr;
×
550
    return new realm_flx_sync_subscription_t(*ptr);
2✔
551
}
2✔
552

553
RLM_API bool realm_sync_subscription_set_refresh(realm_flx_sync_subscription_set_t* subscription_set)
554
{
×
555
    REALM_ASSERT(subscription_set != nullptr);
×
556
    return wrap_err([&]() {
×
557
        subscription_set->refresh();
×
558
        return true;
×
559
    });
×
560
}
×
561

562
RLM_API realm_flx_sync_mutable_subscription_set_t*
563
realm_sync_make_subscription_set_mutable(realm_flx_sync_subscription_set_t* subscription_set)
564
{
24✔
565
    REALM_ASSERT(subscription_set != nullptr);
24✔
566
    return wrap_err([&]() {
24✔
567
        return new realm_flx_sync_mutable_subscription_set_t{subscription_set->make_mutable_copy()};
24✔
568
    });
24✔
569
}
24✔
570

571
RLM_API bool realm_sync_subscription_set_clear(realm_flx_sync_mutable_subscription_set_t* subscription_set)
572
{
2✔
573
    REALM_ASSERT(subscription_set != nullptr);
2✔
574
    return wrap_err([&]() {
2✔
575
        subscription_set->clear();
2✔
576
        return true;
2✔
577
    });
2✔
578
}
2✔
579

580
RLM_API bool
581
realm_sync_subscription_set_insert_or_assign_results(realm_flx_sync_mutable_subscription_set_t* subscription_set,
582
                                                     realm_results_t* results, const char* name, size_t* index,
583
                                                     bool* inserted)
584
{
10✔
585
    REALM_ASSERT(subscription_set != nullptr && results != nullptr);
10✔
586
    return wrap_err([&]() {
10✔
587
        auto realm_query = add_ordering_to_realm_query(results->get_query(), results->get_ordering());
10✔
588
        const auto [it, successful] = name ? subscription_set->insert_or_assign(name, realm_query)
8✔
589
                                           : subscription_set->insert_or_assign(realm_query);
7✔
590
        *index = std::distance(subscription_set->begin(), it);
10✔
591
        *inserted = successful;
10✔
592
        return true;
10✔
593
    });
10✔
594
}
10✔
595

596
RLM_API bool
597
realm_sync_subscription_set_insert_or_assign_query(realm_flx_sync_mutable_subscription_set_t* subscription_set,
598
                                                   realm_query_t* query, const char* name, size_t* index,
599
                                                   bool* inserted)
600
{
8✔
601
    REALM_ASSERT(subscription_set != nullptr && query != nullptr);
8✔
602
    return wrap_err([&]() {
8✔
603
        auto realm_query = add_ordering_to_realm_query(query->get_query(), query->get_ordering());
8✔
604
        const auto [it, successful] = name ? subscription_set->insert_or_assign(name, realm_query)
4✔
605
                                           : subscription_set->insert_or_assign(realm_query);
8✔
606
        *index = std::distance(subscription_set->begin(), it);
8✔
607
        *inserted = successful;
8✔
608
        return true;
8✔
609
    });
8✔
610
}
8✔
611

612
RLM_API bool realm_sync_subscription_set_erase_by_id(realm_flx_sync_mutable_subscription_set_t* subscription_set,
613
                                                     const realm_object_id_t* id, bool* erased)
614
{
2✔
615
    REALM_ASSERT(subscription_set != nullptr && id != nullptr);
2✔
616
    *erased = false;
2✔
617
    return wrap_err([&] {
2✔
618
        *erased = subscription_set->erase_by_id(from_capi(*id));
2✔
619
        return true;
2✔
620
    });
2✔
621
}
2✔
622

623
RLM_API bool realm_sync_subscription_set_erase_by_name(realm_flx_sync_mutable_subscription_set_t* subscription_set,
624
                                                       const char* name, bool* erased)
625
{
2✔
626
    REALM_ASSERT(subscription_set != nullptr && name != nullptr);
2✔
627
    *erased = false;
2✔
628
    return wrap_err([&]() {
2✔
629
        *erased = subscription_set->erase(name);
2✔
630
        return true;
2✔
631
    });
2✔
632
}
2✔
633

634
RLM_API bool realm_sync_subscription_set_erase_by_query(realm_flx_sync_mutable_subscription_set_t* subscription_set,
635
                                                        realm_query_t* query, bool* erased)
636
{
2✔
637
    REALM_ASSERT(subscription_set != nullptr && query != nullptr);
2✔
638
    *erased = false;
2✔
639
    return wrap_err([&]() {
2✔
640
        auto realm_query = add_ordering_to_realm_query(query->get_query(), query->get_ordering());
2✔
641
        *erased = subscription_set->erase(realm_query);
2✔
642
        return true;
2✔
643
    });
2✔
644
}
2✔
645

646
RLM_API bool realm_sync_subscription_set_erase_by_results(realm_flx_sync_mutable_subscription_set_t* subscription_set,
647
                                                          realm_results_t* results, bool* erased)
648
{
2✔
649
    REALM_ASSERT(subscription_set != nullptr && results != nullptr);
2✔
650
    *erased = false;
2✔
651
    return wrap_err([&]() {
2✔
652
        auto realm_query = add_ordering_to_realm_query(results->get_query(), results->get_ordering());
2✔
653
        *erased = subscription_set->erase(realm_query);
2✔
654
        return true;
2✔
655
    });
2✔
656
}
2✔
657

658
RLM_API bool
659
realm_sync_subscription_set_erase_by_class_name(realm_flx_sync_mutable_subscription_set_t* subscription_set,
660
                                                const char* object_class_name, bool* erased)
661
{
4✔
662
    REALM_ASSERT(subscription_set != nullptr && object_class_name != nullptr);
4✔
663
    *erased = false;
4✔
664
    return wrap_err([&]() {
4✔
665
        *erased = subscription_set->erase_by_class_name(object_class_name);
4✔
666
        return true;
4✔
667
    });
4✔
668
}
4✔
669

670
RLM_API realm_flx_sync_subscription_set_t*
671
realm_sync_subscription_set_commit(realm_flx_sync_mutable_subscription_set_t* subscription_set)
672
{
22✔
673
    REALM_ASSERT(subscription_set != nullptr);
22✔
674
    return wrap_err([&]() {
22✔
675
        return new realm_flx_sync_subscription_set_t{std::move(*subscription_set).commit()};
22✔
676
    });
22✔
677
}
22✔
678

679
RLM_API realm_async_open_task_t* realm_open_synchronized(realm_config_t* config) noexcept
680
{
4✔
681
    return wrap_err([config] {
4✔
682
        return new realm_async_open_task_t(Realm::get_synchronized_realm(*config));
4✔
683
    });
4✔
684
}
4✔
685

686
RLM_API void realm_async_open_task_start(realm_async_open_task_t* task, realm_async_open_task_completion_func_t done,
687
                                         realm_userdata_t userdata, realm_free_userdata_func_t userdata_free) noexcept
688
{
4✔
689
    auto cb = [done, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](ThreadSafeReference realm,
4✔
690
                                                                                       std::exception_ptr error) {
4✔
691
        if (error) {
4✔
692
            realm_async_error_t c_error(std::move(error));
2✔
693
            done(userdata.get(), nullptr, &c_error);
2✔
694
        }
2✔
695
        else {
2✔
696
            auto tsr = new realm_t::thread_safe_reference(std::move(realm));
2✔
697
            done(userdata.get(), tsr, nullptr);
2✔
698
        }
2✔
699
    };
4✔
700
    (*task)->start(std::move(cb));
4✔
701
}
4✔
702

703
RLM_API void realm_async_open_task_cancel(realm_async_open_task_t* task) noexcept
704
{
×
705
    (*task)->cancel();
×
706
}
×
707

708
RLM_API realm_async_open_task_progress_notification_token_t*
709
realm_async_open_task_register_download_progress_notifier(realm_async_open_task_t* task,
710
                                                          realm_sync_progress_func_t notifier,
711
                                                          realm_userdata_t userdata,
712
                                                          realm_free_userdata_func_t userdata_free) noexcept
713
{
×
714
    auto cb = [notifier, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](uint64_t transferred,
×
715
                                                                                           uint64_t transferrable) {
×
716
        notifier(userdata.get(), transferred, transferrable);
×
717
    };
×
718
    auto token = (*task)->register_download_progress_notifier(std::move(cb));
×
719
    return new realm_async_open_task_progress_notification_token_t{(*task), token};
×
720
}
×
721

722
RLM_API realm_sync_session_t* realm_sync_session_get(const realm_t* realm) noexcept
723
{
×
724
    if (auto session = (*realm)->sync_session()) {
×
725
        return new realm_sync_session_t(std::move(session));
×
726
    }
×
727

728
    return nullptr;
×
729
}
×
730

731
RLM_API realm_sync_session_state_e realm_sync_session_get_state(const realm_sync_session_t* session) noexcept
732
{
×
733
    return realm_sync_session_state_e((*session)->state());
×
734
}
×
735

736
RLM_API realm_sync_connection_state_e
737
realm_sync_session_get_connection_state(const realm_sync_session_t* session) noexcept
738
{
×
739
    return realm_sync_connection_state_e((*session)->connection_state());
×
740
}
×
741

742
RLM_API realm_user_t* realm_sync_session_get_user(const realm_sync_session_t* session) noexcept
743
{
×
744
    return new realm_user_t((*session)->user());
×
745
}
×
746

747
RLM_API const char* realm_sync_session_get_partition_value(const realm_sync_session_t* session) noexcept
748
{
×
749
    return (*session)->config().partition_value.c_str();
×
750
}
×
751

752
RLM_API const char* realm_sync_session_get_file_path(const realm_sync_session_t* session) noexcept
753
{
×
754
    return (*session)->path().c_str();
×
755
}
×
756

757
RLM_API void realm_sync_session_pause(realm_sync_session_t* session) noexcept
758
{
×
759
    (*session)->pause();
×
760
}
×
761

762
RLM_API void realm_sync_session_resume(realm_sync_session_t* session) noexcept
763
{
×
764
    (*session)->resume();
×
765
}
×
766

767
RLM_API void realm_sync_session_get_file_ident(realm_sync_session_t* session, realm_salted_file_ident_t* out) noexcept
NEW
768
{
×
NEW
769
    auto file_ident = (*session)->get_file_ident();
×
NEW
770
    out->ident = file_ident.ident;
×
NEW
771
    out->salt = file_ident.salt;
×
NEW
772
}
×
773

774
RLM_API bool realm_sync_immediately_run_file_actions(realm_app_t* realm_app, const char* sync_path,
775
                                                     bool* did_run) noexcept
776
{
8✔
777
    return wrap_err([&]() {
8✔
778
        *did_run = (*realm_app)->sync_manager()->immediately_run_file_actions(sync_path);
8✔
779
        return true;
8✔
780
    });
8✔
781
}
8✔
782

783
RLM_API realm_sync_session_connection_state_notification_token_t*
784
realm_sync_session_register_connection_state_change_callback(realm_sync_session_t* session,
785
                                                             realm_sync_connection_state_changed_func_t callback,
786
                                                             realm_userdata_t userdata,
787
                                                             realm_free_userdata_func_t userdata_free) noexcept
788
{
×
789
    std::function<realm::SyncSession::ConnectionStateChangeCallback> cb =
×
790
        [callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](auto old_state, auto new_state) {
×
791
            callback(userdata.get(), realm_sync_connection_state_e(old_state),
×
792
                     realm_sync_connection_state_e(new_state));
×
793
        };
×
794
    auto token = (*session)->register_connection_change_callback(std::move(cb));
×
795
    return new realm_sync_session_connection_state_notification_token_t{(*session), token};
×
796
}
×
797

798
RLM_API realm_sync_session_connection_state_notification_token_t* realm_sync_session_register_progress_notifier(
799
    realm_sync_session_t* session, realm_sync_progress_func_t notifier, realm_sync_progress_direction_e direction,
800
    bool is_streaming, realm_userdata_t userdata, realm_free_userdata_func_t userdata_free) noexcept
801
{
×
802
    std::function<realm::SyncSession::ProgressNotifierCallback> cb =
×
803
        [notifier, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](uint64_t transferred,
×
804
                                                                                     uint64_t transferrable) {
×
805
            notifier(userdata.get(), transferred, transferrable);
×
806
        };
×
807
    auto token = (*session)->register_progress_notifier(std::move(cb), SyncSession::ProgressDirection(direction),
×
808
                                                        is_streaming);
×
809
    return new realm_sync_session_connection_state_notification_token_t{(*session), token};
×
810
}
×
811

812
RLM_API void realm_sync_session_wait_for_download_completion(realm_sync_session_t* session,
813
                                                             realm_sync_wait_for_completion_func_t done,
814
                                                             realm_userdata_t userdata,
815
                                                             realm_free_userdata_func_t userdata_free) noexcept
816
{
×
817
    util::UniqueFunction<void(Status)> cb =
×
818
        [done, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](Status s) {
×
819
            if (!s.is_ok()) {
×
820
                realm_error_t error = to_capi(s);
×
821
                done(userdata.get(), &error);
×
822
            }
×
823
            else {
×
824
                done(userdata.get(), nullptr);
×
825
            }
×
826
        };
×
827
    (*session)->wait_for_download_completion(std::move(cb));
×
828
}
×
829

830
RLM_API void realm_sync_session_wait_for_upload_completion(realm_sync_session_t* session,
831
                                                           realm_sync_wait_for_completion_func_t done,
832
                                                           realm_userdata_t userdata,
833
                                                           realm_free_userdata_func_t userdata_free) noexcept
834
{
×
835
    util::UniqueFunction<void(Status)> cb =
×
836
        [done, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](Status s) {
×
837
            if (!s.is_ok()) {
×
838
                realm_error_t error = to_capi(s);
×
839
                done(userdata.get(), &error);
×
840
            }
×
841
            else {
×
842
                done(userdata.get(), nullptr);
×
843
            }
×
844
        };
×
845
    (*session)->wait_for_upload_completion(std::move(cb));
×
846
}
×
847

848
RLM_API void realm_sync_session_handle_error_for_testing(const realm_sync_session_t* session,
849
                                                         realm_errno_e error_code, const char* error_str,
850
                                                         bool is_fatal)
851
{
×
852
    REALM_ASSERT(session);
×
853
    SyncSession::OnlyForTesting::handle_error(
×
854
        *session->get(),
×
855
        sync::SessionErrorInfo{Status{static_cast<ErrorCodes::Error>(error_code), error_str}, !is_fatal});
×
856
}
×
857

858
} // namespace realm::c_api
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc