1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/file_util.h"
18 #include "base/files/file_enumerator.h"
19 #include "base/files/file_path.h"
20 #include "base/logging.h"
21 #include "base/metrics/histogram.h"
22 #include "base/process/process_handle.h"
23 #include "base/rand_util.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_util.h"
26 #include "base/strings/utf_string_conversions.h"
27 #include "base/threading/thread_restrictions.h"
28 #include "base/time/time.h"
29 #include "base/win/scoped_handle.h"
30 #include "base/win/windows_version.h"
36 const DWORD kFileShareAll
=
37 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
41 FilePath
MakeAbsoluteFilePath(const FilePath
& input
) {
42 ThreadRestrictions::AssertIOAllowed();
43 wchar_t file_path
[MAX_PATH
];
44 if (!_wfullpath(file_path
, input
.value().c_str(), MAX_PATH
))
46 return FilePath(file_path
);
49 bool DeleteFile(const FilePath
& path
, bool recursive
) {
50 ThreadRestrictions::AssertIOAllowed();
52 if (path
.value().length() >= MAX_PATH
)
55 // On XP SHFileOperation will return ERROR_ACCESS_DENIED instead of
56 // ERROR_FILE_NOT_FOUND, so just shortcut this here.
61 // If not recursing, then first check to see if |path| is a directory.
62 // If it is, then remove it with RemoveDirectory.
64 if (GetFileInfo(path
, &file_info
) && file_info
.is_directory
)
65 return RemoveDirectory(path
.value().c_str()) != 0;
67 // Otherwise, it's a file, wildcard or non-existant. Try DeleteFile first
68 // because it should be faster. If DeleteFile fails, then we fall through
69 // to SHFileOperation, which will do the right thing.
70 if (::DeleteFile(path
.value().c_str()) != 0)
74 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs,
75 // so we have to use wcscpy because wcscpy_s writes non-NULLs
76 // into the rest of the buffer.
77 wchar_t double_terminated_path
[MAX_PATH
+ 1] = {0};
78 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation
79 wcscpy(double_terminated_path
, path
.value().c_str());
81 SHFILEOPSTRUCT file_operation
= {0};
82 file_operation
.wFunc
= FO_DELETE
;
83 file_operation
.pFrom
= double_terminated_path
;
84 file_operation
.fFlags
= FOF_NOERRORUI
| FOF_SILENT
| FOF_NOCONFIRMATION
;
86 file_operation
.fFlags
|= FOF_NORECURSION
| FOF_FILESONLY
;
87 int err
= SHFileOperation(&file_operation
);
89 // Since we're passing flags to the operation telling it to be silent,
90 // it's possible for the operation to be aborted/cancelled without err
91 // being set (although MSDN doesn't give any scenarios for how this can
92 // happen). See MSDN for SHFileOperation and SHFILEOPTSTRUCT.
93 if (file_operation
.fAnyOperationsAborted
)
96 // Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting
97 // an empty directory and some return 0x402 when they should be returning
98 // ERROR_FILE_NOT_FOUND. MSDN says Vista and up won't return 0x402. Windows 7
99 // can return DE_INVALIDFILES (0x7C) for nonexistent directories.
100 return (err
== 0 || err
== ERROR_FILE_NOT_FOUND
|| err
== 0x402 ||
104 bool DeleteFileAfterReboot(const FilePath
& path
) {
105 ThreadRestrictions::AssertIOAllowed();
107 if (path
.value().length() >= MAX_PATH
)
110 return MoveFileEx(path
.value().c_str(), NULL
,
111 MOVEFILE_DELAY_UNTIL_REBOOT
|
112 MOVEFILE_REPLACE_EXISTING
) != FALSE
;
115 bool ReplaceFile(const FilePath
& from_path
,
116 const FilePath
& to_path
,
117 File::Error
* error
) {
118 ThreadRestrictions::AssertIOAllowed();
119 // Try a simple move first. It will only succeed when |to_path| doesn't
121 if (::MoveFile(from_path
.value().c_str(), to_path
.value().c_str()))
123 // Try the full-blown replace if the move fails, as ReplaceFile will only
124 // succeed when |to_path| does exist. When writing to a network share, we may
125 // not be able to change the ACLs. Ignore ACL errors then
126 // (REPLACEFILE_IGNORE_MERGE_ERRORS).
127 if (::ReplaceFile(to_path
.value().c_str(), from_path
.value().c_str(), NULL
,
128 REPLACEFILE_IGNORE_MERGE_ERRORS
, NULL
, NULL
)) {
132 *error
= File::OSErrorToFileError(GetLastError());
136 bool CopyDirectory(const FilePath
& from_path
, const FilePath
& to_path
,
138 // NOTE(maruel): Previous version of this function used to call
139 // SHFileOperation(). This used to copy the file attributes and extended
140 // attributes, OLE structured storage, NTFS file system alternate data
141 // streams, SECURITY_DESCRIPTOR. In practice, this is not what we want, we
142 // want the containing directory to propagate its SECURITY_DESCRIPTOR.
143 ThreadRestrictions::AssertIOAllowed();
145 // NOTE: I suspect we could support longer paths, but that would involve
146 // analyzing all our usage of files.
147 if (from_path
.value().length() >= MAX_PATH
||
148 to_path
.value().length() >= MAX_PATH
) {
152 // This function does not properly handle destinations within the source.
153 FilePath real_to_path
= to_path
;
154 if (PathExists(real_to_path
)) {
155 real_to_path
= MakeAbsoluteFilePath(real_to_path
);
156 if (real_to_path
.empty())
159 real_to_path
= MakeAbsoluteFilePath(real_to_path
.DirName());
160 if (real_to_path
.empty())
163 FilePath real_from_path
= MakeAbsoluteFilePath(from_path
);
164 if (real_from_path
.empty())
166 if (real_to_path
.value().size() >= real_from_path
.value().size() &&
167 real_to_path
.value().compare(0, real_from_path
.value().size(),
168 real_from_path
.value()) == 0) {
172 int traverse_type
= FileEnumerator::FILES
;
174 traverse_type
|= FileEnumerator::DIRECTORIES
;
175 FileEnumerator
traversal(from_path
, recursive
, traverse_type
);
177 if (!PathExists(from_path
)) {
178 DLOG(ERROR
) << "CopyDirectory() couldn't stat source directory: "
179 << from_path
.value().c_str();
182 // TODO(maruel): This is not necessary anymore.
183 DCHECK(recursive
|| DirectoryExists(from_path
));
185 FilePath current
= from_path
;
186 bool from_is_dir
= DirectoryExists(from_path
);
188 FilePath from_path_base
= from_path
;
189 if (recursive
&& DirectoryExists(to_path
)) {
190 // If the destination already exists and is a directory, then the
191 // top level of source needs to be copied.
192 from_path_base
= from_path
.DirName();
195 while (success
&& !current
.empty()) {
196 // current is the source path, including from_path, so append
197 // the suffix after from_path to to_path to create the target_path.
198 FilePath
target_path(to_path
);
199 if (from_path_base
!= current
) {
200 if (!from_path_base
.AppendRelativePath(current
, &target_path
)) {
207 if (!DirectoryExists(target_path
) &&
208 !::CreateDirectory(target_path
.value().c_str(), NULL
)) {
209 DLOG(ERROR
) << "CopyDirectory() couldn't create directory: "
210 << target_path
.value().c_str();
213 } else if (!internal::CopyFileUnsafe(current
, target_path
)) {
214 DLOG(ERROR
) << "CopyDirectory() couldn't create file: "
215 << target_path
.value().c_str();
219 current
= traversal
.Next();
220 if (!current
.empty())
221 from_is_dir
= traversal
.GetInfo().IsDirectory();
227 bool PathExists(const FilePath
& path
) {
228 ThreadRestrictions::AssertIOAllowed();
229 return (GetFileAttributes(path
.value().c_str()) != INVALID_FILE_ATTRIBUTES
);
232 bool PathIsWritable(const FilePath
& path
) {
233 ThreadRestrictions::AssertIOAllowed();
235 CreateFile(path
.value().c_str(), FILE_ADD_FILE
, kFileShareAll
,
236 NULL
, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
238 if (dir
== INVALID_HANDLE_VALUE
)
245 bool DirectoryExists(const FilePath
& path
) {
246 ThreadRestrictions::AssertIOAllowed();
247 DWORD fileattr
= GetFileAttributes(path
.value().c_str());
248 if (fileattr
!= INVALID_FILE_ATTRIBUTES
)
249 return (fileattr
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
253 bool GetTempDir(FilePath
* path
) {
254 wchar_t temp_path
[MAX_PATH
+ 1];
255 DWORD path_len
= ::GetTempPath(MAX_PATH
, temp_path
);
256 if (path_len
>= MAX_PATH
|| path_len
<= 0)
258 // TODO(evanm): the old behavior of this function was to always strip the
259 // trailing slash. We duplicate this here, but it shouldn't be necessary
260 // when everyone is using the appropriate FilePath APIs.
261 *path
= FilePath(temp_path
).StripTrailingSeparators();
265 FilePath
GetHomeDir() {
266 char16 result
[MAX_PATH
];
267 if (SUCCEEDED(SHGetFolderPath(NULL
, CSIDL_PROFILE
, NULL
, SHGFP_TYPE_CURRENT
,
270 return FilePath(result
);
273 // Fall back to the temporary directory on failure.
275 if (GetTempDir(&temp
))
279 return FilePath(L
"C:\\");
282 bool CreateTemporaryFile(FilePath
* path
) {
283 ThreadRestrictions::AssertIOAllowed();
287 if (!GetTempDir(path
))
290 if (CreateTemporaryFileInDir(*path
, &temp_file
)) {
298 // On POSIX we have semantics to create and open a temporary file
300 // TODO(jrg): is there equivalent call to use on Windows instead of
302 FILE* CreateAndOpenTemporaryFileInDir(const FilePath
& dir
, FilePath
* path
) {
303 ThreadRestrictions::AssertIOAllowed();
304 if (!CreateTemporaryFileInDir(dir
, path
)) {
307 // Open file in binary mode, to avoid problems with fwrite. On Windows
308 // it replaces \n's with \r\n's, which may surprise you.
309 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx
310 return OpenFile(*path
, "wb+");
313 bool CreateTemporaryFileInDir(const FilePath
& dir
, FilePath
* temp_file
) {
314 ThreadRestrictions::AssertIOAllowed();
316 wchar_t temp_name
[MAX_PATH
+ 1];
318 if (!GetTempFileName(dir
.value().c_str(), L
"", 0, temp_name
)) {
319 DPLOG(WARNING
) << "Failed to get temporary file name in "
320 << UTF16ToUTF8(dir
.value());
324 wchar_t long_temp_name
[MAX_PATH
+ 1];
325 DWORD long_name_len
= GetLongPathName(temp_name
, long_temp_name
, MAX_PATH
);
326 if (long_name_len
> MAX_PATH
|| long_name_len
== 0) {
327 // GetLongPathName() failed, but we still have a temporary file.
328 *temp_file
= FilePath(temp_name
);
332 FilePath::StringType long_temp_name_str
;
333 long_temp_name_str
.assign(long_temp_name
, long_name_len
);
334 *temp_file
= FilePath(long_temp_name_str
);
338 bool CreateTemporaryDirInDir(const FilePath
& base_dir
,
339 const FilePath::StringType
& prefix
,
341 ThreadRestrictions::AssertIOAllowed();
343 FilePath path_to_create
;
345 for (int count
= 0; count
< 50; ++count
) {
346 // Try create a new temporary directory with random generated name. If
347 // the one exists, keep trying another path name until we reach some limit.
348 string16 new_dir_name
;
349 new_dir_name
.assign(prefix
);
350 new_dir_name
.append(IntToString16(GetCurrentProcId()));
351 new_dir_name
.push_back('_');
352 new_dir_name
.append(IntToString16(RandInt(0, kint16max
)));
354 path_to_create
= base_dir
.Append(new_dir_name
);
355 if (::CreateDirectory(path_to_create
.value().c_str(), NULL
)) {
356 *new_dir
= path_to_create
;
364 bool CreateNewTempDirectory(const FilePath::StringType
& prefix
,
365 FilePath
* new_temp_path
) {
366 ThreadRestrictions::AssertIOAllowed();
368 FilePath system_temp_dir
;
369 if (!GetTempDir(&system_temp_dir
))
372 return CreateTemporaryDirInDir(system_temp_dir
, prefix
, new_temp_path
);
375 bool CreateDirectoryAndGetError(const FilePath
& full_path
,
376 File::Error
* error
) {
377 ThreadRestrictions::AssertIOAllowed();
379 // If the path exists, we've succeeded if it's a directory, failed otherwise.
380 const wchar_t* full_path_str
= full_path
.value().c_str();
381 DWORD fileattr
= ::GetFileAttributes(full_path_str
);
382 if (fileattr
!= INVALID_FILE_ATTRIBUTES
) {
383 if ((fileattr
& FILE_ATTRIBUTE_DIRECTORY
) != 0) {
384 DVLOG(1) << "CreateDirectory(" << full_path_str
<< "), "
385 << "directory already exists.";
388 DLOG(WARNING
) << "CreateDirectory(" << full_path_str
<< "), "
389 << "conflicts with existing file.";
391 *error
= File::FILE_ERROR_NOT_A_DIRECTORY
;
396 // Invariant: Path does not exist as file or directory.
398 // Attempt to create the parent recursively. This will immediately return
399 // true if it already exists, otherwise will create all required parent
400 // directories starting with the highest-level missing parent.
401 FilePath
parent_path(full_path
.DirName());
402 if (parent_path
.value() == full_path
.value()) {
404 *error
= File::FILE_ERROR_NOT_FOUND
;
408 if (!CreateDirectoryAndGetError(parent_path
, error
)) {
409 DLOG(WARNING
) << "Failed to create one of the parent directories.";
411 DCHECK(*error
!= File::FILE_OK
);
416 if (!::CreateDirectory(full_path_str
, NULL
)) {
417 DWORD error_code
= ::GetLastError();
418 if (error_code
== ERROR_ALREADY_EXISTS
&& DirectoryExists(full_path
)) {
419 // This error code ERROR_ALREADY_EXISTS doesn't indicate whether we
420 // were racing with someone creating the same directory, or a file
421 // with the same path. If DirectoryExists() returns true, we lost the
422 // race to create the same directory.
426 *error
= File::OSErrorToFileError(error_code
);
427 DLOG(WARNING
) << "Failed to create directory " << full_path_str
428 << ", last error is " << error_code
<< ".";
436 bool NormalizeFilePath(const FilePath
& path
, FilePath
* real_path
) {
437 ThreadRestrictions::AssertIOAllowed();
438 FilePath mapped_file
;
439 if (!NormalizeToNativeFilePath(path
, &mapped_file
))
441 // NormalizeToNativeFilePath() will return a path that starts with
442 // "\Device\Harddisk...". Helper DevicePathToDriveLetterPath()
443 // will find a drive letter which maps to the path's device, so
444 // that we return a path starting with a drive letter.
445 return DevicePathToDriveLetterPath(mapped_file
, real_path
);
448 bool DevicePathToDriveLetterPath(const FilePath
& nt_device_path
,
449 FilePath
* out_drive_letter_path
) {
450 ThreadRestrictions::AssertIOAllowed();
452 // Get the mapping of drive letters to device paths.
453 const int kDriveMappingSize
= 1024;
454 wchar_t drive_mapping
[kDriveMappingSize
] = {'\0'};
455 if (!::GetLogicalDriveStrings(kDriveMappingSize
- 1, drive_mapping
)) {
456 DLOG(ERROR
) << "Failed to get drive mapping.";
460 // The drive mapping is a sequence of null terminated strings.
461 // The last string is empty.
462 wchar_t* drive_map_ptr
= drive_mapping
;
463 wchar_t device_path_as_string
[MAX_PATH
];
464 wchar_t drive
[] = L
" :";
466 // For each string in the drive mapping, get the junction that links
467 // to it. If that junction is a prefix of |device_path|, then we
468 // know that |drive| is the real path prefix.
469 while (*drive_map_ptr
) {
470 drive
[0] = drive_map_ptr
[0]; // Copy the drive letter.
472 if (QueryDosDevice(drive
, device_path_as_string
, MAX_PATH
)) {
473 FilePath
device_path(device_path_as_string
);
474 if (device_path
== nt_device_path
||
475 device_path
.IsParent(nt_device_path
)) {
476 *out_drive_letter_path
= FilePath(drive
+
477 nt_device_path
.value().substr(wcslen(device_path_as_string
)));
481 // Move to the next drive letter string, which starts one
482 // increment after the '\0' that terminates the current string.
483 while (*drive_map_ptr
++);
486 // No drive matched. The path does not start with a device junction
487 // that is mounted as a drive letter. This means there is no drive
488 // letter path to the volume that holds |device_path|, so fail.
492 bool NormalizeToNativeFilePath(const FilePath
& path
, FilePath
* nt_path
) {
493 ThreadRestrictions::AssertIOAllowed();
494 // In Vista, GetFinalPathNameByHandle() would give us the real path
495 // from a file handle. If we ever deprecate XP, consider changing the
496 // code below to a call to GetFinalPathNameByHandle(). The method this
497 // function uses is explained in the following msdn article:
498 // http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx
499 base::win::ScopedHandle
file_handle(
500 ::CreateFile(path
.value().c_str(),
505 FILE_ATTRIBUTE_NORMAL
,
510 // Create a file mapping object. Can't easily use MemoryMappedFile, because
511 // we only map the first byte, and need direct access to the handle. You can
512 // not map an empty file, this call fails in that case.
513 base::win::ScopedHandle
file_map_handle(
514 ::CreateFileMapping(file_handle
.Get(),
518 1, // Just one byte. No need to look at the data.
520 if (!file_map_handle
)
523 // Use a view of the file to get the path to the file.
524 void* file_view
= MapViewOfFile(file_map_handle
.Get(),
525 FILE_MAP_READ
, 0, 0, 1);
529 // The expansion of |path| into a full path may make it longer.
530 // GetMappedFileName() will fail if the result is longer than MAX_PATH.
531 // Pad a bit to be safe. If kMaxPathLength is ever changed to be less
532 // than MAX_PATH, it would be nessisary to test that GetMappedFileName()
533 // not return kMaxPathLength. This would mean that only part of the
534 // path fit in |mapped_file_path|.
535 const int kMaxPathLength
= MAX_PATH
+ 10;
536 wchar_t mapped_file_path
[kMaxPathLength
];
537 bool success
= false;
538 HANDLE cp
= GetCurrentProcess();
539 if (::GetMappedFileNameW(cp
, file_view
, mapped_file_path
, kMaxPathLength
)) {
540 *nt_path
= FilePath(mapped_file_path
);
543 ::UnmapViewOfFile(file_view
);
547 // TODO(rkc): Work out if we want to handle NTFS junctions here or not, handle
548 // them if we do decide to.
549 bool IsLink(const FilePath
& file_path
) {
553 bool GetFileInfo(const FilePath
& file_path
, File::Info
* results
) {
554 ThreadRestrictions::AssertIOAllowed();
556 WIN32_FILE_ATTRIBUTE_DATA attr
;
557 if (!GetFileAttributesEx(file_path
.value().c_str(),
558 GetFileExInfoStandard
, &attr
)) {
563 size
.HighPart
= attr
.nFileSizeHigh
;
564 size
.LowPart
= attr
.nFileSizeLow
;
565 results
->size
= size
.QuadPart
;
567 results
->is_directory
=
568 (attr
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
569 results
->last_modified
= Time::FromFileTime(attr
.ftLastWriteTime
);
570 results
->last_accessed
= Time::FromFileTime(attr
.ftLastAccessTime
);
571 results
->creation_time
= Time::FromFileTime(attr
.ftCreationTime
);
576 FILE* OpenFile(const FilePath
& filename
, const char* mode
) {
577 ThreadRestrictions::AssertIOAllowed();
578 std::wstring w_mode
= ASCIIToWide(std::string(mode
));
579 return _wfsopen(filename
.value().c_str(), w_mode
.c_str(), _SH_DENYNO
);
582 FILE* FileToFILE(File file
, const char* mode
) {
586 _open_osfhandle(reinterpret_cast<intptr_t>(file
.GetPlatformFile()), 0);
589 file
.TakePlatformFile();
590 FILE* stream
= _fdopen(fd
, mode
);
596 int ReadFile(const FilePath
& filename
, char* data
, int max_size
) {
597 ThreadRestrictions::AssertIOAllowed();
598 base::win::ScopedHandle
file(CreateFile(filename
.value().c_str(),
600 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
603 FILE_FLAG_SEQUENTIAL_SCAN
,
609 if (::ReadFile(file
, data
, max_size
, &read
, NULL
))
615 int WriteFile(const FilePath
& filename
, const char* data
, int size
) {
616 ThreadRestrictions::AssertIOAllowed();
617 base::win::ScopedHandle
file(CreateFile(filename
.value().c_str(),
625 DPLOG(WARNING
) << "CreateFile failed for path "
626 << UTF16ToUTF8(filename
.value());
631 BOOL result
= ::WriteFile(file
, data
, size
, &written
, NULL
);
632 if (result
&& static_cast<int>(written
) == size
)
637 DPLOG(WARNING
) << "writing file " << UTF16ToUTF8(filename
.value())
640 // Didn't write all the bytes.
641 DLOG(WARNING
) << "wrote" << written
<< " bytes to "
642 << UTF16ToUTF8(filename
.value()) << " expected " << size
;
647 int AppendToFile(const FilePath
& filename
, const char* data
, int size
) {
648 ThreadRestrictions::AssertIOAllowed();
649 base::win::ScopedHandle
file(CreateFile(filename
.value().c_str(),
657 DPLOG(WARNING
) << "CreateFile failed for path "
658 << UTF16ToUTF8(filename
.value());
663 BOOL result
= ::WriteFile(file
, data
, size
, &written
, NULL
);
664 if (result
&& static_cast<int>(written
) == size
)
669 DPLOG(WARNING
) << "writing file " << UTF16ToUTF8(filename
.value())
672 // Didn't write all the bytes.
673 DLOG(WARNING
) << "wrote" << written
<< " bytes to "
674 << UTF16ToUTF8(filename
.value()) << " expected " << size
;
679 // Gets the current working directory for the process.
680 bool GetCurrentDirectory(FilePath
* dir
) {
681 ThreadRestrictions::AssertIOAllowed();
683 wchar_t system_buffer
[MAX_PATH
];
684 system_buffer
[0] = 0;
685 DWORD len
= ::GetCurrentDirectory(MAX_PATH
, system_buffer
);
686 if (len
== 0 || len
> MAX_PATH
)
688 // TODO(evanm): the old behavior of this function was to always strip the
689 // trailing slash. We duplicate this here, but it shouldn't be necessary
690 // when everyone is using the appropriate FilePath APIs.
691 std::wstring
dir_str(system_buffer
);
692 *dir
= FilePath(dir_str
).StripTrailingSeparators();
696 // Sets the current working directory for the process.
697 bool SetCurrentDirectory(const FilePath
& directory
) {
698 ThreadRestrictions::AssertIOAllowed();
699 BOOL ret
= ::SetCurrentDirectory(directory
.value().c_str());
703 int GetMaximumPathComponentLength(const FilePath
& path
) {
704 ThreadRestrictions::AssertIOAllowed();
706 wchar_t volume_path
[MAX_PATH
];
707 if (!GetVolumePathNameW(path
.NormalizePathSeparators().value().c_str(),
709 arraysize(volume_path
))) {
713 DWORD max_length
= 0;
714 if (!GetVolumeInformationW(volume_path
, NULL
, 0, NULL
, &max_length
, NULL
,
719 // Length of |path| with path separator appended.
720 size_t prefix
= path
.StripTrailingSeparators().value().size() + 1;
721 // The whole path string must be shorter than MAX_PATH. That is, it must be
722 // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1).
723 int whole_path_limit
= std::max(0, MAX_PATH
- 1 - static_cast<int>(prefix
));
724 return std::min(whole_path_limit
, static_cast<int>(max_length
));
727 // -----------------------------------------------------------------------------
731 bool MoveUnsafe(const FilePath
& from_path
, const FilePath
& to_path
) {
732 ThreadRestrictions::AssertIOAllowed();
734 // NOTE: I suspect we could support longer paths, but that would involve
735 // analyzing all our usage of files.
736 if (from_path
.value().length() >= MAX_PATH
||
737 to_path
.value().length() >= MAX_PATH
) {
740 if (MoveFileEx(from_path
.value().c_str(), to_path
.value().c_str(),
741 MOVEFILE_COPY_ALLOWED
| MOVEFILE_REPLACE_EXISTING
) != 0)
744 // Keep the last error value from MoveFileEx around in case the below
747 DWORD last_error
= ::GetLastError();
749 if (DirectoryExists(from_path
)) {
750 // MoveFileEx fails if moving directory across volumes. We will simulate
751 // the move by using Copy and Delete. Ideally we could check whether
752 // from_path and to_path are indeed in different volumes.
753 ret
= internal::CopyAndDeleteDirectory(from_path
, to_path
);
757 // Leave a clue about what went wrong so that it can be (at least) picked
758 // up by a PLOG entry.
759 ::SetLastError(last_error
);
765 bool CopyFileUnsafe(const FilePath
& from_path
, const FilePath
& to_path
) {
766 ThreadRestrictions::AssertIOAllowed();
768 // NOTE: I suspect we could support longer paths, but that would involve
769 // analyzing all our usage of files.
770 if (from_path
.value().length() >= MAX_PATH
||
771 to_path
.value().length() >= MAX_PATH
) {
775 // Unlike the posix implementation that copies the file manually and discards
776 // the ACL bits, CopyFile() copies the complete SECURITY_DESCRIPTOR and access
777 // bits, which is usually not what we want. We can't do much about the
778 // SECURITY_DESCRIPTOR but at least remove the read only bit.
779 const wchar_t* dest
= to_path
.value().c_str();
780 if (!::CopyFile(from_path
.value().c_str(), dest
, false)) {
784 DWORD attrs
= GetFileAttributes(dest
);
785 if (attrs
== INVALID_FILE_ATTRIBUTES
) {
788 if (attrs
& FILE_ATTRIBUTE_READONLY
) {
789 SetFileAttributes(dest
, attrs
& ~FILE_ATTRIBUTE_READONLY
);
794 bool CopyAndDeleteDirectory(const FilePath
& from_path
,
795 const FilePath
& to_path
) {
796 ThreadRestrictions::AssertIOAllowed();
797 if (CopyDirectory(from_path
, to_path
, true)) {
798 if (DeleteFile(from_path
, true))
801 // Like Move, this function is not transactional, so we just
802 // leave the copied bits behind if deleting from_path fails.
803 // If to_path exists previously then we have already overwritten
804 // it by now, we don't get better off by deleting the new bits.
809 } // namespace internal