diff --git a/src/basicio.cpp b/src/basicio.cpp index 2b56d246..fd3d4540 100644 --- a/src/basicio.cpp +++ b/src/basicio.cpp @@ -28,9 +28,6 @@ #include "rcsid.hpp" EXIV2_RCSID("@(#) $Id$"); -// Define DEBUG_MAKERNOTE to output debug information to std::cerr -#undef DEBUG_MAKERNOTE - // ***************************************************************************** // included header files #ifdef _MSC_VER @@ -57,6 +54,10 @@ EXIV2_RCSID("@(#) $Id$"); # include // for getpid, stat #endif +#if defined WIN32 && !defined __CYGWIN__ +# include +#endif + // ***************************************************************************** // class member definitions namespace Exiv2 { @@ -99,6 +100,8 @@ namespace Exiv2 { { assert(fp_ != 0); if (opMode_ == opMode) return 0; + OpMode oldOpMode = opMode_; + opMode_ = opMode; bool reopen = true; std::string mode = "r+b"; @@ -125,11 +128,10 @@ namespace Exiv2 { if (!reopen) { // Don't do anything when switching _from_ opSeek mode; we // flush when switching _to_ opSeek. - if (opMode_ == opSeek) return 0; + if (oldOpMode == opSeek) return 0; // Flush. On msvcrt fflush does not do the job fseek(fp_, 0, SEEK_CUR); - opMode_ = opMode; return 0; } @@ -137,7 +139,6 @@ namespace Exiv2 { long offset = ftell(fp_); if (offset == -1) return -1; if (open(mode) != 0) return 1; - opMode_ = opMode; return fseek(fp_, offset, SEEK_SET); } @@ -233,8 +234,8 @@ namespace Exiv2 { assert(pos == BasicIo::end); fileSeek = SEEK_END; } - - opMode_ = opSeek; + + if (switchMode(opSeek) != 0) return 1; return fseek(fp_, offset, fileSeek); } @@ -247,13 +248,14 @@ namespace Exiv2 { long FileIo::size() const { -#if defined WIN32 && !defined __CYGWIN__ - // On msvcrt stat only works if the file is not open, or so it seems - assert(fp_ == 0); -#endif if (fp_ != 0) { fflush(fp_); +#if defined WIN32 && !defined __CYGWIN__ + // This is required on msvcrt before stat after writing to a file + _commit(_fileno(fp_)); +#endif } + struct stat buf; int ret = stat(path_.c_str(), &buf); diff --git a/src/iotest.cpp b/src/iotest.cpp index 441c5935..ac82a607 100644 --- a/src/iotest.cpp +++ b/src/iotest.cpp @@ -73,11 +73,6 @@ try { memIo1.seek(0, BasicIo::beg); fileOut1.write(memIo1); - // On Win32 files must be closed before stat - memIo1.close(); - fileIn.close(); - fileOut1.close(); - // Make sure they are all the same size if(fileIn.size() != memIo1.size() || memIo1.size() != fileOut1.size()) { std::cerr << argv[0] << @@ -102,10 +97,7 @@ try { if (rc != 0) return rc; // Another test of reading and writing - if (fileOut1.open() != 0) { - throw Error(9, fileOut1.path(), strError()); - } - + fileOut1.seek(0, BasicIo::beg); memIo2.seek(0, BasicIo::beg); FileIo fileOut2(argv[3]); if (fileOut2.open("w+b") != 0) { @@ -154,18 +146,13 @@ int WriteReadSeek(BasicIo &io) std::cerr << ": WRS initial write failed\n"; return 2; } - - // On Win32 files must be closed before stat - io.close(); - + if (io.size() != len1) { std::cerr << ": WRS size is not " << len1 << "\n"; return 2; } - if (io.open() != 0) { - throw Error(9, io.path(), strError()); - } + io.seek(-len1, BasicIo::cur); int c = EOF; memset(buf, -1, sizeof(buf));