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 
21 FileStorage::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 
39 FileSystem::Result FileStorage::read(const u64 offset, void *buffer, const Size size) const
40 {
41  Size sz = size;
42 
44  if (!fd || !fd->open)
45  {
46  return FileSystem::IOError;
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 
55 FileSystem::Result FileStorage::write(const u64 offset, void *buffer, const Size size)
56 {
57  Size sz = size;
58 
60  if (!fd || !fd->open)
61  {
62  return FileSystem::IOError;
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 }
FileDescriptor::Entry::position
Size position
< Process identifier of the filesystem
Definition: FileDescriptor.h:50
FileStorage::read
virtual FileSystem::Result read(const u64 offset, void *buffer, const Size size) const
Read a contiguous set of data.
Definition: FileStorage.cpp:39
StrictSingleton< FileDescriptor >::instance
static FileDescriptor * instance()
Retrieve the instance.
Definition: Singleton.h:53
FileSystemClient::statFile
FileSystem::Result statFile(const char *path, FileSystem::FileStat *st) const
Retrieve status of a file.
Definition: FileSystemClient.cpp:186
FileDescriptor::Entry::open
bool open
< Current position indicator.
Definition: FileDescriptor.h:51
FileStorage::FileStorage
FileStorage(const char *path, Size offset=ZERO)
Constructor function.
Definition: FileStorage.cpp:21
FileSystem::IOError
@ IOError
Definition: FileSystem.h:58
FileDescriptor.h
FileSystem::Success
@ Success
Definition: FileSystem.h:54
FileStorage::initialize
virtual FileSystem::Result initialize()
Initialize the Storage device.
Definition: FileStorage.cpp:28
FileStorage::m_fd
Size m_fd
File descriptor of the file.
Definition: FileStorage.h:92
FileDescriptor::getEntry
Entry * getEntry(const Size index)
Retrieve a file descriptor Entry.
Definition: FileDescriptor.cpp:58
FileSystemClient::writeFile
FileSystem::Result writeFile(const Size descriptor, const void *buf, Size *size) const
Write a file.
Definition: FileSystemClient.cpp:257
u64
unsigned long long u64
Unsigned 64-bit number.
Definition: Types.h:50
FileSystem::FileStat::size
Size size
< File access permission bits.
Definition: FileSystem.h:119
FileDescriptor::Entry
Describes a single file opened by a user process.
Definition: FileDescriptor.h:46
FileSystemClient::openFile
FileSystem::Result openFile(const char *path, Size &descriptor) const
Open a file.
Definition: FileSystemClient.cpp:198
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
FileStorage::capacity
virtual u64 capacity() const
Retrieve maximum storage capacity.
Definition: FileStorage.cpp:71
FileStorage::m_stat
FileSystem::FileStat m_stat
Status of the file for Storage I/O.
Definition: FileStorage.h:98
FileSystemClient::readFile
FileSystem::Result readFile(const Size descriptor, void *buf, Size *size) const
Read a file.
Definition: FileSystemClient.cpp:229
FileStorage::m_path
const char * m_path
Path to the file.
Definition: FileStorage.h:89
FileStorage::write
virtual FileSystem::Result write(const u64 offset, void *buffer, const Size size)
Write a contiguous set of data.
Definition: FileStorage.cpp:55
FileStorage::m_file
FileSystemClient m_file
Client for file system I/O.
Definition: FileStorage.h:95
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
FileStorage.h
FileStorage::m_offset
Size m_offset
Offset used as a base for I/O.
Definition: FileStorage.h:101