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>
103 template<
class StreamType>
129 for (
auto&
f: files) {
138 if (name ==
"cerr" || name ==
"stderr")
141 if (name ==
"cout" || name ==
"stdout")
150 auto i = files.find(file->
name());
151 if (
i == files.end())
152 fatal(
"Attempted to close an unregistred file stream");
162 const string old_dir(dir);
167 if (dir[dir.size() - 1] != PATH_SEPARATOR)
168 dir += PATH_SEPARATOR;
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) {
184 i->second->setDirectory(dir + PATH_SEPARATOR +
i->first);
194 panic(
"Output directory not set!");
202 return !isAbsolute(name) ? dir +
name :
name;
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);
250 if (
i != files.end())
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);
302 if (
i != files.end()) {
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] !=
'.')
330 remove(name + PATH_SEPARATOR + de->d_name, recursive);
332 de = readdir(subdir);
341 if (::
remove(fname.c_str()) != 0) {
342 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()
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?
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.