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 "net/disk_cache/flash/storage.h"
9 #include "base/logging.h"
10 #include "base/platform_file.h"
11 #include "net/base/net_errors.h"
12 #include "net/disk_cache/flash/format.h"
14 namespace disk_cache {
16 Storage::Storage(const base::FilePath& path,
18 : path_(path), size_(size) {
19 COMPILE_ASSERT(kFlashPageSize % 2 == 0, invalid_page_size);
20 COMPILE_ASSERT(kFlashBlockSize % kFlashPageSize == 0, invalid_block_size);
21 DCHECK(size_ % kFlashBlockSize == 0);
24 bool Storage::Init() {
25 int flags = base::PLATFORM_FILE_READ |
26 base::PLATFORM_FILE_WRITE |
27 base::PLATFORM_FILE_OPEN_ALWAYS;
29 file_ = base::CreatePlatformFile(path_, flags, NULL, NULL);
30 if (file_ == base::kInvalidPlatformFileValue)
33 // TODO(agayev): if file already exists, do some validation and return either
34 // true or false based on the result.
37 fallocate(file_, 0, 0, size_);
44 base::ClosePlatformFile(file_);
47 bool Storage::Read(void* buffer, int32 size, int32 offset) {
48 DCHECK(offset >= 0 && offset + size <= size_);
50 int rv = base::ReadPlatformFile(file_, offset, static_cast<char*>(buffer),
55 bool Storage::Write(const void* buffer, int32 size, int32 offset) {
56 DCHECK(offset >= 0 && offset + size <= size_);
58 int rv = base::WritePlatformFile(file_, offset,
59 static_cast<const char*>(buffer), size);
63 } // namespace disk_cache