Add File::open_error().
authorCarl Hetherington <cth@carlh.net>
Fri, 23 Dec 2022 19:14:59 +0000 (20:14 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 23 Dec 2022 19:14:59 +0000 (20:14 +0100)
src/file.cc
src/file.h

index b79051a346284d4c8b42e11bc7a8a1f3f6b34056..e13954bda44245454137953cf2ebc26840fd8995 100644 (file)
@@ -51,11 +51,18 @@ File::File(boost::filesystem::path path, std::string mode)
        : _path(path)
 {
 #ifdef LIBDCP_WINDOWS
+       SetLastError(0);
        std::wstring mode_wide(mode.begin(), mode.end());
        /* c_str() here should give a UTF-16 string */
        _file = _wfopen(fix_long_path(path).c_str(), mode_wide.c_str());
+       if (!_file) {
+               _open_error = GetLastError();
+       }
 #else
         _file = fopen(path.c_str(), mode.c_str());
+       if (!_file) {
+               _open_error = errno;
+       }
 #endif
 }
 
@@ -63,6 +70,7 @@ File::File(boost::filesystem::path path, std::string mode)
 File::File(File&& other)
        : _path(other._path)
        , _file(other._file)
+       , _open_error(other._open_error)
 {
        other._file = nullptr;
 }
@@ -74,6 +82,7 @@ File::operator=(File&& other)
        if (*this != other) {
                close();
                _file = other._file;
+               _open_error = other._open_error;
                other._file = nullptr;
        }
        return *this;
index edc431bdf3251da03282f9a6bfed331727455444..22083a9efded891e8de479ec8ee094c3adf8fb62 100644 (file)
@@ -101,9 +101,17 @@ public:
                return _file;
        }
 
+       /** @return Error returned by the fopen / _wfopen call;
+        *  errno on POSIX, GetLastError() on Windows.
+        */
+       int open_error() const {
+               return _open_error;
+       }
+
 private:
        boost::filesystem::path _path;
        FILE* _file = nullptr;
+       int _open_error = 0;
 };