ipc/chromium/src/base/file_util_win.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "base/file_util.h"
michael@0 6
michael@0 7 #include <windows.h>
michael@0 8 #include <shellapi.h>
michael@0 9 #include <shlobj.h>
michael@0 10 #include <time.h>
michael@0 11 #include <string>
michael@0 12
michael@0 13 #include "base/file_path.h"
michael@0 14 #include "base/logging.h"
michael@0 15 #include "base/scoped_handle.h"
michael@0 16 #include "base/string_util.h"
michael@0 17 #include "base/time.h"
michael@0 18 #include "base/win_util.h"
michael@0 19
michael@0 20 namespace file_util {
michael@0 21
michael@0 22 bool AbsolutePath(FilePath* path) {
michael@0 23 wchar_t file_path_buf[MAX_PATH];
michael@0 24 if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH))
michael@0 25 return false;
michael@0 26 *path = FilePath(file_path_buf);
michael@0 27 return true;
michael@0 28 }
michael@0 29
michael@0 30 bool Delete(const FilePath& path, bool recursive) {
michael@0 31 if (path.value().length() >= MAX_PATH)
michael@0 32 return false;
michael@0 33
michael@0 34 // If we're not recursing use DeleteFile; it should be faster. DeleteFile
michael@0 35 // fails if passed a directory though, which is why we fall through on
michael@0 36 // failure to the SHFileOperation.
michael@0 37 if (!recursive && DeleteFile(path.value().c_str()) != 0)
michael@0 38 return true;
michael@0 39
michael@0 40 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs,
michael@0 41 // so we have to use wcscpy because wcscpy_s writes non-NULLs
michael@0 42 // into the rest of the buffer.
michael@0 43 wchar_t double_terminated_path[MAX_PATH + 1] = {0};
michael@0 44 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation
michael@0 45 wcscpy(double_terminated_path, path.value().c_str());
michael@0 46
michael@0 47 SHFILEOPSTRUCT file_operation = {0};
michael@0 48 file_operation.wFunc = FO_DELETE;
michael@0 49 file_operation.pFrom = double_terminated_path;
michael@0 50 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION;
michael@0 51 if (!recursive)
michael@0 52 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY;
michael@0 53 int err = SHFileOperation(&file_operation);
michael@0 54 // Some versions of Windows return ERROR_FILE_NOT_FOUND when
michael@0 55 // deleting an empty directory.
michael@0 56 return (err == 0 || err == ERROR_FILE_NOT_FOUND);
michael@0 57 }
michael@0 58
michael@0 59 bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
michael@0 60 // NOTE: I suspect we could support longer paths, but that would involve
michael@0 61 // analyzing all our usage of files.
michael@0 62 if (from_path.value().length() >= MAX_PATH ||
michael@0 63 to_path.value().length() >= MAX_PATH) {
michael@0 64 return false;
michael@0 65 }
michael@0 66 return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(),
michael@0 67 false) != 0);
michael@0 68 }
michael@0 69
michael@0 70 bool ShellCopy(const FilePath& from_path, const FilePath& to_path,
michael@0 71 bool recursive) {
michael@0 72 // NOTE: I suspect we could support longer paths, but that would involve
michael@0 73 // analyzing all our usage of files.
michael@0 74 if (from_path.value().length() >= MAX_PATH ||
michael@0 75 to_path.value().length() >= MAX_PATH) {
michael@0 76 return false;
michael@0 77 }
michael@0 78
michael@0 79 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs,
michael@0 80 // so we have to use wcscpy because wcscpy_s writes non-NULLs
michael@0 81 // into the rest of the buffer.
michael@0 82 wchar_t double_terminated_path_from[MAX_PATH + 1] = {0};
michael@0 83 wchar_t double_terminated_path_to[MAX_PATH + 1] = {0};
michael@0 84 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation
michael@0 85 wcscpy(double_terminated_path_from, from_path.value().c_str());
michael@0 86 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation
michael@0 87 wcscpy(double_terminated_path_to, to_path.value().c_str());
michael@0 88
michael@0 89 SHFILEOPSTRUCT file_operation = {0};
michael@0 90 file_operation.wFunc = FO_COPY;
michael@0 91 file_operation.pFrom = double_terminated_path_from;
michael@0 92 file_operation.pTo = double_terminated_path_to;
michael@0 93 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION |
michael@0 94 FOF_NOCONFIRMMKDIR;
michael@0 95 if (!recursive)
michael@0 96 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY;
michael@0 97
michael@0 98 return (SHFileOperation(&file_operation) == 0);
michael@0 99 }
michael@0 100
michael@0 101 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path,
michael@0 102 bool recursive) {
michael@0 103 if (recursive)
michael@0 104 return ShellCopy(from_path, to_path, true);
michael@0 105
michael@0 106 // Instead of creating a new directory, we copy the old one to include the
michael@0 107 // security information of the folder as part of the copy.
michael@0 108 if (!PathExists(to_path)) {
michael@0 109 // Except that Vista fails to do that, and instead do a recursive copy if
michael@0 110 // the target directory doesn't exist.
michael@0 111 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA)
michael@0 112 CreateDirectory(to_path);
michael@0 113 else
michael@0 114 ShellCopy(from_path, to_path, false);
michael@0 115 }
michael@0 116
michael@0 117 FilePath directory = from_path.Append(L"*.*");
michael@0 118 return ShellCopy(directory, to_path, false);
michael@0 119 }
michael@0 120
michael@0 121 bool PathExists(const FilePath& path) {
michael@0 122 return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES);
michael@0 123 }
michael@0 124
michael@0 125 bool PathIsWritable(const FilePath& path) {
michael@0 126 HANDLE dir =
michael@0 127 CreateFile(path.value().c_str(), FILE_ADD_FILE,
michael@0 128 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
michael@0 129 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
michael@0 130
michael@0 131 if (dir == INVALID_HANDLE_VALUE)
michael@0 132 return false;
michael@0 133
michael@0 134 CloseHandle(dir);
michael@0 135 return true;
michael@0 136 }
michael@0 137
michael@0 138 bool DirectoryExists(const FilePath& path) {
michael@0 139 DWORD fileattr = GetFileAttributes(path.value().c_str());
michael@0 140 if (fileattr != INVALID_FILE_ATTRIBUTES)
michael@0 141 return (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0;
michael@0 142 return false;
michael@0 143 }
michael@0 144
michael@0 145 bool GetTempDir(FilePath* path) {
michael@0 146 wchar_t temp_path[MAX_PATH + 1];
michael@0 147 DWORD path_len = ::GetTempPath(MAX_PATH, temp_path);
michael@0 148 if (path_len >= MAX_PATH || path_len <= 0)
michael@0 149 return false;
michael@0 150 // TODO(evanm): the old behavior of this function was to always strip the
michael@0 151 // trailing slash. We duplicate this here, but it shouldn't be necessary
michael@0 152 // when everyone is using the appropriate FilePath APIs.
michael@0 153 std::wstring path_str(temp_path);
michael@0 154 TrimTrailingSeparator(&path_str);
michael@0 155 *path = FilePath(path_str);
michael@0 156 return true;
michael@0 157 }
michael@0 158
michael@0 159 bool GetShmemTempDir(FilePath* path) {
michael@0 160 return GetTempDir(path);
michael@0 161 }
michael@0 162
michael@0 163 bool CreateTemporaryFileName(FilePath* path) {
michael@0 164 std::wstring temp_path, temp_file;
michael@0 165
michael@0 166 if (!GetTempDir(&temp_path))
michael@0 167 return false;
michael@0 168
michael@0 169 if (CreateTemporaryFileNameInDir(temp_path, &temp_file)) {
michael@0 170 *path = FilePath(temp_file);
michael@0 171 return true;
michael@0 172 }
michael@0 173
michael@0 174 return false;
michael@0 175 }
michael@0 176
michael@0 177 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) {
michael@0 178 return CreateAndOpenTemporaryFile(path);
michael@0 179 }
michael@0 180
michael@0 181 // On POSIX we have semantics to create and open a temporary file
michael@0 182 // atomically.
michael@0 183 // TODO(jrg): is there equivalent call to use on Windows instead of
michael@0 184 // going 2-step?
michael@0 185 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
michael@0 186 std::wstring wstring_path;
michael@0 187 if (!CreateTemporaryFileNameInDir(dir.value(), &wstring_path)) {
michael@0 188 return NULL;
michael@0 189 }
michael@0 190 *path = FilePath(wstring_path);
michael@0 191 // Open file in binary mode, to avoid problems with fwrite. On Windows
michael@0 192 // it replaces \n's with \r\n's, which may surprise you.
michael@0 193 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx
michael@0 194 return OpenFile(*path, "wb+");
michael@0 195 }
michael@0 196
michael@0 197 bool CreateTemporaryFileNameInDir(const std::wstring& dir,
michael@0 198 std::wstring* temp_file) {
michael@0 199 wchar_t temp_name[MAX_PATH + 1];
michael@0 200
michael@0 201 if (!GetTempFileName(dir.c_str(), L"", 0, temp_name))
michael@0 202 return false; // fail!
michael@0 203
michael@0 204 DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH);
michael@0 205 if (path_len > MAX_PATH + 1 || path_len == 0)
michael@0 206 return false; // fail!
michael@0 207
michael@0 208 temp_file->assign(temp_name, path_len);
michael@0 209 return true;
michael@0 210 }
michael@0 211
michael@0 212 bool CreateNewTempDirectory(const FilePath::StringType& prefix,
michael@0 213 FilePath* new_temp_path) {
michael@0 214 FilePath system_temp_dir;
michael@0 215 if (!GetTempDir(&system_temp_dir))
michael@0 216 return false;
michael@0 217
michael@0 218 FilePath path_to_create;
michael@0 219 srand(static_cast<uint32_t>(time(NULL)));
michael@0 220
michael@0 221 int count = 0;
michael@0 222 while (count < 50) {
michael@0 223 // Try create a new temporary directory with random generated name. If
michael@0 224 // the one exists, keep trying another path name until we reach some limit.
michael@0 225 path_to_create = system_temp_dir;
michael@0 226 std::wstring new_dir_name;
michael@0 227 new_dir_name.assign(prefix);
michael@0 228 new_dir_name.append(IntToWString(rand() % kint16max));
michael@0 229 path_to_create = path_to_create.Append(new_dir_name);
michael@0 230
michael@0 231 if (::CreateDirectory(path_to_create.value().c_str(), NULL))
michael@0 232 break;
michael@0 233 count++;
michael@0 234 }
michael@0 235
michael@0 236 if (count == 50) {
michael@0 237 return false;
michael@0 238 }
michael@0 239
michael@0 240 *new_temp_path = path_to_create;
michael@0 241 return true;
michael@0 242 }
michael@0 243
michael@0 244 bool CreateDirectory(const FilePath& full_path) {
michael@0 245 if (DirectoryExists(full_path))
michael@0 246 return true;
michael@0 247 int err = SHCreateDirectoryEx(NULL, full_path.value().c_str(), NULL);
michael@0 248 return err == ERROR_SUCCESS;
michael@0 249 }
michael@0 250
michael@0 251 bool GetFileInfo(const FilePath& file_path, FileInfo* results) {
michael@0 252 WIN32_FILE_ATTRIBUTE_DATA attr;
michael@0 253 if (!GetFileAttributesEx(file_path.ToWStringHack().c_str(),
michael@0 254 GetFileExInfoStandard, &attr)) {
michael@0 255 return false;
michael@0 256 }
michael@0 257
michael@0 258 ULARGE_INTEGER size;
michael@0 259 size.HighPart = attr.nFileSizeHigh;
michael@0 260 size.LowPart = attr.nFileSizeLow;
michael@0 261 results->size = size.QuadPart;
michael@0 262
michael@0 263 results->is_directory =
michael@0 264 (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
michael@0 265 return true;
michael@0 266 }
michael@0 267
michael@0 268 FILE* OpenFile(const FilePath& filename, const char* mode) {
michael@0 269 std::wstring w_mode = ASCIIToWide(std::string(mode));
michael@0 270 FILE* file;
michael@0 271 if (_wfopen_s(&file, filename.value().c_str(), w_mode.c_str()) != 0) {
michael@0 272 return NULL;
michael@0 273 }
michael@0 274 return file;
michael@0 275 }
michael@0 276
michael@0 277 FILE* OpenFile(const std::string& filename, const char* mode) {
michael@0 278 FILE* file;
michael@0 279 if (fopen_s(&file, filename.c_str(), mode) != 0) {
michael@0 280 return NULL;
michael@0 281 }
michael@0 282 return file;
michael@0 283 }
michael@0 284
michael@0 285 int ReadFile(const FilePath& filename, char* data, int size) {
michael@0 286 ScopedHandle file(CreateFile(filename.value().c_str(),
michael@0 287 GENERIC_READ,
michael@0 288 FILE_SHARE_READ | FILE_SHARE_WRITE,
michael@0 289 NULL,
michael@0 290 OPEN_EXISTING,
michael@0 291 FILE_FLAG_SEQUENTIAL_SCAN,
michael@0 292 NULL));
michael@0 293 if (file == INVALID_HANDLE_VALUE)
michael@0 294 return -1;
michael@0 295
michael@0 296 int ret_value;
michael@0 297 DWORD read;
michael@0 298 if (::ReadFile(file, data, size, &read, NULL) && read == size) {
michael@0 299 ret_value = static_cast<int>(read);
michael@0 300 } else {
michael@0 301 ret_value = -1;
michael@0 302 }
michael@0 303
michael@0 304 return ret_value;
michael@0 305 }
michael@0 306
michael@0 307 int WriteFile(const FilePath& filename, const char* data, int size) {
michael@0 308 ScopedHandle file(CreateFile(filename.value().c_str(),
michael@0 309 GENERIC_WRITE,
michael@0 310 0,
michael@0 311 NULL,
michael@0 312 CREATE_ALWAYS,
michael@0 313 0,
michael@0 314 NULL));
michael@0 315 if (file == INVALID_HANDLE_VALUE) {
michael@0 316 CHROMIUM_LOG(WARNING) << "CreateFile failed for path " << filename.value() <<
michael@0 317 " error code=" << GetLastError() <<
michael@0 318 " error text=" << win_util::FormatLastWin32Error();
michael@0 319 return -1;
michael@0 320 }
michael@0 321
michael@0 322 DWORD written;
michael@0 323 BOOL result = ::WriteFile(file, data, size, &written, NULL);
michael@0 324 if (result && written == size)
michael@0 325 return static_cast<int>(written);
michael@0 326
michael@0 327 if (!result) {
michael@0 328 // WriteFile failed.
michael@0 329 CHROMIUM_LOG(WARNING) << "writing file " << filename.value() <<
michael@0 330 " failed, error code=" << GetLastError() <<
michael@0 331 " description=" << win_util::FormatLastWin32Error();
michael@0 332 } else {
michael@0 333 // Didn't write all the bytes.
michael@0 334 CHROMIUM_LOG(WARNING) << "wrote" << written << " bytes to " <<
michael@0 335 filename.value() << " expected " << size;
michael@0 336 }
michael@0 337 return -1;
michael@0 338 }
michael@0 339
michael@0 340 // Gets the current working directory for the process.
michael@0 341 bool GetCurrentDirectory(FilePath* dir) {
michael@0 342 wchar_t system_buffer[MAX_PATH];
michael@0 343 system_buffer[0] = 0;
michael@0 344 DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer);
michael@0 345 if (len == 0 || len > MAX_PATH)
michael@0 346 return false;
michael@0 347 // TODO(evanm): the old behavior of this function was to always strip the
michael@0 348 // trailing slash. We duplicate this here, but it shouldn't be necessary
michael@0 349 // when everyone is using the appropriate FilePath APIs.
michael@0 350 std::wstring dir_str(system_buffer);
michael@0 351 file_util::TrimTrailingSeparator(&dir_str);
michael@0 352 *dir = FilePath(dir_str);
michael@0 353 return true;
michael@0 354 }
michael@0 355
michael@0 356 // Sets the current working directory for the process.
michael@0 357 bool SetCurrentDirectory(const FilePath& directory) {
michael@0 358 BOOL ret = ::SetCurrentDirectory(directory.value().c_str());
michael@0 359 return ret != 0;
michael@0 360 }
michael@0 361
michael@0 362 // Deprecated functions ----------------------------------------------------
michael@0 363
michael@0 364 void InsertBeforeExtension(std::wstring* path_str,
michael@0 365 const std::wstring& suffix) {
michael@0 366 FilePath path(*path_str);
michael@0 367 InsertBeforeExtension(&path, suffix);
michael@0 368 path_str->assign(path.value());
michael@0 369 }
michael@0 370 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) {
michael@0 371 FilePath path(*file_name);
michael@0 372 ReplaceExtension(&path, extension);
michael@0 373 file_name->assign(path.value());
michael@0 374 }
michael@0 375 } // namespace file_util

mercurial