2 * Copyright (c) 2015 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2013 Andreas Sandberg
15 * Copyright (c) 2005 The Regents of The University of Michigan
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * Authors: Nathan Binkert
47 #include "base/output.hh"
51 #include <sys/types.h>
61 #include "base/logging.hh"
65 OutputDirectory simout
;
68 OutputStream::OutputStream(const std::string
&name
, std::ostream
*stream
)
69 : _name(name
), _stream(stream
)
73 OutputStream::~OutputStream()
78 OutputStream::relocate(const OutputDirectory
&dir
)
82 template<class StreamType
>
83 OutputFile
<StreamType
>::OutputFile(const OutputDirectory
&dir
,
84 const std::string
&name
,
85 std::ios_base::openmode mode
,
87 : OutputStream(name
, new stream_type_t()),
88 _mode(mode
), _recreateable(recreateable
),
89 _fstream(static_cast<stream_type_t
*>(_stream
))
91 _fstream
->open(dir
.resolve(_name
).c_str(), _mode
);
93 assert(_fstream
->is_open());
96 template<class StreamType
>
97 OutputFile
<StreamType
>::~OutputFile()
99 if (_fstream
->is_open())
103 template<class StreamType
>
105 OutputFile
<StreamType
>::relocate(const OutputDirectory
&dir
)
109 _fstream
->open(dir
.resolve(_name
).c_str(), _mode
);
113 OutputStream
OutputDirectory::stdout("stdout", &cout
);
114 OutputStream
OutputDirectory::stderr("stderr", &cerr
);
117 * @file This file manages creating / deleting output files for the simulator.
119 OutputDirectory::OutputDirectory()
122 OutputDirectory::OutputDirectory(const std::string
&name
)
127 OutputDirectory::~OutputDirectory()
129 for (auto& f
: files
) {
136 OutputDirectory::checkForStdio(const string
&name
)
138 if (name
== "cerr" || name
== "stderr")
141 if (name
== "cout" || name
== "stdout")
148 OutputDirectory::close(OutputStream
*file
)
150 auto i
= files
.find(file
->name());
151 if (i
== files
.end())
152 fatal("Attempted to close an unregistred file stream");
160 OutputDirectory::setDirectory(const string
&d
)
162 const string
old_dir(dir
);
166 // guarantee that directory ends with a path separator
167 if (dir
[dir
.size() - 1] != PATH_SEPARATOR
)
168 dir
+= PATH_SEPARATOR
;
170 // Try to create the directory. If it already exists, that's ok;
171 // otherwise, fail if we couldn't create it.
172 if ((mkdir(dir
.c_str(), 0755) != 0) && (errno
!= EEXIST
))
173 fatal("Failed to create new output subdirectory '%s'\n", dir
);
175 // Check if we need to recreate anything
176 if (!old_dir
.empty()) {
177 // Recreate output files
178 for (file_map_t::iterator i
= files
.begin(); i
!= files
.end(); ++i
) {
179 i
->second
->relocate(*this);
182 // Relocate sub-directories
183 for (dir_map_t::iterator i
= dirs
.begin(); i
!= dirs
.end(); ++i
) {
184 i
->second
->setDirectory(dir
+ PATH_SEPARATOR
+ i
->first
);
191 OutputDirectory::directory() const
194 panic("Output directory not set!");
200 OutputDirectory::resolve(const string
&name
) const
202 return !isAbsolute(name
) ? dir
+ name
: name
;
206 OutputDirectory::create(const string
&name
, bool binary
, bool no_gz
)
208 OutputStream
*file
= checkForStdio(name
);
212 const ios_base::openmode
mode(
213 ios::trunc
| (binary
? ios::binary
: (ios::openmode
)0));
214 const bool recreateable(!isAbsolute(name
));
216 return open(name
, mode
, recreateable
, no_gz
);
220 OutputDirectory::open(const std::string
&name
,
221 ios_base::openmode mode
,
227 if (!no_gz
&& name
.find(".gz", name
.length() - 3) < name
.length()) {
228 // Although we are creating an output stream, we still need to pass the
229 // correct mode for gzofstream as this used directly to set the file
231 mode
|= std::ios::out
;
232 os
= new OutputFile
<gzofstream
>(*this, name
, mode
, recreateable
);
234 os
= new OutputFile
<ofstream
>(*this, name
, mode
, recreateable
);
243 OutputDirectory::find(const string
&name
) const
245 OutputStream
*file
= checkForStdio(name
);
249 auto i
= files
.find(name
);
250 if (i
!= files
.end())
258 OutputDirectory::findOrCreate(const std::string
&name
, bool binary
)
260 OutputStream
*os(find(name
));
264 return create(name
, binary
);
268 OutputDirectory::isFile(const string
&name
) const
270 // definitely a file if in our data structure
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
);
279 OutputDirectory::createSubdirectory(const string
&name
)
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");
285 OutputDirectory
*dir(new OutputDirectory(new_dir
));
292 OutputDirectory::remove(const string
&name
, bool recursive
)
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");
300 // close and release file if we have it open
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
);
310 // assume 'name' is a directory
312 DIR *subdir
= opendir(fname
.c_str());
314 // silently ignore removal request for non-existent directory
315 if ((!subdir
) && (errno
== ENOENT
))
318 // fail on other errors
321 fatal("Error opening directory for recursive removal '%s'\n",
325 struct dirent
*de
= readdir(subdir
);
327 // ignore files starting with a '.'; user must delete those
328 // manually if they really want to
329 if (de
->d_name
[0] != '.')
330 remove(name
+ PATH_SEPARATOR
+ de
->d_name
, recursive
);
332 de
= readdir(subdir
);
338 // try to force recognition that we deleted the files in the directory
341 if (::remove(fname
.c_str()) != 0) {
342 perror("Warning! 'remove' failed. Could not erase directory.");