Skip to content

Commit 78dc525

Browse files
committed
Consistent filename checks
1 parent e4caa7d commit 78dc525

File tree

6 files changed

+88
-110
lines changed

6 files changed

+88
-110
lines changed

core/include/core/dataio.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,5 @@ g3_ostream_to_path(std::ostream &stream, const std::string &path, bool append=fa
5959
*/
6060
void g3_stream_close(std::ios &stream);
6161

62-
/**
63-
* Check that the input filename is a valid filename on disk.
64-
*
65-
* @throws runtime_error If filename is invalid or missing.
66-
*/
67-
void g3_check_input_path(const std::string &path);
68-
69-
/**
70-
* Check that the output filename is a valid filename on disk.
71-
*
72-
* @throws runtime_error If filename is empty, or its parent directory is
73-
* missing.
74-
*/
75-
void g3_check_output_path(const std::string &path);
76-
7762
#endif
7863

core/src/G3MultiFileWriter.cxx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ G3MultiFileWriter::G3MultiFileWriter(boost::python::object filename,
4040

4141
if (fstr.check()) {
4242
filename_ = fstr();
43-
g3_check_output_path(filename_);
4443

4544
if (snprintf(NULL, 0, filename_.c_str(), 0) < 0)
4645
log_fatal("Cannot format filename. Should be "
@@ -119,8 +118,6 @@ G3MultiFileWriter::CheckNewFile(G3FramePtr frame)
119118
} else {
120119
filename = boost::python::extract<std::string>(
121120
filename_callback_(frame, seqno++))();
122-
123-
g3_check_output_path(filename);
124121
}
125122

126123
current_filename_ = filename;

core/src/G3Reader.cxx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ G3Reader::G3Reader(const std::string &filename, int n_frames_to_read,
88
n_frames_read_(0), n_frames_cur_(0), timeout_(timeout),
99
track_filename_(track_filename), buffersize_(buffersize)
1010
{
11-
g3_check_input_path(filename);
1211
StartFile(filename);
1312
}
1413

@@ -21,10 +20,9 @@ G3Reader::G3Reader(const std::vector<std::string> &filename, int n_frames_to_rea
2120
if (filename.size() == 0)
2221
log_fatal("Empty file list provided to G3Reader");
2322

24-
for (auto i = filename.begin(); i != filename.end(); i++){
25-
g3_check_input_path(*i);
23+
for (auto i = filename.begin(); i != filename.end(); i++)
2624
filename_.push_back(*i);
27-
}
25+
2826
StartFile(filename_.front());
2927
filename_.pop_front();
3028
}

core/src/G3Writer.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ G3Writer::G3Writer(std::string filename,
77
bool append, size_t buffersize) :
88
filename_(filename), stream_(nullptr), streams_(streams)
99
{
10-
g3_check_output_path(filename);
1110
g3_ostream_to_path(stream_, filename, append, buffersize);
1211
}
1312

core/src/dataio.cxx

Lines changed: 84 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,35 @@ get_codec(const std::string &path, const std::string &ext=".g3")
231231
log_fatal("Invalid filename %s", path.c_str());
232232
}
233233

234+
static Codec
235+
check_input_path(const std::string &path, const std::string &ext)
236+
{
237+
std::filesystem::path fpath(path);
238+
if (!std::filesystem::exists(fpath) ||
239+
!std::filesystem::is_regular_file(fpath))
240+
log_fatal("Could not find file %s", path.c_str());
241+
242+
return get_codec(path, ext);
243+
}
244+
245+
static Codec
246+
check_output_path(const std::string &path, const std::string &ext)
247+
{
248+
std::filesystem::path fpath(path);
249+
250+
if (fpath.empty())
251+
log_fatal("Empty file path");
252+
253+
if (fpath.has_parent_path()) {
254+
auto ppath = fpath.parent_path();
255+
if (!std::filesystem::exists(ppath))
256+
log_fatal("Parent path does not exist: %s",
257+
ppath.string().c_str());
258+
}
259+
260+
return get_codec(path, ext);
261+
}
262+
234263
class InputFileStreamCounter : public std::streambuf {
235264
public:
236265
InputFileStreamCounter(const std::string& path, size_t size)
@@ -286,57 +315,6 @@ class InputFileStreamCounter : public std::streambuf {
286315
size_t bytes_;
287316
};
288317

289-
void
290-
g3_istream_from_path(std::istream &stream, const std::string &path, float timeout,
291-
size_t buffersize, const std::string &ext)
292-
{
293-
g3_stream_close(stream);
294-
295-
std::streambuf *sbuf = nullptr;
296-
297-
// Figure out what kind of ultimate data source this is
298-
if (path.find("tcp://") == 0) {
299-
sbuf = new RemoteInputStreamBuffer(path, timeout, buffersize);
300-
} else {
301-
// Simple file case
302-
switch(get_codec(path, ext)) {
303-
#ifdef ZLIB_FOUND
304-
case GZ:
305-
sbuf = new GZipDecoder(path, buffersize);
306-
break;
307-
#endif
308-
#ifdef BZIP2_FOUND
309-
case BZIP2:
310-
sbuf = new BZip2Decoder(path, buffersize);
311-
break;
312-
#endif
313-
#ifdef LZMA_FOUND
314-
case LZMA:
315-
sbuf = new LZMADecoder(path, buffersize);
316-
break;
317-
#endif
318-
default:
319-
// Read buffer
320-
sbuf = new InputFileStreamCounter(path, buffersize);
321-
break;
322-
}
323-
}
324-
325-
stream.rdbuf(sbuf);
326-
}
327-
328-
int
329-
g3_istream_handle(std::istream &stream)
330-
{
331-
std::streambuf* sbuf = stream.rdbuf();
332-
if (!sbuf)
333-
return -1;
334-
RemoteInputStreamBuffer* rbuf = dynamic_cast<RemoteInputStreamBuffer*>(sbuf);
335-
if (!rbuf)
336-
return -1;
337-
return rbuf->fd();
338-
}
339-
340318
class OutputFileStreamCounter : public std::streambuf {
341319
public:
342320
OutputFileStreamCounter(const std::string& path, size_t size, bool append)
@@ -395,6 +373,60 @@ class OutputFileStreamCounter : public std::streambuf {
395373
size_t bytes_;
396374
};
397375

376+
377+
void
378+
g3_istream_from_path(std::istream &stream, const std::string &path, float timeout,
379+
size_t buffersize, const std::string &ext)
380+
{
381+
g3_stream_close(stream);
382+
383+
std::streambuf *sbuf = nullptr;
384+
385+
// Figure out what kind of ultimate data source this is
386+
if (path.find("tcp://") == 0) {
387+
sbuf = new RemoteInputStreamBuffer(path, timeout, buffersize);
388+
goto done;
389+
}
390+
391+
// Simple file case
392+
switch(check_input_path(path, ext)) {
393+
#ifdef ZLIB_FOUND
394+
case GZ:
395+
sbuf = new GZipDecoder(path, buffersize);
396+
break;
397+
#endif
398+
#ifdef BZIP2_FOUND
399+
case BZIP2:
400+
sbuf = new BZip2Decoder(path, buffersize);
401+
break;
402+
#endif
403+
#ifdef LZMA_FOUND
404+
case LZMA:
405+
sbuf = new LZMADecoder(path, buffersize);
406+
break;
407+
#endif
408+
default:
409+
// Read buffer
410+
sbuf = new InputFileStreamCounter(path, buffersize);
411+
break;
412+
}
413+
414+
done:
415+
stream.rdbuf(sbuf);
416+
}
417+
418+
int
419+
g3_istream_handle(std::istream &stream)
420+
{
421+
std::streambuf* sbuf = stream.rdbuf();
422+
if (!sbuf)
423+
return -1;
424+
RemoteInputStreamBuffer* rbuf = dynamic_cast<RemoteInputStreamBuffer*>(sbuf);
425+
if (!rbuf)
426+
return -1;
427+
return rbuf->fd();
428+
}
429+
398430
void
399431
g3_ostream_to_path(std::ostream &stream, const std::string &path, bool append,
400432
size_t buffersize, const std::string &ext)
@@ -403,7 +435,7 @@ g3_ostream_to_path(std::ostream &stream, const std::string &path, bool append,
403435

404436
std::streambuf *sbuf = nullptr;
405437

406-
Codec codec = get_codec(path, ext);
438+
Codec codec = check_output_path(path, ext);
407439
if (append && codec != NONE)
408440
log_fatal("Cannot append to compressed file.");
409441

@@ -439,31 +471,3 @@ g3_stream_close(std::ios &stream)
439471
delete sbuf;
440472
stream.rdbuf(nullptr);
441473
}
442-
443-
void
444-
g3_check_input_path(const std::string &path)
445-
{
446-
if (path.find("://") != path.npos)
447-
return;
448-
449-
std::filesystem::path fpath(path);
450-
if (!std::filesystem::exists(fpath) ||
451-
!std::filesystem::is_regular_file(fpath))
452-
log_fatal("Could not find file %s", path.c_str());
453-
}
454-
455-
void
456-
g3_check_output_path(const std::string &path)
457-
{
458-
std::filesystem::path fpath(path);
459-
460-
if (fpath.empty())
461-
log_fatal("Empty file path");
462-
463-
if (!fpath.has_parent_path())
464-
return;
465-
466-
if (!std::filesystem::exists(fpath.parent_path()))
467-
log_fatal("Parent path does not exist: %s",
468-
fpath.parent_path().string().c_str());
469-
}

gcp/src/ARCFileReader.cxx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ ARCFileReader::ARCFileReader(const std::string &path,
138138
buffersize_(buffersize)
139139
{
140140
SetExperiment(experiment);
141-
g3_check_input_path(path);
142141
StartFile(path);
143142
}
144143

@@ -152,15 +151,11 @@ ARCFileReader::ARCFileReader(const std::vector<std::string> &filename,
152151
if (filename.size() == 0)
153152
log_fatal("Empty file list provided to G3Reader");
154153

155-
for (auto i = filename.begin(); i != filename.end(); i++){
156-
g3_check_input_path(*i);
154+
for (auto i = filename.begin(); i != filename.end(); i++)
157155
filename_.push_back(*i);
158-
}
159156

160-
const std::string path = filename_.front();
157+
StartFile(filename_.front());
161158
filename_.pop_front();
162-
StartFile(path);
163-
164159
}
165160

166161
ARCFileReader::~ARCFileReader()

0 commit comments

Comments
 (0)