Go to the documentation of this file.
47 #include <sys/types.h>
65 : _name(
name), _stream(stream)
78 template<
class StreamType>
80 const std::string &
name,
81 std::ios_base::openmode
mode,
84 _mode(
mode), _recreateable(recreateable),
94 template<
class StreamType>
97 if (_fstream->is_open())
101 template<
class StreamType>
107 _fstream->open(dir.
resolve(_name).c_str(), _mode);
136 if (
name ==
"cerr" ||
name ==
"stderr")
139 if (
name ==
"cout" ||
name ==
"stdout")
155 fatal(
"Attempted to close an unregistred file stream");
165 const string old_dir(
dir);
175 if ((mkdir(
dir.c_str(), 0755) != 0) && (errno != EEXIST))
176 fatal(
"Failed to create new output subdirectory '%s'\n",
dir);
179 if (!old_dir.empty()) {
181 for (file_map_t::iterator
i =
files.begin();
i !=
files.end(); ++
i) {
182 i->second->relocate(*
this);
186 for (dir_map_t::iterator
i =
dirs.begin();
i !=
dirs.end(); ++
i) {
197 panic(
"Output directory not set!");
215 const ios_base::openmode
mode(
216 ios::trunc | (binary ? ios::binary : (ios::openmode)0));
224 ios_base::openmode
mode,
230 if (!no_gz &&
name.find(
".gz",
name.length() - 3) <
name.length()) {
234 mode |= std::ios::out;
274 if (
find(
name) != NULL)
return true;
277 int st = stat(
name.c_str(), &st_buf);
278 return (
st == 0) && S_ISREG(st_buf.st_mode);
285 if (new_dir.find(
directory()) == string::npos)
286 fatal(
"Attempting to create subdirectory not in m5 output dir\n");
299 if (fname.find(
directory()) == string::npos)
300 fatal(
"Attempting to remove file/dir not in output dir\n");
304 auto i =
files.find(fname);
310 if (::
remove(fname.c_str()) != 0)
311 fatal(
"Could not erase file '%s'\n", fname);
315 DIR *subdir = opendir(fname.c_str());
318 if ((!subdir) && (errno == ENOENT))
324 fatal(
"Error opening directory for recursive removal '%s'\n",
328 struct dirent *
de = readdir(subdir);
332 if (
de->d_name[0] !=
'.')
335 de = readdir(subdir);
344 if (::
remove(fname.c_str()) != 0) {
345 perror(
"Warning! 'remove' failed. Could not erase directory.");
#define fatal(...)
This implements a cprintf based fatal() function.
~OutputDirectory()
Destructor.
Interface for creating files in a gem5 output directory.
OutputStream * find(const std::string &name) const
Finds stream associated with an open file or stdout/stderr.
OutputDirectory()
Constructor.
const std::string _name
Name in output directory.
static const char PATH_SEPARATOR
System-specific path separator character.
const std::string & name() const
Get the file name in the output directory.
OutputStream * create(const std::string &name, bool binary=false, bool no_gz=false)
Creates a file in this directory (optionally compressed).
void setDirectory(const std::string &dir)
Sets name of this directory.
OutputDirectory * createSubdirectory(const std::string &name)
Creates a subdirectory within this directory.
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
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.
OutputStream * findOrCreate(const std::string &name, bool binary=false)
static OutputStream stdout
std::ostream * stream() const
Get the output underlying output stream.
std::string dir
Name of this directory.
const std::ios_base::openmode _mode
File mode when opened.
file_map_t files
Open file streams within this directory.
void close(OutputStream *file)
Closes an output file and free the corresponding OutputFile.
const std::string & name()
static OutputStream * checkForStdio(const std::string &name)
Determines whether given file name corresponds to standard output streams.
static OutputStream stderr
OutputFile(const OutputDirectory &dir, const std::string &name, std::ios_base::openmode mode, bool recreateable)
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
stream_type_t *const _fstream
Pointer to the file stream.
Overload hash function for BasicBlockRange type.
virtual void relocate(const OutputDirectory &dir)
Re-create the in a new location if recreateable.
dir_map_t dirs
Output sub-directories.
void relocate(const OutputDirectory &dir) override
Re-create the file in a new location if it is relocatable.
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).
void remove(const std::string &name, bool recursive=false)
Removes a specified file or subdirectory.
const std::string & directory() const
Gets name of this directory.
OutputStream(const std::string &name, std::ostream *stream)
Wrap an existing stream.
#define panic(...)
This implements a cprintf based panic() function.
Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17