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 "base/platform_file.h" 11#include "net/base/net_errors.h" 12#include "net/disk_cache/flash/format.h" 13 14namespace disk_cache { 15 16Storage::Storage(const base::FilePath& path, 17 int32 size) 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); 22} 23 24bool Storage::Init() { 25 int flags = base::PLATFORM_FILE_READ | 26 base::PLATFORM_FILE_WRITE | 27 base::PLATFORM_FILE_OPEN_ALWAYS; 28 29 file_ = base::CreatePlatformFile(path_, flags, NULL, NULL); 30 if (file_ == base::kInvalidPlatformFileValue) 31 return false; 32 33 // TODO(agayev): if file already exists, do some validation and return either 34 // true or false based on the result. 35 36#if defined(OS_LINUX) 37 fallocate(file_, 0, 0, size_); 38#endif 39 40 return true; 41} 42 43Storage::~Storage() { 44 base::ClosePlatformFile(file_); 45} 46 47bool Storage::Read(void* buffer, int32 size, int32 offset) { 48 DCHECK(offset >= 0 && offset + size <= size_); 49 50 int rv = base::ReadPlatformFile(file_, offset, static_cast<char*>(buffer), 51 size); 52 return rv == size; 53} 54 55bool Storage::Write(const void* buffer, int32 size, int32 offset) { 56 DCHECK(offset >= 0 && offset + size <= size_); 57 58 int rv = base::WritePlatformFile(file_, offset, 59 static_cast<const char*>(buffer), size); 60 return rv == size; 61} 62 63} // namespace disk_cache 64