blob: 8ca84c34bb146c93c705e1030cb6eb081234caa3 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 06:47:31 +020011#include "rtc_base/win32filesystem.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#include <shellapi.h>
14#include <shlobj.h>
15#include <tchar.h>
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/win32.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
jbauch555604a2016-04-26 03:13:22 -070018#include <memory>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#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.orgf0488722014-05-13 18:00:26 +000026
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
34namespace rtc {
35
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036bool Win32Filesystem::DeleteFile(const Pathname &filename) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010037 RTC_LOG(LS_INFO) << "Deleting file " << filename.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038 if (!IsFile(filename)) {
nisseede5da42017-01-12 05:15:36 -080039 RTC_DCHECK(IsFile(filename));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040 return false;
41 }
42 return ::DeleteFile(ToUtf16(filename.pathname()).c_str()) != 0;
43}
44
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045bool Win32Filesystem::MoveFile(const Pathname &old_path,
46 const Pathname &new_path) {
47 if (!IsFile(old_path)) {
nisseede5da42017-01-12 05:15:36 -080048 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049 return false;
50 }
Mirko Bonadei675513b2017-11-09 11:09:25 +010051 RTC_LOG(LS_INFO) << "Moving " << old_path.pathname() << " to "
52 << new_path.pathname();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053 return ::MoveFile(ToUtf16(old_path.pathname()).c_str(),
54 ToUtf16(new_path.pathname()).c_str()) != 0;
55}
56
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057bool Win32Filesystem::IsFolder(const Pathname &path) {
58 WIN32_FILE_ATTRIBUTE_DATA data = {0};
59 if (0 == ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(),
60 GetFileExInfoStandard, &data))
61 return false;
62 return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ==
63 FILE_ATTRIBUTE_DIRECTORY;
64}
65
66bool Win32Filesystem::IsFile(const Pathname &path) {
67 WIN32_FILE_ATTRIBUTE_DATA data = {0};
68 if (0 == ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(),
69 GetFileExInfoStandard, &data))
70 return false;
71 return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
72}
73
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074bool Win32Filesystem::GetFileSize(const Pathname &pathname, size_t *size) {
75 WIN32_FILE_ATTRIBUTE_DATA data = {0};
76 if (::GetFileAttributesEx(ToUtf16(pathname.pathname()).c_str(),
77 GetFileExInfoStandard, &data) == 0)
78 return false;
79 *size = data.nFileSizeLow;
80 return true;
81}
82
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083} // namespace rtc