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),
92 template<
class StreamType>
99 template<
class StreamType>
125 for (
auto&
f: files) {
134 if (name ==
"cerr" || name ==
"stderr")
137 if (name ==
"cout" || name ==
"stdout")
146 if (file == &stdout || file == &stderr) {
151 auto i = files.find(file->
name());
152 if (
i == files.end())
153 fatal(
"Attempted to close an unregistred file stream");
163 const string old_dir(dir);
168 if (dir[dir.size() - 1] != PATH_SEPARATOR)
169 dir += PATH_SEPARATOR;
173 if ((mkdir(dir.c_str(), 0755) != 0) && (errno != EEXIST))
174 fatal(
"Failed to create new output subdirectory '%s'\n", dir);
177 if (!old_dir.empty()) {
179 for (file_map_t::iterator
i = files.begin();
i != files.end(); ++
i) {
180 i->second->relocate(*
this);
184 for (dir_map_t::iterator
i = dirs.begin();
i != dirs.end(); ++
i) {
185 i->second->setDirectory(dir + PATH_SEPARATOR +
i->first);
195 panic(
"Output directory not set!");
203 return !isAbsolute(name) ? dir +
name :
name;
213 const ios_base::openmode
mode(
214 ios::trunc | (binary ? ios::binary : (ios::openmode)0));
217 return open(name, mode, recreateable, no_gz);
222 ios_base::openmode
mode,
228 if (!no_gz && name.find(
".gz", name.length() - 3) < name.length()) {
232 mode |= std::ios::out;
250 auto i = files.find(name);
251 if (
i != files.end())
265 return create(name, binary);
272 if (find(name) != NULL)
return true;
275 int st = stat(name.c_str(), &st_buf);
276 return (st == 0) && S_ISREG(st_buf.st_mode);
282 const string new_dir =
resolve(name);
283 if (new_dir.find(directory()) == string::npos)
284 fatal(
"Attempting to create subdirectory not in m5 output dir\n");
295 const string fname =
resolve(name);
297 if (fname.find(directory()) == string::npos)
298 fatal(
"Attempting to remove file/dir not in output dir\n");
302 auto i = files.find(fname);
303 if (
i != files.end()) {
308 if (::
remove(fname.c_str()) != 0)
309 fatal(
"Could not erase file '%s'\n", fname);
313 DIR *subdir = opendir(fname.c_str());
316 if ((!subdir) && (errno == ENOENT))
322 fatal(
"Error opening directory for recursive removal '%s'\n",
326 struct dirent *
de = readdir(subdir);
330 if (de->d_name[0] !=
'.')
331 remove(name + PATH_SEPARATOR + de->d_name, recursive);
333 de = readdir(subdir);
342 if (::
remove(fname.c_str()) != 0) {
343 perror(
"Warning! 'remove' failed. Could not erase directory.");
bool isFile(const std::string &name) const
Determines whether a file name corresponds to a file in this directory.
#define panic(...)
This implements a cprintf based panic() function.
bool recreateable() const override
Can the file be recreated if the output directory is moved?
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this 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.
#define fatal(...)
This implements a cprintf based fatal() function.
const std::string & name()
const Info * resolve(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.
std::ostream *const _stream
Underlying output stream.
Overload hash function for BasicBlockRange type.
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.
const std::string & name() const
Get the file name in the output directory.
const bool _recreateable
Can the file be recreated in a new location?
std::ostream * stream() const
Get the output underlying output stream.
const std::string _name
Name in output directory.
virtual bool recreateable() const
Can the file be recreated if the output directory is moved?
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.
OutputFile(const OutputDirectory &dir, const std::string &name, std::ios_base::openmode mode, bool recreateable)
OutputStream * find(const std::string &name) const
Finds stream associated with an open file or stdout/stderr.
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.
Interface for creating files in a gem5 output directory.
stream_type_t *const _fstream
Pointer to the file stream.
friend class OutputDirectory
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.