Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f9bc1b05 authored by Tianjie Xu's avatar Tianjie Xu
Browse files

Add the Release function for TemporaryFiles

Some tests may create a File* by calling fdopen() on the temp file's
fd. We should release the ownership of fd in this case to avoid the
double close.

Bug: 65430057
Test: libbase unit tests pass
Change-Id: I54fcce2029f9a574f53afdbdda737ee58620c73a
parent cfadedb1
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -26,6 +26,10 @@ class TemporaryFile {
  TemporaryFile();
  ~TemporaryFile();

  // Release the ownership of fd, caller is reponsible for closing the
  // fd or stream properly.
  int release();

  int fd;
  char path[1024];

+9 −1
Original line number Diff line number Diff line
@@ -85,10 +85,18 @@ TemporaryFile::TemporaryFile() {
}

TemporaryFile::~TemporaryFile() {
  if (fd != -1) {
    close(fd);
  }
  unlink(path);
}

int TemporaryFile::release() {
  int result = fd;
  fd = -1;
  return result;
}

void TemporaryFile::init(const std::string& tmp_dir) {
  snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(),
           OS_PATH_SEPARATOR);