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 std::string resolved_path
= dir
.resolve(_name
);
89 _fstream
->open(resolved_path
.c_str(), _mode
);
91 panic_if(!_fstream
->is_open(), "Failed to open \"%s\"\n", resolved_path
);
94 template<class StreamType
>
95 OutputFile
<StreamType
>::~OutputFile()
97 if (_fstream
->is_open())
101 template<class StreamType
>
103 OutputFile
<StreamType
>::relocate(const OutputDirectory
&dir
)
107 _fstream
->open(dir
.resolve(_name
).c_str(), _mode
);
111 OutputStream
OutputDirectory::stdout("stdout", &cout
);
112 OutputStream
OutputDirectory::stderr("stderr", &cerr
);
115 * @file This file manages creating / deleting output files for the simulator.
117 OutputDirectory::OutputDirectory()
120 OutputDirectory::OutputDirectory(const std::string
&name
)
125 OutputDirectory::~OutputDirectory()
127 for (auto& f
: files
) {
134 OutputDirectory::checkForStdio(const string
&name
)
136 if (name
== "cerr" || name
== "stderr")
139 if (name
== "cout" || name
== "stdout")
146 OutputDirectory::close(OutputStream
*file
)
148 if (file
== &stdout
|| file
== &stderr
) {
149 file
->stream()->flush();
153 auto i
= files
.find(file
->name());
154 if (i
== files
.end())
155 fatal("Attempted to close an unregistred file stream");
163 OutputDirectory::setDirectory(const string
&d
)
165 const string
old_dir(dir
);
169 // guarantee that directory ends with a path separator
170 if (dir
[dir
.size() - 1] != PATH_SEPARATOR
)
171 dir
+= PATH_SEPARATOR
;
173 // Try to create the directory. If it already exists, that's ok;
174 // otherwise, fail if we couldn't create it.
175 if ((mkdir(dir
.c_str(), 0755) != 0) && (errno
!= EEXIST
))
176 fatal("Failed to create new output subdirectory '%s'\n", dir
);
178 // Check if we need to recreate anything
179 if (!old_dir
.empty()) {
180 // Recreate output files
181 for (file_map_t::iterator i
= files
.begin(); i
!= files
.end(); ++i
) {
182 i
->second
->relocate(*this);
185 // Relocate sub-directories
186 for (dir_map_t::iterator i
= dirs
.begin(); i
!= dirs
.end(); ++i
) {
187 i
->second
->setDirectory(dir
+ PATH_SEPARATOR
+ i
->first
);
194 OutputDirectory::directory() const
197 panic("Output directory not set!");
203 OutputDirectory::resolve(const string
&name
) const
205 return !isAbsolute(name
) ? dir
+ name
: name
;
209 OutputDirectory::create(const string
&name
, bool binary
, bool no_gz
)
211 OutputStream
*file
= checkForStdio(name
);
215 const ios_base::openmode
mode(
216 ios::trunc
| (binary
? ios::binary
: (ios::openmode
)0));
217 const bool recreateable(!isAbsolute(name
));
219 return open(name
, mode
, recreateable
, no_gz
);
223 OutputDirectory::open(const std::string
&name
,
224 ios_base::openmode mode
,
230 if (!no_gz
&& name
.find(".gz", name
.length() - 3) < name
.length()) {
231 // Although we are creating an output stream, we still need to pass the
232 // correct mode for gzofstream as this used directly to set the file
234 mode
|= std::ios::out
;
235 os
= new OutputFile
<gzofstream
>(*this, name
, mode
, recreateable
);
237 os
= new OutputFile
<ofstream
>(*this, name
, mode
, recreateable
);
246 OutputDirectory::find(const string
&name
) const
248 OutputStream
*file
= checkForStdio(name
);
252 auto i
= files
.find(name
);
253 if (i
!= files
.end())
261 OutputDirectory::findOrCreate(const std::string
&name
, bool binary
)
263 OutputStream
*os(find(name
));
267 return create(name
, binary
);
271 OutputDirectory::isFile(const string
&name
) const
273 // definitely a file if in our data structure
274 if (find(name
) != NULL
) return true;
277 int st
= stat(name
.c_str(), &st_buf
);
278 return (st
== 0) && S_ISREG(st_buf
.st_mode
);
282 OutputDirectory::createSubdirectory(const string
&name
)
284 const string new_dir
= resolve(name
);
285 if (new_dir
.find(directory()) == string::npos
)
286 fatal("Attempting to create subdirectory not in m5 output dir\n");
288 OutputDirectory
*dir(new OutputDirectory(new_dir
));
295 OutputDirectory::remove(const string
&name
, bool recursive
)
297 const string fname
= resolve(name
);
299 if (fname
.find(directory()) == string::npos
)
300 fatal("Attempting to remove file/dir not in output dir\n");
303 // close and release file if we have it open
304 auto i
= files
.find(fname
);
305 if (i
!= files
.end()) {
310 if (::remove(fname
.c_str()) != 0)
311 fatal("Could not erase file '%s'\n", fname
);
313 // assume 'name' is a directory
315 DIR *subdir
= opendir(fname
.c_str());
317 // silently ignore removal request for non-existent directory
318 if ((!subdir
) && (errno
== ENOENT
))
321 // fail on other errors
324 fatal("Error opening directory for recursive removal '%s'\n",
328 struct dirent
*de
= readdir(subdir
);
330 // ignore files starting with a '.'; user must delete those
331 // manually if they really want to
332 if (de
->d_name
[0] != '.')
333 remove(name
+ PATH_SEPARATOR
+ de
->d_name
, recursive
);
335 de
= readdir(subdir
);
341 // try to force recognition that we deleted the files in the directory
344 if (::remove(fname
.c_str()) != 0) {
345 perror("Warning! 'remove' failed. Could not erase directory.");