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.
This commit is contained in:
Torsten Dreyer
2016-05-06 22:01:42 +02:00
parent 74b4df9452
commit 561e451192
3 changed files with 24 additions and 8 deletions
+2 -2
View File
@@ -612,7 +612,7 @@ HTTPRepository::failure() const
virtual void gotBodyData(const char* s, int n) virtual void gotBodyData(const char* s, int n)
{ {
if (!file.get()) { if (!file.get()) {
file.reset(new SGFile(pathInRepo.str())); file.reset(new SGBinaryFile(pathInRepo.str()));
if (!file->open(SG_IO_OUT)) { if (!file->open(SG_IO_OUT)) {
SG_LOG(SG_TERRASYNC, SG_WARN, "unable to create file " << pathInRepo); SG_LOG(SG_TERRASYNC, SG_WARN, "unable to create file " << pathInRepo);
_directory->repository()->http->cancelRequest(this, "Unable to create output file"); _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 std::string fileName; // if empty, we're getting the directory itself
SGPath pathInRepo; SGPath pathInRepo;
simgear::sha1nfo hashContext; simgear::sha1nfo hashContext;
std::auto_ptr<SGFile> file; std::auto_ptr<SGBinaryFile> file;
}; };
class DirGetRequest : public HTTPRepoGetRequest class DirGetRequest : public HTTPRepoGetRequest
+15 -5
View File
@@ -45,8 +45,9 @@
#include "sg_file.hxx" #include "sg_file.hxx"
SGFile::SGFile(const std::string &file, int repeat_) SGFile::SGFile(const std::string &file, int repeat_, int extraoflags_ )
: file_name(file), fp(-1), eof_flag(true), repeat(repeat_), iteration(0) : file_name(file), fp(-1), eof_flag(true), repeat(repeat_), iteration(0),
extraoflags(extraoflags_)
{ {
set_type( sgFileType ); set_type( sgFileType );
} }
@@ -70,13 +71,13 @@ bool SGFile::open( const SGProtocolDir d ) {
if ( get_dir() == SG_IO_OUT ) { if ( get_dir() == SG_IO_OUT ) {
#ifdef _WIN32 #ifdef _WIN32
int mode = 00666; int mode = _S_IREAD | _S_IWRITE;
#else #else
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
#endif #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 ) { } 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 { } else {
SG_LOG( SG_IO, SG_ALERT, SG_LOG( SG_IO, SG_ALERT,
"Error: bidirection mode not available for files." ); "Error: bidirection mode not available for files." );
@@ -178,3 +179,12 @@ bool SGFile::close() {
eof_flag = true; eof_flag = true;
return true; return true;
} }
SGBinaryFile::SGBinaryFile( const std::string& file, int repeat_ ) :
#ifdef _WIN32
SGFile(file,repeat_, _O_BINARY)
#else
SGFile(file,repeat_, 0)
#endif
{
}
+7 -1
View File
@@ -39,6 +39,7 @@ class SGFile : public SGIOChannel {
const int repeat; const int repeat;
int iteration; // number of current repetition, int iteration; // number of current repetition,
// starting at 0 // starting at 0
int extraoflags;
public: public:
@@ -50,7 +51,7 @@ public:
* @param file name of file to open * @param file name of file to open
* @param repeat On eof restart at the beginning of the file * @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 * Create an SGFile from an existing, open file-descriptor
@@ -85,4 +86,9 @@ public:
virtual bool eof() const { return eof_flag; }; virtual bool eof() const { return eof_flag; };
}; };
class SGBinaryFile : public SGFile {
public:
SGBinaryFile( const std::string& file, int repeat_ = 1 );
};
#endif // _SG_FILE_HXX #endif // _SG_FILE_HXX