blob: ac49c640752a52a3b7a0c632fda97ec611723291 [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"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021#include "webrtc/base/fileutils.h"
22#include "webrtc/base/pathutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023#include "webrtc/base/stream.h"
24#include "webrtc/base/stringutils.h"
25
26// In several places in this file, we test the integrity level of the process
27// before calling GetLongPathName. We do this because calling GetLongPathName
28// when running under protected mode IE (a low integrity process) can result in
29// a virtualized path being returned, which is wrong if you only plan to read.
30// TODO: Waiting to hear back from IE team on whether this is the
31// best approach; IEIsProtectedModeProcess is another possible solution.
32
33namespace rtc {
34
35bool Win32Filesystem::CreateFolder(const Pathname &pathname) {
36 if (pathname.pathname().empty() || !pathname.filename().empty())
37 return false;
38
39 std::wstring path16;
40 if (!Utf8ToWindowsFilename(pathname.pathname(), &path16))
41 return false;
42
43 DWORD res = ::GetFileAttributes(path16.c_str());
44 if (res != INVALID_FILE_ATTRIBUTES) {
45 // Something exists at this location, check if it is a directory
46 return ((res & FILE_ATTRIBUTE_DIRECTORY) != 0);
47 } else if ((GetLastError() != ERROR_FILE_NOT_FOUND)
48 && (GetLastError() != ERROR_PATH_NOT_FOUND)) {
49 // Unexpected error
50 return false;
51 }
52
53 // Directory doesn't exist, look up one directory level
54 if (!pathname.parent_folder().empty()) {
55 Pathname parent(pathname);
56 parent.SetFolder(pathname.parent_folder());
57 if (!CreateFolder(parent)) {
58 return false;
59 }
60 }
61
62 return (::CreateDirectory(path16.c_str(), NULL) != 0);
63}
64
65FileStream *Win32Filesystem::OpenFile(const Pathname &filename,
66 const std::string &mode) {
67 FileStream *fs = new FileStream();
68 if (fs && !fs->Open(filename.pathname().c_str(), mode.c_str(), NULL)) {
69 delete fs;
70 fs = NULL;
71 }
72 return fs;
73}
74
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075bool Win32Filesystem::DeleteFile(const Pathname &filename) {
76 LOG(LS_INFO) << "Deleting file " << filename.pathname();
77 if (!IsFile(filename)) {
78 ASSERT(IsFile(filename));
79 return false;
80 }
81 return ::DeleteFile(ToUtf16(filename.pathname()).c_str()) != 0;
82}
83
84bool Win32Filesystem::DeleteEmptyFolder(const Pathname &folder) {
85 LOG(LS_INFO) << "Deleting folder " << folder.pathname();
86
87 std::string no_slash(folder.pathname(), 0, folder.pathname().length()-1);
88 return ::RemoveDirectory(ToUtf16(no_slash).c_str()) != 0;
89}
90
91bool Win32Filesystem::GetTemporaryFolder(Pathname &pathname, bool create,
92 const std::string *append) {
93 wchar_t buffer[MAX_PATH + 1];
tfarina5237aaf2015-11-10 23:44:30 -080094 if (!::GetTempPath(arraysize(buffer), buffer))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095 return false;
96 if (!IsCurrentProcessLowIntegrity() &&
tfarina5237aaf2015-11-10 23:44:30 -080097 !::GetLongPathName(buffer, buffer, arraysize(buffer)))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098 return false;
99 size_t len = strlen(buffer);
100 if ((len > 0) && (buffer[len-1] != '\\')) {
tfarina5237aaf2015-11-10 23:44:30 -0800101 len += strcpyn(buffer + len, arraysize(buffer) - len, L"\\");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102 }
tfarina5237aaf2015-11-10 23:44:30 -0800103 if (len >= arraysize(buffer) - 1)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104 return false;
105 pathname.clear();
106 pathname.SetFolder(ToUtf8(buffer));
107 if (append != NULL) {
108 ASSERT(!append->empty());
109 pathname.AppendFolder(*append);
110 }
111 return !create || CreateFolder(pathname);
112}
113
114std::string Win32Filesystem::TempFilename(const Pathname &dir,
115 const std::string &prefix) {
116 wchar_t filename[MAX_PATH];
117 if (::GetTempFileName(ToUtf16(dir.pathname()).c_str(),
118 ToUtf16(prefix).c_str(), 0, filename) != 0)
119 return ToUtf8(filename);
120 ASSERT(false);
121 return "";
122}
123
124bool Win32Filesystem::MoveFile(const Pathname &old_path,
125 const Pathname &new_path) {
126 if (!IsFile(old_path)) {
127 ASSERT(IsFile(old_path));
128 return false;
129 }
130 LOG(LS_INFO) << "Moving " << old_path.pathname()
131 << " to " << new_path.pathname();
132 return ::MoveFile(ToUtf16(old_path.pathname()).c_str(),
133 ToUtf16(new_path.pathname()).c_str()) != 0;
134}
135
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136bool Win32Filesystem::IsFolder(const Pathname &path) {
137 WIN32_FILE_ATTRIBUTE_DATA data = {0};
138 if (0 == ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(),
139 GetFileExInfoStandard, &data))
140 return false;
141 return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ==
142 FILE_ATTRIBUTE_DIRECTORY;
143}
144
145bool Win32Filesystem::IsFile(const Pathname &path) {
146 WIN32_FILE_ATTRIBUTE_DATA data = {0};
147 if (0 == ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(),
148 GetFileExInfoStandard, &data))
149 return false;
150 return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
151}
152
153bool Win32Filesystem::IsAbsent(const Pathname& path) {
154 WIN32_FILE_ATTRIBUTE_DATA data = {0};
155 if (0 != ::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(),
156 GetFileExInfoStandard, &data))
157 return false;
158 DWORD err = ::GetLastError();
159 return (ERROR_FILE_NOT_FOUND == err || ERROR_PATH_NOT_FOUND == err);
160}
161
162bool Win32Filesystem::CopyFile(const Pathname &old_path,
163 const Pathname &new_path) {
164 return ::CopyFile(ToUtf16(old_path.pathname()).c_str(),
165 ToUtf16(new_path.pathname()).c_str(), TRUE) != 0;
166}
167
168bool Win32Filesystem::IsTemporaryPath(const Pathname& pathname) {
169 TCHAR buffer[MAX_PATH + 1];
tfarina5237aaf2015-11-10 23:44:30 -0800170 if (!::GetTempPath(arraysize(buffer), buffer))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171 return false;
172 if (!IsCurrentProcessLowIntegrity() &&
tfarina5237aaf2015-11-10 23:44:30 -0800173 !::GetLongPathName(buffer, buffer, arraysize(buffer)))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000174 return false;
175 return (::strnicmp(ToUtf16(pathname.pathname()).c_str(),
176 buffer, strlen(buffer)) == 0);
177}
178
179bool Win32Filesystem::GetFileSize(const Pathname &pathname, size_t *size) {
180 WIN32_FILE_ATTRIBUTE_DATA data = {0};
181 if (::GetFileAttributesEx(ToUtf16(pathname.pathname()).c_str(),
182 GetFileExInfoStandard, &data) == 0)
183 return false;
184 *size = data.nFileSizeLow;
185 return true;
186}
187
188bool Win32Filesystem::GetFileTime(const Pathname& path, FileTimeType which,
189 time_t* time) {
190 WIN32_FILE_ATTRIBUTE_DATA data = {0};
191 if (::GetFileAttributesEx(ToUtf16(path.pathname()).c_str(),
192 GetFileExInfoStandard, &data) == 0)
193 return false;
194 switch (which) {
195 case FTT_CREATED:
196 FileTimeToUnixTime(data.ftCreationTime, time);
197 break;
198 case FTT_MODIFIED:
199 FileTimeToUnixTime(data.ftLastWriteTime, time);
200 break;
201 case FTT_ACCESSED:
202 FileTimeToUnixTime(data.ftLastAccessTime, time);
203 break;
204 default:
205 return false;
206 }
207 return true;
208}
209
210bool Win32Filesystem::GetAppPathname(Pathname* path) {
211 TCHAR buffer[MAX_PATH + 1];
tfarina5237aaf2015-11-10 23:44:30 -0800212 if (0 == ::GetModuleFileName(NULL, buffer, arraysize(buffer)))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000213 return false;
214 path->SetPathname(ToUtf8(buffer));
215 return true;
216}
217
218bool Win32Filesystem::GetAppDataFolder(Pathname* path, bool per_user) {
219 ASSERT(!organization_name_.empty());
220 ASSERT(!application_name_.empty());
221 TCHAR buffer[MAX_PATH + 1];
222 int csidl = per_user ? CSIDL_LOCAL_APPDATA : CSIDL_COMMON_APPDATA;
223 if (!::SHGetSpecialFolderPath(NULL, buffer, csidl, TRUE))
224 return false;
225 if (!IsCurrentProcessLowIntegrity() &&
tfarina5237aaf2015-11-10 23:44:30 -0800226 !::GetLongPathName(buffer, buffer, arraysize(buffer)))
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000227 return false;
tfarina5237aaf2015-11-10 23:44:30 -0800228 size_t len = strcatn(buffer, arraysize(buffer), __T("\\"));
229 len += strcpyn(buffer + len, arraysize(buffer) - len,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000230 ToUtf16(organization_name_).c_str());
231 if ((len > 0) && (buffer[len-1] != __T('\\'))) {
tfarina5237aaf2015-11-10 23:44:30 -0800232 len += strcpyn(buffer + len, arraysize(buffer) - len, __T("\\"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000233 }
tfarina5237aaf2015-11-10 23:44:30 -0800234 len += strcpyn(buffer + len, arraysize(buffer) - len,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000235 ToUtf16(application_name_).c_str());
236 if ((len > 0) && (buffer[len-1] != __T('\\'))) {
tfarina5237aaf2015-11-10 23:44:30 -0800237 len += strcpyn(buffer + len, arraysize(buffer) - len, __T("\\"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000238 }
tfarina5237aaf2015-11-10 23:44:30 -0800239 if (len >= arraysize(buffer) - 1)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240 return false;
241 path->clear();
242 path->SetFolder(ToUtf8(buffer));
243 return CreateFolder(*path);
244}
245
246bool Win32Filesystem::GetAppTempFolder(Pathname* path) {
247 if (!GetAppPathname(path))
248 return false;
249 std::string filename(path->filename());
250 return GetTemporaryFolder(*path, true, &filename);
251}
252
jlmiller@webrtc.orgea1c8422015-01-22 17:44:19 +0000253bool Win32Filesystem::GetDiskFreeSpace(const Pathname& path,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200254 int64_t* free_bytes) {
jlmiller@webrtc.orgea1c8422015-01-22 17:44:19 +0000255 if (!free_bytes) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000256 return false;
257 }
258 char drive[4];
259 std::wstring drive16;
260 const wchar_t* target_drive = NULL;
261 if (path.GetDrive(drive, sizeof(drive))) {
262 drive16 = ToUtf16(drive);
263 target_drive = drive16.c_str();
264 } else if (path.folder().substr(0, 2) == "\\\\") {
265 // UNC path, fail.
266 // TODO: Handle UNC paths.
267 return false;
268 } else {
269 // The path is probably relative. GetDriveType and GetDiskFreeSpaceEx
270 // use the current drive if NULL is passed as the drive name.
271 // TODO: Add method to Pathname to determine if the path is relative.
272 // TODO: Add method to Pathname to convert a path to absolute.
273 }
jlmiller@webrtc.orgea1c8422015-01-22 17:44:19 +0000274 UINT drive_type = ::GetDriveType(target_drive);
275 if ((drive_type == DRIVE_REMOTE) || (drive_type == DRIVE_UNKNOWN)) {
276 LOG(LS_VERBOSE) << "Remote or unknown drive: " << drive;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000277 return false;
278 }
279
Peter Boström0c4e06b2015-10-07 12:23:21 +0200280 int64_t total_number_of_bytes; // receives the number of bytes on disk
281 int64_t total_number_of_free_bytes; // receives the free bytes on disk
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000282 // make sure things won't change in 64 bit machine
283 // TODO replace with compile time assert
Peter Boström0c4e06b2015-10-07 12:23:21 +0200284 ASSERT(sizeof(ULARGE_INTEGER) == sizeof(uint64_t)); // NOLINT
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 if (::GetDiskFreeSpaceEx(target_drive,
jlmiller@webrtc.orgea1c8422015-01-22 17:44:19 +0000286 (PULARGE_INTEGER)free_bytes,
287 (PULARGE_INTEGER)&total_number_of_bytes,
288 (PULARGE_INTEGER)&total_number_of_free_bytes)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000289 return true;
290 } else {
jlmiller@webrtc.orgea1c8422015-01-22 17:44:19 +0000291 LOG(LS_VERBOSE) << "GetDiskFreeSpaceEx returns error.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000292 return false;
293 }
294}
295
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000296} // namespace rtc