Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the env TEMPDIR if available instead of P_tmpdir. #4921

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Fixed forgetting to insert a backlink when inserting a mixed link directly using Table::FieldValues. ([#4899](https://github.com/realm/realm-core/issues/4899) since the introduction of Mixed in v11.0.0)
* Using "sort", "distinct", or "limit" as field name in query expression would cause an "Invalid predicate" error ([#7545](https://github.com/realm/realm-java/issues/7545) since v10.1.2)
* Crash when quering with 'Not()' followed by empty group. ([#4168](https://github.com/realm/realm-core/issues/4168) since v1.0.0)
* Change Apple/Linux temp dir created with `util::make_temp_dir()` to default to the environment's TMPDIR if available. Make `TestFile` clean up the directories it creates when finished. ([#4921](https://github.com/realm/realm-core/issues/4921))

### Breaking changes
* `App::Config::transport_factory` was replaced with `App::Config::transport`. It should now be an instance of `GenericNetworkTransport` rather than a factory for making instances. This allows the SDK to control which thread constructs the transport layer. ([#4903](https://github.com/realm/realm-core/pull/4903))
Expand Down
7 changes: 6 additions & 1 deletion src/realm/util/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,12 @@ std::string make_temp_dir()
}
return std::string(buffer);
#else
std::string tmp = std::string(P_tmpdir) + std::string("/realm_XXXXXX") + std::string("\0", 1);
char* tmp_dir_env = getenv("TMPDIR");
std::string base_dir = tmp_dir_env ? tmp_dir_env : std::string(P_tmpdir);
if (!base_dir.empty() && base_dir[base_dir.length() - 1] != '/') {
base_dir = base_dir + "/";
}
std::string tmp = base_dir + std::string("realm_XXXXXX") + std::string("\0", 1);
std::unique_ptr<char[]> buffer = std::make_unique<char[]>(tmp.size()); // Throws
memcpy(buffer.get(), tmp.c_str(), tmp.size());
if (mkdtemp(buffer.get()) == 0) {
Expand Down
7 changes: 3 additions & 4 deletions test/object-store/migrations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2266,13 +2266,12 @@ TEST_CASE("migration: AdditiveDiscovered") {
}},
};

TestFile config;
config.cache = false;
config.schema = schema;

std::vector<SchemaMode> additive_modes = {SchemaMode::AdditiveDiscovered, SchemaMode::AdditiveExplicit};

for (auto mode : additive_modes) {
TestFile config;
config.cache = false;
config.schema = schema;
config.schema_mode = mode;
auto realm = Realm::get_shared_realm(config);
realm->update_schema(schema);
Expand Down
16 changes: 9 additions & 7 deletions test/object-store/util/test_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ using namespace realm;
TestFile::TestFile()
{
disable_sync_to_disk();
path = util::format("%1/realm.XXXXXX", util::make_temp_dir());
m_temp_dir = util::make_temp_dir();
if (m_temp_dir.size() == 0 || m_temp_dir[m_temp_dir.size() - 1] != '/') {
m_temp_dir = m_temp_dir + "/";
}
path = util::format("%1realm.XXXXXX", m_temp_dir);
int fd = mkstemp(path.data());
if (fd < 0) {
int err = errno;
Expand All @@ -80,12 +84,10 @@ TestFile::TestFile()

TestFile::~TestFile()
{
if (!m_persist)
#ifdef _WIN32
_unlink(path.c_str());
#else // POSIX
unlink(path.c_str());
#endif
if (!m_persist) {
util::File::try_remove(path);
util::try_remove_dir_recursive(m_temp_dir);
}
}

DBOptions TestFile::options() const
Expand Down
1 change: 1 addition & 0 deletions test/object-store/util/test_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ struct TestFile : realm::Realm::Config {

private:
bool m_persist = false;
std::string m_temp_dir;
};

struct InMemoryTestFile : TestFile {
Expand Down