blob: 9e4ef51ffed7b4b53c17cf8d993f2e05eff1e9eb [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
11#include "webrtc/base/win32filesystem.h"
12
13#include "webrtc/base/win32.h"
14#include <shellapi.h>
15#include <shlobj.h>
16#include <tchar.h>
17
jbauch555604a2016-04-26 03:13:22 -070018#include <memory>
19
tfarina5237aaf2015-11-10 23:44:30 -080020#include "webrtc/base/arraysize.h"
nissec80e7412017-01-11 05:56:46 -080021#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022#include "webrtc/base/fileutils.h"
23#include "webrtc/base/pathutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024#include "webrtc/base/stream.h"
25#include "webrtc/base/stringutils.h"
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
34namespace rtc {
35
36bool Win32Filesystem::CreateFolder(const Pathname &pathname) {
37 if (pathname.pathname().empty() || !pathname.filename().empty())
38 return false;
39
40 std::wstring path16;
41 if (!Utf8ToWindowsFilename(pathname.pathname(), &path16))
42 return false;
43
44 DWORD res = ::GetFileAttributes(path16.c_str());
45 if (res != INVALID_FILE_ATTRIBUTES) {
46 // Something exists at this location, check if it is a directory
47 return ((res & FILE_ATTRIBUTE_DIRECTORY) != 0);
48 } else if ((GetLastError() != ERROR_FILE_NOT_FOUND)
49 && (GetLastError() != ERROR_PATH_NOT_FOUND)) {
50 // Unexpected error
51 return false;
52 }
53
54 // Directory doesn't exist, look up one directory level
55 if (!pathname.parent_folder().empty()) {
56 Pathname parent(pathname);
57 parent.SetFolder(pathname.parent_folder());
58 if (!CreateFolder(parent)) {
59 return false;
60 }
61 }
62
63 return (::CreateDirectory(path16.c_str(), NULL) != 0);
64}
65
66FileStream *Win32Filesystem::OpenFile(const Pathname &filename,
67 const std::string &mode) {
68 FileStream *fs = new FileStream();
69 if (fs && !fs->Open(filename.pathname().c_str(), mode.c_str(), NULL)) {
70 delete fs;
71 fs = NULL;
72 }
73 return fs;
74}
75
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076bool Win32Filesystem::DeleteFile(const Pathname &filename) {
77 LOG(LS_INFO) << "Deleting file " << filename.pathname();
78 if (!IsFile(filename)) {
nisseede5da42017-01-12 05:15:36 -080079 RTC_DCHECK(IsFile(filename));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 return false;
81 }
82 return ::DeleteFile(ToUtf16(filename.pathname()).c_str()) != 0;
83}
84
85bool Win32Filesystem::DeleteEmptyFolder(const Pathname &folder) {
86 LOG(LS_INFO) << "Deleting folder " << folder.pathname();
87
88 std::string no_slash(folder.pathname(), 0, folder.pathname().length()-1);
89 return ::RemoveDirectory(ToUtf16(no_slash).c_str()) != 0;
90}
91
92bool Win32Filesystem::GetTemporaryFolder(Pathname &pathname, bool create,
93 const std::string *append) {
94 wchar_t buffer[MAX_PATH + 1];
tfarina5237aaf2015-11-10 23:44:30 -080095 if (!::GetTempPath(arraysize(buffer), buffer))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 return false;
97 if (!IsCurrentProcessLowIntegrity() &&
tfarina5237aaf2015-11-10 23:44:30 -080098 !::GetLongPathName(buffer, buffer, arraysize(buffer)))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099 return false;
100 size_t len = strlen(buffer);
101 if ((len > 0) && (buffer[len-1] != '\\')) {
tfarina5237aaf2015-11-10 23:44:30 -0800102 len += strcpyn(buffer + len, arraysize(buffer) - len, L"\\");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103 }
tfarina5237aaf2015-11-10 23:44:30 -0800104 if (len >= arraysize(buffer) - 1)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105 return false;
106 pathname.clear();
107 pathname.SetFolder(ToUtf8(buffer));
108 if (append != NULL) {
nisseede5da42017-01-12 05:15:36 -0800109 RTC_DCHECK(!append->empty());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110 pathname.AppendFolder(*append);
111 }
112 return !create || CreateFolder(pathname);
113}
114
115std::string Win32Filesystem::TempFilename(const Pathname &dir,
116 const std::string &prefix) {
117 wchar_t filename[MAX_PATH];
118 if (::GetTempFileName(ToUtf16(dir.pathname()).c_str(),
119 ToUtf16(prefix).c_str(), 0, filename) != 0)
120 return ToUtf8(filename);
nissec80e7412017-01-11 05:56:46 -0800121 RTC_NOTREACHED();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122 return "";
123}
124
125bool Win32Filesystem::MoveFile(const Pathname &old_path,
126 const Pathname &new_path) {
127 if (!IsFile(old_path)) {
nisseede5da42017-01-12 05:15:36 -0800128 RTC_DCHECK(IsFile(old_path));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129 return false;
130 }
131 LOG(LS_INFO) << "Moving " << old_path.pathname()
132 << " to " << new_path.pathname();
133 return ::MoveFile(ToUtf16(old_path.pathname()).c_str(),
134 ToUtf16(new_path.pathname()).c_str()) != 0;
135}
136
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137bool Win32Filesystem::IsFolder(const Pathname &path) {
138 WIN32_FILE_ATTRIBUTE_DATA data = {0};
139 if (0 == ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(),
140 GetFileExInfoStandard, &data))
141 return false;
142 return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ==
143 FILE_ATTRIBUTE_DIRECTORY;
144}
145
146bool Win32Filesystem::IsFile(const Pathname &path) {
147 WIN32_FILE_ATTRIBUTE_DATA data = {0};
148 if (0 == ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(),
149 GetFileExInfoStandard, &data))
150 return false;
151 return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
152}
153
154bool Win32Filesystem::IsAbsent(const Pathname& path) {
155 WIN32_FILE_ATTRIBUTE_DATA data = {0};
156 if (0 != ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(),
157 GetFileExInfoStandard, &data))
158 return false;
159 DWORD err = ::GetLastError();
160 return (ERROR_FILE_NOT_FOUND == err || ERROR_PATH_NOT_FOUND == err);
161}
162
163bool Win32Filesystem::CopyFile(const Pathname &old_path,
164 const Pathname &new_path) {
165 return ::CopyFile(ToUtf16(old_path.pathname()).c_str(),
166 ToUtf16(new_path.pathname()).c_str(), TRUE) != 0;
167}
168
169bool Win32Filesystem::IsTemporaryPath(const Pathname& pathname) {
170 TCHAR buffer[MAX_PATH + 1];
tfarina5237aaf2015-11-10 23:44:30 -0800171 if (!::GetTempPath(arraysize(buffer), buffer))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172 return false;
173 if (!IsCurrentProcessLowIntegrity() &&
tfarina5237aaf2015-11-10 23:44:30 -0800174 !::GetLongPathName(buffer, buffer, arraysize(buffer)))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175 return false;
176 return (::strnicmp(ToUtf16(pathname.pathname()).c_str(),
177 buffer, strlen(buffer)) == 0);
178}
179
180bool Win32Filesystem::GetFileSize(const Pathname &pathname, size_t *size) {
181 WIN32_FILE_ATTRIBUTE_DATA data = {0};
182 if (::GetFileAttributesEx(ToUtf16(pathname.pathname()).c_str(),
183 GetFileExInfoStandard, &data) == 0)
184 return false;
185 *size = data.nFileSizeLow;
186 return true;
187}
188
189bool Win32Filesystem::GetFileTime(const Pathname& path, FileTimeType which,
190 time_t* time) {
191 WIN32_FILE_ATTRIBUTE_DATA data = {0};
192 if (::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(),
193 GetFileExInfoStandard, &data) == 0)
194 return false;
195 switch (which) {
196 case FTT_CREATED:
197 FileTimeToUnixTime(data.ftCreationTime, time);
198 break;
199 case FTT_MODIFIED:
200 FileTimeToUnixTime(data.ftLastWriteTime, time);
201 break;
202 case FTT_ACCESSED:
203 FileTimeToUnixTime(data.ftLastAccessTime, time);
204 break;
205 default:
206 return false;
207 }
208 return true;
209}
210
211bool Win32Filesystem::GetAppPathname(Pathname* path) {
212 TCHAR buffer[MAX_PATH + 1];
tfarina5237aaf2015-11-10 23:44:30 -0800213 if (0 == ::GetModuleFileName(NULL, buffer, arraysize(buffer)))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000214 return false;
215 path->SetPathname(ToUtf8(buffer));
216 return true;
217}
218
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000219bool Win32Filesystem::GetAppTempFolder(Pathname* path) {
220 if (!GetAppPathname(path))
221 return false;
222 std::string filename(path->filename());
223 return GetTemporaryFolder(*path, true, &filename);
224}
225
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000226} // namespace rtc