WSL/SLF GitLab Repository

Commit 8bba4407 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

Fixed a mistake in my last commit and added file name checks when creating files.

parent f0230ef0
......@@ -186,14 +186,8 @@ void Config::parseFile(const std::string& filename)
{
std::ifstream fin; //Input file streams
if (!IOUtils::validFileName(filename)) {
throw InvalidFileNameException(filename,AT);
}
//Check whether file exists
if (!IOUtils::fileExists(filename)) {
throw FileNotFoundException(filename, AT);
}
if (!IOUtils::validFileAndPath(filename)) throw InvalidFileNameException(filename,AT);
if (!IOUtils::fileExists(filename)) throw FileNotFoundException(filename, AT);
//Open file
fin.open (filename.c_str(), ifstream::in);
......@@ -361,9 +355,8 @@ std::string Config::clean_import_path(const std::string& in_path) const
void Config::write(const std::string& filename) const
{
std::ofstream fout;
fout.open(filename.c_str());
if (!IOUtils::validFileAndPath(filename)) throw InvalidFileNameException(filename,AT);
std::ofstream fout(filename.c_str(), ios::out);
if (fout.fail())
throw FileAccessException(filename, AT);
......
......@@ -48,7 +48,7 @@ void copy_file(const std::string& src, const std::string& dest)
std::ifstream fin(src.c_str(), std::ios::binary);
if (fin.fail()) throw FileAccessException(src, AT);
if (!IOUtils::validFileName(dest)) throw InvalidFileNameException(dest, AT);
if (!IOUtils::validFileAndPath(dest)) throw InvalidFileNameException(dest, AT);
std::ofstream fout(dest.c_str(), std::ios::binary);
if (fout.fail()) {
fin.close();
......@@ -80,6 +80,7 @@ std::string cleanPath(std::string in_path, const bool& resolve)
#else //POSIX
std::replace(in_path.begin(), in_path.end(), '\\', '/');
errno = 0;
char *real_path = realpath(in_path.c_str(), NULL); //POSIX 2008
if (real_path!=NULL) {
const std::string tmp(real_path);
......@@ -136,8 +137,7 @@ std::string getFilename(const std::string& path)
return path;
}
//this checks for path+file
bool validFileName(const std::string& filename)
bool validFileAndPath(const std::string& filename)
{
#if defined _WIN32 || defined __MINGW32__ || defined __CYGWIN__
const size_t startpos = filename.find_first_not_of(" \t\n"); // Find the first character position after excluding leading blank spaces
......@@ -153,7 +153,6 @@ bool validFileName(const std::string& filename)
return true;
}
bool isAbsolutePath(const std::string& in_path)
{
#if defined _WIN32 || defined __MINGW32__ || defined __CYGWIN__
......
......@@ -48,7 +48,7 @@ namespace IOUtils {
std::list<std::string> readDirectory(const std::string& path, const std::string& pattern="");
bool validFileName(const std::string& filename);
bool validFileAndPath(const std::string& filename);
bool fileExists(const std::string& filename);
......
......@@ -952,7 +952,7 @@ double USERInterpolation::getQualityRating(const Date& i_date, const MeteoData::
param = in_param;
filename = getGridFileName();
if (!IOUtils::validFileName(filename)) {
if (!IOUtils::validFileAndPath(filename)) {
cerr << "[E] Invalid grid filename for "+algo+" interpolation algorithm: " << filename << "\n";
return 0.0;
}
......
......@@ -205,6 +205,7 @@ std::string ProcessingBlock::getName() const {
void ProcessingBlock::readCorrections(const std::string& filter, const std::string& filename, const char& c_type, const double& init, std::vector<double> &corrections)
{
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
errno = 0;
std::ifstream fin(filename.c_str(), std::ifstream::in);
if (fin.fail()) {
std::ostringstream ss;
......
......@@ -730,6 +730,7 @@ int A3DIO::create1DFile(const std::vector< std::vector<MeteoData> >& data)
const size_t size = data[ii].size();
if(size>0) {
const std::string filename = tmp_path+"/meteo1D_"+data[ii][0].meta.getStationID()+".txt";
if (!IOUtils::validFileAndPath(filename)) throw InvalidFileNameException(filename,AT);
std::ofstream file(filename.c_str(), std::ios::out | std::ios::trunc);
if(!file) {
throw FileAccessException("[E] Can not open file "+filename, AT);
......@@ -821,7 +822,7 @@ void A3DIO::open2DFile(const std::vector< std::vector<MeteoData> >& data,
out << year;
const std::string filename = fileprefix+out.str()+".txt";
if (!IOUtils::validFileAndPath(filename)) throw InvalidFileNameException(filename,AT);
file.open(filename.c_str(), ios::out | ios::trunc);
if(!file) {
throw FileAccessException("Can not create file "+filename, AT);
......
......@@ -135,6 +135,7 @@ void ALPUG::readMetaData()
const string filename = cfg.get("METAFILE", "Input");
const string metafile = inpath + "/" + filename;
if (!IOUtils::fileExists(metafile)) throw FileAccessException(metafile, AT); //prevent invalid filenames
errno = 0;
std::ifstream fin(metafile.c_str(), std::ifstream::in);
if (fin.fail()) {
ostringstream ss;
......@@ -341,6 +342,7 @@ void ALPUG::readMetoFile(const size_t& station_index, const Date& dateStart, con
const string file_and_path = inpath + "/" + filename;
if (!IOUtils::fileExists(file_and_path)) throw FileAccessException(file_and_path, AT); //prevent invalid filenames
errno = 0;
std::ifstream fin(file_and_path.c_str(), ios::in|ios::binary); //ascii does end of line translation, which messes up the pointer code
if (fin.fail())
throw FileAccessException("Could not open \'" + file_and_path +"\'. Possible reason: " + strerror(errno) + "\n", AT);
......
......@@ -170,10 +170,11 @@ void ARCIO::read2DGrid_internal(Grid2DObject& grid_out, const std::string& full_
std::string line;
std::map<std::string, std::string> header; // A map to save key value pairs of the file header
if (!IOUtils::validFileName(full_name)) throw InvalidFileNameException(full_name, AT);
if (!IOUtils::validFileAndPath(full_name)) throw InvalidFileNameException(full_name, AT);
if (!IOUtils::fileExists(full_name)) throw FileNotFoundException(full_name, AT);
fin.clear();
errno = 0;
fin.open (full_name.c_str(), ifstream::in);
if (fin.fail()) {
ostringstream ss;
......@@ -329,8 +330,9 @@ void ARCIO::readPOI(std::vector<Coords>&)
void ARCIO::write2DGrid(const Grid2DObject& grid_in, const std::string& name)
{
const std::string full_name = grid2dpath_out+"/"+name;
if (!IOUtils::fileExists(full_name)) throw FileAccessException(full_name, AT); //prevent invalid filenames
fout.open(full_name.c_str());
if (!IOUtils::validFileAndPath(full_name)) throw InvalidFileNameException(full_name,AT);
errno = 0;
fout.open(full_name.c_str(), ios::out);
if (fout.fail()) {
ostringstream ss;
ss << "Error opening file \"" << full_name << "\", possible reason: " << strerror(errno);
......
......@@ -929,6 +929,7 @@ void CNRMIO::write2DGrid_internal(const Grid2DObject& grid_in, const std::string
ncpp::start_definitions(filename, ncid);
} else {
if (!IOUtils::validFileAndPath(filename)) throw InvalidFileNameException(filename,AT);
ncpp::create_file(filename, NC_CLASSIC_MODEL, ncid);
ncpp::add_attribute(ncid, NC_GLOBAL, "Conventions", "CF-1.3");
......
......@@ -435,6 +435,7 @@ void GRIBIO::read2DGrid(Grid2DObject& grid_out, const std::string& i_name)
{
const std::string filename = grid2dpath_in+"/"+i_name;
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
errno = 0;
fp = fopen(filename.c_str(),"r");
if(fp==NULL) {
ostringstream ss;
......@@ -463,6 +464,7 @@ void GRIBIO::read2DGrid(Grid2DObject& grid_out, const std::string& i_name)
void GRIBIO::indexFile(const std::string& filename)
{
if (!IOUtils::fileExists(filename)) throw FileAccessException(filename, AT); //prevent invalid filenames
errno = 0;
fp = fopen(filename.c_str(),"r");
if(fp==NULL) {
ostringstream ss;
......
......@@ -134,7 +134,9 @@ void GeotopIO::writeMeteoData( const std::vector<std::vector<MeteoData> >& vecMe
}
//write the meta data file _meteo.txt
fout.open(string(path + "/_meteo.txt").c_str());
const string meteoFile = path + "/_meteo.txt";
if (!IOUtils::validFileAndPath(meteoFile)) throw InvalidFileNameException(meteoFile,AT);
fout.open(meteoFile.c_str(), ios::out);
fout << "/* Automatically generated by MeteoIO */\n\n";
fout << "1: double matrix meteo_station{" << vecMeteo.size() << ",13}\n";
for (size_t ii = 0; ii < vecMeteo.size(); ii++) {
......@@ -162,7 +164,8 @@ void GeotopIO::writeMeteoData( const std::vector<std::vector<MeteoData> >& vecMe
ss.fill('0');
ss << path << "/" << "_meteo" << setw(4) << (ii + 1) << ".txt";
fout.open(ss.str().c_str());
if (!IOUtils::validFileAndPath(ss.str())) throw InvalidFileNameException(ss.str(),AT);
fout.open(ss.str().c_str(), ios::out);
if (fout.fail())
throw FileAccessException(ss.str().c_str(), AT);
......@@ -220,10 +223,10 @@ void GeotopIO::readStationData(const Date&, std::vector<StationData>& vecMeta) {
void GeotopIO::readMeteoData(const Date& dateStart, const Date& dateEnd,
std::vector<std::vector<MeteoData> >& vecMeteo, const size_t& /*stationindex*/) {
vecMeteo.clear();
string line, filename, path, prefix;
string line;
cfg.getValue("METEOPATH", "Input", path);
cfg.getValue("METEOPREFIX", "Input", prefix);
const string path = cfg.get("METEOPATH", "Input");
const string prefix = cfg.get("METEOPREFIX", "Input");
//read geotop.inpts to find out how many stations exist
//at what locations they are and what column headers to use
......@@ -246,13 +249,9 @@ void GeotopIO::readMeteoData(const Date& dateStart, const Date& dateEnd,
ss.fill('0');
ss << path << "/" << prefix << setw(4) << (ii + 1) << ".txt";
filename = ss.str();
if (!IOUtils::validFileName(filename))
throw InvalidFileNameException(filename, AT);
if (!IOUtils::fileExists(filename))
throw FileNotFoundException(filename, AT);
const string filename = ss.str();
if (!IOUtils::validFileAndPath(filename)) throw InvalidFileNameException(filename, AT);
if (!IOUtils::fileExists(filename)) throw FileNotFoundException(filename, AT);
fin.clear();
fin.open(filename.c_str(), std::ifstream::in);
......@@ -404,12 +403,8 @@ void GeotopIO::readMetaData(const std::string& metafile) {
std::vector<std::string> tmpvec;
unsigned int stationNumber = 1; //Since the stations don't have a name, they will be numbered
if (!IOUtils::validFileName(metafile)) {
throw InvalidFileNameException(metafile, AT);
}
if (!IOUtils::fileExists(metafile)) {
throw FileNotFoundException(metafile, AT);
}
if (!IOUtils::validFileAndPath(metafile)) throw InvalidFileNameException(metafile, AT);
if (!IOUtils::fileExists(metafile)) throw FileNotFoundException(metafile, AT);
fin.clear();
fin.open(metafile.c_str(), std::ifstream::in);
......
......@@ -79,12 +79,8 @@ void GrassIO::read2DGrid(Grid2DObject& grid_out, const std::string& filename)
std::string line;
std::map<std::string, std::string> header; // A map to save key value pairs of the file header
if (!IOUtils::validFileName(filename)) {
throw InvalidFileNameException(filename, AT);
}
if (!IOUtils::fileExists(filename)) {
throw FileNotFoundException(filename, AT);
}
if (!IOUtils::validFileAndPath(filename)) throw InvalidFileNameException(filename, AT);
if (!IOUtils::fileExists(filename)) throw FileNotFoundException(filename, AT);
fin.clear();
fin.open (filename.c_str(), ifstream::in);
......@@ -226,7 +222,8 @@ void GrassIO::readPOI(std::vector<Coords>&)
void GrassIO::write2DGrid(const Grid2DObject& grid_in, const std::string& name)
{
fout.open(name.c_str());
if (!IOUtils::validFileAndPath(name)) throw InvalidFileNameException(name, AT);
fout.open(name.c_str(), ios::out);
if (fout.fail()) {
throw FileAccessException(name, AT);
}
......
......@@ -534,6 +534,7 @@ void NetCDFIO::write2DGrid_internal(Grid2DObject grid_in, const std::string& fil
ncpp::start_definitions(filename, ncid);
} else {
if (!IOUtils::validFileAndPath(filename)) throw InvalidFileNameException(filename, AT);
ncpp::create_file(filename, NC_CLASSIC_MODEL, ncid);
ncpp::add_attribute(ncid, NC_GLOBAL, "Conventions", "CF-1.3");
create_variable = create_dimensions = true;
......
......@@ -103,14 +103,11 @@ void PGMIO::read2DGrid_internal(Grid2DObject& grid_out, const std::string& full_
std::vector<std::string> tmpvec;
std::string line;
if (!IOUtils::validFileName(full_name)) {
throw InvalidFileNameException(full_name, AT);
}
if (!IOUtils::fileExists(full_name)) {
throw FileNotFoundException(full_name, AT);
}
if (!IOUtils::validFileAndPath(full_name)) throw InvalidFileNameException(full_name, AT);
if (!IOUtils::fileExists(full_name)) throw FileNotFoundException(full_name, AT);
fin.clear();
errno = 0;
fin.open (full_name.c_str(), ifstream::in);
if (fin.fail()) {
ostringstream ss;
......@@ -246,7 +243,9 @@ void PGMIO::write2DGrid(const Grid2DObject& grid_in, const std::string& name)
{
const std::string full_name = grid2dpath_out+"/"+name;
const unsigned int nr_colors = 256;
fout.open(full_name.c_str());
if (!IOUtils::validFileAndPath(full_name)) throw InvalidFileNameException(full_name, AT);
errno = 0;
fout.open(full_name.c_str(), ios::out);
if (fout.fail()) {
ostringstream ss;
ss << "Error opening file \"" << full_name << "\", possible reason: " << strerror(errno);
......
......@@ -318,9 +318,7 @@ Grid2DObject PNGIO::scaleGrid(const Grid2DObject& grid_in)
void PNGIO::setFile(const std::string& filename, png_structp& png_ptr, png_infop& info_ptr, const size_t &width, const size_t &height)
{
// Open file for writing (binary mode)
if (!IOUtils::validFileName(filename)) {
throw InvalidFileNameException(filename, AT);
}
if (!IOUtils::validFileAndPath(filename)) throw InvalidFileNameException(filename, AT);
errno=0;
fp = fopen(filename.c_str(), "wb");
if (fp == NULL) {
......@@ -674,8 +672,8 @@ void PNGIO::writeWorldFile(const Grid2DObject& grid_in, const std::string& filen
world_ref.setProj(coordout, coordoutparam);
world_ref.moveByXY(.5*cellsize, (double(grid_in.getNy())+.5)*cellsize); //moving to center of upper left cell
std::ofstream fout;
fout.open(world_file.c_str());
if (!IOUtils::validFileAndPath(world_file)) throw InvalidFileNameException(world_file, AT);
std::ofstream fout(world_file.c_str(), ios::out);
if (fout.fail()) {
throw FileAccessException(world_file, AT);
}
......
......@@ -166,7 +166,7 @@ void SMETIO::parseInputOutputSection()
const string extension = IOUtils::getExtension(filename);
const std::string file_and_path = (!extension.empty())? inpath+"/"+filename : inpath+"/"+filename+dflt_extension;
if (!IOUtils::validFileName(file_and_path)) //Check whether filename is valid
if (!IOUtils::validFileAndPath(file_and_path)) //Check whether filename is valid
throw InvalidFileNameException(file_and_path, AT);
vecFiles.push_back(file_and_path);
vec_smet_reader.push_back(smet::SMETReader(file_and_path));
......@@ -455,7 +455,7 @@ void SMETIO::writeMeteoData(const std::vector< std::vector<MeteoData> >& vecMete
}
const string filename = outpath + "/" + sd.stationID + ".smet";
if (!IOUtils::validFileName(filename)) //Check whether filename is valid
if (!IOUtils::validFileAndPath(filename)) //Check whether filename is valid
throw InvalidFileNameException(filename, AT);
//2. check which meteo parameter fields are actually in use
......
......@@ -223,18 +223,8 @@ void SNIO::readStationData(const Date&, std::vector<StationData>& vecStation)
bool SNIO::readStationMetaData(const std::string& metafile, const std::string& stationID, StationData& sd)
{
if (!IOUtils::validFileName(metafile)) {
std::ostringstream ss;
ss << "\"" << metafile << "\" is not a valid file name. Please check your METAFILE key!";
throw InvalidFileNameException(metafile, AT);
}
if (!IOUtils::fileExists(metafile)) {
std::ostringstream ss;
ss << "File \"" << metafile << "\" does not exist. Please check your METAFILE key!";
throw FileNotFoundException(ss.str(), AT);
}
if (!IOUtils::validFileAndPath(metafile)) throw InvalidFileNameException("\"" + metafile + "\" is not a valid file name. Please check your METAFILE key!", AT);
if (!IOUtils::fileExists(metafile)) throw FileNotFoundException( "File \"" + metafile + "\" does not exist. Please check your METAFILE key!", AT);
fin.clear();
fin.open (metafile.c_str(), std::ifstream::in);
......@@ -280,10 +270,8 @@ std::string SNIO::getStationID(const std::string& filename)
* This function will return the station name as retrieved from
* the first line of a SNIO formatted meteo file
*/
if ( !IOUtils::validFileName(filename) )
throw InvalidFileNameException(filename, AT);
if ( !IOUtils::fileExists(filename) )
throw FileNotFoundException(filename, AT);
if ( !IOUtils::validFileAndPath(filename) ) throw InvalidFileNameException(filename, AT);
if ( !IOUtils::fileExists(filename) ) throw FileNotFoundException(filename, AT);
fin.clear();
fin.open (filename.c_str(), std::ifstream::in);
......@@ -359,7 +347,7 @@ void SNIO::readMetaData()
const std::string file_and_path = (extension!="")? inpath+"/"+filename : inpath+"/"+filename+dflt_extension;
const string station_id = getStationID(file_and_path);
if (!IOUtils::validFileName(file_and_path)) //Check whether filename is valid
if (!IOUtils::validFileAndPath(file_and_path)) //Check whether filename is valid
throw InvalidFileNameException(file_and_path, AT);
StationData sd(Coords(), station_id);
......@@ -399,10 +387,8 @@ void SNIO::readMeteoData(const Date& dateStart, const Date& dateEnd,
for (size_t ii=0; ii<vecAllStations.size(); ii++){
const std::string file_with_path = vecFilenames[ii];
if ( !IOUtils::validFileName(file_with_path) )
throw InvalidFileNameException(file_with_path, AT);
if ( !IOUtils::fileExists(file_with_path) )
throw FileNotFoundException(file_with_path, AT);
if ( !IOUtils::validFileAndPath(file_with_path) ) throw InvalidFileNameException(file_with_path, AT);
if ( !IOUtils::fileExists(file_with_path) ) throw FileNotFoundException(file_with_path, AT);
fin.clear();
fin.open (file_with_path.c_str(), std::ifstream::in);
......@@ -613,6 +599,7 @@ void SNIO::writeMeteoData(const std::vector< std::vector<MeteoData> >& vecMeteo,
std::string station_id = vecMeteo[ii].front().meta.getStationID();
if (station_id.empty()) station_id = "UNKNOWN";
const std::string output_name = outpath + "/" + station_id + ".inp";
if (!IOUtils::validFileAndPath(output_name)) throw InvalidFileNameException(output_name,AT);
if( !IOUtils::fileExists(output_name) ) {
fout.open(output_name.c_str());
writeStationHeader(vecMeteo[ii], station_id);
......
......@@ -112,6 +112,22 @@ bool SMETCommon::fileExists(const std::string& filename)
}
#endif
bool SMETCommon::validFileAndPath(const std::string& filename)
{
#if defined _WIN32 || defined __MINGW32__ || defined __CYGWIN__
const size_t startpos = filename.find_first_not_of(" \t\n"); // Find the first character position after excluding leading blank spaces
const size_t invalid_char = filename.find_first_of("\000*:<>?|"); //find possible invalid characters
#else
const size_t startpos = filename.find_first_not_of(" \t\n"); // Find the first character position after excluding leading blank spaces
const size_t invalid_char = filename.find_first_of("\000"); //find possible invalid characters
#endif
if ((startpos!=0) || (invalid_char!=std::string::npos) || (filename==".") || (filename=="..")) {
return false;
}
return true;
}
double SMETCommon::convert_to_double(const std::string& in_string)
{
char* conversion_end = NULL;
......@@ -449,6 +465,8 @@ bool SMETWriter::valid_header()
void SMETWriter::write(const std::vector<std::string>& vec_timestamp, const std::vector<double>& data)
{
if (!SMETCommon::validFileAndPath(filename)) throw SMETException("Invalid file name \""+filename+"\"", AT);
errno = 0;
fout.open(filename.c_str(), ios::binary);
if (fout.fail()) {
ostringstream ss;
......@@ -495,6 +513,8 @@ void SMETWriter::write(const std::vector<std::string>& vec_timestamp, const std:
void SMETWriter::write(const std::vector<double>& data)
{
if (!SMETCommon::validFileAndPath(filename)) throw SMETException("Invalid file name \""+filename+"\"", AT);
errno = 0;
fout.open(filename.c_str(), ios::binary);
if (fout.fail()) {
ostringstream ss;
......@@ -609,6 +629,7 @@ void SMETWriter::write_data_line_binary(const std::vector<double>& data)
if (!file_is_binary){ //open it as binary file
fout.close();
errno = 0;
fout.open(filename.c_str(), ios::out | ios::app | ios::binary); //reopen as binary file
if (fout.fail()) {
ostringstream ss;
......@@ -693,6 +714,7 @@ SMETReader::SMETReader(const std::string& in_fname)
std::ifstream fin; //Input file streams
fin.clear();
if (!SMETCommon::fileExists(filename)) throw SMETException(filename, AT); //prevent invalid filenames
errno = 0;
fin.open (filename.c_str(), ios::in|ios::binary); //ascii does end of line translation, which messes up the pointer code
if (fin.fail()) {
ostringstream ss;
......@@ -999,6 +1021,7 @@ void SMETReader::read(std::vector<std::string>& vec_timestamp, std::vector<doubl
ifstream fin;
fin.clear();
if (!SMETCommon::fileExists(filename)) throw SMETException(filename, AT); //prevent invalid filenames
errno = 0;
fin.open (filename.c_str(), ios::in|ios::binary); //ascii mode messes up pointer code on windows (automatic eol translation)
if (fin.fail()) {
ostringstream ss;
......@@ -1049,6 +1072,7 @@ void SMETReader::read(std::vector<double>& vec_data)
ifstream fin;
if (!SMETCommon::fileExists(filename)) throw SMETException(filename, AT); //prevent invalid filenames
errno = 0;
fin.open (filename.c_str(), mode);
if (fin.fail()) {
ostringstream ss;
......
......@@ -63,6 +63,7 @@ class SMETException : public std::exception {
*/
class SMETCommon {
public:
static bool validFileAndPath(const std::string& filename);
static bool fileExists(const std::string& filename);
static double convert_to_double(const std::string& in_string);
static int convert_to_int(const std::string& in_string);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment