output.cc (11293:25352d3d491e) | output.cc (11359:b0b976a1ceda) |
---|---|
1/* | 1/* |
2 * Copyright (c) 2015 ARM Limited 3 * All rights reserved 4 * 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. 13 * 14 * Copyright (c) 2013 Andreas Sandberg |
|
2 * Copyright (c) 2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 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 --- 12 unchanged lines hidden (view full) --- 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. 27 * 28 * Authors: Nathan Binkert 29 * Chris Emmons | 15 * Copyright (c) 2005 The Regents of The University of Michigan 16 * All rights reserved. 17 * 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 --- 12 unchanged lines hidden (view full) --- 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. 40 * 41 * Authors: Nathan Binkert 42 * Chris Emmons |
43 * Andreas Sandberg 44 * Sascha Bischoff |
|
30 */ 31 32#include <sys/stat.h> 33#include <sys/types.h> 34#include <dirent.h> 35#include <unistd.h> 36 37#include <cassert> --- 6 unchanged lines hidden (view full) --- 44 45#include "base/misc.hh" 46#include "base/output.hh" 47 48using namespace std; 49 50OutputDirectory simout; 51 | 45 */ 46 47#include <sys/stat.h> 48#include <sys/types.h> 49#include <dirent.h> 50#include <unistd.h> 51 52#include <cassert> --- 6 unchanged lines hidden (view full) --- 59 60#include "base/misc.hh" 61#include "base/output.hh" 62 63using namespace std; 64 65OutputDirectory simout; 66 |
67 68OutputStream::OutputStream(const std::string &name, std::ostream *stream) 69 : _name(name), _stream(stream) 70{ 71} 72 73OutputStream::~OutputStream() 74{ 75} 76 77void 78OutputStream::relocate(const OutputDirectory &dir) 79{ 80} 81 82template<class StreamType> 83OutputFile<StreamType>::OutputFile(const OutputDirectory &dir, 84 const std::string &name, 85 std::ios_base::openmode mode, 86 bool recreateable) 87 : OutputStream(name, new stream_type_t()), 88 _mode(mode), _recreateable(recreateable), 89 _fstream(static_cast<stream_type_t *>(_stream)) 90{ 91 _fstream->open(dir.resolve(_name).c_str(), _mode); 92 93 assert(_fstream->is_open()); 94} 95 96template<class StreamType> 97OutputFile<StreamType>::~OutputFile() 98{ 99 if (_fstream->is_open()) 100 _fstream->close(); 101} 102 103template<class StreamType> 104void 105OutputFile<StreamType>::relocate(const OutputDirectory &dir) 106{ 107 if (_recreateable) { 108 _fstream->close(); 109 _fstream->open(dir.resolve(_name).c_str(), _mode); 110 } 111} 112 113OutputStream OutputDirectory::stdout("stdout", &cout); 114OutputStream OutputDirectory::stderr("stderr", &cerr); 115 |
|
52/** 53 * @file This file manages creating / deleting output files for the simulator. 54 */ 55OutputDirectory::OutputDirectory() 56{} 57 | 116/** 117 * @file This file manages creating / deleting output files for the simulator. 118 */ 119OutputDirectory::OutputDirectory() 120{} 121 |
122OutputDirectory::OutputDirectory(const std::string &name) 123{ 124 setDirectory(name); 125} 126 |
|
58OutputDirectory::~OutputDirectory() 59{ | 127OutputDirectory::~OutputDirectory() 128{ |
60 for (map_t::iterator i = files.begin(); i != files.end(); i++) { 61 if (i->second) 62 delete i->second; | 129 for (auto& f: files) { 130 if (f.second) 131 delete f.second; |
63 } 64} 65 | 132 } 133} 134 |
66std::ostream * 67OutputDirectory::checkForStdio(const string &name) const | 135OutputStream * 136OutputDirectory::checkForStdio(const string &name) |
68{ 69 if (name == "cerr" || name == "stderr") | 137{ 138 if (name == "cerr" || name == "stderr") |
70 return &cerr; | 139 return &stderr; |
71 72 if (name == "cout" || name == "stdout") | 140 141 if (name == "cout" || name == "stdout") |
73 return &cout; | 142 return &stdout; |
74 75 return NULL; 76} 77 | 143 144 return NULL; 145} 146 |
78ostream * 79OutputDirectory::openFile(const string &filename, 80 ios_base::openmode mode, bool no_gz) 81{ 82 bool gz = !no_gz; 83 gz = gz && filename.find(".gz", filename.length()-3) < filename.length(); 84 if (gz) { 85 gzofstream *file = new gzofstream(filename.c_str(), mode); 86 if (!file->is_open()) 87 fatal("Cannot open file %s", filename); 88 assert(files.find(filename) == files.end()); 89 files[filename] = file; 90 return file; 91 } else { 92 ofstream *file = new ofstream(filename.c_str(), mode); 93 if (!file->is_open()) 94 fatal("Cannot open file %s", filename); 95 assert(files.find(filename) == files.end()); 96 files[filename] = file; 97 return file; 98 } 99} 100 | |
101void | 147void |
102OutputDirectory::close(ostream *openStream) { 103 map_t::iterator i; 104 for (i = files.begin(); i != files.end(); i++) { 105 if (i->second != openStream) 106 continue; 107 108 ofstream *fs = dynamic_cast<ofstream*>(i->second); 109 if (fs) { 110 fs->close(); 111 delete i->second; 112 break; 113 } else { 114 gzofstream *gfs = dynamic_cast<gzofstream*>(i->second); 115 if (gfs) { 116 gfs->close(); 117 delete i->second; 118 break; 119 } 120 } 121 } 122 | 148OutputDirectory::close(OutputStream *file) 149{ 150 auto i = files.find(file->name()); |
123 if (i == files.end()) 124 fatal("Attempted to close an unregistred file stream"); 125 126 files.erase(i); | 151 if (i == files.end()) 152 fatal("Attempted to close an unregistred file stream"); 153 154 files.erase(i); |
155 156 delete file; |
|
127} 128 129void 130OutputDirectory::setDirectory(const string &d) 131{ | 157} 158 159void 160OutputDirectory::setDirectory(const string &d) 161{ |
132 if (!dir.empty()) 133 panic("Output directory already set!\n"); | 162 const string old_dir(dir); |
134 135 dir = d; 136 137 // guarantee that directory ends with a path separator 138 if (dir[dir.size() - 1] != PATH_SEPARATOR) 139 dir += PATH_SEPARATOR; | 163 164 dir = d; 165 166 // guarantee that directory ends with a path separator 167 if (dir[dir.size() - 1] != PATH_SEPARATOR) 168 dir += PATH_SEPARATOR; |
169 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); 174 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); 180 } 181 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); 185 } 186 } 187 |
|
140} 141 142const string & 143OutputDirectory::directory() const 144{ 145 if (dir.empty()) 146 panic("Output directory not set!"); 147 148 return dir; 149} 150 151string 152OutputDirectory::resolve(const string &name) const 153{ | 188} 189 190const string & 191OutputDirectory::directory() const 192{ 193 if (dir.empty()) 194 panic("Output directory not set!"); 195 196 return dir; 197} 198 199string 200OutputDirectory::resolve(const string &name) const 201{ |
154 return (name[0] != PATH_SEPARATOR) ? dir + name : name; | 202 return !isAbsolute(name) ? dir + name : name; |
155} 156 | 203} 204 |
157ostream * | 205OutputStream * |
158OutputDirectory::create(const string &name, bool binary, bool no_gz) 159{ | 206OutputDirectory::create(const string &name, bool binary, bool no_gz) 207{ |
160 ostream *file = checkForStdio(name); | 208 OutputStream *file = checkForStdio(name); |
161 if (file) 162 return file; 163 | 209 if (file) 210 return file; 211 |
164 string filename = resolve(name); 165 ios_base::openmode mode = 166 ios::trunc | (binary ? ios::binary : (ios::openmode)0); 167 file = openFile(filename, mode, no_gz); | 212 const ios_base::openmode mode( 213 ios::trunc | (binary ? ios::binary : (ios::openmode)0)); 214 const bool recreateable(!isAbsolute(name)); |
168 | 215 |
169 return file; | 216 return open(name, mode, recreateable, no_gz); |
170} 171 | 217} 218 |
172ostream * | 219OutputStream * 220OutputDirectory::open(const std::string &name, 221 ios_base::openmode mode, 222 bool recreateable, 223 bool no_gz) 224{ 225 OutputStream *os; 226 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 230 // mode. 231 mode |= std::ios::out; 232 os = new OutputFile<gzofstream>(*this, name, mode, recreateable); 233 } else { 234 os = new OutputFile<ofstream>(*this, name, mode, recreateable); 235 } 236 237 files[name] = os; 238 239 return os; 240} 241 242OutputStream * |
173OutputDirectory::find(const string &name) const 174{ | 243OutputDirectory::find(const string &name) const 244{ |
175 ostream *file = checkForStdio(name); | 245 OutputStream *file = checkForStdio(name); |
176 if (file) 177 return file; 178 | 246 if (file) 247 return file; 248 |
179 const string filename = resolve(name); 180 map_t::const_iterator i = files.find(filename); | 249 auto i = files.find(name); |
181 if (i != files.end()) 182 return (*i).second; 183 184 return NULL; 185} 186 | 250 if (i != files.end()) 251 return (*i).second; 252 253 return NULL; 254} 255 |
187bool 188OutputDirectory::isFile(const std::ostream *os) | 256 257OutputStream * 258OutputDirectory::findOrCreate(const std::string &name, bool binary) |
189{ | 259{ |
190 return os && os != &cerr && os != &cout; | 260 OutputStream *os(find(name)); 261 if (os) 262 return os; 263 else 264 return create(name, binary); |
191} 192 193bool 194OutputDirectory::isFile(const string &name) const 195{ 196 // definitely a file if in our data structure 197 if (find(name) != NULL) return true; 198 199 struct stat st_buf; 200 int st = stat(name.c_str(), &st_buf); 201 return (st == 0) && S_ISREG(st_buf.st_mode); 202} 203 | 265} 266 267bool 268OutputDirectory::isFile(const string &name) const 269{ 270 // definitely a file if in our data structure 271 if (find(name) != NULL) return true; 272 273 struct stat st_buf; 274 int st = stat(name.c_str(), &st_buf); 275 return (st == 0) && S_ISREG(st_buf.st_mode); 276} 277 |
204string 205OutputDirectory::createSubdirectory(const string &name) const | 278OutputDirectory * 279OutputDirectory::createSubdirectory(const string &name) |
206{ 207 const string new_dir = resolve(name); 208 if (new_dir.find(directory()) == string::npos) 209 fatal("Attempting to create subdirectory not in m5 output dir\n"); 210 | 280{ 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"); 284 |
211 // if it already exists, that's ok; otherwise, fail if we couldn't create 212 if ((mkdir(new_dir.c_str(), 0755) != 0) && (errno != EEXIST)) 213 fatal("Failed to create new output subdirectory '%s'\n", new_dir); | 285 OutputDirectory *dir(new OutputDirectory(new_dir)); 286 dirs[name] = dir; |
214 | 287 |
215 return name + PATH_SEPARATOR; | 288 return dir; |
216} 217 218void 219OutputDirectory::remove(const string &name, bool recursive) 220{ 221 const string fname = resolve(name); 222 223 if (fname.find(directory()) == string::npos) 224 fatal("Attempting to remove file/dir not in output dir\n"); 225 226 if (isFile(fname)) { 227 // close and release file if we have it open | 289} 290 291void 292OutputDirectory::remove(const string &name, bool recursive) 293{ 294 const string fname = resolve(name); 295 296 if (fname.find(directory()) == string::npos) 297 fatal("Attempting to remove file/dir not in output dir\n"); 298 299 if (isFile(fname)) { 300 // close and release file if we have it open |
228 map_t::iterator itr = files.find(fname); 229 if (itr != files.end()) { 230 delete itr->second; 231 files.erase(itr); | 301 auto i = files.find(fname); 302 if (i != files.end()) { 303 delete i->second; 304 files.erase(i); |
232 } 233 234 if (::remove(fname.c_str()) != 0) 235 fatal("Could not erase file '%s'\n", fname); 236 } else { 237 // assume 'name' is a directory 238 if (recursive) { 239 DIR *subdir = opendir(fname.c_str()); 240 241 // silently ignore removal request for non-existent directory 242 if ((!subdir) && (errno == ENOENT)) 243 return; 244 245 // fail on other errors 246 if (!subdir) { 247 perror("opendir"); 248 fatal("Error opening directory for recursive removal '%s'\n", | 305 } 306 307 if (::remove(fname.c_str()) != 0) 308 fatal("Could not erase file '%s'\n", fname); 309 } else { 310 // assume 'name' is a directory 311 if (recursive) { 312 DIR *subdir = opendir(fname.c_str()); 313 314 // silently ignore removal request for non-existent directory 315 if ((!subdir) && (errno == ENOENT)) 316 return; 317 318 // fail on other errors 319 if (!subdir) { 320 perror("opendir"); 321 fatal("Error opening directory for recursive removal '%s'\n", |
249 fname); | 322 fname); |
250 } 251 252 struct dirent *de = readdir(subdir); 253 while (de != NULL) { 254 // ignore files starting with a '.'; user must delete those 255 // manually if they really want to 256 if (de->d_name[0] != '.') 257 remove(name + PATH_SEPARATOR + de->d_name, recursive); --- 15 unchanged lines hidden --- | 323 } 324 325 struct dirent *de = readdir(subdir); 326 while (de != NULL) { 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); --- 15 unchanged lines hidden --- |