66 : _name(
name), _stream(stream)
79template<
class StreamType>
81 const std::string &
name,
82 std::ios_base::openmode
mode,
85 _mode(
mode), _recreateable(recreateable),
95template<
class StreamType>
98 if (_fstream->is_open())
102template<
class StreamType>
108 _fstream->open(dir.
resolve(_name).c_str(), _mode);
137 if (
name ==
"cerr" ||
name ==
"stderr")
140 if (
name ==
"cout" ||
name ==
"stdout")
156 fatal(
"Attempted to close an unregistred file stream");
166 const std::string old_dir(
dir);
176 if ((mkdir(
dir.c_str(), 0755) != 0) && (errno != EEXIST))
177 fatal(
"Failed to create new output subdirectory '%s'\n",
dir);
180 if (!old_dir.empty()) {
182 for (file_map_t::iterator
i =
files.begin();
i !=
files.end(); ++
i) {
183 i->second->relocate(*
this);
187 for (dir_map_t::iterator
i =
dirs.begin();
i !=
dirs.end(); ++
i) {
198 panic(
"Output directory not set!");
216 const std::ios_base::openmode
mode(
217 std::ios::trunc | (binary ? std::ios::binary : (std::ios::openmode)0));
225 std::ios_base::openmode
mode,
231 if (!no_gz &&
name.find(
".gz",
name.length() - 3) <
name.length()) {
235 mode |= std::ios::out;
275 if (
find(
name) != NULL)
return true;
278 int st = stat(
name.c_str(), &st_buf);
279 return (
st == 0) && S_ISREG(st_buf.st_mode);
286 if (new_dir.find(
directory()) == std::string::npos)
287 fatal(
"Attempting to create subdirectory not in m5 output dir\n");
300 if (fname.find(
directory()) == std::string::npos)
301 fatal(
"Attempting to remove file/dir not in output dir\n");
305 auto i =
files.find(fname);
312 fatal(
"Could not erase file '%s'\n", fname);
316 DIR *subdir = opendir(fname.c_str());
319 if ((!subdir) && (errno == ENOENT))
325 fatal(
"Error opening directory for recursive removal '%s'\n",
329 struct dirent *
de = readdir(subdir);
333 if (
de->d_name[0] !=
'.')
336 de = readdir(subdir);
346 perror(
"Warning! 'remove' failed. Could not erase directory.");
Interface for creating files in a gem5 output directory.
void close(OutputStream *file)
Closes an output file and free the corresponding OutputFile.
~OutputDirectory()
Destructor.
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
void remove(const std::string &name, bool recursive=false)
Removes a specified file or subdirectory.
bool isFile(const std::string &name) const
Determines whether a file name corresponds to a file in this directory.
static bool isAbsolute(const std::string &name)
Test if a path is absolute.
std::string dir
Name of this directory.
static OutputStream * checkForStdio(const std::string &name)
Determines whether given file name corresponds to standard output streams.
OutputDirectory()
Constructor.
OutputDirectory * createSubdirectory(const std::string &name)
Creates a subdirectory within this directory.
static const char PATH_SEPARATOR
System-specific path separator character.
static OutputStream stdout
OutputStream * open(const std::string &name, std::ios_base::openmode mode, bool recreateable=true, bool no_gz=false)
Open a file in this directory (optionally compressed).
const std::string & directory() const
Gets name of this directory.
OutputStream * create(const std::string &name, bool binary=false, bool no_gz=false)
Creates a file in this directory (optionally compressed).
dir_map_t dirs
Output sub-directories.
void setDirectory(const std::string &dir)
Sets name of this directory.
OutputStream * findOrCreate(const std::string &name, bool binary=false)
OutputStream * find(const std::string &name) const
Finds stream associated with an open file or stdout/stderr.
static OutputStream stderr
file_map_t files
Open file streams within this directory.
void relocate(const OutputDirectory &dir) override
Re-create the file in a new location if it is relocatable.
OutputFile(const OutputDirectory &dir, const std::string &name, std::ios_base::openmode mode, bool recreateable)
stream_type_t *const _fstream
Pointer to the file stream.
const std::ios_base::openmode _mode
File mode when opened.
virtual void relocate(const OutputDirectory &dir)
Re-create the in a new location if recreateable.
const std::string & name() const
Get the file name in the output directory.
const std::string _name
Name in output directory.
OutputStream(const std::string &name, std::ostream *stream)
Wrap an existing stream.
std::ostream * stream() const
Get the output underlying output stream.
#define panic(...)
This implements a cprintf based panic() function.
#define fatal(...)
This implements a cprintf based fatal() function.
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
const std::string & name()