| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "net/disk_cache/flash/storage.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "net/base/net_errors.h" | |
| 11 #include "net/disk_cache/flash/format.h" | |
| 12 | |
| 13 namespace disk_cache { | |
| 14 | |
| 15 Storage::Storage(const base::FilePath& path, | |
| 16 int32 size) | |
| 17 : path_(path), size_(size) { | |
| 18 COMPILE_ASSERT(kFlashPageSize % 2 == 0, invalid_page_size); | |
| 19 COMPILE_ASSERT(kFlashBlockSize % kFlashPageSize == 0, invalid_block_size); | |
| 20 DCHECK(size_ % kFlashBlockSize == 0); | |
| 21 } | |
| 22 | |
| 23 bool Storage::Init() { | |
| 24 int flags = base::File::FLAG_READ | | |
| 25 base::File::FLAG_WRITE | | |
| 26 base::File::FLAG_OPEN_ALWAYS; | |
| 27 | |
| 28 file_.Initialize(path_, flags); | |
| 29 if (!file_.IsValid()) | |
| 30 return false; | |
| 31 | |
| 32 // TODO(agayev): if file already exists, do some validation and return either | |
| 33 // true or false based on the result. | |
| 34 | |
| 35 #if defined(OS_LINUX) | |
| 36 fallocate(file_.GetPlatformFile(), 0, 0, size_); | |
| 37 #endif | |
| 38 | |
| 39 return true; | |
| 40 } | |
| 41 | |
| 42 Storage::~Storage() { | |
| 43 } | |
| 44 | |
| 45 bool Storage::Read(void* buffer, int32 size, int32 offset) { | |
| 46 DCHECK(offset >= 0 && offset + size <= size_); | |
| 47 | |
| 48 int rv = file_.Read(offset, static_cast<char*>(buffer), size); | |
| 49 return rv == size; | |
| 50 } | |
| 51 | |
| 52 bool Storage::Write(const void* buffer, int32 size, int32 offset) { | |
| 53 DCHECK(offset >= 0 && offset + size <= size_); | |
| 54 | |
| 55 int rv = file_.Write(offset, static_cast<const char*>(buffer), size); | |
| 56 return rv == size; | |
| 57 } | |
| 58 | |
| 59 } // namespace disk_cache | |
| OLD | NEW |