summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorcpu@chromium.org <cpu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-04 17:10:17 +0000
committercpu@chromium.org <cpu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-04 17:10:17 +0000
commit53af568e133242dd1412073caea1f95b8b4743fd (patch)
treea87242c30a34a4e5837187cbf8006d7215d85193 /base
parentff622aa41f2eb00c4f0359a1d661f77ea17d29e1 (diff)
downloadchromium_src-53af568e133242dd1412073caea1f95b8b4743fd.zip
chromium_src-53af568e133242dd1412073caea1f95b8b4743fd.tar.gz
chromium_src-53af568e133242dd1412073caea1f95b8b4743fd.tar.bz2
Factor out NormalizeToNativeFilePath from NormalizeFilePath
- Also do some cleaning up, fixing invalid usage of ScopedHandle BUG=50774 TEST=existing unit tests suffice Review URL: http://codereview.chromium.org/3043050 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54918 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/file_util.h8
-rw-r--r--base/file_util_win.cc80
2 files changed, 46 insertions, 42 deletions
diff --git a/base/file_util.h b/base/file_util.h
index bd973b23..91b16d2 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -307,6 +307,14 @@ bool IsDotDot(const FilePath& path);
// or if |real_path| would be longer than MAX_PATH characters.
bool NormalizeFilePath(const FilePath& path, FilePath* real_path);
+#if defined(OS_WIN)
+// Given an existing file in |path|, it returns in |real_path| the path
+// in the native NT format, of the form "\Device\HarddiskVolumeXX\..".
+// Returns false it it fails. Empty files cannot be resolved with this
+// function.
+bool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path);
+#endif
+
// Used to hold information about a given file path. See GetFileInfo below.
struct FileInfo {
// The size of the file in bytes. Undefined when is_directory is true.
diff --git a/base/file_util_win.cc b/base/file_util_win.cc
index 4adab2c..4090ce4 100644
--- a/base/file_util_win.cc
+++ b/base/file_util_win.cc
@@ -27,6 +27,9 @@ namespace file_util {
namespace {
+const DWORD kFileShareAll =
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
+
// Helper for NormalizeFilePath(), defined below.
bool DevicePathToDriveLetterPath(const FilePath& device_path,
FilePath* drive_letter_path) {
@@ -288,8 +291,7 @@ bool PathExists(const FilePath& path) {
bool PathIsWritable(const FilePath& path) {
HANDLE dir =
- CreateFile(path.value().c_str(), FILE_ADD_FILE,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+ CreateFile(path.value().c_str(), FILE_ADD_FILE, kFileShareAll,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (dir == INVALID_HANDLE_VALUE)
@@ -325,8 +327,7 @@ bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle,
bool GetFileCreationLocalTime(const std::wstring& filename,
LPSYSTEMTIME creation_time) {
ScopedHandle file_handle(
- CreateFile(filename.c_str(), GENERIC_READ,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
+ CreateFile(filename.c_str(), GENERIC_READ, kFileShareAll, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time);
}
@@ -697,8 +698,7 @@ bool SetLastModifiedTime(const FilePath& file_path, base::Time last_modified) {
FILETIME timestamp(last_modified.ToFileTime());
ScopedHandle file_handle(
CreateFile(file_path.value().c_str(), FILE_WRITE_ATTRIBUTES,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
+ kFileShareAll, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
BOOL ret = SetFileTime(file_handle.Get(), NULL, &timestamp, &timestamp);
return ret != 0;
}
@@ -720,7 +720,7 @@ int ReadFile(const FilePath& filename, char* data, int size) {
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL));
- if (file == INVALID_HANDLE_VALUE)
+ if (!file)
return -1;
DWORD read;
@@ -738,7 +738,7 @@ int WriteFile(const FilePath& filename, const char* data, int size) {
CREATE_ALWAYS,
0,
NULL));
- if (file == INVALID_HANDLE_VALUE) {
+ if (!file) {
LOG(WARNING) << "CreateFile failed for path " << filename.value() <<
" error code=" << GetLastError() <<
" error text=" << win_util::FormatLastWin32Error();
@@ -969,51 +969,52 @@ bool HasFileBeenModifiedSince(const FileEnumerator::FindInfo& find_info,
}
bool NormalizeFilePath(const FilePath& path, FilePath* real_path) {
- ScopedHandle path_handle(
- ::CreateFile(path.value().c_str(),
- GENERIC_READ,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL));
- if (!path_handle.IsValid())
+ FilePath mapped_file;
+ if (!NormalizeToNativeFilePath(path, &mapped_file))
return false;
+ // NormalizeToNativeFilePath() will return a path that starts with
+ // "\Device\Harddisk...". Helper DevicePathToDriveLetterPath()
+ // will find a drive letter which maps to the path's device, so
+ // that we return a path starting with a drive letter.
+ return DevicePathToDriveLetterPath(mapped_file, real_path);
+}
+bool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path) {
// In Vista, GetFinalPathNameByHandle() would give us the real path
// from a file handle. If we ever deprecate XP, consider changing the
// code below to a call to GetFinalPathNameByHandle(). The method this
// function uses is explained in the following msdn article:
// http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx
- DWORD file_size_high = 0;
- DWORD file_size_low = ::GetFileSize(path_handle.Get(), &file_size_high);
- if (file_size_low == 0 && file_size_high == 0) {
- // It is not possible to map an empty file.
- LOG(ERROR) << "NormalizeFilePath failed: Empty file.";
+ ScopedHandle file_handle(
+ ::CreateFile(path.value().c_str(),
+ GENERIC_READ,
+ kFileShareAll,
+ NULL,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL));
+ if (!file_handle)
return false;
- }
// Create a file mapping object. Can't easily use MemoryMappedFile, because
- // we only map the first byte, and need direct access to the handle.
+ // we only map the first byte, and need direct access to the handle. You can
+ // not map an empty file, this call fails in that case.
ScopedHandle file_map_handle(
- ::CreateFileMapping(path_handle.Get(),
+ ::CreateFileMapping(file_handle.Get(),
NULL,
PAGE_READONLY,
0,
1, // Just one byte. No need to look at the data.
NULL));
-
- if (!file_map_handle.IsValid())
+ if (!file_map_handle)
return false;
// Use a view of the file to get the path to the file.
- void* file_view = MapViewOfFile(
- file_map_handle.Get(), FILE_MAP_READ, 0, 0, 1);
+ void* file_view = MapViewOfFile(file_map_handle.Get(),
+ FILE_MAP_READ, 0, 0, 1);
if (!file_view)
return false;
- bool success = false;
-
// The expansion of |path| into a full path may make it longer.
// GetMappedFileName() will fail if the result is longer than MAX_PATH.
// Pad a bit to be safe. If kMaxPathLength is ever changed to be less
@@ -1022,18 +1023,13 @@ bool NormalizeFilePath(const FilePath& path, FilePath* real_path) {
// path fit in |mapped_file_path|.
const int kMaxPathLength = MAX_PATH + 10;
wchar_t mapped_file_path[kMaxPathLength];
- if (::GetMappedFileName(GetCurrentProcess(),
- file_view,
- mapped_file_path,
- kMaxPathLength)) {
- // GetMappedFileName() will return a path that starts with
- // "\Device\Harddisk...". Helper DevicePathToDriveLetterPath()
- // will find a drive letter which maps to the path's device, so
- // that we return a path starting with a drive letter.
- FilePath mapped_file(mapped_file_path);
- success = DevicePathToDriveLetterPath(mapped_file, real_path);
+ bool success = false;
+ HANDLE cp = GetCurrentProcess();
+ if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) {
+ *nt_path = FilePath(mapped_file_path);
+ success = true;
}
- UnmapViewOfFile(file_view);
+ ::UnmapViewOfFile(file_view);
return success;
}