henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/base/win32filesystem.h" |
| 12 | |
| 13 | #include "webrtc/base/win32.h" |
| 14 | #include <shellapi.h> |
| 15 | #include <shlobj.h> |
| 16 | #include <tchar.h> |
| 17 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 18 | #include <memory> |
| 19 | |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 20 | #include "webrtc/base/arraysize.h" |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame^] | 21 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | #include "webrtc/base/fileutils.h" |
| 23 | #include "webrtc/base/pathutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | #include "webrtc/base/stream.h" |
| 25 | #include "webrtc/base/stringutils.h" |
| 26 | |
| 27 | // In several places in this file, we test the integrity level of the process |
| 28 | // before calling GetLongPathName. We do this because calling GetLongPathName |
| 29 | // when running under protected mode IE (a low integrity process) can result in |
| 30 | // a virtualized path being returned, which is wrong if you only plan to read. |
| 31 | // TODO: Waiting to hear back from IE team on whether this is the |
| 32 | // best approach; IEIsProtectedModeProcess is another possible solution. |
| 33 | |
| 34 | namespace rtc { |
| 35 | |
| 36 | bool Win32Filesystem::CreateFolder(const Pathname &pathname) { |
| 37 | if (pathname.pathname().empty() || !pathname.filename().empty()) |
| 38 | return false; |
| 39 | |
| 40 | std::wstring path16; |
| 41 | if (!Utf8ToWindowsFilename(pathname.pathname(), &path16)) |
| 42 | return false; |
| 43 | |
| 44 | DWORD res = ::GetFileAttributes(path16.c_str()); |
| 45 | if (res != INVALID_FILE_ATTRIBUTES) { |
| 46 | // Something exists at this location, check if it is a directory |
| 47 | return ((res & FILE_ATTRIBUTE_DIRECTORY) != 0); |
| 48 | } else if ((GetLastError() != ERROR_FILE_NOT_FOUND) |
| 49 | && (GetLastError() != ERROR_PATH_NOT_FOUND)) { |
| 50 | // Unexpected error |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | // Directory doesn't exist, look up one directory level |
| 55 | if (!pathname.parent_folder().empty()) { |
| 56 | Pathname parent(pathname); |
| 57 | parent.SetFolder(pathname.parent_folder()); |
| 58 | if (!CreateFolder(parent)) { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return (::CreateDirectory(path16.c_str(), NULL) != 0); |
| 64 | } |
| 65 | |
| 66 | FileStream *Win32Filesystem::OpenFile(const Pathname &filename, |
| 67 | const std::string &mode) { |
| 68 | FileStream *fs = new FileStream(); |
| 69 | if (fs && !fs->Open(filename.pathname().c_str(), mode.c_str(), NULL)) { |
| 70 | delete fs; |
| 71 | fs = NULL; |
| 72 | } |
| 73 | return fs; |
| 74 | } |
| 75 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 76 | bool Win32Filesystem::DeleteFile(const Pathname &filename) { |
| 77 | LOG(LS_INFO) << "Deleting file " << filename.pathname(); |
| 78 | if (!IsFile(filename)) { |
| 79 | ASSERT(IsFile(filename)); |
| 80 | return false; |
| 81 | } |
| 82 | return ::DeleteFile(ToUtf16(filename.pathname()).c_str()) != 0; |
| 83 | } |
| 84 | |
| 85 | bool Win32Filesystem::DeleteEmptyFolder(const Pathname &folder) { |
| 86 | LOG(LS_INFO) << "Deleting folder " << folder.pathname(); |
| 87 | |
| 88 | std::string no_slash(folder.pathname(), 0, folder.pathname().length()-1); |
| 89 | return ::RemoveDirectory(ToUtf16(no_slash).c_str()) != 0; |
| 90 | } |
| 91 | |
| 92 | bool Win32Filesystem::GetTemporaryFolder(Pathname &pathname, bool create, |
| 93 | const std::string *append) { |
| 94 | wchar_t buffer[MAX_PATH + 1]; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 95 | if (!::GetTempPath(arraysize(buffer), buffer)) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 96 | return false; |
| 97 | if (!IsCurrentProcessLowIntegrity() && |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 98 | !::GetLongPathName(buffer, buffer, arraysize(buffer))) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 99 | return false; |
| 100 | size_t len = strlen(buffer); |
| 101 | if ((len > 0) && (buffer[len-1] != '\\')) { |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 102 | len += strcpyn(buffer + len, arraysize(buffer) - len, L"\\"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | } |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 104 | if (len >= arraysize(buffer) - 1) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 105 | return false; |
| 106 | pathname.clear(); |
| 107 | pathname.SetFolder(ToUtf8(buffer)); |
| 108 | if (append != NULL) { |
| 109 | ASSERT(!append->empty()); |
| 110 | pathname.AppendFolder(*append); |
| 111 | } |
| 112 | return !create || CreateFolder(pathname); |
| 113 | } |
| 114 | |
| 115 | std::string Win32Filesystem::TempFilename(const Pathname &dir, |
| 116 | const std::string &prefix) { |
| 117 | wchar_t filename[MAX_PATH]; |
| 118 | if (::GetTempFileName(ToUtf16(dir.pathname()).c_str(), |
| 119 | ToUtf16(prefix).c_str(), 0, filename) != 0) |
| 120 | return ToUtf8(filename); |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame^] | 121 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 122 | return ""; |
| 123 | } |
| 124 | |
| 125 | bool Win32Filesystem::MoveFile(const Pathname &old_path, |
| 126 | const Pathname &new_path) { |
| 127 | if (!IsFile(old_path)) { |
| 128 | ASSERT(IsFile(old_path)); |
| 129 | return false; |
| 130 | } |
| 131 | LOG(LS_INFO) << "Moving " << old_path.pathname() |
| 132 | << " to " << new_path.pathname(); |
| 133 | return ::MoveFile(ToUtf16(old_path.pathname()).c_str(), |
| 134 | ToUtf16(new_path.pathname()).c_str()) != 0; |
| 135 | } |
| 136 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 137 | bool Win32Filesystem::IsFolder(const Pathname &path) { |
| 138 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 139 | if (0 == ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(), |
| 140 | GetFileExInfoStandard, &data)) |
| 141 | return false; |
| 142 | return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == |
| 143 | FILE_ATTRIBUTE_DIRECTORY; |
| 144 | } |
| 145 | |
| 146 | bool Win32Filesystem::IsFile(const Pathname &path) { |
| 147 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 148 | if (0 == ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(), |
| 149 | GetFileExInfoStandard, &data)) |
| 150 | return false; |
| 151 | return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0; |
| 152 | } |
| 153 | |
| 154 | bool Win32Filesystem::IsAbsent(const Pathname& path) { |
| 155 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 156 | if (0 != ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(), |
| 157 | GetFileExInfoStandard, &data)) |
| 158 | return false; |
| 159 | DWORD err = ::GetLastError(); |
| 160 | return (ERROR_FILE_NOT_FOUND == err || ERROR_PATH_NOT_FOUND == err); |
| 161 | } |
| 162 | |
| 163 | bool Win32Filesystem::CopyFile(const Pathname &old_path, |
| 164 | const Pathname &new_path) { |
| 165 | return ::CopyFile(ToUtf16(old_path.pathname()).c_str(), |
| 166 | ToUtf16(new_path.pathname()).c_str(), TRUE) != 0; |
| 167 | } |
| 168 | |
| 169 | bool Win32Filesystem::IsTemporaryPath(const Pathname& pathname) { |
| 170 | TCHAR buffer[MAX_PATH + 1]; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 171 | if (!::GetTempPath(arraysize(buffer), buffer)) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 172 | return false; |
| 173 | if (!IsCurrentProcessLowIntegrity() && |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 174 | !::GetLongPathName(buffer, buffer, arraysize(buffer))) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | return false; |
| 176 | return (::strnicmp(ToUtf16(pathname.pathname()).c_str(), |
| 177 | buffer, strlen(buffer)) == 0); |
| 178 | } |
| 179 | |
| 180 | bool Win32Filesystem::GetFileSize(const Pathname &pathname, size_t *size) { |
| 181 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 182 | if (::GetFileAttributesEx(ToUtf16(pathname.pathname()).c_str(), |
| 183 | GetFileExInfoStandard, &data) == 0) |
| 184 | return false; |
| 185 | *size = data.nFileSizeLow; |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | bool Win32Filesystem::GetFileTime(const Pathname& path, FileTimeType which, |
| 190 | time_t* time) { |
| 191 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 192 | if (::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(), |
| 193 | GetFileExInfoStandard, &data) == 0) |
| 194 | return false; |
| 195 | switch (which) { |
| 196 | case FTT_CREATED: |
| 197 | FileTimeToUnixTime(data.ftCreationTime, time); |
| 198 | break; |
| 199 | case FTT_MODIFIED: |
| 200 | FileTimeToUnixTime(data.ftLastWriteTime, time); |
| 201 | break; |
| 202 | case FTT_ACCESSED: |
| 203 | FileTimeToUnixTime(data.ftLastAccessTime, time); |
| 204 | break; |
| 205 | default: |
| 206 | return false; |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | bool Win32Filesystem::GetAppPathname(Pathname* path) { |
| 212 | TCHAR buffer[MAX_PATH + 1]; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 213 | if (0 == ::GetModuleFileName(NULL, buffer, arraysize(buffer))) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 214 | return false; |
| 215 | path->SetPathname(ToUtf8(buffer)); |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | bool Win32Filesystem::GetAppDataFolder(Pathname* path, bool per_user) { |
| 220 | ASSERT(!organization_name_.empty()); |
| 221 | ASSERT(!application_name_.empty()); |
| 222 | TCHAR buffer[MAX_PATH + 1]; |
| 223 | int csidl = per_user ? CSIDL_LOCAL_APPDATA : CSIDL_COMMON_APPDATA; |
| 224 | if (!::SHGetSpecialFolderPath(NULL, buffer, csidl, TRUE)) |
| 225 | return false; |
| 226 | if (!IsCurrentProcessLowIntegrity() && |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 227 | !::GetLongPathName(buffer, buffer, arraysize(buffer))) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 228 | return false; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 229 | size_t len = strcatn(buffer, arraysize(buffer), __T("\\")); |
| 230 | len += strcpyn(buffer + len, arraysize(buffer) - len, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 231 | ToUtf16(organization_name_).c_str()); |
| 232 | if ((len > 0) && (buffer[len-1] != __T('\\'))) { |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 233 | len += strcpyn(buffer + len, arraysize(buffer) - len, __T("\\")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 234 | } |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 235 | len += strcpyn(buffer + len, arraysize(buffer) - len, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 236 | ToUtf16(application_name_).c_str()); |
| 237 | if ((len > 0) && (buffer[len-1] != __T('\\'))) { |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 238 | len += strcpyn(buffer + len, arraysize(buffer) - len, __T("\\")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 239 | } |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 240 | if (len >= arraysize(buffer) - 1) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 241 | return false; |
| 242 | path->clear(); |
| 243 | path->SetFolder(ToUtf8(buffer)); |
| 244 | return CreateFolder(*path); |
| 245 | } |
| 246 | |
| 247 | bool Win32Filesystem::GetAppTempFolder(Pathname* path) { |
| 248 | if (!GetAppPathname(path)) |
| 249 | return false; |
| 250 | std::string filename(path->filename()); |
| 251 | return GetTemporaryFolder(*path, true, &filename); |
| 252 | } |
| 253 | |
jlmiller@webrtc.org | ea1c842 | 2015-01-22 17:44:19 +0000 | [diff] [blame] | 254 | bool Win32Filesystem::GetDiskFreeSpace(const Pathname& path, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 255 | int64_t* free_bytes) { |
jlmiller@webrtc.org | ea1c842 | 2015-01-22 17:44:19 +0000 | [diff] [blame] | 256 | if (!free_bytes) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 257 | return false; |
| 258 | } |
| 259 | char drive[4]; |
| 260 | std::wstring drive16; |
| 261 | const wchar_t* target_drive = NULL; |
| 262 | if (path.GetDrive(drive, sizeof(drive))) { |
| 263 | drive16 = ToUtf16(drive); |
| 264 | target_drive = drive16.c_str(); |
| 265 | } else if (path.folder().substr(0, 2) == "\\\\") { |
| 266 | // UNC path, fail. |
| 267 | // TODO: Handle UNC paths. |
| 268 | return false; |
| 269 | } else { |
| 270 | // The path is probably relative. GetDriveType and GetDiskFreeSpaceEx |
| 271 | // use the current drive if NULL is passed as the drive name. |
| 272 | // TODO: Add method to Pathname to determine if the path is relative. |
| 273 | // TODO: Add method to Pathname to convert a path to absolute. |
| 274 | } |
jlmiller@webrtc.org | ea1c842 | 2015-01-22 17:44:19 +0000 | [diff] [blame] | 275 | UINT drive_type = ::GetDriveType(target_drive); |
| 276 | if ((drive_type == DRIVE_REMOTE) || (drive_type == DRIVE_UNKNOWN)) { |
| 277 | LOG(LS_VERBOSE) << "Remote or unknown drive: " << drive; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 281 | int64_t total_number_of_bytes; // receives the number of bytes on disk |
| 282 | int64_t total_number_of_free_bytes; // receives the free bytes on disk |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 283 | // make sure things won't change in 64 bit machine |
| 284 | // TODO replace with compile time assert |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 285 | ASSERT(sizeof(ULARGE_INTEGER) == sizeof(uint64_t)); // NOLINT |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 286 | if (::GetDiskFreeSpaceEx(target_drive, |
jlmiller@webrtc.org | ea1c842 | 2015-01-22 17:44:19 +0000 | [diff] [blame] | 287 | (PULARGE_INTEGER)free_bytes, |
| 288 | (PULARGE_INTEGER)&total_number_of_bytes, |
| 289 | (PULARGE_INTEGER)&total_number_of_free_bytes)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 290 | return true; |
| 291 | } else { |
jlmiller@webrtc.org | ea1c842 | 2015-01-22 17:44:19 +0000 | [diff] [blame] | 292 | LOG(LS_VERBOSE) << "GetDiskFreeSpaceEx returns error."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 293 | return false; |
| 294 | } |
| 295 | } |
| 296 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 297 | } // namespace rtc |