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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/win32filesystem.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #include <shellapi.h> |
| 14 | #include <shlobj.h> |
| 15 | #include <tchar.h> |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/win32.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 18 | #include <memory> |
| 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/arraysize.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/fileutils.h" |
| 23 | #include "rtc_base/pathutils.h" |
| 24 | #include "rtc_base/stream.h" |
| 25 | #include "rtc_base/stringutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | bool Win32Filesystem::DeleteFile(const Pathname &filename) { |
| 37 | LOG(LS_INFO) << "Deleting file " << filename.pathname(); |
| 38 | if (!IsFile(filename)) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 39 | RTC_DCHECK(IsFile(filename)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | return false; |
| 41 | } |
| 42 | return ::DeleteFile(ToUtf16(filename.pathname()).c_str()) != 0; |
| 43 | } |
| 44 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 45 | std::string Win32Filesystem::TempFilename(const Pathname &dir, |
| 46 | const std::string &prefix) { |
| 47 | wchar_t filename[MAX_PATH]; |
| 48 | if (::GetTempFileName(ToUtf16(dir.pathname()).c_str(), |
| 49 | ToUtf16(prefix).c_str(), 0, filename) != 0) |
| 50 | return ToUtf8(filename); |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 51 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 52 | return ""; |
| 53 | } |
| 54 | |
| 55 | bool Win32Filesystem::MoveFile(const Pathname &old_path, |
| 56 | const Pathname &new_path) { |
| 57 | if (!IsFile(old_path)) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 58 | RTC_DCHECK(IsFile(old_path)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | LOG(LS_INFO) << "Moving " << old_path.pathname() |
| 62 | << " to " << new_path.pathname(); |
| 63 | return ::MoveFile(ToUtf16(old_path.pathname()).c_str(), |
| 64 | ToUtf16(new_path.pathname()).c_str()) != 0; |
| 65 | } |
| 66 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | bool Win32Filesystem::IsFolder(const Pathname &path) { |
| 68 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 69 | if (0 == ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(), |
| 70 | GetFileExInfoStandard, &data)) |
| 71 | return false; |
| 72 | return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == |
| 73 | FILE_ATTRIBUTE_DIRECTORY; |
| 74 | } |
| 75 | |
| 76 | bool Win32Filesystem::IsFile(const Pathname &path) { |
| 77 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 78 | if (0 == ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(), |
| 79 | GetFileExInfoStandard, &data)) |
| 80 | return false; |
| 81 | return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0; |
| 82 | } |
| 83 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | bool Win32Filesystem::GetFileSize(const Pathname &pathname, size_t *size) { |
| 85 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 86 | if (::GetFileAttributesEx(ToUtf16(pathname.pathname()).c_str(), |
| 87 | GetFileExInfoStandard, &data) == 0) |
| 88 | return false; |
| 89 | *size = data.nFileSizeLow; |
| 90 | return true; |
| 91 | } |
| 92 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | } // namespace rtc |