blob: cd439660dbdf9ed2c1b40f0c52a5da5d59d4eed7 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 Bonadei92ea95e2017-09-15 04:47:3111#include "rtc_base/win32filesystem.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
henrike@webrtc.orgf0488722014-05-13 18:00:2613#include <shellapi.h>
14#include <shlobj.h>
15#include <tchar.h>
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "rtc_base/win32.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2617
jbauch555604a2016-04-26 10:13:2218#include <memory>
19
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "rtc_base/arraysize.h"
21#include "rtc_base/checks.h"
22#include "rtc_base/fileutils.h"
Yves Gerey2e00abc2018-10-05 13:39:2423#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3124#include "rtc_base/pathutils.h"
25#include "rtc_base/stream.h"
26#include "rtc_base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2627
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
35namespace rtc {
36
Yves Gerey665174f2018-06-19 13:03:0537bool Win32Filesystem::DeleteFile(const Pathname& filename) {
Mirko Bonadei675513b2017-11-09 10:09:2538 RTC_LOG(LS_INFO) << "Deleting file " << filename.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:2639 if (!IsFile(filename)) {
nisseede5da42017-01-12 13:15:3640 RTC_DCHECK(IsFile(filename));
henrike@webrtc.orgf0488722014-05-13 18:00:2641 return false;
42 }
43 return ::DeleteFile(ToUtf16(filename.pathname()).c_str()) != 0;
44}
45
Yves Gerey665174f2018-06-19 13:03:0546bool Win32Filesystem::MoveFile(const Pathname& old_path,
47 const Pathname& new_path) {
henrike@webrtc.orgf0488722014-05-13 18:00:2648 if (!IsFile(old_path)) {
nisseede5da42017-01-12 13:15:3649 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:2650 return false;
51 }
Mirko Bonadei675513b2017-11-09 10:09:2552 RTC_LOG(LS_INFO) << "Moving " << old_path.pathname() << " to "
53 << new_path.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:2654 return ::MoveFile(ToUtf16(old_path.pathname()).c_str(),
55 ToUtf16(new_path.pathname()).c_str()) != 0;
56}
57
Yves Gerey665174f2018-06-19 13:03:0558bool Win32Filesystem::IsFolder(const Pathname& path) {
henrike@webrtc.orgf0488722014-05-13 18:00:2659 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 Gerey665174f2018-06-19 13:03:0564 FILE_ATTRIBUTE_DIRECTORY;
henrike@webrtc.orgf0488722014-05-13 18:00:2665}
66
Yves Gerey665174f2018-06-19 13:03:0567bool Win32Filesystem::IsFile(const Pathname& path) {
henrike@webrtc.orgf0488722014-05-13 18:00:2668 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 Gerey665174f2018-06-19 13:03:0575bool Win32Filesystem::GetFileSize(const Pathname& pathname, size_t* size) {
henrike@webrtc.orgf0488722014-05-13 18:00:2676 WIN32_FILE_ATTRIBUTE_DATA data = {0};
77 if (::GetFileAttributesEx(ToUtf16(pathname.pathname()).c_str(),
78 GetFileExInfoStandard, &data) == 0)
Yves Gerey665174f2018-06-19 13:03:0579 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:2680 *size = data.nFileSizeLow;
81 return true;
82}
83
henrike@webrtc.orgf0488722014-05-13 18:00:2684} // namespace rtc