Skip to content

Commit

Permalink
Use open() with O_TMPFILE to create the cache file
Browse files Browse the repository at this point in the history
  • Loading branch information
fdegros committed Aug 8, 2022
1 parent d320ad6 commit 0ce80a7
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions lib/reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <unistd.h>

#include "error.h"
#include "path.h"
#include "scoped_file.h"

zip_int64_t Reader::reader_count_ = 0;
Expand Down Expand Up @@ -101,40 +100,19 @@ class CacheFileReader : public UnbufferedReader {

private:
// Creates a new, empty and hidden cache file.
// Throws std::runtime_error in case of error.
// Throws std::system_error in case of error.
static ScopedFile CreateCacheFile() {
char name[] = "/tmp/XXXXXX";
ScopedFile file(open("/tmp", O_TMPFILE | O_RDWR | O_EXCL, 0));

// Create cache file.
ScopedFile file(mkstemp(name));
if (!file.IsValid())
ThrowSystemError("Cannot create cache file ", Path(name));

Log(LOG_DEBUG, "Created cache file ", Path(name));

// Unlink cache file.
if (unlink(name) < 0)
ThrowSystemError("Cannot unlink cache file ", Path(name));

// Assert that the cache file doesn't have any links to it.
struct stat st;
if (fstat(file.GetDescriptor(), &st) < 0)
ThrowSystemError("Cannot stat cache file ", Path(name));

if (st.st_nlink != 0)
throw std::runtime_error(
StrCat("Cache file ", Path(name), " has ", st.st_nlink, " links"));

if (st.st_size != 0)
throw std::runtime_error(
StrCat("Cache file ", Path(name), " is not empty"));
ThrowSystemError("Cannot create cache file");

return file;
}

// Gets the file descriptor to the global cache file.
// Creates this cache file if necessary.
// Throws std::runtime_error in case of error.
// Throws std::system_error in case of error.
static int GetCacheFile() {
static const ScopedFile file = CreateCacheFile();
return file.GetDescriptor();
Expand Down

0 comments on commit 0ce80a7

Please sign in to comment.