Create temporary files in the system's temp directory.
In Brillo, the temporary directory might not be /tmp. This patch uses
base::GetTempDir() instead and removes the hard-coded mentions of /tmp
when creating temporary directories.
Bug: 22024447
Test: `update_engine_client --update --omaha_url=...` mounts /system on the temp directory under /data/local/tmp
Change-Id: Ibb52cae01419511f91bdfbf6f228b74a581edde9
diff --git a/delta_performer.cc b/delta_performer.cc
index 6094c5a..2538329 100644
--- a/delta_performer.cc
+++ b/delta_performer.cc
@@ -877,7 +877,7 @@
&output_positions));
string temp_filename;
- TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/au_patch.XXXXXX",
+ TEST_AND_RETURN_FALSE(utils::MakeTempFile("au_patch.XXXXXX",
&temp_filename,
nullptr));
ScopedPathUnlinker path_unlinker(temp_filename);
@@ -943,7 +943,7 @@
&output_positions));
string temp_filename;
- TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/au_patch.XXXXXX",
+ TEST_AND_RETURN_FALSE(utils::MakeTempFile("au_patch.XXXXXX",
&temp_filename,
nullptr));
ScopedPathUnlinker path_unlinker(temp_filename);
diff --git a/fake_p2p_manager_configuration.h b/fake_p2p_manager_configuration.h
index f639851..6874efb 100644
--- a/fake_p2p_manager_configuration.h
+++ b/fake_p2p_manager_configuration.h
@@ -35,7 +35,7 @@
class FakeP2PManagerConfiguration : public P2PManager::Configuration {
public:
FakeP2PManagerConfiguration() {
- EXPECT_TRUE(utils::MakeTempDirectory("/tmp/p2p-tc.XXXXXX", &p2p_dir_));
+ EXPECT_TRUE(utils::MakeTempDirectory("p2p-tc.XXXXXX", &p2p_dir_));
}
~FakeP2PManagerConfiguration() {
diff --git a/postinstall_runner_action.cc b/postinstall_runner_action.cc
index 184d66d..c6879d8 100644
--- a/postinstall_runner_action.cc
+++ b/postinstall_runner_action.cc
@@ -48,8 +48,8 @@
ScopedActionCompleter completer(processor_, this);
// Make mountpoint.
- TEST_AND_RETURN(utils::MakeTempDirectory("/tmp/au_postint_mount.XXXXXX",
- &temp_rootfs_dir_));
+ TEST_AND_RETURN(
+ utils::MakeTempDirectory("au_postint_mount.XXXXXX", &temp_rootfs_dir_));
ScopedDirRemover temp_dir_remover(temp_rootfs_dir_);
const string mountable_device =
diff --git a/test_utils.h b/test_utils.h
index 7a77299..6108b6f 100644
--- a/test_utils.h
+++ b/test_utils.h
@@ -162,7 +162,7 @@
class ScopedTempFile {
public:
ScopedTempFile() {
- EXPECT_TRUE(utils::MakeTempFile("/tmp/update_engine_test_temp_file.XXXXXX",
+ EXPECT_TRUE(utils::MakeTempFile("update_engine_test_temp_file.XXXXXX",
&path_,
nullptr));
unlinker_.reset(new ScopedPathUnlinker(path_));
diff --git a/utils.cc b/utils.cc
index cf7913c..6290478 100644
--- a/utils.cc
+++ b/utils.cc
@@ -130,6 +130,23 @@
}
}
+// If |path| is absolute, or explicit relative to the current working directory,
+// leaves it as is. Otherwise, uses the system's temp directory, as defined by
+// base::GetTempDir() and prepends it to |path|. On success stores the full
+// temporary path in |template_path| and returns true.
+bool GetTempName(const string& path, base::FilePath* template_path) {
+ if (path[0] == '/' || base::StartsWithASCII(path, "./", true) ||
+ base::StartsWithASCII(path, "../", true)) {
+ *template_path = base::FilePath(path);
+ return true;
+ }
+
+ base::FilePath temp_dir;
+ TEST_AND_RETURN_FALSE(base::GetTempDir(&temp_dir));
+ *template_path = temp_dir.Append(path);
+ return true;
+}
+
} // namespace
namespace utils {
@@ -565,28 +582,17 @@
return FileExists(volume_path.c_str());
}
-// If |path| is absolute, or explicit relative to the current working directory,
-// leaves it as is. Otherwise, if TMPDIR is defined in the environment and is
-// non-empty, prepends it to |path|. Otherwise, prepends /tmp. Returns the
-// resulting path.
-static const string PrependTmpdir(const string& path) {
- if (path[0] == '/' || base::StartsWithASCII(path, "./", true) ||
- base::StartsWithASCII(path, "../", true))
- return path;
-
- const char *tmpdir = getenv("TMPDIR");
- const string prefix = (tmpdir && *tmpdir ? tmpdir : "/tmp");
- return prefix + "/" + path;
-}
-
bool MakeTempFile(const string& base_filename_template,
string* filename,
int* fd) {
- const string filename_template = PrependTmpdir(base_filename_template);
+ base::FilePath filename_template;
+ TEST_AND_RETURN_FALSE(
+ GetTempName(base_filename_template, &filename_template));
DCHECK(filename || fd);
- vector<char> buf(filename_template.size() + 1);
- memcpy(buf.data(), filename_template.data(), filename_template.size());
- buf[filename_template.size()] = '\0';
+ vector<char> buf(filename_template.value().size() + 1);
+ memcpy(buf.data(), filename_template.value().data(),
+ filename_template.value().size());
+ buf[filename_template.value().size()] = '\0';
int mkstemp_fd = mkstemp(buf.data());
TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0);
@@ -603,11 +609,13 @@
bool MakeTempDirectory(const string& base_dirname_template,
string* dirname) {
- const string dirname_template = PrependTmpdir(base_dirname_template);
+ base::FilePath dirname_template;
+ TEST_AND_RETURN_FALSE(GetTempName(base_dirname_template, &dirname_template));
DCHECK(dirname);
- vector<char> buf(dirname_template.size() + 1);
- memcpy(buf.data(), dirname_template.data(), dirname_template.size());
- buf[dirname_template.size()] = '\0';
+ vector<char> buf(dirname_template.value().size() + 1);
+ memcpy(buf.data(), dirname_template.value().data(),
+ dirname_template.value().size());
+ buf[dirname_template.value().size()] = '\0';
char* return_code = mkdtemp(buf.data());
TEST_AND_RETURN_FALSE_ERRNO(return_code != nullptr);
diff --git a/utils.h b/utils.h
index 6668c39..252feab 100644
--- a/utils.h
+++ b/utils.h
@@ -131,9 +131,8 @@
// If |base_filename_template| is neither absolute (starts with "/") nor
// explicitly relative to the current working directory (starts with "./" or
-// "../"), then it is prepended the value of TMPDIR, which defaults to /tmp if
-// it isn't set or is empty. It then calls mkstemp(3) with the resulting
-// template. Writes the name of a new temporary file to |filename|. If |fd| is
+// "../"), then it is prepended the system's temporary directory. On success,
+// stores the name of the new temporary file in |filename|. If |fd| is
// non-null, the file descriptor returned by mkstemp is written to it and
// kept open; otherwise, it is closed. The template must end with "XXXXXX".
// Returns true on success.
@@ -141,12 +140,11 @@
std::string* filename,
int* fd);
-// If |base_filename_template| is neither absolute (starts with "/") nor
+// If |base_dirname_template| is neither absolute (starts with "/") nor
// explicitly relative to the current working directory (starts with "./" or
-// "../"), then it is prepended the value of TMPDIR, which defaults to /tmp if
-// it isn't set or is empty. It then calls mkdtemp() with the resulting
-// template. Writes the name of the new temporary directory to |dirname|.
-// The template must end with "XXXXXX". Returns true on success.
+// "../"), then it is prepended the system's temporary directory. On success,
+// stores the name of the new temporary directory in |dirname|. The template
+// must end with "XXXXXX". Returns true on success.
bool MakeTempDirectory(const std::string& base_dirname_template,
std::string* dirname);