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

realm / realm-core / github_pull_request_279261

11 Oct 2023 02:16PM UTC coverage: 91.624% (+0.06%) from 91.563%
github_pull_request_279261

Pull #6763

Evergreen

finnschiermer
Merge branch 'master' of github.com:realm/realm-core into fsa/enhance-freelist-check
Pull Request #6763: add freelist verification at more points during commit

94332 of 173512 branches covered (0.0%)

124 of 124 new or added lines in 2 files covered. (100.0%)

29 existing lines in 10 files now uncovered.

230660 of 251746 relevant lines covered (91.62%)

6863226.4 hits per line

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

85.71
/src/realm/util/fifo_helper.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2019 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/util/fifo_helper.hpp>
20

21
#include <realm/exceptions.hpp>
22
#include <realm/util/errno.hpp>
23

24
#include <sstream>
25
#include <system_error>
26
#include <sys/stat.h>
27

28
// FIFOs do not work on Windows.
29
namespace realm::util {
30

31
namespace {
32
void check_is_fifo(std::string_view path)
33
{
116,450✔
34
#ifndef _WIN32
116,450✔
35
    struct stat stat_buf;
116,450✔
36
    if (stat(path.data(), &stat_buf) == 0) {
116,450✔
37
        if ((stat_buf.st_mode & S_IFMT) != S_IFIFO) {
116,450✔
38
            throw FileAccessError(
16✔
39
                ErrorCodes::FileAlreadyExists,
16✔
40
                util::format("Cannot create fifo at path '%1': a non-fifo entry already exists at that path.", path),
16✔
41
                path);
16✔
42
        }
16✔
43
    }
116,450✔
44
#endif
116,450✔
45
}
116,450✔
46
} // Anonymous namespace
47

48
void create_fifo(std::string_view path)
49
{
177,074✔
50
#ifndef _WIN32
177,074✔
51
#ifdef REALM_ANDROID
177,074✔
52
    // Upgrading apps on Android Huawai devices sometimes leave FIFO files with the wrong
2,822✔
53
    // file owners. This results in the Android sandbox preventing Realm from opening the
2,822✔
54
    // database file. To prevent this from happening we create all FIFO files with full permissions.
2,822✔
55
    // This should be safe as the app is already running inside a protected part of the filesystem,
2,822✔
56
    // so no outside users have access to the filesystem where these files are.
2,822✔
57
    // It does make storing Realms on external storage slightly more unsafe, but users already have
2,822✔
58
    // full access to modify or copy files from there, so there should be no change from a pratical
2,822✔
59
    // security standpoint.
2,822✔
60
    // See more here: https://github.com/realm/realm-java/issues/3972#issuecomment-313675948
2,822✔
61
    mode_t mode = 0666;
177,074✔
62
#else
63
    mode_t mode = 0600;
64
#endif
65

2,822✔
66
    // Create and open the named pipe
2,822✔
67
    int ret = mkfifo(path.data(), mode);
177,074✔
68
    if (ret == -1) {
177,074✔
69
        int err = errno;
116,450✔
70
#ifdef REALM_ANDROID
116,450✔
71
        // Workaround for a mkfifo bug on Blackberry devices:
254✔
72
        // When the fifo already exists, mkfifo fails with error ENOSYS which is not correct.
254✔
73
        // In this case, we use stat to check if the path exists and it is a fifo.
254✔
74
        if (err == ENOSYS) {
116,450✔
75
            err = EEXIST;
×
76
        }
×
77
#endif
116,450✔
78
        // the fifo already existing isn't an error
254✔
79
        if (err == EEXIST) {
116,450✔
80
            // If the file already exists, verify it is a FIFO
254✔
81
            return check_is_fifo(path);
116,450✔
82
        }
116,450✔
83
        throw SystemError(err, format_errno("Failed to create fifo at '%2': %1", err, path));
×
84
    }
×
85
#endif
177,074✔
86
}
177,074✔
87

88
bool try_create_fifo(std::string_view path, bool has_more_fallbacks)
89
{
177,070✔
90
    if (has_more_fallbacks) {
177,070✔
91
        try {
177,070✔
92
            create_fifo(path);
177,070✔
93
            return true;
177,070✔
94
        }
177,070✔
95
        catch (...) {
12✔
96
            return false;
12✔
97
        }
12✔
UNCOV
98
    }
×
UNCOV
99
    else {
×
UNCOV
100
        create_fifo(path);
×
UNCOV
101
        return true;
×
UNCOV
102
    }
×
103
}
177,070✔
104

105
} // namespace realm::util
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