|
16 | 16 | // For fifo's and system call |
17 | 17 | #include <cstdlib> |
18 | 18 | #include <sys/types.h> // POSIX only |
19 | | -#include <sys/stat.h> // POISX only |
| 19 | +#include <sys/stat.h> // POSIX only |
20 | 20 | #include <signal.h> |
21 | 21 | #include <sys/wait.h> |
22 | 22 | #include <cstdio> |
@@ -166,19 +166,40 @@ bool GeneratorFileOrCmd::terminateCmd() |
166 | 166 | return true; |
167 | 167 | } |
168 | 168 | // ----------------------------------------------------------------- |
169 | | -bool GeneratorFileOrCmd::makeTemp() |
| 169 | +bool GeneratorFileOrCmd::makeTemp(const bool& fromName) |
170 | 170 | { |
171 | | - mFileNames.clear(); |
172 | | - char buf[] = "generatorFifoXXXXXX"; |
173 | | - auto fp = mkstemp(buf); |
174 | | - if (fp < 0) { |
175 | | - LOG(fatal) << "Failed to make temporary file: " |
176 | | - << std::strerror(errno); |
177 | | - return false; |
| 171 | + if (fromName) { |
| 172 | + if (mFileNames.empty()) { |
| 173 | + LOG(fatal) << "No file names to make temporary file from"; |
| 174 | + return false; |
| 175 | + } else if (mFileNames.size() > 1) { |
| 176 | + LOG(warning) << "More than one file name to make temporary file from"; |
| 177 | + LOG(warning) << "Using the first one: " << mFileNames.front(); |
| 178 | + LOG(warning) << "Removing all the others"; |
| 179 | + mFileNames.erase(++mFileNames.begin(), mFileNames.end()); |
| 180 | + } else { |
| 181 | + LOG(debug) << "Making temporary file from: " << mFileNames.front(); |
| 182 | + } |
| 183 | + std::ofstream ofs(mFileNames.front().c_str()); |
| 184 | + if (!ofs) { |
| 185 | + LOG(fatal) << "Failed to create temporary file: " << mFileNames.front(); |
| 186 | + return false; |
| 187 | + } |
| 188 | + mTemporary = std::string(mFileNames.front()); |
| 189 | + ofs.close(); |
| 190 | + } else { |
| 191 | + mFileNames.clear(); |
| 192 | + char buf[] = "generatorFifoXXXXXX"; |
| 193 | + auto fp = mkstemp(buf); |
| 194 | + if (fp < 0) { |
| 195 | + LOG(fatal) << "Failed to make temporary file: " |
| 196 | + << std::strerror(errno); |
| 197 | + return false; |
| 198 | + } |
| 199 | + mTemporary = std::string(buf); |
| 200 | + mFileNames.push_back(mTemporary); |
| 201 | + close(fp); |
178 | 202 | } |
179 | | - mTemporary = std::string(buf); |
180 | | - mFileNames.push_back(mTemporary); |
181 | | - close(fp); |
182 | 203 | return true; |
183 | 204 | } |
184 | 205 | // ----------------------------------------------------------------- |
|
0 commit comments