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

realm / realm-core / thomas.goyne_442

02 Jul 2024 07:51PM UTC coverage: 90.995% (+0.02%) from 90.974%
thomas.goyne_442

push

Evergreen

web-flow
[RCORE-2146] CAPI Remove `is_fatal` flag flip (#7751)

102372 of 180620 branches covered (56.68%)

0 of 1 new or added line in 1 file covered. (0.0%)

625 existing lines in 26 files now uncovered.

215592 of 236928 relevant lines covered (90.99%)

5608163.57 hits per line

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

87.5
/src/realm/sync/noinst/client_reset_operation.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 implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#include <realm/sync/noinst/client_reset_operation.hpp>
20

21
#include <realm/sync/history.hpp>
22
#include <realm/sync/noinst/client_history_impl.hpp>
23
#include <realm/sync/noinst/client_reset.hpp>
24
#include <realm/transaction.hpp>
25
#include <realm/util/scope_exit.hpp>
26

27
namespace realm::_impl::client_reset {
28

29
namespace {
30

31
constexpr static std::string_view c_fresh_suffix(".fresh");
32

33
} // namespace
34

35
std::string get_fresh_path_for(const std::string& path)
36
{
178,026✔
37
    const size_t suffix_len = c_fresh_suffix.size();
178,026✔
38
    REALM_ASSERT(path.length());
178,026✔
39
    REALM_ASSERT_DEBUG_EX(
178,026✔
40
        path.size() < suffix_len || path.substr(path.size() - suffix_len, suffix_len) != c_fresh_suffix, path);
178,026✔
41
    return path + c_fresh_suffix.data();
178,026✔
42
}
178,026✔
43

44
bool is_fresh_path(const std::string& path)
45
{
4,060✔
46
    const size_t suffix_len = c_fresh_suffix.size();
4,060✔
47
    REALM_ASSERT(path.length());
4,060✔
48
    if (path.size() < suffix_len) {
4,060✔
49
        return false;
×
50
    }
×
51
    return path.substr(path.size() - suffix_len, suffix_len) == c_fresh_suffix;
4,060✔
52
}
4,060✔
53

54
bool perform_client_reset(util::Logger& logger, DB& db, sync::ClientReset&& reset_config,
55
                          sync::SaltedFileIdent new_file_ident, sync::SubscriptionStore* sub_store,
56
                          util::FunctionRef<void(int64_t)> on_flx_version)
472✔
57
{
472✔
58
    REALM_ASSERT(reset_config.mode != ClientResyncMode::Manual);
472✔
59
    REALM_ASSERT(reset_config.fresh_copy);
472✔
60
    logger.debug(util::LogCategory::reset,
472✔
61
                 "Possibly beginning client reset operation: realm_path = %1, mode = %2, action = %3, error = %4",
472✔
62
                 db.get_path(), reset_config.mode, reset_config.action, reset_config.error);
63

472✔
64
    auto always_try_clean_up = util::make_scope_exit([&]() noexcept {
472✔
65
        std::string path_to_clean = reset_config.fresh_copy->get_path();
472✔
66
        try {
472✔
67
            reset_config.fresh_copy->close();
472✔
68
            constexpr bool delete_lockfile = true;
472✔
69
            DB::delete_files(path_to_clean, nullptr, delete_lockfile);
472✔
70
        }
472✔
UNCOV
71
        catch (const std::exception& err) {
×
72
            logger.warn(util::LogCategory::reset,
×
73
                        "In ClientResetOperation::finalize, the fresh copy '%1' could not be cleaned up due to "
×
74
                        "an exception: '%2'",
×
75
                        path_to_clean, err.what());
UNCOV
76
            // ignored, this is just a best effort
×
77
        }
472✔
78
    });
79

80
    // only do the reset if there is data to reset
81
    // if there is nothing in this Realm, then there is nothing to reset and
82
    // sync should be able to continue as normal
472✔
83
    auto latest_version = db.get_version_id_of_latest_snapshot();
472✔
84
    bool local_realm_exists = latest_version.version > 1;
472✔
85
    if (!local_realm_exists) {
4✔
86
        logger.debug(util::LogCategory::reset,
4✔
87
                     "Local Realm file has never been written to, so skipping client reset.");
4✔
88
        return false;
4✔
89
    }
90

468✔
91
    auto notify_before = std::move(reset_config.notify_before_client_reset);
468✔
92
    auto notify_after = std::move(reset_config.notify_after_client_reset);
93

468✔
94
    VersionID frozen_before_state_version = notify_before ? notify_before() : latest_version;
95

96
    // If m_notify_after is set, pin the previous state to keep it around.
468✔
97
    TransactionRef previous_state;
468✔
98
    if (notify_after) {
268✔
99
        previous_state = db.start_frozen(frozen_before_state_version);
268✔
100
    }
468✔
101
    bool did_recover = client_reset::perform_client_reset_diff(db, reset_config, new_file_ident, logger, sub_store,
468✔
102
                                                               on_flx_version); // throws
103

468✔
104
    if (notify_after) {
208✔
105
        notify_after(previous_state->get_version_of_current_transaction(), did_recover);
208✔
106
    }
107

468✔
108
    return true;
472✔
109
}
110

111
} // namespace realm::_impl::client_reset
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