Skip to content

Commit 2ccb487

Browse files
committed
Files checking feature added
A check is done on the type before opening parameters input, parameters output or wave sound output. The regular file type is always allowed. The directory type is always rejected. Others are accepted or rejected depending the calling parameters.
1 parent 4b2f150 commit 2ccb487

2 files changed

Lines changed: 120 additions & 38 deletions

File tree

cpp_version/params_io_handler.cpp

Lines changed: 93 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,44 @@ string params_fio_handler_base::GetInfos()
4343
info_fio_params.clear();
4444
return the_return;
4545
}
46+
optional<string> params_fio_handler_base::
47+
FileTypeChecker( const filesystem::path& the_path,
48+
const string&prefix,
49+
const bool&check_exists, const bool& check_not_fifo, const bool&check_not_dev_char)
50+
{
51+
filesystem::file_status the_file_status( filesystem::status( the_path ));
52+
if ( filesystem::exists( the_file_status ) == false )
53+
{
54+
if ( check_exists )
55+
return prefix + string(" file ") + the_path.filename().string() + string(" not found");
56+
else
57+
return nullopt;
58+
}
59+
if ( filesystem::is_regular_file( the_file_status ))
60+
return nullopt;
61+
if ( filesystem::is_fifo( the_file_status ) )
62+
{
63+
if ( check_not_fifo )
64+
return prefix + string(" ") + the_path.filename().string() + string(" should NOT be a fifo");
65+
else
66+
return nullopt;
67+
}
68+
if (filesystem::is_directory( the_file_status ) )
69+
return prefix + string(" ") + the_path.filename().string() + string(" should NOT be a directory");
70+
71+
// Since we ask to follow symlinks to the destination,
72+
// it is claimed as a symlink only if it points on nothing.
73+
if (filesystem::is_symlink( the_file_status ) )
74+
return prefix + string(" ") + the_path.filename().string() + string(" should NOT be a symlink");
75+
if ( filesystem::is_character_file( the_file_status ) )
76+
{
77+
if ( check_not_dev_char )
78+
return prefix + string(" ") + the_path.filename().string() + string(" should NOT be a char device");
79+
else
80+
return nullopt;
81+
}
82+
return prefix + string(" ") + the_path.filename().string() + string(" should NOT be a dev block nor socket");
83+
}
4684

4785

4886
params_input_handler::params_input_handler():
@@ -59,11 +97,11 @@ params_input_handler::params_input_handler():
5997
{}
6098

6199

