FileDevice.h
Go to the documentation of this file.
1 /****
2  * FileDevice.h
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the IFS Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include <Storage/Device.h>
23 #include "../IFS/FileSystem.h"
24 
25 namespace Storage
26 {
30 class FileDevice : public Device
31 {
32 public:
41  : name(name), size(size), fileSystem(fileSys), file(file)
42  {
43  }
44 
53  FileDevice(const String& name, IFS::IFileSystem& fileSys, IFS::FileHandle file)
54  : name(name), fileSystem(fileSys), file(file)
55  {
56  size = IFS::FileSystem::cast(fileSys).getSize(file);
57  auto blockSize = getBlockSize();
58  auto blockCount = (size + blockSize - 1) / blockSize;
59  size = blockCount * blockSize;
60  }
61 
63  {
64  fileSystem.close(file);
65  }
66 
67  String getName() const override
68  {
69  return name.c_str();
70  }
71 
72  Type getType() const override
73  {
74  return Type::file;
75  }
76 
77  storage_size_t getSize() const override
78  {
79  return size;
80  }
81 
82  size_t getBlockSize() const override
83  {
84  // Use block size compatible with most disk drives
85  return 512;
86  }
87 
88  bool read(storage_size_t address, void* buffer, size_t len) override;
89  bool write(storage_size_t address, const void* data, size_t len) override;
90  bool erase_range(storage_size_t address, storage_size_t len) override;
91 
92 private:
93  CString name;
94  storage_size_t size;
95  IFS::IFileSystem& fileSystem;
96  IFS::FileHandle file;
97 };
98 
99 } // namespace Storage
uint32_t storage_size_t
Definition: Components/Storage/src/include/Storage/Types.h:19
Class to manage a NUL-terminated C-style string When storing persistent strings in RAM the regular St...
Definition: CString.h:27
const char * c_str() const
Definition: CString.h:100
file_size_t getSize(FileHandle file)
Get size of file.
static constexpr FileSystem & cast(IFileSystem &fs)
Definition: Components/IFS/src/include/IFS/FileSystem.h:42
Installable File System base class.
Definition: IFileSystem.h:100
virtual int close(FileHandle file)=0
close an open file
Represents a storage device (e.g. flash memory)
Definition: Components/Storage/src/include/Storage/Device.h:34
Type
Storage type.
Definition: Components/Storage/src/include/Storage/Device.h:42
Create custom storage device using backing file.
Definition: FileDevice.h:31
FileDevice(const String &name, IFS::IFileSystem &fileSys, IFS::FileHandle file)
Construct a device using existing file.
Definition: FileDevice.h:53
bool read(storage_size_t address, void *buffer, size_t len) override
Read data from the storage device.
bool erase_range(storage_size_t address, storage_size_t len) override
Erase a region of storage in preparation for writing.
size_t getBlockSize() const override
Obtain smallest allocation unit for erase operations.
Definition: FileDevice.h:82
FileDevice(const String &name, IFS::IFileSystem &fileSys, IFS::FileHandle file, storage_size_t size)
Construct a file device with custom size.
Definition: FileDevice.h:40
storage_size_t getSize() const override
Obtain addressable size of this device.
Definition: FileDevice.h:77
Type getType() const override
Obtain device type.
Definition: FileDevice.h:72
bool write(storage_size_t address, const void *data, size_t len) override
Write data to the storage device.
String getName() const override
Obtain unique device name.
Definition: FileDevice.h:67
~FileDevice()
Definition: FileDevice.h:62
The String class.
Definition: WString.h:137
int16_t FileHandle
File handle.
Definition: Stat.h:40
Definition: FileDevice.h:26