summaryrefslogtreecommitdiffstats
path: root/chromium/net/disk_cache/flash/storage.cc
diff options
context:
space:
mode:
authorZeno Albisser <zeno.albisser@digia.com>2013-08-15 21:46:11 +0200
committerZeno Albisser <zeno.albisser@digia.com>2013-08-15 21:46:11 +0200
commit679147eead574d186ebf3069647b4c23e8ccace6 (patch)
treefc247a0ac8ff119f7c8550879ebb6d3dd8d1ff69 /chromium/net/disk_cache/flash/storage.cc
Initial import.
Diffstat (limited to 'chromium/net/disk_cache/flash/storage.cc')
-rw-r--r--chromium/net/disk_cache/flash/storage.cc63
1 files changed, 63 insertions, 0 deletions
diff --git a/chromium/net/disk_cache/flash/storage.cc b/chromium/net/disk_cache/flash/storage.cc
new file mode 100644
index 00000000000..c7136ef869b
--- /dev/null
+++ b/chromium/net/disk_cache/flash/storage.cc
@@ -0,0 +1,63 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/disk_cache/flash/storage.h"
+
+#include <fcntl.h>
+
+#include "base/logging.h"
+#include "base/platform_file.h"
+#include "net/base/net_errors.h"
+#include "net/disk_cache/flash/format.h"
+
+namespace disk_cache {
+
+Storage::Storage(const base::FilePath& path,
+ int32 size)
+ : path_(path), size_(size) {
+ COMPILE_ASSERT(kFlashPageSize % 2 == 0, invalid_page_size);
+ COMPILE_ASSERT(kFlashBlockSize % kFlashPageSize == 0, invalid_block_size);
+ DCHECK(size_ % kFlashBlockSize == 0);
+}
+
+bool Storage::Init() {
+ int flags = base::PLATFORM_FILE_READ |
+ base::PLATFORM_FILE_WRITE |
+ base::PLATFORM_FILE_OPEN_ALWAYS;
+
+ file_ = base::CreatePlatformFile(path_, flags, NULL, NULL);
+ if (file_ == base::kInvalidPlatformFileValue)
+ return false;
+
+ // TODO(agayev): if file already exists, do some validation and return either
+ // true or false based on the result.
+
+#if defined(OS_LINUX)
+ fallocate(file_, 0, 0, size_);
+#endif
+
+ return true;
+}
+
+Storage::~Storage() {
+ base::ClosePlatformFile(file_);
+}
+
+bool Storage::Read(void* buffer, int32 size, int32 offset) {
+ DCHECK(offset >= 0 && offset + size <= size_);
+
+ int rv = base::ReadPlatformFile(file_, offset, static_cast<char*>(buffer),
+ size);
+ return rv == size;
+}
+
+bool Storage::Write(const void* buffer, int32 size, int32 offset) {
+ DCHECK(offset >= 0 && offset + size <= size_);
+
+ int rv = base::WritePlatformFile(file_, offset,
+ static_cast<const char*>(buffer), size);
+ return rv == size;
+}
+
+} // namespace disk_cache