1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/file_util_win.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,375 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "base/file_util.h" 1.9 + 1.10 +#include <windows.h> 1.11 +#include <shellapi.h> 1.12 +#include <shlobj.h> 1.13 +#include <time.h> 1.14 +#include <string> 1.15 + 1.16 +#include "base/file_path.h" 1.17 +#include "base/logging.h" 1.18 +#include "base/scoped_handle.h" 1.19 +#include "base/string_util.h" 1.20 +#include "base/time.h" 1.21 +#include "base/win_util.h" 1.22 + 1.23 +namespace file_util { 1.24 + 1.25 +bool AbsolutePath(FilePath* path) { 1.26 + wchar_t file_path_buf[MAX_PATH]; 1.27 + if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) 1.28 + return false; 1.29 + *path = FilePath(file_path_buf); 1.30 + return true; 1.31 +} 1.32 + 1.33 +bool Delete(const FilePath& path, bool recursive) { 1.34 + if (path.value().length() >= MAX_PATH) 1.35 + return false; 1.36 + 1.37 + // If we're not recursing use DeleteFile; it should be faster. DeleteFile 1.38 + // fails if passed a directory though, which is why we fall through on 1.39 + // failure to the SHFileOperation. 1.40 + if (!recursive && DeleteFile(path.value().c_str()) != 0) 1.41 + return true; 1.42 + 1.43 + // SHFILEOPSTRUCT wants the path to be terminated with two NULLs, 1.44 + // so we have to use wcscpy because wcscpy_s writes non-NULLs 1.45 + // into the rest of the buffer. 1.46 + wchar_t double_terminated_path[MAX_PATH + 1] = {0}; 1.47 +#pragma warning(suppress:4996) // don't complain about wcscpy deprecation 1.48 + wcscpy(double_terminated_path, path.value().c_str()); 1.49 + 1.50 + SHFILEOPSTRUCT file_operation = {0}; 1.51 + file_operation.wFunc = FO_DELETE; 1.52 + file_operation.pFrom = double_terminated_path; 1.53 + file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION; 1.54 + if (!recursive) 1.55 + file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; 1.56 + int err = SHFileOperation(&file_operation); 1.57 + // Some versions of Windows return ERROR_FILE_NOT_FOUND when 1.58 + // deleting an empty directory. 1.59 + return (err == 0 || err == ERROR_FILE_NOT_FOUND); 1.60 +} 1.61 + 1.62 +bool CopyFile(const FilePath& from_path, const FilePath& to_path) { 1.63 + // NOTE: I suspect we could support longer paths, but that would involve 1.64 + // analyzing all our usage of files. 1.65 + if (from_path.value().length() >= MAX_PATH || 1.66 + to_path.value().length() >= MAX_PATH) { 1.67 + return false; 1.68 + } 1.69 + return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(), 1.70 + false) != 0); 1.71 +} 1.72 + 1.73 +bool ShellCopy(const FilePath& from_path, const FilePath& to_path, 1.74 + bool recursive) { 1.75 + // NOTE: I suspect we could support longer paths, but that would involve 1.76 + // analyzing all our usage of files. 1.77 + if (from_path.value().length() >= MAX_PATH || 1.78 + to_path.value().length() >= MAX_PATH) { 1.79 + return false; 1.80 + } 1.81 + 1.82 + // SHFILEOPSTRUCT wants the path to be terminated with two NULLs, 1.83 + // so we have to use wcscpy because wcscpy_s writes non-NULLs 1.84 + // into the rest of the buffer. 1.85 + wchar_t double_terminated_path_from[MAX_PATH + 1] = {0}; 1.86 + wchar_t double_terminated_path_to[MAX_PATH + 1] = {0}; 1.87 +#pragma warning(suppress:4996) // don't complain about wcscpy deprecation 1.88 + wcscpy(double_terminated_path_from, from_path.value().c_str()); 1.89 +#pragma warning(suppress:4996) // don't complain about wcscpy deprecation 1.90 + wcscpy(double_terminated_path_to, to_path.value().c_str()); 1.91 + 1.92 + SHFILEOPSTRUCT file_operation = {0}; 1.93 + file_operation.wFunc = FO_COPY; 1.94 + file_operation.pFrom = double_terminated_path_from; 1.95 + file_operation.pTo = double_terminated_path_to; 1.96 + file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | 1.97 + FOF_NOCONFIRMMKDIR; 1.98 + if (!recursive) 1.99 + file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; 1.100 + 1.101 + return (SHFileOperation(&file_operation) == 0); 1.102 +} 1.103 + 1.104 +bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, 1.105 + bool recursive) { 1.106 + if (recursive) 1.107 + return ShellCopy(from_path, to_path, true); 1.108 + 1.109 + // Instead of creating a new directory, we copy the old one to include the 1.110 + // security information of the folder as part of the copy. 1.111 + if (!PathExists(to_path)) { 1.112 + // Except that Vista fails to do that, and instead do a recursive copy if 1.113 + // the target directory doesn't exist. 1.114 + if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) 1.115 + CreateDirectory(to_path); 1.116 + else 1.117 + ShellCopy(from_path, to_path, false); 1.118 + } 1.119 + 1.120 + FilePath directory = from_path.Append(L"*.*"); 1.121 + return ShellCopy(directory, to_path, false); 1.122 +} 1.123 + 1.124 +bool PathExists(const FilePath& path) { 1.125 + return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); 1.126 +} 1.127 + 1.128 +bool PathIsWritable(const FilePath& path) { 1.129 + HANDLE dir = 1.130 + CreateFile(path.value().c_str(), FILE_ADD_FILE, 1.131 + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 1.132 + NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 1.133 + 1.134 + if (dir == INVALID_HANDLE_VALUE) 1.135 + return false; 1.136 + 1.137 + CloseHandle(dir); 1.138 + return true; 1.139 +} 1.140 + 1.141 +bool DirectoryExists(const FilePath& path) { 1.142 + DWORD fileattr = GetFileAttributes(path.value().c_str()); 1.143 + if (fileattr != INVALID_FILE_ATTRIBUTES) 1.144 + return (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0; 1.145 + return false; 1.146 +} 1.147 + 1.148 +bool GetTempDir(FilePath* path) { 1.149 + wchar_t temp_path[MAX_PATH + 1]; 1.150 + DWORD path_len = ::GetTempPath(MAX_PATH, temp_path); 1.151 + if (path_len >= MAX_PATH || path_len <= 0) 1.152 + return false; 1.153 + // TODO(evanm): the old behavior of this function was to always strip the 1.154 + // trailing slash. We duplicate this here, but it shouldn't be necessary 1.155 + // when everyone is using the appropriate FilePath APIs. 1.156 + std::wstring path_str(temp_path); 1.157 + TrimTrailingSeparator(&path_str); 1.158 + *path = FilePath(path_str); 1.159 + return true; 1.160 +} 1.161 + 1.162 +bool GetShmemTempDir(FilePath* path) { 1.163 + return GetTempDir(path); 1.164 +} 1.165 + 1.166 +bool CreateTemporaryFileName(FilePath* path) { 1.167 + std::wstring temp_path, temp_file; 1.168 + 1.169 + if (!GetTempDir(&temp_path)) 1.170 + return false; 1.171 + 1.172 + if (CreateTemporaryFileNameInDir(temp_path, &temp_file)) { 1.173 + *path = FilePath(temp_file); 1.174 + return true; 1.175 + } 1.176 + 1.177 + return false; 1.178 +} 1.179 + 1.180 +FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { 1.181 + return CreateAndOpenTemporaryFile(path); 1.182 +} 1.183 + 1.184 +// On POSIX we have semantics to create and open a temporary file 1.185 +// atomically. 1.186 +// TODO(jrg): is there equivalent call to use on Windows instead of 1.187 +// going 2-step? 1.188 +FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { 1.189 + std::wstring wstring_path; 1.190 + if (!CreateTemporaryFileNameInDir(dir.value(), &wstring_path)) { 1.191 + return NULL; 1.192 + } 1.193 + *path = FilePath(wstring_path); 1.194 + // Open file in binary mode, to avoid problems with fwrite. On Windows 1.195 + // it replaces \n's with \r\n's, which may surprise you. 1.196 + // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx 1.197 + return OpenFile(*path, "wb+"); 1.198 +} 1.199 + 1.200 +bool CreateTemporaryFileNameInDir(const std::wstring& dir, 1.201 + std::wstring* temp_file) { 1.202 + wchar_t temp_name[MAX_PATH + 1]; 1.203 + 1.204 + if (!GetTempFileName(dir.c_str(), L"", 0, temp_name)) 1.205 + return false; // fail! 1.206 + 1.207 + DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); 1.208 + if (path_len > MAX_PATH + 1 || path_len == 0) 1.209 + return false; // fail! 1.210 + 1.211 + temp_file->assign(temp_name, path_len); 1.212 + return true; 1.213 +} 1.214 + 1.215 +bool CreateNewTempDirectory(const FilePath::StringType& prefix, 1.216 + FilePath* new_temp_path) { 1.217 + FilePath system_temp_dir; 1.218 + if (!GetTempDir(&system_temp_dir)) 1.219 + return false; 1.220 + 1.221 + FilePath path_to_create; 1.222 + srand(static_cast<uint32_t>(time(NULL))); 1.223 + 1.224 + int count = 0; 1.225 + while (count < 50) { 1.226 + // Try create a new temporary directory with random generated name. If 1.227 + // the one exists, keep trying another path name until we reach some limit. 1.228 + path_to_create = system_temp_dir; 1.229 + std::wstring new_dir_name; 1.230 + new_dir_name.assign(prefix); 1.231 + new_dir_name.append(IntToWString(rand() % kint16max)); 1.232 + path_to_create = path_to_create.Append(new_dir_name); 1.233 + 1.234 + if (::CreateDirectory(path_to_create.value().c_str(), NULL)) 1.235 + break; 1.236 + count++; 1.237 + } 1.238 + 1.239 + if (count == 50) { 1.240 + return false; 1.241 + } 1.242 + 1.243 + *new_temp_path = path_to_create; 1.244 + return true; 1.245 +} 1.246 + 1.247 +bool CreateDirectory(const FilePath& full_path) { 1.248 + if (DirectoryExists(full_path)) 1.249 + return true; 1.250 + int err = SHCreateDirectoryEx(NULL, full_path.value().c_str(), NULL); 1.251 + return err == ERROR_SUCCESS; 1.252 +} 1.253 + 1.254 +bool GetFileInfo(const FilePath& file_path, FileInfo* results) { 1.255 + WIN32_FILE_ATTRIBUTE_DATA attr; 1.256 + if (!GetFileAttributesEx(file_path.ToWStringHack().c_str(), 1.257 + GetFileExInfoStandard, &attr)) { 1.258 + return false; 1.259 + } 1.260 + 1.261 + ULARGE_INTEGER size; 1.262 + size.HighPart = attr.nFileSizeHigh; 1.263 + size.LowPart = attr.nFileSizeLow; 1.264 + results->size = size.QuadPart; 1.265 + 1.266 + results->is_directory = 1.267 + (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; 1.268 + return true; 1.269 +} 1.270 + 1.271 +FILE* OpenFile(const FilePath& filename, const char* mode) { 1.272 + std::wstring w_mode = ASCIIToWide(std::string(mode)); 1.273 + FILE* file; 1.274 + if (_wfopen_s(&file, filename.value().c_str(), w_mode.c_str()) != 0) { 1.275 + return NULL; 1.276 + } 1.277 + return file; 1.278 +} 1.279 + 1.280 +FILE* OpenFile(const std::string& filename, const char* mode) { 1.281 + FILE* file; 1.282 + if (fopen_s(&file, filename.c_str(), mode) != 0) { 1.283 + return NULL; 1.284 + } 1.285 + return file; 1.286 +} 1.287 + 1.288 +int ReadFile(const FilePath& filename, char* data, int size) { 1.289 + ScopedHandle file(CreateFile(filename.value().c_str(), 1.290 + GENERIC_READ, 1.291 + FILE_SHARE_READ | FILE_SHARE_WRITE, 1.292 + NULL, 1.293 + OPEN_EXISTING, 1.294 + FILE_FLAG_SEQUENTIAL_SCAN, 1.295 + NULL)); 1.296 + if (file == INVALID_HANDLE_VALUE) 1.297 + return -1; 1.298 + 1.299 + int ret_value; 1.300 + DWORD read; 1.301 + if (::ReadFile(file, data, size, &read, NULL) && read == size) { 1.302 + ret_value = static_cast<int>(read); 1.303 + } else { 1.304 + ret_value = -1; 1.305 + } 1.306 + 1.307 + return ret_value; 1.308 +} 1.309 + 1.310 +int WriteFile(const FilePath& filename, const char* data, int size) { 1.311 + ScopedHandle file(CreateFile(filename.value().c_str(), 1.312 + GENERIC_WRITE, 1.313 + 0, 1.314 + NULL, 1.315 + CREATE_ALWAYS, 1.316 + 0, 1.317 + NULL)); 1.318 + if (file == INVALID_HANDLE_VALUE) { 1.319 + CHROMIUM_LOG(WARNING) << "CreateFile failed for path " << filename.value() << 1.320 + " error code=" << GetLastError() << 1.321 + " error text=" << win_util::FormatLastWin32Error(); 1.322 + return -1; 1.323 + } 1.324 + 1.325 + DWORD written; 1.326 + BOOL result = ::WriteFile(file, data, size, &written, NULL); 1.327 + if (result && written == size) 1.328 + return static_cast<int>(written); 1.329 + 1.330 + if (!result) { 1.331 + // WriteFile failed. 1.332 + CHROMIUM_LOG(WARNING) << "writing file " << filename.value() << 1.333 + " failed, error code=" << GetLastError() << 1.334 + " description=" << win_util::FormatLastWin32Error(); 1.335 + } else { 1.336 + // Didn't write all the bytes. 1.337 + CHROMIUM_LOG(WARNING) << "wrote" << written << " bytes to " << 1.338 + filename.value() << " expected " << size; 1.339 + } 1.340 + return -1; 1.341 +} 1.342 + 1.343 +// Gets the current working directory for the process. 1.344 +bool GetCurrentDirectory(FilePath* dir) { 1.345 + wchar_t system_buffer[MAX_PATH]; 1.346 + system_buffer[0] = 0; 1.347 + DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer); 1.348 + if (len == 0 || len > MAX_PATH) 1.349 + return false; 1.350 + // TODO(evanm): the old behavior of this function was to always strip the 1.351 + // trailing slash. We duplicate this here, but it shouldn't be necessary 1.352 + // when everyone is using the appropriate FilePath APIs. 1.353 + std::wstring dir_str(system_buffer); 1.354 + file_util::TrimTrailingSeparator(&dir_str); 1.355 + *dir = FilePath(dir_str); 1.356 + return true; 1.357 +} 1.358 + 1.359 +// Sets the current working directory for the process. 1.360 +bool SetCurrentDirectory(const FilePath& directory) { 1.361 + BOOL ret = ::SetCurrentDirectory(directory.value().c_str()); 1.362 + return ret != 0; 1.363 +} 1.364 + 1.365 +// Deprecated functions ---------------------------------------------------- 1.366 + 1.367 +void InsertBeforeExtension(std::wstring* path_str, 1.368 + const std::wstring& suffix) { 1.369 + FilePath path(*path_str); 1.370 + InsertBeforeExtension(&path, suffix); 1.371 + path_str->assign(path.value()); 1.372 +} 1.373 +void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { 1.374 + FilePath path(*file_name); 1.375 + ReplaceExtension(&path, extension); 1.376 + file_name->assign(path.value()); 1.377 +} 1.378 +} // namespace file_util