henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/win32filesystem.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | #include <shellapi.h> |
| 14 | #include <shlobj.h> |
| 15 | #include <tchar.h> |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "rtc_base/win32.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 17 | |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 18 | #include <memory> |
| 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 20 | #include "rtc_base/arraysize.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/fileutils.h" |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 24 | #include "rtc_base/pathutils.h" |
| 25 | #include "rtc_base/stream.h" |
| 26 | #include "rtc_base/stringutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 27 | |
| 28 | // In several places in this file, we test the integrity level of the process |
| 29 | // before calling GetLongPathName. We do this because calling GetLongPathName |
| 30 | // when running under protected mode IE (a low integrity process) can result in |
| 31 | // a virtualized path being returned, which is wrong if you only plan to read. |
| 32 | // TODO: Waiting to hear back from IE team on whether this is the |
| 33 | // best approach; IEIsProtectedModeProcess is another possible solution. |
| 34 | |
| 35 | namespace rtc { |
| 36 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 37 | bool Win32Filesystem::DeleteFile(const Pathname& filename) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 38 | RTC_LOG(LS_INFO) << "Deleting file " << filename.pathname(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 39 | if (!IsFile(filename)) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 40 | RTC_DCHECK(IsFile(filename)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 41 | return false; |
| 42 | } |
| 43 | return ::DeleteFile(ToUtf16(filename.pathname()).c_str()) != 0; |
| 44 | } |
| 45 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 46 | bool Win32Filesystem::MoveFile(const Pathname& old_path, |
| 47 | const Pathname& new_path) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 48 | if (!IsFile(old_path)) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 49 | RTC_DCHECK(IsFile(old_path)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 50 | return false; |
| 51 | } |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 52 | RTC_LOG(LS_INFO) << "Moving " << old_path.pathname() << " to " |
| 53 | << new_path.pathname(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 54 | return ::MoveFile(ToUtf16(old_path.pathname()).c_str(), |
| 55 | ToUtf16(new_path.pathname()).c_str()) != 0; |
| 56 | } |
| 57 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 58 | bool Win32Filesystem::IsFolder(const Pathname& path) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 59 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 60 | if (0 == ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(), |
| 61 | GetFileExInfoStandard, &data)) |
| 62 | return false; |
| 63 | return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 64 | FILE_ATTRIBUTE_DIRECTORY; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 65 | } |
| 66 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 67 | bool Win32Filesystem::IsFile(const Pathname& path) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 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) == 0; |
| 73 | } |
| 74 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 75 | bool Win32Filesystem::GetFileSize(const Pathname& pathname, size_t* size) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 76 | WIN32_FILE_ATTRIBUTE_DATA data = {0}; |
| 77 | if (::GetFileAttributesEx(ToUtf16(pathname.pathname()).c_str(), |
| 78 | GetFileExInfoStandard, &data) == 0) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 79 | return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 80 | *size = data.nFileSizeLow; |
| 81 | return true; |
| 82 | } |
| 83 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 84 | } // namespace rtc |