blob: 024aa84a12215cb9f4ec612dfbf22b247089affe [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) {
37 LOG(LS_INFO) << "Deleting file " << filename.pathname();
38 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 +000045std::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);
nissec80e7412017-01-11 05:56:46 -080051 RTC_NOTREACHED();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052 return "";
53}
54
55bool Win32Filesystem::MoveFile(const Pathname &old_path,
56 const Pathname &new_path) {
57 if (!IsFile(old_path)) {
nisseede5da42017-01-12 05:15:36 -080058 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059 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.orgf0488722014-05-13 18:00:26 +000067bool 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
76bool 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.orgf0488722014-05-13 18:00:26 +000084bool 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.orgf0488722014-05-13 18:00:26 +000093} // namespace rtc