|
| 1 | +#include "src/impl.h" |
| 2 | +#include "libplatform/impl.h" /* for platform_win32_impl.h which declares Utf8ToFilename */ |
| 3 | +#include <windows.h> |
| 4 | + |
| 5 | +namespace mp4v2 { |
| 6 | + using namespace impl; |
| 7 | +} |
| 8 | + |
| 9 | +namespace mp4v2 { namespace platform { namespace io { |
| 10 | + |
| 11 | +/////////////////////////////////////////////////////////////////////////////// |
| 12 | + |
| 13 | +static DWORD getAttributes ( string path_ ); |
| 14 | + |
| 15 | +/** |
| 16 | + * Call GetFileAttributesW throw exceptions for errors |
| 17 | + * |
| 18 | + * @param path_ the path to get attributes for |
| 19 | + * |
| 20 | + * @retval INVALID_FILE_ATTRIBUTES @p path_ doesn't exist |
| 21 | + * @retval anything else the attributes of @p path_ |
| 22 | + */ |
| 23 | +static DWORD |
| 24 | +getAttributes ( string path_ ) |
| 25 | +{ |
| 26 | + win32::Utf8ToFilename filename(path_); |
| 27 | + |
| 28 | + if (!filename.IsUTF16Valid()) |
| 29 | + { |
| 30 | + // throw an exception to avoid changing the |
| 31 | + // signature of this function and dealing with all |
| 32 | + // the places it's called. |
| 33 | + ostringstream msg; |
| 34 | + msg << "can't convert file to UTF-16(" << filename.utf8 << ")"; |
| 35 | + throw new Exception(msg.str(),__FILE__,__LINE__,__FUNCTION__); |
| 36 | + } |
| 37 | + |
| 38 | + DWORD attributes = ::GetFileAttributesW(filename); |
| 39 | + if( attributes == INVALID_FILE_ATTRIBUTES ) |
| 40 | + { |
| 41 | + DWORD last_err = GetLastError(); |
| 42 | + |
| 43 | + // Distinguish between an error and the path not existing |
| 44 | + if ((last_err == ERROR_FILE_NOT_FOUND) || (last_err == ERROR_PATH_NOT_FOUND)) |
| 45 | + { |
| 46 | + return attributes; |
| 47 | + } |
| 48 | + |
| 49 | + // Anything else is an error |
| 50 | + ostringstream msg; |
| 51 | + msg << "GetFileAttributes(" << filename.utf8 << ") failed (" << last_err << ")"; |
| 52 | + throw new Exception(msg.str(),__FILE__,__LINE__,__FUNCTION__); |
| 53 | + } |
| 54 | + |
| 55 | + // path exists so return its attributes |
| 56 | + return attributes; |
| 57 | +} |
| 58 | + |
| 59 | +bool |
| 60 | +FileSystem::exists( string path_ ) |
| 61 | +{ |
| 62 | + return( getAttributes(path_) != INVALID_FILE_ATTRIBUTES ); |
| 63 | +} |
| 64 | + |
| 65 | +/////////////////////////////////////////////////////////////////////////////// |
| 66 | + |
| 67 | +bool |
| 68 | +FileSystem::isDirectory( string path_ ) |
| 69 | +{ |
| 70 | + DWORD attributes = getAttributes( path_ ); |
| 71 | + if( attributes == INVALID_FILE_ATTRIBUTES ) |
| 72 | + return false; |
| 73 | + |
| 74 | + return ( ( attributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY ); |
| 75 | +} |
| 76 | + |
| 77 | +/////////////////////////////////////////////////////////////////////////////// |
| 78 | + |
| 79 | +bool |
| 80 | +FileSystem::isFile( string path_ ) |
| 81 | +{ |
| 82 | + DWORD attributes = getAttributes( path_ ); |
| 83 | + if( attributes == INVALID_FILE_ATTRIBUTES ) |
| 84 | + return false; |
| 85 | + |
| 86 | + return ( ( attributes & FILE_ATTRIBUTE_DIRECTORY ) != FILE_ATTRIBUTE_DIRECTORY ); |
| 87 | +} |
| 88 | + |
| 89 | +/////////////////////////////////////////////////////////////////////////////// |
| 90 | + |
| 91 | +bool |
| 92 | +FileSystem::getFileSize( string path_, File::Size& size_ ) |
| 93 | +{ |
| 94 | + win32::Utf8ToFilename filename(path_); |
| 95 | + |
| 96 | + if (!filename.IsUTF16Valid()) |
| 97 | + { |
| 98 | + // The logging is done |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + size_ = 0; |
| 103 | + WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 104 | + if( !GetFileAttributesExW( filename, GetFileExInfoStandard, (LPVOID)&data ) ) |
| 105 | + { |
| 106 | + log.errorf("%s: GetFileAttributesExW(%s) failed (%d)",__FUNCTION__,filename.utf8.c_str(), |
| 107 | + GetLastError()); |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + size_ = ( (File::Size)data.nFileSizeHigh << 32 ) | data.nFileSizeLow; |
| 112 | + return false; |
| 113 | +} |
| 114 | + |
| 115 | +/////////////////////////////////////////////////////////////////////////////// |
| 116 | + |
| 117 | +bool |
| 118 | +FileSystem::rename( string from, string to ) |
| 119 | +{ |
| 120 | + win32::Utf8ToFilename from_file(from); |
| 121 | + win32::Utf8ToFilename to_file(to); |
| 122 | + |
| 123 | + if (!from_file.IsUTF16Valid() || !to_file.IsUTF16Valid()) |
| 124 | + { |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + if (!::MoveFileExW( from_file, to_file, |
| 129 | + MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH ) ) |
| 130 | + { |
| 131 | + log.errorf("%s: MoveFileExW(%s,%s) failed (%d)",__FUNCTION__,from_file.utf8.c_str(),to_file.utf8.c_str(), |
| 132 | + GetLastError()); |
| 133 | + return true; |
| 134 | + } |
| 135 | + |
| 136 | + return false; |
| 137 | +} |
| 138 | + |
| 139 | +/////////////////////////////////////////////////////////////////////////////// |
| 140 | + |
| 141 | +string FileSystem::DIR_SEPARATOR = "\\"; |
| 142 | +string FileSystem::PATH_SEPARATOR = ";"; |
| 143 | + |
| 144 | +/////////////////////////////////////////////////////////////////////////////// |
| 145 | + |
| 146 | +}}} // namespace mp4v2::platform::io |
0 commit comments