51 #include <sys/types.h>
69 : _name(name), _stream(stream)
82 template<
class StreamType>
84 const std::string &
name,
85 std::ios_base::openmode
mode,
88 _mode(mode), _recreateable(recreateable),
96 template<
class StreamType>
99 if (_fstream->is_open())
103 template<
class StreamType>
109 _fstream->open(dir.
resolve(_name).c_str(), _mode);
138 if (name ==
"cerr" || name ==
"stderr")
141 if (name ==
"cout" || name ==
"stdout")
152 fatal(
"Attempted to close an unregistred file stream");
162 const string old_dir(
dir);
172 if ((mkdir(
dir.c_str(), 0755) != 0) && (errno != EEXIST))
173 fatal(
"Failed to create new output subdirectory '%s'\n",
dir);
176 if (!old_dir.empty()) {
178 for (file_map_t::iterator
i =
files.begin();
i !=
files.end(); ++
i) {
179 i->second->relocate(*
this);
183 for (dir_map_t::iterator
i =
dirs.begin();
i !=
dirs.end(); ++
i) {
194 panic(
"Output directory not set!");
212 const ios_base::openmode
mode(
213 ios::trunc | (binary ? ios::binary : (ios::openmode)0));
216 return open(name, mode, recreateable, no_gz);
221 ios_base::openmode
mode,
227 if (!no_gz && name.find(
".gz", name.length() - 3) < name.length()) {
231 mode |= std::ios::out;
249 auto i =
files.find(name);
264 return create(name, binary);
271 if (
find(name) != NULL)
return true;
274 int st = stat(name.c_str(), &st_buf);
275 return (st == 0) && S_ISREG(st_buf.st_mode);
281 const string new_dir =
resolve(name);
282 if (new_dir.find(
directory()) == string::npos)
283 fatal(
"Attempting to create subdirectory not in m5 output dir\n");
294 const string fname =
resolve(name);
296 if (fname.find(
directory()) == string::npos)
297 fatal(
"Attempting to remove file/dir not in output dir\n");
301 auto i =
files.find(fname);
307 if (::
remove(fname.c_str()) != 0)
308 fatal(
"Could not erase file '%s'\n", fname);
312 DIR *subdir = opendir(fname.c_str());
315 if ((!subdir) && (errno == ENOENT))
321 fatal(
"Error opening directory for recursive removal '%s'\n",
325 struct dirent *
de = readdir(subdir);
329 if (de->d_name[0] !=
'.')
332 de = readdir(subdir);
341 if (::
remove(fname.c_str()) != 0) {
342 perror(
"Warning! 'remove' failed. Could not erase directory.");
OutputDirectory * createSubdirectory(const std::string &name)
Creates a subdirectory within this directory.
virtual void relocate(const OutputDirectory &dir)
Re-create the in a new location if recreateable.
const std::string & name()
OutputStream * create(const std::string &name, bool binary=false, bool no_gz=false)
Creates a file in this directory (optionally compressed).
OutputDirectory()
Constructor.
void setDirectory(const std::string &dir)
Sets name of this directory.
void relocate(const OutputDirectory &dir) override
Re-create the file in a new location if it is relocatable.
bool isFile(const std::string &name) const
Determines whether a file name corresponds to a file in this directory.
file_map_t files
Open file streams within this directory.
const std::string _name
Name in output directory.
const std::string & name() const
Get the file name in the output directory.
const std::ios_base::openmode _mode
File mode when opened.
~OutputDirectory()
Destructor.
void remove(const std::string &name, bool recursive=false)
Removes a specified file or subdirectory.
void close(OutputStream *file)
Closes an output file and free the corresponding OutputFile.
OutputStream * find(const std::string &name) const
Finds stream associated with an open file or stdout/stderr.
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
OutputFile(const OutputDirectory &dir, const std::string &name, std::ios_base::openmode mode, bool recreateable)
const std::string & directory() const
Gets name of this directory.
OutputStream * findOrCreate(const std::string &name, bool binary=false)
static OutputStream stdout
static OutputStream stderr
OutputStream(const std::string &name, std::ostream *stream)
Wrap an existing stream.
static OutputStream * checkForStdio(const std::string &name)
Determines whether given file name corresponds to standard output streams.
static bool isAbsolute(const std::string &name)
Test if a path is absolute.
std::string dir
Name of this directory.
static const char PATH_SEPARATOR
System-specific path separator character.
dir_map_t dirs
Output sub-directories.
Interface for creating files in a gem5 output directory.
stream_type_t *const _fstream
Pointer to the file stream.
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).