openMSX
LocalFileReference.cc
Go to the documentation of this file.
1 #include "LocalFileReference.hh"
2 #include "File.hh"
3 #include "Filename.hh"
4 #include "FileOperations.hh"
5 #include "FileException.hh"
6 #include "build-info.hh"
7 #include <cstdio>
8 #include <cassert>
9 
10 namespace openmsx {
11 
13 {
14  init(file);
15 }
16 
18 {
19  File file(filename);
20  init(file);
21 }
22 
24  : LocalFileReference(filename.getResolved())
25 {
26 }
27 
29  : tmpFile(std::move(other.tmpFile))
30  , tmpDir (std::move(other.tmpDir ))
31 {
32  other.tmpDir.clear();
33 }
34 
36 {
37  cleanup();
38  tmpFile = std::move(other.tmpFile);
39  tmpDir = std::move(other.tmpDir);
40  other.tmpDir.clear();
41  return *this;
42 }
43 
45 {
46  cleanup();
47 }
48 
49 void LocalFileReference::init(File& file)
50 {
51  tmpFile = file.getLocalReference();
52  if (!tmpFile.empty()) {
53  // file is backed on the (local) filesystem,
54  // we can simply use the path to that file
55  assert(tmpDir.empty()); // no need to delete file/dir later
56  return;
57  }
58 
59  // create temp dir
60 #if defined(_WIN32) || PLATFORM_ANDROID
62 #else
63  // TODO - why not just use getTempDir()?
64  tmpDir = strCat("/tmp/openmsx.", int(getpid()));
65 #endif
66  // it's possible this directory already exists, in that case the
67  // following function does nothing
68  FileOperations::mkdirp(tmpDir);
69 
70  // create temp file
71  auto fp = FileOperations::openUniqueFile(tmpDir, tmpFile);
72  if (!fp) {
73  throw FileException("Couldn't create temp file");
74  }
75 
76  // write temp file
77  auto mmap = file.mmap();
78  if (fwrite(mmap.data(), 1, mmap.size(), fp.get()) != mmap.size()) {
79  throw FileException("Couldn't write temp file");
80  }
81 }
82 
83 void LocalFileReference::cleanup()
84 {
85  if (!tmpDir.empty()) {
86  FileOperations::unlink(tmpFile);
87  // it's possible the directory is not empty, in that case
88  // the following function will fail, we ignore that error
89  FileOperations::rmdir(tmpDir);
90  }
91 }
92 
93 const std::string& LocalFileReference::getFilename() const
94 {
95  assert(!tmpFile.empty());
96  return tmpFile;
97 }
98 
99 } // namespace openmsx
FileException.hh
openmsx::LocalFileReference
Helper class to use files in APIs other than openmsx::File.
Definition: LocalFileReference.hh:30
openmsx::File::mmap
span< uint8_t > mmap()
Map file in memory.
Definition: File.cc:93
openmsx::LocalFileReference::operator=
LocalFileReference & operator=(const LocalFileReference &)=delete
openmsx::LocalFileReference::getFilename
const std::string & getFilename() const
Returns path to a local uncompressed version of this file.
Definition: LocalFileReference.cc:93
openmsx::LocalFileReference::~LocalFileReference
~LocalFileReference()
Definition: LocalFileReference.cc:44
openmsx::FileOperations::unlink
int unlink(const std::string &path)
Call unlink() in a platform-independent manner.
Definition: FileOperations.cc:282
openmsx::LocalFileReference::LocalFileReference
LocalFileReference()=default
Filename.hh
LocalFileReference.hh
openmsx::FileOperations::nativePathSeparator
const char nativePathSeparator
Definition: FileOperations.hh:18
openmsx::FileOperations::mkdirp
void mkdirp(string_view path_)
Acts like the unix command "mkdir -p".
Definition: FileOperations.cc:248
File.hh
openmsx::filename
constexpr const char *const filename
Definition: FirmwareSwitch.cc:10
build-info.hh
openmsx::FileOperations::openUniqueFile
FILE_t openUniqueFile(const std::string &directory, std::string &filename)
Open a new file with a unique name in the provided directory.
Definition: FileOperations.cc:793
openmsx::FileOperations::getTempDir
string getTempDir()
Get the name of the temp directory on the system.
Definition: FileOperations.cc:762
FileOperations.hh
openmsx::File
Definition: File.hh:15
openmsx::FileOperations::rmdir
int rmdir(const std::string &path)
Call rmdir() in a platform-independent manner.
Definition: FileOperations.cc:291
openmsx::Filename
This class represents a filename.
Definition: Filename.hh:17
strCat
std::string strCat(Ts &&...ts)
Definition: strCat.hh:573
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5