From 8d93206dd33ed0079af6670a0ecd41a3b203d9a0 Mon Sep 17 00:00:00 2001 From: Torsten Dreyer Date: Fri, 6 May 2016 22:01:42 +0200 Subject: [PATCH] Introduce SGBinaryFile For Windows, default file mode is TEXT. If binary files should be created _O_BINARY shall be or'ed to the open flags. This is not necessary on *nixes. Introduce a SGBinaryFile as extension to SGFile which adds the flag upon construction on Windows. This should keep existing behaviour for all other usages of SGFile. --- simgear/io/HTTPRepository.cxx | 4 ++-- simgear/io/sg_file.cxx | 20 +++++++++++++++----- simgear/io/sg_file.hxx | 8 +++++++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/simgear/io/HTTPRepository.cxx b/simgear/io/HTTPRepository.cxx index e5d82469..09f2f8e5 100644 --- a/simgear/io/HTTPRepository.cxx +++ b/simgear/io/HTTPRepository.cxx @@ -612,7 +612,7 @@ HTTPRepository::failure() const virtual void gotBodyData(const char* s, int n) { if (!file.get()) { - file.reset(new SGFile(pathInRepo.str())); + file.reset(new SGBinaryFile(pathInRepo.str())); if (!file->open(SG_IO_OUT)) { SG_LOG(SG_TERRASYNC, SG_WARN, "unable to create file " << pathInRepo); _directory->repository()->http->cancelRequest(this, "Unable to create output file"); @@ -664,7 +664,7 @@ HTTPRepository::failure() const std::string fileName; // if empty, we're getting the directory itself SGPath pathInRepo; simgear::sha1nfo hashContext; - std::auto_ptr file; + std::auto_ptr file; }; class DirGetRequest : public HTTPRepoGetRequest diff --git a/simgear/io/sg_file.cxx b/simgear/io/sg_file.cxx index 0cf66f77..71de546e 100644 --- a/simgear/io/sg_file.cxx +++ b/simgear/io/sg_file.cxx @@ -45,8 +45,9 @@ #include "sg_file.hxx" -SGFile::SGFile(const std::string &file, int repeat_) - : file_name(file), fp(-1), eof_flag(true), repeat(repeat_), iteration(0) +SGFile::SGFile(const std::string &file, int repeat_, int extraoflags_ ) + : file_name(file), fp(-1), eof_flag(true), repeat(repeat_), iteration(0), + extraoflags(extraoflags_) { set_type( sgFileType ); } @@ -70,13 +71,13 @@ bool SGFile::open( const SGProtocolDir d ) { if ( get_dir() == SG_IO_OUT ) { #ifdef _WIN32 - int mode = 00666; + int mode = _S_IREAD | _S_IWRITE; #else mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; #endif - fp = ::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode ); + fp = ::open( file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC | extraoflags, mode ); } else if ( get_dir() == SG_IO_IN ) { - fp = ::open( file_name.c_str(), O_RDONLY ); + fp = ::open( file_name.c_str(), O_RDONLY | extraoflags ); } else { SG_LOG( SG_IO, SG_ALERT, "Error: bidirection mode not available for files." ); @@ -178,3 +179,12 @@ bool SGFile::close() { eof_flag = true; return true; } + +SGBinaryFile::SGBinaryFile( const std::string& file, int repeat_ ) : +#ifdef _WIN32 + SGFile(file,repeat_, _O_BINARY) +#else + SGFile(file,repeat_, 0) +#endif +{ +} diff --git a/simgear/io/sg_file.hxx b/simgear/io/sg_file.hxx index 950cc5d7..53da227a 100644 --- a/simgear/io/sg_file.hxx +++ b/simgear/io/sg_file.hxx @@ -39,6 +39,7 @@ class SGFile : public SGIOChannel { const int repeat; int iteration; // number of current repetition, // starting at 0 + int extraoflags; public: @@ -50,7 +51,7 @@ public: * @param file name of file to open * @param repeat On eof restart at the beginning of the file */ - SGFile( const std::string& file, int repeat_ = 1 ); + SGFile( const std::string& file, int repeat_ = 1, int extraoflags = 0); /** * Create an SGFile from an existing, open file-descriptor @@ -85,4 +86,9 @@ public: virtual bool eof() const { return eof_flag; }; }; +class SGBinaryFile : public SGFile { +public: + SGBinaryFile( const std::string& file, int repeat_ = 1 ); +}; + #endif // _SG_FILE_HXX -- 2.39.5