FreeNOS
FileStorage.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "FileDescriptor.h"
19#include "FileStorage.h"
20
21FileStorage::FileStorage(const char *path, const Size offset)
22 : m_path(path)
23 , m_fd(0)
24 , m_offset(offset)
25{
26}
27
29{
30 const FileSystem::Result statResult = m_file.statFile(m_path, &m_stat);
31 if (statResult != FileSystem::Success)
32 {
33 return statResult;
34 }
35
36 return m_file.openFile(m_path, m_fd);
37}
38
39FileSystem::Result FileStorage::read(const u64 offset, void *buffer, const Size size) const
40{
41 Size sz = size;
42
44 if (!fd || !fd->open)
45 {
47 }
48
49 // Update the file position pointer
50 fd->position = m_offset + offset;
51
52 return m_file.readFile(m_fd, buffer, &sz);
53}
54
55FileSystem::Result FileStorage::write(const u64 offset, void *buffer, const Size size)
56{
57 Size sz = size;
58
60 if (!fd || !fd->open)
61 {
63 }
64
65 // Update the file position pointer
66 fd->position = m_offset + offset;
67
68 return m_file.writeFile(m_fd, buffer, &sz);
69}
70
72{
73 return m_stat.size;
74}
Entry * getEntry(const Size index)
Retrieve a file descriptor Entry.
Size m_fd
File descriptor of the file.
Definition FileStorage.h:92
virtual FileSystem::Result initialize()
Initialize the Storage device.
Size m_offset
Offset used as a base for I/O.
FileSystem::FileStat m_stat
Status of the file for Storage I/O.
Definition FileStorage.h:98
virtual FileSystem::Result read(const u64 offset, void *buffer, const Size size) const
Read a contiguous set of data.
FileSystemClient m_file
Client for file system I/O.
Definition FileStorage.h:95
const char * m_path
Path to the file.
Definition FileStorage.h:89
virtual FileSystem::Result write(const u64 offset, void *buffer, const Size size)
Write a contiguous set of data.
virtual u64 capacity() const
Retrieve maximum storage capacity.
FileStorage(const char *path, Size offset=ZERO)
Constructor function.
FileSystem::Result writeFile(const Size descriptor, const void *buf, Size *size) const
Write a file.
FileSystem::Result statFile(const char *path, FileSystem::FileStat *st) const
Retrieve status of a file.
FileSystem::Result readFile(const Size descriptor, void *buf, Size *size) const
Read a file.
FileSystem::Result openFile(const char *path, Size &descriptor) const
Open a file.
static FileDescriptor * instance()
Retrieve the instance.
Definition Singleton.h:53
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
unsigned long long u64
Unsigned 64-bit number.
Definition Types.h:50
Result
Result code for filesystem Actions.
Definition FileSystem.h:53
Describes a single file opened by a user process.
bool open
< Current position indicator.
Size position
< Process identifier of the filesystem
Size size
< File access permission bits.
Definition FileSystem.h:119