2 * Copyright (c) 2015 ARM Limited
3 * Copyright (c) 2020 Barkhausen Institut
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
15 * Copyright (c) 2013 Andreas Sandberg
16 * Copyright (c) 2005 The Regents of The University of Michigan
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are
21 * met: redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer;
23 * redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution;
26 * neither the name of the copyright holders nor the names of its
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 #include "base/output.hh"
47 #include <sys/types.h>
57 #include "base/logging.hh"
61 OutputDirectory simout
;
64 OutputStream::OutputStream(const std::string
&name
, std::ostream
*stream
)
65 : _name(name
), _stream(stream
)
69 OutputStream::~OutputStream()
74 OutputStream::relocate(const OutputDirectory
&dir
)
78 template<class StreamType
>
79 OutputFile
<StreamType
>::OutputFile(const OutputDirectory
&dir
,
80 const std::string
&name
,
81 std::ios_base::openmode mode
,
83 : OutputStream(name
, new stream_type_t()),
84 _mode(mode
), _recreateable(recreateable
),
85 _fstream(static_cast<stream_type_t
*>(_stream
))
87 _fstream
->open(dir
.resolve(_name
).c_str(), _mode
);
89 assert(_fstream
->is_open());
92 template<class StreamType
>
93 OutputFile
<StreamType
>::~OutputFile()
95 if (_fstream
->is_open())
99 template<class StreamType
>
101 OutputFile
<StreamType
>::relocate(const OutputDirectory
&dir
)
105 _fstream
->open(dir
.resolve(_name
).c_str(), _mode
);
109 OutputStream
OutputDirectory::stdout("stdout", &cout
);
110 OutputStream
OutputDirectory::stderr("stderr", &cerr
);
113 * @file This file manages creating / deleting output files for the simulator.
115 OutputDirectory::OutputDirectory()
118 OutputDirectory::OutputDirectory(const std::string
&name
)
123 OutputDirectory::~OutputDirectory()
125 for (auto& f
: files
) {
132 OutputDirectory::checkForStdio(const string
&name
)
134 if (name
== "cerr" || name
== "stderr")
137 if (name
== "cout" || name
== "stdout")
144 OutputDirectory::close(OutputStream
*file
)
146 if (file
== &stdout
|| file
== &stderr
) {
147 file
->stream()->flush();
151 auto i
= files
.find(file
->name());
152 if (i
== files
.end())
153 fatal("Attempted to close an unregistred file stream");
161 OutputDirectory::setDirectory(const string
&d
)
163 const string
old_dir(dir
);
167 // guarantee that directory ends with a path separator
168 if (dir
[dir
.size() - 1] != PATH_SEPARATOR
)
169 dir
+= PATH_SEPARATOR
;
171 // Try to create the directory. If it already exists, that's ok;
172 // otherwise, fail if we couldn't create it.
173 if ((mkdir(dir
.c_str(), 0755) != 0) && (errno
!= EEXIST
))
174 fatal("Failed to create new output subdirectory '%s'\n", dir
);
176 // Check if we need to recreate anything
177 if (!old_dir
.empty()) {
178 // Recreate output files
179 for (file_map_t::iterator i
= files
.begin(); i
!= files
.end(); ++i
) {
180 i
->second
->relocate(*this);
183 // Relocate sub-directories
184 for (dir_map_t::iterator i
= dirs
.begin(); i
!= dirs
.end(); ++i
) {
185 i
->second
->setDirectory(dir
+ PATH_SEPARATOR
+ i
->first
);
192 OutputDirectory::directory() const
195 panic("Output directory not set!");
201 OutputDirectory::resolve(const string
&name
) const
203 return !isAbsolute(name
) ? dir
+ name
: name
;
207 OutputDirectory::create(const string
&name
, bool binary
, bool no_gz
)
209 OutputStream
*file
= checkForStdio(name
);
213 const ios_base::openmode
mode(
214 ios::trunc
| (binary
? ios::binary
: (ios::openmode
)0));
215 const bool recreateable(!isAbsolute(name
));
217 return open(name
, mode
, recreateable
, no_gz
);
221 OutputDirectory::open(const std::string
&name
,
222 ios_base::openmode mode
,
228 if (!no_gz
&& name
.find(".gz", name
.length() - 3) < name
.length()) {
229 // Although we are creating an output stream, we still need to pass the
230 // correct mode for gzofstream as this used directly to set the file
232 mode
|= std::ios::out
;
233 os
= new OutputFile
<gzofstream
>(*this, name
, mode
, recreateable
);
235 os
= new OutputFile
<ofstream
>(*this, name
, mode
, recreateable
);
244 OutputDirectory::find(const string
&name
) const
246 OutputStream
*file
= checkForStdio(name
);
250 auto i
= files
.find(name
);
251 if (i
!= files
.end())
259 OutputDirectory::findOrCreate(const std::string
&name
, bool binary
)
261 OutputStream
*os(find(name
));
265 return create(name
, binary
);
269 OutputDirectory::isFile(const string
&name
) const
271 // definitely a file if in our data structure
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
);
280 OutputDirectory::createSubdirectory(const string
&name
)
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");
286 OutputDirectory
*dir(new OutputDirectory(new_dir
));
293 OutputDirectory::remove(const string
&name
, bool recursive
)
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");
301 // close and release file if we have it open
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
);
311 // assume 'name' is a directory
313 DIR *subdir
= opendir(fname
.c_str());
315 // silently ignore removal request for non-existent directory
316 if ((!subdir
) && (errno
== ENOENT
))
319 // fail on other errors
322 fatal("Error opening directory for recursive removal '%s'\n",
326 struct dirent
*de
= readdir(subdir
);
328 // ignore files starting with a '.'; user must delete those
329 // manually if they really want to
330 if (de
->d_name
[0] != '.')
331 remove(name
+ PATH_SEPARATOR
+ de
->d_name
, recursive
);
333 de
= readdir(subdir
);
339 // try to force recognition that we deleted the files in the directory
342 if (::remove(fname
.c_str()) != 0) {
343 perror("Warning! 'remove' failed. Could not erase directory.");