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

realm / realm-core / 2214

10 Apr 2024 11:21PM UTC coverage: 91.813% (-0.8%) from 92.623%
2214

push

Evergreen

web-flow
Add missing availability checks for SecCopyErrorMessageString (#7577)

This requires iOS 11.3 and we currently target iOS 11.

94848 of 175770 branches covered (53.96%)

7 of 22 new or added lines in 2 files covered. (31.82%)

1815 existing lines in 77 files now uncovered.

242945 of 264608 relevant lines covered (91.81%)

6136478.37 hits per line

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

63.2
/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_app_user_subscription_token::~realm_app_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(app::AppConfig::MetadataMode::NoEncryption) ==
51
              RLM_SYNC_CLIENT_METADATA_MODE_PLAINTEXT);
52
static_assert(realm_sync_client_metadata_mode_e(app::AppConfig::MetadataMode::Encryption) ==
53
              RLM_SYNC_CLIENT_METADATA_MODE_ENCRYPTED);
54
static_assert(realm_sync_client_metadata_mode_e(app::AppConfig::MetadataMode::InMemory) ==
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_reconnect_mode(realm_sync_client_config_t* config,
145
                                                         realm_sync_client_reconnect_mode_e mode) noexcept
146
{
2✔
147
    config->reconnect_mode = ReconnectMode(mode);
2✔
148
}
2✔
149
RLM_API void realm_sync_client_config_set_multiplex_sessions(realm_sync_client_config_t* config,
150
                                                             bool multiplex) noexcept
151
{
4✔
152
    config->multiplex_sessions = multiplex;
4✔
153
}
4✔
154

155
RLM_API void realm_sync_client_config_set_user_agent_binding_info(realm_sync_client_config_t* config,
156
                                                                  const char* info) noexcept
157
{
2✔
158
    config->user_agent_binding_info = info;
2✔
159
}
2✔
160

161
RLM_API void realm_sync_client_config_set_user_agent_application_info(realm_sync_client_config_t* config,
162
                                                                      const char* info) noexcept
163
{
2✔
164
    config->user_agent_application_info = info;
2✔
165
}
2✔
166

167
RLM_API void realm_sync_client_config_set_connect_timeout(realm_sync_client_config_t* config,
168
                                                          uint64_t timeout) noexcept
169
{
2✔
170
    config->timeouts.connect_timeout = timeout;
2✔
171
}
2✔
172

173
RLM_API void realm_sync_client_config_set_connection_linger_time(realm_sync_client_config_t* config,
174
                                                                 uint64_t time) noexcept
175
{
2✔
176
    config->timeouts.connection_linger_time = time;
2✔
177
}
2✔
178

179
RLM_API void realm_sync_client_config_set_ping_keepalive_period(realm_sync_client_config_t* config,
180
                                                                uint64_t period) noexcept
181
{
2✔
182
    config->timeouts.ping_keepalive_period = period;
2✔
183
}
2✔
184

185
RLM_API void realm_sync_client_config_set_pong_keepalive_timeout(realm_sync_client_config_t* config,
186
                                                                 uint64_t timeout) noexcept
187
{
2✔
188
    config->timeouts.pong_keepalive_timeout = timeout;
2✔
189
}
2✔
190

191
RLM_API void realm_sync_client_config_set_fast_reconnect_limit(realm_sync_client_config_t* config,
192
                                                               uint64_t limit) noexcept
193
{
2✔
194
    config->timeouts.fast_reconnect_limit = limit;
2✔
195
}
2✔
196

197
RLM_API void realm_sync_client_config_set_resumption_delay_interval(realm_sync_client_config_t* config,
198
                                                                    uint64_t interval) noexcept
199
{
2✔
200
    config->timeouts.reconnect_backoff_info.resumption_delay_interval = std::chrono::milliseconds{interval};
2✔
201
}
2✔
202

203
RLM_API void realm_sync_client_config_set_max_resumption_delay_interval(realm_sync_client_config_t* config,
204
                                                                        uint64_t interval) noexcept
205
{
2✔
206
    config->timeouts.reconnect_backoff_info.max_resumption_delay_interval = std::chrono::milliseconds{interval};
2✔
207
}
2✔
208

209
RLM_API void realm_sync_client_config_set_resumption_delay_backoff_multiplier(realm_sync_client_config_t* config,
210
                                                                              int multiplier) noexcept
211
{
2✔
212
    config->timeouts.reconnect_backoff_info.resumption_delay_backoff_multiplier = multiplier;
2✔
213
}
2✔
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
UNCOV
246
{
×
UNCOV
247
    return new realm_sync_config(*user, realm::SyncConfig::FLXSyncEnabled{});
×
UNCOV
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
UNCOV
252
{
×
UNCOV
253
    config->stop_policy = SyncSessionStopPolicy(policy);
×
UNCOV
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
UNCOV
299
{
×
UNCOV
300
    config->client_validate_ssl = validate;
×
UNCOV
301
}
×
302

303
RLM_API void realm_sync_config_set_ssl_trust_certificate_path(realm_sync_config_t* config, const char* path) noexcept
UNCOV
304
{
×
UNCOV
305
    config->ssl_trust_certificate_path = std::string(path);
×
UNCOV
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
UNCOV
312
{
×
UNCOV
313
    auto cb = [callback, userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](
×
UNCOV
314
                  const std::string& server_address, SyncConfig::ProxyConfig::port_type server_port,
×
UNCOV
315
                  const char* pem_data, size_t pem_size, int preverify_ok, int depth) {
×
UNCOV
316
        return callback(userdata.get(), server_address.c_str(), server_port, pem_data, pem_size, preverify_ok, depth);
×
UNCOV
317
    };
×
318

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

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

327
RLM_API void realm_sync_config_set_authorization_header_name(realm_sync_config_t* config, const char* name) noexcept
UNCOV
328
{
×
UNCOV
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
UNCOV
334
{
×
UNCOV
335
    config->custom_http_headers[name] = value;
×
UNCOV
336
}
×
337

338
RLM_API void realm_sync_config_set_recovery_directory_path(realm_sync_config_t* config, const char* path) noexcept
UNCOV
339
{
×
UNCOV
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
UNCOV
363
{
×
UNCOV
364
    REALM_ASSERT(subscription != nullptr);
×
UNCOV
365
    return to_capi(subscription->object_class_name);
×
UNCOV
366
}
×
367

368
RLM_API realm_string_t
369
realm_sync_subscription_query_string(const realm_flx_sync_subscription_t* subscription) noexcept
UNCOV
370
{
×
UNCOV
371
    REALM_ASSERT(subscription != nullptr);
×
UNCOV
372
    return to_capi(subscription->query_string);
×
UNCOV
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,
4✔
424
               userdata = SharedUserdata(userdata, FreeUserdata(userdata_free))](ThreadSafeReference realm) {
2✔
UNCOV
425
        auto tsr = new realm_t::thread_safe_reference(std::move(realm));
×
UNCOV
426
        callback(tsr, userdata.get());
×
UNCOV
427
    };
×
428
    config->subscription_initializer = std::move(cb);
4✔
429
    config->rerun_init_subscription_on_open = rerun_on_open;
4✔
430
}
4✔
431

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
729
    return nullptr;
×
UNCOV
730
}
×
731

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

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

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

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

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

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

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

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

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

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

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

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

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

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

859
} // 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

© 2025 Coveralls, Inc