summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
Diffstat (limited to 'base')
-rw-r--r--base/platform_file.h37
-rw-r--r--base/platform_file_posix.cc8
-rw-r--r--base/platform_file_win.cc8
3 files changed, 53 insertions, 0 deletions
diff --git a/base/platform_file.h b/base/platform_file.h
index 0dbf4e4..4ab1ace 100644
--- a/base/platform_file.h
+++ b/base/platform_file.h
@@ -12,6 +12,8 @@
#include <string>
+#include "base/ref_counted.h"
+
class FilePath;
namespace base {
@@ -53,6 +55,41 @@ PlatformFile CreatePlatformFile(const std::wstring& name,
// Closes a file handle
bool ClosePlatformFile(PlatformFile file);
+// Get the length of an underlying file. Returns false on error. Otherwise
+// *size is set to the length of the file, in bytes.
+bool GetPlatformFileSize(PlatformFile file, uint64* size);
+
+// This is a reference counted PlatformFile. When the ref count drops to zero,
+// the file handle is closed. See the comments in base/ref_counted.h for
+// details on how to use it.
+class RefCountedPlatformFile :
+ public base::RefCountedThreadSafe<RefCountedPlatformFile> {
+ public:
+ RefCountedPlatformFile(PlatformFile f) : file_(f) { }
+
+ ~RefCountedPlatformFile() {
+ if (file_ != kInvalidPlatformFileValue) {
+ ClosePlatformFile(file_);
+ file_ = kInvalidPlatformFileValue;
+ }
+ }
+
+ PlatformFile get() const {
+ return file_;
+ }
+
+ PlatformFile release() {
+ PlatformFile f = file_;
+ file_ = kInvalidPlatformFileValue;
+ return f;
+ }
+
+ private:
+ PlatformFile file_;
+
+ DISALLOW_COPY_AND_ASSIGN(RefCountedPlatformFile);
+};
+
} // namespace base
#endif // BASE_PLATFORM_FILE_H_
diff --git a/base/platform_file_posix.cc b/base/platform_file_posix.cc
index 46039b9..bfd40e9 100644
--- a/base/platform_file_posix.cc
+++ b/base/platform_file_posix.cc
@@ -77,4 +77,12 @@ bool ClosePlatformFile(PlatformFile file) {
return close(file);
}
+bool GetPlatformFileSize(PlatformFile file, uint64* out_size) {
+ struct stat st;
+ if (fstat(file, &st))
+ return false;
+ *out_size = st.st_size;
+ return true;
+}
+
} // namespace base
diff --git a/base/platform_file_win.cc b/base/platform_file_win.cc
index 1143487..ccaee1e 100644
--- a/base/platform_file_win.cc
+++ b/base/platform_file_win.cc
@@ -75,4 +75,12 @@ bool ClosePlatformFile(PlatformFile file) {
return (CloseHandle(file) == 0);
}
+bool GetPlatformFileSize(PlatformFile file, uint64* out_size) {
+ LARGE_INTEGER size;
+ if (!GetFileSizeEx(file, &size))
+ return false;
+ *out_size = size.QuadPart;
+ return true;
+}
+
} // namespace disk_cache