summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-26 01:04:08 +0000
committerdarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-26 01:04:08 +0000
commitf5e3da4d564980a048f375cf1a824a01df03382a (patch)
tree68a0562ee30146d65071d722744503d47255dec4
parentec9207d3a86ed6ccbefc56d95034897f48f52674 (diff)
downloadchromium_src-f5e3da4d564980a048f375cf1a824a01df03382a.zip
chromium_src-f5e3da4d564980a048f375cf1a824a01df03382a.tar.gz
chromium_src-f5e3da4d564980a048f375cf1a824a01df03382a.tar.bz2
Add file_util::GetFileInfo as a means to provide more information about a file
path. This CL changes GetFileSize to be implemented in terms of GetFileInfo since under the hood on all platforms the same system call is used. R=mark Review URL: http://codereview.chromium.org/4286 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2624 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/file_util.cc8
-rw-r--r--base/file_util.h14
-rw-r--r--base/file_util_posix.cc5
-rw-r--r--base/file_util_win.cc20
4 files changed, 35 insertions, 12 deletions
diff --git a/base/file_util.cc b/base/file_util.cc
index 0d6fab1..bec910b 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -290,5 +290,13 @@ bool ReadFileToString(const std::wstring& path, std::string* contents) {
return true;
}
+bool GetFileSize(const std::wstring& file_path, int64* file_size) {
+ FileInfo info;
+ if (!GetFileInfo(file_path, &info))
+ return false;
+ *file_size = info.size;
+ return true;
+}
+
} // namespace
diff --git a/base/file_util.h b/base/file_util.h
index 45d4a33..9d52f36 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -232,6 +232,20 @@ bool CreateDirectory(const std::wstring& full_path);
// Returns the file size. Returns true on success.
bool GetFileSize(const std::wstring& file_path, int64* file_size);
+// 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.
+ int64 size;
+
+ // True if the file corresponds to a directory.
+ bool is_directory;
+
+ // Add additional fields here as needed.
+};
+
+// Returns information about the given file path.
+bool GetFileInfo(const std::wstring& file_path, FileInfo* info);
+
// Reads the given number of bytes from the file into the buffer. Returns
// the number of read bytes, or -1 on error.
int ReadFile(const std::wstring& filename, char* data, int size);
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index 4f1b1ba..65dbfcb 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -300,11 +300,12 @@ bool CreateDirectory(const std::wstring& full_path) {
return true;
}
-bool GetFileSize(const std::wstring& file_path, int64* file_size) {
+bool GetFileInfo(const std::wstring& file_path, FileInfo* results) {
struct stat64 file_info;
if (stat64(WideToUTF8(file_path).c_str(), &file_info) != 0)
return false;
- *file_size = file_info.st_size;
+ results->is_directory = S_ISDIR(file_info.st_mode);
+ results->size = file_info.st_size;
return true;
}
diff --git a/base/file_util_win.cc b/base/file_util_win.cc
index 168e26e..6a89b3f 100644
--- a/base/file_util_win.cc
+++ b/base/file_util_win.cc
@@ -432,18 +432,18 @@ bool CreateDirectory(const std::wstring& full_path) {
return err == ERROR_SUCCESS;
}
-bool GetFileSize(const std::wstring& file_path, int64* file_size) {
- ScopedHandle file_handle(
- CreateFile(file_path.c_str(), GENERIC_READ,
- FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
-
- LARGE_INTEGER win32_file_size = {0};
- if (!GetFileSizeEx(file_handle, &win32_file_size)) {
+bool GetFileInfo(const std::wstring& file_path, FileInfo* results) {
+ WIN32_FILE_ATTRIBUTE_DATA attr;
+ if (!GetFileAttributesEx(file_path.c_str(), GetFileExInfoStandard, &attr))
return false;
- }
- *file_size = win32_file_size.QuadPart;
+ ULARGE_INTEGER size;
+ size.HighPart = attr.nFileSizeHigh;
+ size.LowPart = attr.nFileSizeLow;
+ results->size = size.QuadPart;
+
+ results->is_directory =
+ (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
return true;
}