FreeNOS
PseudoFile.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 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 <Assert.h>
19 #include <String.h>
20 #include <MemoryBlock.h>
21 #include "PseudoFile.h"
22 
24  : File(inode, FileSystem::RegularFile)
25 {
26  m_size = ZERO;
27  m_buffer = ZERO;
29 }
30 
32  const char *str)
33  : File(inode, FileSystem::RegularFile)
34 {
36  m_size = String::length(str);
37  m_buffer = new char[m_size + 1];
38  assert(m_buffer != NULL);
40 }
41 
43 {
44  if (m_buffer)
45  {
46  delete[] m_buffer;
47  }
48 }
49 
51  Size & size,
52  const Size offset)
53 {
54  // Bounds checking
55  if (offset >= m_size)
56  {
57  size = 0;
58  return FileSystem::Success;
59  }
60 
61  // How much bytes to copy?
62  const Size bytes = m_size - offset > size ? size : m_size - offset;
63  size = bytes;
64 
65  // Copy the buffers
66  return buffer.write(m_buffer + offset, bytes);
67 }
68 
70  Size & size,
71  const Size offset)
72 {
73  // Check for the buffer size
74  if (!m_buffer || m_size < (size + offset))
75  {
76  // Allocate a new buffer and copy the old data
77  char *new_buffer = new char[size+offset];
78  assert(new_buffer != NULL);
79  MemoryBlock::set(new_buffer, 0, sizeof(size+offset));
80 
81  // Inherit from the old buffer, if needed
82  if (m_buffer)
83  {
84  MemoryBlock::copy(new_buffer, m_buffer, m_size);
85  delete[] m_buffer;
86  }
87 
88  // Assign buffer
89  m_buffer = new_buffer;
90  m_size = size+offset;
91  }
92 
93  // Copy the input data in the current buffer
94  return buffer.read(m_buffer + offset, size);
95 }
MemoryBlock::copy
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Definition: MemoryBlock.cpp:36
String::length
Size length() const
Same as count().
Definition: String.cpp:105
MemoryBlock::set
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Definition: MemoryBlock.cpp:25
Assert.h
PseudoFile.h
MemoryBlock.h
PseudoFile::read
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read bytes from the file.
Definition: PseudoFile.cpp:50
FileSystem::Success
@ Success
Definition: FileSystem.h:54
File
Represents a file present on a FileSystem.
Definition: File.h:39
PseudoFile::write
virtual FileSystem::Result write(IOBuffer &buffer, Size &size, const Size offset)
Write bytes to the file.
Definition: PseudoFile.cpp:69
File::m_access
FileSystem::FileModes m_access
Access permissions.
Definition: File.h:145
File::m_size
Size m_size
Size of the file, in bytes.
Definition: File.h:148
FileSystem::OwnerRW
@ OwnerRW
Definition: FileSystem.h:90
IOBuffer
Abstract Input/Output buffer.
Definition: IOBuffer.h:37
NULL
#define NULL
NULL means zero.
Definition: Macros.h:39
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
PseudoFile::m_buffer
char * m_buffer
Buffer from which we read.
Definition: PseudoFile.h:92
FileSystem::RegularFile
@ RegularFile
Definition: FileSystem.h:72
PseudoFile::PseudoFile
PseudoFile(const u32 inode)
Default constructor.
Definition: PseudoFile.cpp:23
PseudoFile::~PseudoFile
virtual ~PseudoFile()
Destructor.
Definition: PseudoFile.cpp:42
assert
#define assert(exp)
Insert program diagnostics.
Definition: assert.h:60
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
String.h
FileSystem
Definition: FileSystem.h:31
IOBuffer::read
FileSystem::Result read(void *buffer, const Size size, const Size offset=ZERO)
Read bytes from the I/O buffer.
Definition: IOBuffer.cpp:156
IOBuffer::write
FileSystem::Result write(const void *buffer, const Size size, const Size offset=ZERO)
Write bytes to the I/O buffer.
Definition: IOBuffer.cpp:180
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43