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

realm / realm-core / 2536

01 Aug 2024 07:02PM UTC coverage: 91.088% (-0.03%) from 91.121%
2536

push

Evergreen

web-flow
Prepare for release 14.11.2 (#7939)

Co-authored-by: nicola-cab <1497069+nicola-cab@users.noreply.github.com>

102758 of 181570 branches covered (56.59%)

216794 of 238004 relevant lines covered (91.09%)

5893031.7 hits per line

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

85.11
/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
{
59,688✔
34
#ifndef _WIN32
59,688✔
35
    struct stat stat_buf;
59,688✔
36
    if (stat(path.data(), &stat_buf) == 0) {
59,688✔
37
        if ((stat_buf.st_mode & S_IFMT) != S_IFIFO) {
59,688✔
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
    }
59,688✔
44
#endif
59,688✔
45
}
59,688✔
46
} // Anonymous namespace
47

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

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

88
bool try_create_fifo(std::string_view path, bool has_more_fallbacks)
89
{
112,960✔
90
    if (has_more_fallbacks) {
112,960✔
91
        try {
112,960✔
92
            create_fifo(path);
112,960✔
93
            return true;
112,960✔
94
        }
112,960✔
95
        catch (...) {
112,960✔
96
            return false;
12✔
97
        }
12✔
98
    }
112,960✔
99
    else {
×
100
        create_fifo(path);
×
101
        return true;
×
102
    }
×
103
}
112,960✔
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

© 2026 Coveralls, Inc