BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSTemporaryFiles.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSGlobalConstants.hh"
22#include "BDSTemporaryFiles.hh"
23#include "BDSUtilities.hh"
24
25#include "globals.hh"
26
27#include <cstdlib>
28#include <cstdio>
29#include <string>
30#include <sys/stat.h>
31#include <unistd.h>
32
34
36 userSpecifiedTemporaryDirectory(""),
37 temporaryDirectory(""),
38 temporaryDirectorySet(false),
39 unNamedFileCount(0)
40{
42 removeTemporaryFiles = BDSGlobalConstants::Instance()->RemoveTemporaryFiles();
43}
44
46{
47 // determine temp directory
48 std::string bdsimExecDir = BDS::GetBDSIMExecPath();
49 std::string workingDirTemp = bdsimExecDir + "temp/";
50 std::vector<G4String> dirsToTry = {"/tmp/", "/temp/", workingDirTemp};
51
52 // replace set of trial paths if one is specified explicitly in the options
54 {
55 if (userSpecifiedTemporaryDirectory.back() != '/')
58 }
59
60 for (const auto& dir : dirsToTry)
61 {
62 if (BDS::DirectoryExists(dir))
63 {// found a dir we can use - create bdsim-specific dir in it
64 // use mkdtemp, which takes template char* (non-const)
65 // use a vector for automatic clearing up with scope
66 G4String templateDir = dir + "bdsim_XXXXXX";
67 std::vector<char> v(templateDir.length() + 1);
68 std::strcpy(&v[0], templateDir.c_str());
69 char* pc = &v[0];
70
71 char* newTempDirC = mkdtemp(pc);
72 if (!newTempDirC)
73 {throw BDSException(__METHOD_NAME__, "Unable to create directory: " + templateDir);}
74 else
75 {
76 G4String newTempDir = G4String(newTempDirC);
77 temporaryDirectory = newTempDir;
78 if (temporaryDirectory.back() != '/')
79 {temporaryDirectory += "/";}
81 }
82 break;
83 }
84 }
85
86 if (!temporaryDirectorySet) // check it's ok
87 {
88 G4cerr << __METHOD_NAME__ << "Could not create temporary dir in one of:" << G4endl;
89 for (const auto& dir : dirsToTry)
90 {G4cerr << dir << G4endl;}
91 throw BDSException(__METHOD_NAME__, "Required for operation.");
92 }
93 else
94 {G4cout << __METHOD_NAME__ << "Created temporary directory in: " << temporaryDirectory << G4endl;}
95}
96
97BDSTemporaryFiles::~BDSTemporaryFiles()
98{
99 if (removeTemporaryFiles == false || (allocatedFiles.empty()))
100 {
101 // no need to warn user about deleting no files.
102 instance = nullptr;
103 return;
104 }
105
106 G4cout << __METHOD_NAME__<< "Removing temporary files" << G4endl;
107
108 for (const auto& fn : allocatedFiles)
109 {
110 if (BDS::FileExists(fn))
111 {
112#ifdef BDSDEBUG
113 G4cout << "Removing \"" << fn << "\"" << G4endl;
114#endif
115 int result = remove(fn.c_str()); // delete file
116 if (result != 0)
117#ifdef BDSDEBUG
118 {G4cout << "Error deleting file: \"" << fn << "\"" << G4endl;}
119#else
120 {continue;}
121#endif
122 }
123 }
124 // remove directory which should be empty (if not won't be deleted)
125 rmdir(temporaryDirectory);
126
127 G4cout << __METHOD_NAME__ << "Temporary files removed" << G4endl;
128}
129
131{
132 if (!instance)
133 {instance = new BDSTemporaryFiles();}
134 return instance;
135}
136
137G4String BDSTemporaryFiles::CreateTemporaryFileUnnamed(const G4String& extension)
138{
141
142 G4String newFileName = temporaryDirectory + "bdsTemp_" + std::to_string(unNamedFileCount) + "." + extension;
143 unNamedFileCount += 1;
144
145 allocatedFiles.push_back(newFileName);
146 WarnOfNewFile(newFileName);
147 return newFileName;
148}
149
150G4String BDSTemporaryFiles::CreateTemporaryFile(const G4String& originalFilePath,
151 G4String fileNamePrefix,
152 G4String fileNameSuffix)
153{
156
157 G4String path = "";
158 G4String fileName = "";
159 BDS::SplitPathAndFileName(originalFilePath, path, fileName);
160 G4String name = "";
161 G4String extension = "";
162 BDS::SplitFileAndExtension(fileName, name, extension);
163
164 if (!fileNamePrefix.empty() && (fileNamePrefix.back() != '_'))
165 {fileNamePrefix += "_";} // ensure ends with "_" for padding
166 if (!fileNameSuffix.empty() && (fileNameSuffix.front() != '_'))
167 {fileNameSuffix = "_" + fileNameSuffix;}// ensure starts with "_" for padding
168
169 G4String newFileName = temporaryDirectory + fileNamePrefix + name + fileNameSuffix + extension;
170
171 allocatedFiles.push_back(newFileName);
172 WarnOfNewFile(newFileName);
173 return newFileName;
174}
175
176void BDSTemporaryFiles::WarnOfNewFile(const G4String& newFileName)
177{
178 G4cout << "New temporary file created: " << newFileName << G4endl;
179}
General exception with possible name of object and message.
Definition: BDSException.hh:35
static BDSGlobalConstants * Instance()
Access method.
Holder for all temporary files created by BDSIM.
BDSTemporaryFiles()
Private default constructor as singleton.
G4bool temporaryDirectorySet
Whether directory has been set and made.
G4bool removeTemporaryFiles
Whether to clean up.
G4String temporaryDirectory
Directory all files will be placed in.
static BDSTemporaryFiles * instance
Singleton instance.
G4int unNamedFileCount
Count of unnamed files created.
static BDSTemporaryFiles * Instance()
Singleton accessor.
G4String CreateTemporaryFileUnnamed(const G4String &extension)
std::vector< G4String > allocatedFiles
Record of of all files allocated.
G4String userSpecifiedTemporaryDirectory
Optional user-specified path to try.
G4String CreateTemporaryFile(const G4String &originalFilePath, G4String fileNamePrefix="", G4String fileNameSuffix="")
void WarnOfNewFile(const G4String &newFileName)
User feedback that new file has been created.
void SplitFileAndExtension(const G4String &fileName, G4String &file, G4String &extension)
Split a filename.ext into filename and extension. Extension includes '.'.
G4bool DirectoryExists(const G4String &path)
Check if directory exists.
G4bool FileExists(const G4String &filename)
Checks if filename exists.
void SplitPathAndFileName(const G4String &filePath, G4String &path, G4String &filename)
std::string GetBDSIMExecPath()