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
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 {
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;
96}
Represents a file present on a FileSystem.
Definition File.h:40
FileSystem::FileModes m_access
Access permissions.
Definition File.h:145
Size m_size
Size of the file, in bytes.
Definition File.h:148
Abstract Input/Output buffer.
Definition IOBuffer.h:38
void addCount(const Size bytes)
Increment byte counter.
Definition IOBuffer.cpp:114
u8 * getBuffer()
Get raw buffer.
Definition IOBuffer.cpp:125
Linnenbank FileSystem (LinnFS).
LinnSuperBlock * getSuperBlock()
Retrieve the superblock pointer.
Storage * getStorage()
Get the underlying Storage object.
u64 getOffsetRange(const LinnInode *inode, const u32 blk, Size &numContiguous)
Calculates the offset inside storage for a given block.
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read bytes from the file.
Definition LinnFile.cpp:37
virtual ~LinnFile()
Destructor function.
Definition LinnFile.cpp:33
LinnFileSystem * m_fs
Filesystem pointer.
Definition LinnFile.h:75
LinnFile(LinnFileSystem *fs, const u32 inode, LinnInode *inodeData)
Constructor function.
Definition LinnFile.cpp:22
LinnInode * m_inodeData
Inode pointer.
Definition LinnFile.h:78
virtual FileSystem::Result read(const u64 offset, void *buffer, const Size size) const =0
Read a contiguous set of data.
#define assert(exp)
Insert program diagnostics.
Definition assert.h:60
unsigned int u32
Unsigned 32-bit number.
Definition Types.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
#define LINN_MAX_BLOCK_SIZE
Maximum blocksize.
#define LINN_INODE_NUM_BLOCKS(super, inode)
Calculate the number of blocks used in an LinnInode.
Definition LinnInode.h:80
Result
Result code for filesystem Actions.
Definition FileSystem.h:53
Structure of an inode on the disk in the LinnFS filesystem.
Definition LinnInode.h:93
le16 mode
Access permissions, as an FileMode.
Definition LinnInode.h:95
le32 size
Size in bytes.
Definition LinnInode.h:98
Linnenbank Filesystem (LinnFS) super block.
le32 blockSize
Size of each data block.