FreeNOS
LinnFile.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 <FreeNOS/User.h>
19 #include "LinnFileSystem.h"
20 #include "LinnFile.h"
21 
23  const u32 inode,
24  LinnInode *inodeData)
25  : File(inode)
26  , m_fs(fs)
27  , m_inodeData(inodeData)
28 {
31 }
32 
34 {
35 }
36 
38  Size & size,
39  const Size offset)
40 {
41  const LinnSuperBlock *sb = m_fs->getSuperBlock();
42  const Size inodeNumBlocks = LINN_INODE_NUM_BLOCKS(sb, m_inodeData);
43  Size bytes = 0, blockNr = 0, blockCount;
44  u64 storageOffset, copyOffset = offset;
45  Size total = 0;
46 
48 
49  // Skip ahead blocks.
50  while ((sb->blockSize * (blockNr + 1)) <= copyOffset)
51  {
52  blockNr++;
53  }
54 
55  // Adjust the copy offset within this block.
56  copyOffset -= sb->blockSize * blockNr;
57 
58  // Loop all blocks.
59  while (blockNr < inodeNumBlocks && total < size && m_inodeData->size - (offset + total) > 0)
60  {
61  // Calculate the offset in storage for this block.
62  storageOffset = m_fs->getOffsetRange(m_inodeData, blockNr, blockCount);
63 
64  // Calculate the number of bytes to copy.
65  bytes = (blockCount * sb->blockSize) - copyOffset;
66 
67  // Respect the inode size.
68  if (bytes > m_inodeData->size - (offset + total))
69  {
70  bytes = m_inodeData->size - (offset + total);
71  }
72 
73  // Respect the remote process buffer.
74  if (bytes > size - total)
75  {
76  bytes = size - total;
77  }
78 
79  // Fetch the next block.
80  if (m_fs->getStorage()->read(storageOffset + copyOffset,
81  buffer.getBuffer() + total, bytes) != FileSystem::Success)
82  {
83  return FileSystem::IOError;
84  }
85  buffer.addCount(bytes);
86 
87  // Update state.
88  total += bytes;
89  copyOffset = 0;
90  blockNr += blockCount;
91  }
92 
93  // Success.
94  size = total;
95  return FileSystem::Success;
96 }
LINN_MAX_BLOCK_SIZE
#define LINN_MAX_BLOCK_SIZE
Maximum blocksize.
Definition: LinnFileSystem.h:51
LinnFile::m_fs
LinnFileSystem * m_fs
Filesystem pointer.
Definition: LinnFile.h:75
LINN_INODE_NUM_BLOCKS
#define LINN_INODE_NUM_BLOCKS(super, inode)
Calculate the number of blocks used in an LinnInode.
Definition: LinnInode.h:80
IOBuffer::addCount
void addCount(const Size bytes)
Increment byte counter.
Definition: IOBuffer.cpp:114
LinnFileSystem
Linnenbank FileSystem (LinnFS).
Definition: LinnFileSystem.h:73
LinnFile::LinnFile
LinnFile(LinnFileSystem *fs, const u32 inode, LinnInode *inodeData)
Constructor function.
Definition: LinnFile.cpp:22
FileSystem::IOError
@ IOError
Definition: FileSystem.h:58
FileSystem::Success
@ Success
Definition: FileSystem.h:54
LinnFileSystem::getStorage
Storage * getStorage()
Get the underlying Storage object.
Definition: LinnFileSystem.h:104
File
Represents a file present on a FileSystem.
Definition: File.h:39
u64
unsigned long long u64
Unsigned 64-bit number.
Definition: Types.h:50
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
LinnSuperBlock
Linnenbank Filesystem (LinnFS) super block.
Definition: LinnSuperBlock.h:113
IOBuffer
Abstract Input/Output buffer.
Definition: IOBuffer.h:37
LinnFile::read
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read bytes from the file.
Definition: LinnFile.cpp:37
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
LinnInode
Structure of an inode on the disk in the LinnFS filesystem.
Definition: LinnInode.h:92
LinnFile::m_inodeData
LinnInode * m_inodeData
Inode pointer.
Definition: LinnFile.h:78
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
LinnFile::~LinnFile
virtual ~LinnFile()
Destructor function.
Definition: LinnFile.cpp:33
IOBuffer::getBuffer
u8 * getBuffer()
Get raw buffer.
Definition: IOBuffer.cpp:125
LinnInode::mode
le16 mode
Access permissions, as an FileMode.
Definition: LinnInode.h:95
assert
#define assert(exp)
Insert program diagnostics.
Definition: assert.h:60
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
LinnSuperBlock::blockSize
le32 blockSize
Size of each data block.
Definition: LinnSuperBlock.h:121
Storage::read
virtual FileSystem::Result read(const u64 offset, void *buffer, const Size size) const =0
Read a contiguous set of data.
LinnFile.h
LinnInode::size
le32 size
Size in bytes.
Definition: LinnInode.h:98
LinnFileSystem::getSuperBlock
LinnSuperBlock * getSuperBlock()
Retrieve the superblock pointer.
Definition: LinnFileSystem.h:92
LinnFileSystem.h
LinnFileSystem::getOffsetRange
u64 getOffsetRange(const LinnInode *inode, const u32 blk, Size &numContiguous)
Calculates the offset inside storage for a given block.
Definition: LinnFileSystem.cpp:132