62-
/* @breif Validate options in the current channel
100+
/* @brief Validate options in the current channel
63101
*
64102
* Each time the software receives a new input (-i), a new output (-o)
65103
* or a flush (calling parameters are over), the pending input options are tested and
66-
* if everythingare is correct, a structure is added to the input channels list.
104+
* if everything is correct, a structure is added to the input channels list.
67105
* If something is wrong, the options of this input are discarded.
68106
* That does NOT prevent the usage of other input channels
69107
*/
@@ -101,7 +139,7 @@ void params_input_handler::AddChannelOptions( const options_list_t & opts_list
101139
if( it.second == in_format->second )
102140
{
103141
info_fio_params << "The input commands format "<<in_format->second;
104-
info_fio_params << " is not yet supported, comming soon"<<endl;
142+
info_fio_params << " is not yet supported, coming soon"<<endl;
105143
return;
106144
}
107145
info_fio_params << "The input commands format "<<in_format->second;
@@ -114,7 +152,7 @@ void params_input_handler::AddChannelOptions( const options_list_t & opts_list
114152
void params_input_handler::CreateInputChannels(const unsigned short&loop_counter)
115153
{
116154
// For the input channels there is a sanity check.
117-
// If the type is explicitely defined, the check verifies the input. If false, the input is omitted.
155+
// If the type is explicitly defined, the check verifies the input. If false, the input is omitted.
118156
// If the type is not defined, the software tries to identify with the sanity checks.
119157
for( auto& chan : fio_channels_with_opts )
120158
{
@@ -136,8 +174,20 @@ void params_input_handler::CreateInputChannels(const unsigned short&loop_counter
136174
ifstream input_stream;
137175
if( (*in_opt_name).second.compare( "-" ))
138176
{
139-
input_stream.open( (*in_opt_name).second, ios_base::in );
140-
if( input_stream.is_open() )
177+
bool file_is_opened = false;
178+
filesystem::path the_path( (*in_opt_name).second );
179+
optional<string> ftc = FileTypeChecker( the_path, "Input commands", true, false, true );
180+
if ( ftc )
181+
info_fio_params << *ftc << endl;
182+
else
183+
{
184+
input_stream.open( the_path, ios_base::in );
185+
file_is_opened = input_stream.is_open();
186+
if ( file_is_opened == false )
187+
cout << "Problem opening " << the_path.filename() << ", check the permissions" << endl;
188+
}
189+
190+
if( file_is_opened )
141191
{
142192
switch( chan.first )
143193
{
@@ -169,11 +219,6 @@ void params_input_handler::CreateInputChannels(const unsigned short&loop_counter
169219
break;
170220
}
171221
}
172-
else
173-
{
174-
info_fio_params << "Problem opening file " << (*in_opt_name).second;
175-
info_fio_params << ", check directory and permissions" << endl;
176-
}
177222
}
178223
else
179224
switch( chan.first )
@@ -233,7 +278,7 @@ params_output_handler::params_output_handler():
233278
*
234279
* Each time the software receives a new input (-i), a new output (-o)
235280
* or a flush (calling parameters are over), the pending output options are tested and
236-
* if everythingare is correct, a structure is added to the output channels list.
281+
* if everything is correct, a structure is added to the output channels list.
237282
* If something is wrong, the options of this output are discarded.
238283
* That does NOT prevent the usage of other output channels
239284
*/
@@ -264,14 +309,14 @@ void params_output_handler::AddChannelOptions( const options_list_t & opts_list
264309
if( it.second == out_format->second )
265310
{
266311
info_fio_params << "The output commands format "<<out_format->second;
267-
info_fio_params << " is not yet supported, comming soon"<<endl;
312+
info_fio_params << " is not yet supported, coming soon"<<endl;
268313
return;
269314
}
270315
info_fio_params << "The output commands format "<<out_format->second;
271316
info_fio_params << " is unknown"<<endl;
272317
return;
273318
}
274-
// no format info, use a defult
319+
// no format info, use a default
275320
else
276321
fio_channels_with_opts.push_back( make_pair( params_io_text, opts_list ));
277322
}
@@ -301,8 +346,20 @@ void params_output_handler::CreateOutputChannels(void)
301346

302347
if( (*out_opt_name).second.compare( "-" ))
303348
{
304-
output_stream.open( (*out_opt_name).second, ios_base::out );
305-
if( output_stream.is_open() )
349+
bool file_is_opened = false;
350+
filesystem::path the_path( (*out_opt_name).second );
351+
optional<string> ftc = FileTypeChecker( the_path, "Output commands", false, false, true );
352+
if ( ftc )
353+
info_fio_params << *ftc << endl;
354+
else
355+
{
356+
output_stream.open( the_path, ios_base::out );
357+
file_is_opened = output_stream.is_open();
358+
if ( file_is_opened == false )
359+
cout << "Problem opening " << the_path.filename() << ", check the permissions" << endl;
360+
}
361+
362+
if( file_is_opened )
306363
{
307364
switch( chan.first )
308365
{
@@ -325,11 +382,6 @@ void params_output_handler::CreateOutputChannels(void)
325382
break;
326383
}
327384
}
328-
else
329-
{
330-
info_fio_params << "Problem opening file " << (*out_opt_name).second;
331-
info_fio_params << ", check directory and permissions" << endl;
332-
}
333385
}
334386
else
335387
switch( chan.first )
@@ -418,22 +470,22 @@ void params_file_handler::AddChannelOptions( const options_list_t & opts_list )
418470
if( it.second == out_format->second )
419471
{
420472
info_fio_params << "The file format format "<<out_format->second;
421-
info_fio_params << " is not yet supported, comming soon"<<endl;
473+
info_fio_params << " is not yet supported, coming soon"<<endl;
422474
return;
423475
}
424476
info_fio_params << "The file format "<<out_format->second;
425477
info_fio_params << " is unknown"<<endl;
426478
return;
427479
}
428-
// no format info, use a defult
480+
// no format info, use a default
429481
else
430482
fio_channels_with_opts.push_back( make_pair( params_file_16BE, opts_list ));
431483
}
432484
void params_file_handler::EnvironementNeeds(void)
433485
{}
434486
void params_file_handler::CreateOutputFile()
435487
{
436-
// This may be temporary as today, there is only one possiblie file at a time
488+
// This may be temporary as today, there is only one possible file at a time
437489
if ( fio_channels_with_opts.empty() == false )
438490
{
439491
auto chan = fio_channels_with_opts.rbegin();
@@ -458,8 +510,23 @@ void params_file_handler::CreateOutputFile()
458510
{
459511
if( (*file_opt_name).second.compare( "-" ))
460512
{
461-
output_file_stream.open( (*file_opt_name).second, ostream::binary | ostream::trunc );
462-
info_fio_params << "Opening the output file" << (*file_opt_name).second << endl;
513+
bool file_is_opened = false;
514+
filesystem::path the_path( (*file_opt_name).second );
515+
optional<string> ftc = FileTypeChecker( the_path, "Output sound file", false, true, true );
516+
if ( ftc )
517+
info_fio_params << *ftc << endl;
518+
else
519+
{
520+
output_file_stream.open( the_path, ostream::binary | ostream::trunc );
521+
file_is_opened = output_file_stream.is_open();
522+
if ( file_is_opened == false )
523+
cout << "Problem opening " << the_path.filename() << ", check the permissions" << endl;
524+
else
525+
info_fio_params << "Opening the output file" << the_path.filename() << endl;
526+
}
527+
// *************************************************
528+
// Should do something if the file can not be opened
529+
// *************************************************
463530
}else{
464531
info_fio_params << "Using the standard output for the file output is discourageous" << endl;
465532
}

cpp_version/params_io_handler.hxx

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <iostream>
66
#include <sstream>
77
#include <set>
8+
#include <optional>
9+
#include <filesystem>
810
// #include <execution>
911

1012
#ifndef __PARAMS_IO_HANDLER__
@@ -60,12 +62,25 @@ protected:
6062
virtual void AddChannelOptions( const options_list_t & ) = 0;
6163
virtual void EnvironementNeeds(void) = 0;
6264

63-
/** @brief parse a boolean parameter
65+
/** @brief Parse a boolean parameter
6466
*
6567
* @return the result or the default
6668
*/
6769
bool GetBoolParam(const options_list_t&the_params_list,
6870
const unsigned char&param_code, const bool default_data = false );
71+
/** @brief Check the files type
72+
*
73+
* Sanity check on the file type before opening it.
74+
* It is used by the input parameters, output parameters wave sound output,
75+
* trigger output (to be written).
76+
* The type regular file is always allowed.
77+
* Some types, such as directory are always rejected.
78+
* Other types are accepted or rejected according with the calling parameters.
79+
* @return nothing if it is accepted, an error string if the type is rejected.
80+
*/
81+
optional<string> FileTypeChecker( const filesystem::path& the_path,
82+
const string&prefix,
83+
const bool&check_exists, const bool&check_not_fifo, const bool&check_not_dev_char);
6984
/** \brief Get information
7085
*
7186
* This returns both the notes and the errors
@@ -98,9 +113,9 @@ class params_input_handler : public params_fio_handler_base {
98113
* and perform the last checks
99114
*/
100115
void CreateInputChannels(const unsigned short&);
101-
/** @brief environement needs
116+
/** @brief environment needs
102117
*
103-
* Make the list of environement needs
118+
* Make the list of environment needs
104119
* such as network alive, jackaudio etc...
105120
*/
106121
void EnvironementNeeds(void);
@@ -134,9 +149,9 @@ class params_output_handler : public params_fio_handler_base {
134149
* and perform the last checks
135150
*/
136151
void CreateOutputChannels(void);
137-
/** @brief environement needs
152+
/** @brief environment needs
138153
*
139-
* Make the list of environement needs
154+
* Make the list of environment needs
140155
* such as network alive, jackaudio etc...
141156
*/
142157
void EnvironementNeeds(void);
@@ -167,9 +182,9 @@ class params_file_handler : public params_fio_handler_base {
167182
* and add it to the list
168183
*/
169184
void AddChannelOptions( const options_list_t & );
170-
/** @brief environement needs
185+
/** @brief environment needs
171186
*
172-
* Make the list of environement needs
187+
* Make the list of environment needs
173188
* such as network alive, jackaudio etc...
174189
*/
175190
void EnvironementNeeds(void);
@@ -209,9 +224,9 @@ public:
209224
* and perform the last checks
210225
*/
211226
void CreateChannels(const unsigned short&n_loops);
212-
/** @brief environement needs
227+
/** @brief environment needs
213228
*
214-
* Make the list of environement needs
229+
* Make the list of environment needs
215230
* such as network alive, jackaudio etc...
216231
*/
217232
void EnvironementNeeds(void);
@@ -232,14 +247,14 @@ public:
232247
*
233248
* Tells if there is at least one output that passed the first sanity check.\n
234249
* That is not a guarantee there is at least one valid channel
235-
* @return true if there is atr least one
250+
* @return true if there is at least one
236251
*/
237252
bool hasOutputChannels()const;
238253
/** @brief Has file defined
239254
*
240-
* Tells if there is a filename (or pipe) that passed the first sanity check.\n
255+
* Tells if there is a file-name (or pipe) that passed the first sanity check.\n
241256
* That is not a guarantee there is at least one valid channel
242-
* @return true if there is atr least one
257+
* @return true if there is at least one
243258
*/
244259
bool hasFileOutput()const;
245260
/** @brief Has really file output

0 commit comments

Comments
 (0)