| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/win32filesystem.h" | 11 #include "webrtc/base/win32filesystem.h" |
| 12 | 12 |
| 13 #include "webrtc/base/win32.h" | 13 #include "webrtc/base/win32.h" |
| 14 #include <shellapi.h> | 14 #include <shellapi.h> |
| 15 #include <shlobj.h> | 15 #include <shlobj.h> |
| 16 #include <tchar.h> | 16 #include <tchar.h> |
| 17 | 17 |
| 18 #include "webrtc/base/arraysize.h" |
| 18 #include "webrtc/base/fileutils.h" | 19 #include "webrtc/base/fileutils.h" |
| 19 #include "webrtc/base/pathutils.h" | 20 #include "webrtc/base/pathutils.h" |
| 20 #include "webrtc/base/scoped_ptr.h" | 21 #include "webrtc/base/scoped_ptr.h" |
| 21 #include "webrtc/base/stream.h" | 22 #include "webrtc/base/stream.h" |
| 22 #include "webrtc/base/stringutils.h" | 23 #include "webrtc/base/stringutils.h" |
| 23 | 24 |
| 24 // In several places in this file, we test the integrity level of the process | 25 // In several places in this file, we test the integrity level of the process |
| 25 // before calling GetLongPathName. We do this because calling GetLongPathName | 26 // before calling GetLongPathName. We do this because calling GetLongPathName |
| 26 // when running under protected mode IE (a low integrity process) can result in | 27 // when running under protected mode IE (a low integrity process) can result in |
| 27 // a virtualized path being returned, which is wrong if you only plan to read. | 28 // a virtualized path being returned, which is wrong if you only plan to read. |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 bool Win32Filesystem::DeleteEmptyFolder(const Pathname &folder) { | 191 bool Win32Filesystem::DeleteEmptyFolder(const Pathname &folder) { |
| 191 LOG(LS_INFO) << "Deleting folder " << folder.pathname(); | 192 LOG(LS_INFO) << "Deleting folder " << folder.pathname(); |
| 192 | 193 |
| 193 std::string no_slash(folder.pathname(), 0, folder.pathname().length()-1); | 194 std::string no_slash(folder.pathname(), 0, folder.pathname().length()-1); |
| 194 return ::RemoveDirectory(ToUtf16(no_slash).c_str()) != 0; | 195 return ::RemoveDirectory(ToUtf16(no_slash).c_str()) != 0; |
| 195 } | 196 } |
| 196 | 197 |
| 197 bool Win32Filesystem::GetTemporaryFolder(Pathname &pathname, bool create, | 198 bool Win32Filesystem::GetTemporaryFolder(Pathname &pathname, bool create, |
| 198 const std::string *append) { | 199 const std::string *append) { |
| 199 wchar_t buffer[MAX_PATH + 1]; | 200 wchar_t buffer[MAX_PATH + 1]; |
| 200 if (!::GetTempPath(ARRAY_SIZE(buffer), buffer)) | 201 if (!::GetTempPath(arraysize(buffer), buffer)) |
| 201 return false; | 202 return false; |
| 202 if (!IsCurrentProcessLowIntegrity() && | 203 if (!IsCurrentProcessLowIntegrity() && |
| 203 !::GetLongPathName(buffer, buffer, ARRAY_SIZE(buffer))) | 204 !::GetLongPathName(buffer, buffer, arraysize(buffer))) |
| 204 return false; | 205 return false; |
| 205 size_t len = strlen(buffer); | 206 size_t len = strlen(buffer); |
| 206 if ((len > 0) && (buffer[len-1] != '\\')) { | 207 if ((len > 0) && (buffer[len-1] != '\\')) { |
| 207 len += strcpyn(buffer + len, ARRAY_SIZE(buffer) - len, L"\\"); | 208 len += strcpyn(buffer + len, arraysize(buffer) - len, L"\\"); |
| 208 } | 209 } |
| 209 if (len >= ARRAY_SIZE(buffer) - 1) | 210 if (len >= arraysize(buffer) - 1) |
| 210 return false; | 211 return false; |
| 211 pathname.clear(); | 212 pathname.clear(); |
| 212 pathname.SetFolder(ToUtf8(buffer)); | 213 pathname.SetFolder(ToUtf8(buffer)); |
| 213 if (append != NULL) { | 214 if (append != NULL) { |
| 214 ASSERT(!append->empty()); | 215 ASSERT(!append->empty()); |
| 215 pathname.AppendFolder(*append); | 216 pathname.AppendFolder(*append); |
| 216 } | 217 } |
| 217 return !create || CreateFolder(pathname); | 218 return !create || CreateFolder(pathname); |
| 218 } | 219 } |
| 219 | 220 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 } | 289 } |
| 289 | 290 |
| 290 bool Win32Filesystem::CopyFile(const Pathname &old_path, | 291 bool Win32Filesystem::CopyFile(const Pathname &old_path, |
| 291 const Pathname &new_path) { | 292 const Pathname &new_path) { |
| 292 return ::CopyFile(ToUtf16(old_path.pathname()).c_str(), | 293 return ::CopyFile(ToUtf16(old_path.pathname()).c_str(), |
| 293 ToUtf16(new_path.pathname()).c_str(), TRUE) != 0; | 294 ToUtf16(new_path.pathname()).c_str(), TRUE) != 0; |
| 294 } | 295 } |
| 295 | 296 |
| 296 bool Win32Filesystem::IsTemporaryPath(const Pathname& pathname) { | 297 bool Win32Filesystem::IsTemporaryPath(const Pathname& pathname) { |
| 297 TCHAR buffer[MAX_PATH + 1]; | 298 TCHAR buffer[MAX_PATH + 1]; |
| 298 if (!::GetTempPath(ARRAY_SIZE(buffer), buffer)) | 299 if (!::GetTempPath(arraysize(buffer), buffer)) |
| 299 return false; | 300 return false; |
| 300 if (!IsCurrentProcessLowIntegrity() && | 301 if (!IsCurrentProcessLowIntegrity() && |
| 301 !::GetLongPathName(buffer, buffer, ARRAY_SIZE(buffer))) | 302 !::GetLongPathName(buffer, buffer, arraysize(buffer))) |
| 302 return false; | 303 return false; |
| 303 return (::strnicmp(ToUtf16(pathname.pathname()).c_str(), | 304 return (::strnicmp(ToUtf16(pathname.pathname()).c_str(), |
| 304 buffer, strlen(buffer)) == 0); | 305 buffer, strlen(buffer)) == 0); |
| 305 } | 306 } |
| 306 | 307 |
| 307 bool Win32Filesystem::GetFileSize(const Pathname &pathname, size_t *size) { | 308 bool Win32Filesystem::GetFileSize(const Pathname &pathname, size_t *size) { |
| 308 WIN32_FILE_ATTRIBUTE_DATA data = {0}; | 309 WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 309 if (::GetFileAttributesEx(ToUtf16(pathname.pathname()).c_str(), | 310 if (::GetFileAttributesEx(ToUtf16(pathname.pathname()).c_str(), |
| 310 GetFileExInfoStandard, &data) == 0) | 311 GetFileExInfoStandard, &data) == 0) |
| 311 return false; | 312 return false; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 330 FileTimeToUnixTime(data.ftLastAccessTime, time); | 331 FileTimeToUnixTime(data.ftLastAccessTime, time); |
| 331 break; | 332 break; |
| 332 default: | 333 default: |
| 333 return false; | 334 return false; |
| 334 } | 335 } |
| 335 return true; | 336 return true; |
| 336 } | 337 } |
| 337 | 338 |
| 338 bool Win32Filesystem::GetAppPathname(Pathname* path) { | 339 bool Win32Filesystem::GetAppPathname(Pathname* path) { |
| 339 TCHAR buffer[MAX_PATH + 1]; | 340 TCHAR buffer[MAX_PATH + 1]; |
| 340 if (0 == ::GetModuleFileName(NULL, buffer, ARRAY_SIZE(buffer))) | 341 if (0 == ::GetModuleFileName(NULL, buffer, arraysize(buffer))) |
| 341 return false; | 342 return false; |
| 342 path->SetPathname(ToUtf8(buffer)); | 343 path->SetPathname(ToUtf8(buffer)); |
| 343 return true; | 344 return true; |
| 344 } | 345 } |
| 345 | 346 |
| 346 bool Win32Filesystem::GetAppDataFolder(Pathname* path, bool per_user) { | 347 bool Win32Filesystem::GetAppDataFolder(Pathname* path, bool per_user) { |
| 347 ASSERT(!organization_name_.empty()); | 348 ASSERT(!organization_name_.empty()); |
| 348 ASSERT(!application_name_.empty()); | 349 ASSERT(!application_name_.empty()); |
| 349 TCHAR buffer[MAX_PATH + 1]; | 350 TCHAR buffer[MAX_PATH + 1]; |
| 350 int csidl = per_user ? CSIDL_LOCAL_APPDATA : CSIDL_COMMON_APPDATA; | 351 int csidl = per_user ? CSIDL_LOCAL_APPDATA : CSIDL_COMMON_APPDATA; |
| 351 if (!::SHGetSpecialFolderPath(NULL, buffer, csidl, TRUE)) | 352 if (!::SHGetSpecialFolderPath(NULL, buffer, csidl, TRUE)) |
| 352 return false; | 353 return false; |
| 353 if (!IsCurrentProcessLowIntegrity() && | 354 if (!IsCurrentProcessLowIntegrity() && |
| 354 !::GetLongPathName(buffer, buffer, ARRAY_SIZE(buffer))) | 355 !::GetLongPathName(buffer, buffer, arraysize(buffer))) |
| 355 return false; | 356 return false; |
| 356 size_t len = strcatn(buffer, ARRAY_SIZE(buffer), __T("\\")); | 357 size_t len = strcatn(buffer, arraysize(buffer), __T("\\")); |
| 357 len += strcpyn(buffer + len, ARRAY_SIZE(buffer) - len, | 358 len += strcpyn(buffer + len, arraysize(buffer) - len, |
| 358 ToUtf16(organization_name_).c_str()); | 359 ToUtf16(organization_name_).c_str()); |
| 359 if ((len > 0) && (buffer[len-1] != __T('\\'))) { | 360 if ((len > 0) && (buffer[len-1] != __T('\\'))) { |
| 360 len += strcpyn(buffer + len, ARRAY_SIZE(buffer) - len, __T("\\")); | 361 len += strcpyn(buffer + len, arraysize(buffer) - len, __T("\\")); |
| 361 } | 362 } |
| 362 len += strcpyn(buffer + len, ARRAY_SIZE(buffer) - len, | 363 len += strcpyn(buffer + len, arraysize(buffer) - len, |
| 363 ToUtf16(application_name_).c_str()); | 364 ToUtf16(application_name_).c_str()); |
| 364 if ((len > 0) && (buffer[len-1] != __T('\\'))) { | 365 if ((len > 0) && (buffer[len-1] != __T('\\'))) { |
| 365 len += strcpyn(buffer + len, ARRAY_SIZE(buffer) - len, __T("\\")); | 366 len += strcpyn(buffer + len, arraysize(buffer) - len, __T("\\")); |
| 366 } | 367 } |
| 367 if (len >= ARRAY_SIZE(buffer) - 1) | 368 if (len >= arraysize(buffer) - 1) |
| 368 return false; | 369 return false; |
| 369 path->clear(); | 370 path->clear(); |
| 370 path->SetFolder(ToUtf8(buffer)); | 371 path->SetFolder(ToUtf8(buffer)); |
| 371 return CreateFolder(*path); | 372 return CreateFolder(*path); |
| 372 } | 373 } |
| 373 | 374 |
| 374 bool Win32Filesystem::GetAppTempFolder(Pathname* path) { | 375 bool Win32Filesystem::GetAppTempFolder(Pathname* path) { |
| 375 if (!GetAppPathname(path)) | 376 if (!GetAppPathname(path)) |
| 376 return false; | 377 return false; |
| 377 std::string filename(path->filename()); | 378 std::string filename(path->filename()); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 temp_path16.append(1, '\0'); | 453 temp_path16.append(1, '\0'); |
| 453 | 454 |
| 454 SHFILEOPSTRUCT file_op = { 0 }; | 455 SHFILEOPSTRUCT file_op = { 0 }; |
| 455 file_op.wFunc = FO_DELETE; | 456 file_op.wFunc = FO_DELETE; |
| 456 file_op.pFrom = temp_path16.c_str(); | 457 file_op.pFrom = temp_path16.c_str(); |
| 457 file_op.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT; | 458 file_op.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT; |
| 458 return (0 == SHFileOperation(&file_op)); | 459 return (0 == SHFileOperation(&file_op)); |
| 459 */ | 460 */ |
| 460 | 461 |
| 461 } // namespace rtc | 462 } // namespace rtc |
| OLD | NEW |