blob: 7e5ec832d65080eeda9587d423f241541be2d20b [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
219bool Win32Filesystem::GetAppDataFolder(Pathname* path, bool per_user) {
nisseede5da42017-01-12 05:15:36 -0800220 RTC_DCHECK(!organization_name_.empty());
221 RTC_DCHECK(!application_name_.empty());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000222 TCHAR buffer[MAX_PATH + 1];
223 int csidl = per_user ? CSIDL_LOCAL_APPDATA : CSIDL_COMMON_APPDATA;
224 if (!::SHGetSpecialFolderPath(NULL, buffer, csidl, TRUE))
225 return false;
226 if (!IsCurrentProcessLowIntegrity() &&
tfarina5237aaf2015-11-10 23:44:30 -0800227 !::GetLongPathName(buffer, buffer, arraysize(buffer)))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000228 return false;
tfarina5237aaf2015-11-10 23:44:30 -0800229 size_t len = strcatn(buffer, arraysize(buffer), __T("\\"));
230 len += strcpyn(buffer + len, arraysize(buffer) - len,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000231 ToUtf16(organization_name_).c_str());
232 if ((len > 0) && (buffer[len-1] != __T('\\'))) {
tfarina5237aaf2015-11-10 23:44:30 -0800233 len += strcpyn(buffer + len, arraysize(buffer) - len, __T("\\"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000234 }
tfarina5237aaf2015-11-10 23:44:30 -0800235 len += strcpyn(buffer + len, arraysize(buffer) - len,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000236 ToUtf16(application_name_).c_str());
237 if ((len > 0) && (buffer[len-1] != __T('\\'))) {
tfarina5237aaf2015-11-10 23:44:30 -0800238 len += strcpyn(buffer + len, arraysize(buffer) - len, __T("\\"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 }
tfarina5237aaf2015-11-10 23:44:30 -0800240 if (len >= arraysize(buffer) - 1)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000241 return false;
242 path->clear();
243 path->SetFolder(ToUtf8(buffer));
244 return CreateFolder(*path);
245}
246
247bool Win32Filesystem::GetAppTempFolder(Pathname* path) {
248 if (!GetAppPathname(path))
249 return false;
250 std::string filename(path->filename());
251 return GetTemporaryFolder(*path, true, &filename);
252}
253
jlmiller@webrtc.orgea1c8422015-01-22 17:44:19 +0000254bool Win32Filesystem::GetDiskFreeSpace(const Pathname& path,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200255 int64_t* free_bytes) {
jlmiller@webrtc.orgea1c8422015-01-22 17:44:19 +0000256 if (!free_bytes) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000257 return false;
258 }
259 char drive[4];
260 std::wstring drive16;
261 const wchar_t* target_drive = NULL;
262 if (path.GetDrive(drive, sizeof(drive))) {
263 drive16 = ToUtf16(drive);
264 target_drive = drive16.c_str();
265 } else if (path.folder().substr(0, 2) == "\\\\") {
266 // UNC path, fail.
267 // TODO: Handle UNC paths.
268 return false;
269 } else {
270 // The path is probably relative. GetDriveType and GetDiskFreeSpaceEx
271 // use the current drive if NULL is passed as the drive name.
272 // TODO: Add method to Pathname to determine if the path is relative.
273 // TODO: Add method to Pathname to convert a path to absolute.
274 }
jlmiller@webrtc.orgea1c8422015-01-22 17:44:19 +0000275 UINT drive_type = ::GetDriveType(target_drive);
276 if ((drive_type == DRIVE_REMOTE) || (drive_type == DRIVE_UNKNOWN)) {
277 LOG(LS_VERBOSE) << "Remote or unknown drive: " << drive;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000278 return false;
279 }
280
Peter Boström0c4e06b2015-10-07 12:23:21 +0200281 int64_t total_number_of_bytes; // receives the number of bytes on disk
282 int64_t total_number_of_free_bytes; // receives the free bytes on disk
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283 // make sure things won't change in 64 bit machine
284 // TODO replace with compile time assert
nisseede5da42017-01-12 05:15:36 -0800285 RTC_DCHECK(sizeof(ULARGE_INTEGER) == sizeof(uint64_t)); // NOLINT
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000286 if (::GetDiskFreeSpaceEx(target_drive,
jlmiller@webrtc.orgea1c8422015-01-22 17:44:19 +0000287 (PULARGE_INTEGER)free_bytes,
288 (PULARGE_INTEGER)&total_number_of_bytes,
289 (PULARGE_INTEGER)&total_number_of_free_bytes)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000290 return true;
291 } else {
jlmiller@webrtc.orgea1c8422015-01-22 17:44:19 +0000292 LOG(LS_VERBOSE) << "GetDiskFreeSpaceEx returns error.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000293 return false;
294 }
295}
296
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297} // namespace rtc