2 * Copyright (c) 2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
33 #include <sys/types.h>
42 #include <gzstream.hh>
44 #include "base/misc.hh"
45 #include "base/output.hh"
49 OutputDirectory simout
;
52 * @file This file manages creating / deleting output files for the simulator.
54 OutputDirectory::OutputDirectory()
57 OutputDirectory::~OutputDirectory()
59 for (map_t::iterator i
= files
.begin(); i
!= files
.end(); i
++) {
66 OutputDirectory::checkForStdio(const string
&name
) const
68 if (name
== "cerr" || name
== "stderr")
71 if (name
== "cout" || name
== "stdout")
78 OutputDirectory::openFile(const string
&filename
,
79 ios_base::openmode mode
)
81 if (filename
.find(".gz", filename
.length()-3) < filename
.length()) {
82 ogzstream
*file
= new ogzstream(filename
.c_str(), mode
);
84 fatal("Cannot open file %s", filename
);
85 assert(files
.find(filename
) == files
.end());
86 files
[filename
] = file
;
89 ofstream
*file
= new ofstream(filename
.c_str(), mode
);
91 fatal("Cannot open file %s", filename
);
92 assert(files
.find(filename
) == files
.end());
93 files
[filename
] = file
;
99 OutputDirectory::close(ostream
*openStream
) {
101 for (i
= files
.begin(); i
!= files
.end(); i
++) {
102 if (i
->second
!= openStream
)
105 ofstream
*fs
= dynamic_cast<ofstream
*>(i
->second
);
111 ogzstream
*gfs
= dynamic_cast<ogzstream
*>(i
->second
);
120 if (i
== files
.end())
121 fatal("Attempted to close an unregistred file stream");
127 OutputDirectory::setDirectory(const string
&d
)
130 panic("Output directory already set!\n");
134 // guarantee that directory ends with a path separator
135 if (dir
[dir
.size() - 1] != PATH_SEPARATOR
)
136 dir
+= PATH_SEPARATOR
;
140 OutputDirectory::directory() const
143 panic("Output directory not set!");
149 OutputDirectory::resolve(const string
&name
) const
151 return (name
[0] != PATH_SEPARATOR
) ? dir
+ name
: name
;
155 OutputDirectory::create(const string
&name
, bool binary
)
157 ostream
*file
= checkForStdio(name
);
161 string filename
= resolve(name
);
162 ios_base::openmode mode
=
163 ios::trunc
| (binary
? ios::binary
: (ios::openmode
)0);
164 file
= openFile(filename
, mode
);
170 OutputDirectory::find(const string
&name
) const
172 ostream
*file
= checkForStdio(name
);
176 const string filename
= resolve(name
);
177 map_t::const_iterator i
= files
.find(filename
);
178 if (i
!= files
.end())
185 OutputDirectory::isFile(const std::ostream
*os
)
187 return os
&& os
!= &cerr
&& os
!= &cout
;
191 OutputDirectory::isFile(const string
&name
) const
193 // definitely a file if in our data structure
194 if (find(name
) != NULL
) return true;
197 int st
= stat(name
.c_str(), &st_buf
);
198 return (st
== 0) && S_ISREG(st_buf
.st_mode
);
202 OutputDirectory::createSubdirectory(const string
&name
) const
204 const string new_dir
= resolve(name
);
205 if (new_dir
.find(directory()) == string::npos
)
206 fatal("Attempting to create subdirectory not in m5 output dir\n");
208 // if it already exists, that's ok; otherwise, fail if we couldn't create
209 if ((mkdir(new_dir
.c_str(), 0755) != 0) && (errno
!= EEXIST
))
210 fatal("Failed to create new output subdirectory '%s'\n", new_dir
);
212 return name
+ PATH_SEPARATOR
;
216 OutputDirectory::remove(const string
&name
, bool recursive
)
218 const string fname
= resolve(name
);
220 if (fname
.find(directory()) == string::npos
)
221 fatal("Attempting to remove file/dir not in output dir\n");
224 // close and release file if we have it open
225 map_t::iterator itr
= files
.find(fname
);
226 if (itr
!= files
.end()) {
231 if (::remove(fname
.c_str()) != 0)
232 fatal("Could not erase file '%s'\n", fname
);
234 // assume 'name' is a directory
236 DIR *subdir
= opendir(fname
.c_str());
238 // silently ignore removal request for non-existent directory
239 if ((!subdir
) && (errno
== ENOENT
))
242 // fail on other errors
245 fatal("Error opening directory for recursive removal '%s'\n",
249 struct dirent
*de
= readdir(subdir
);
251 // ignore files starting with a '.'; user must delete those
252 // manually if they really want to
253 if (de
->d_name
[0] != '.')
254 remove(name
+ PATH_SEPARATOR
+ de
->d_name
, recursive
);
256 de
= readdir(subdir
);
260 // try to force recognition that we deleted the files in the directory
263 if (::remove(fname
.c_str()) != 0) {
264 perror("Warning! 'remove' failed. Could not erase directory.");