FreeNOS
LinnDirectory.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 <Assert.h>
20#include <MemoryBlock.h>
21#include <String.h>
22#include "LinnDirectory.h"
23#include "LinnFile.h"
24
26 const u32 inode,
27 LinnInode *inodeData)
28 : Directory(inode)
29 , m_fs(fs)
30 , m_inodeData(inodeData)
31{
34}
35
37 Size & size,
38 const Size offset)
39{
42 LinnInode *dInode;
43 Size bytes = ZERO, blk;
44 Dirent tmp;
45
46 // Read directory entries
47 for (u32 ent = 0; ent < m_inodeData->size / sizeof(LinnDirectoryEntry); ent++)
48 {
49 // Point to correct (direct) block
50 if ((blk = (ent * sizeof(LinnDirectoryEntry)) / sb->blockSize)
52 {
53 break;
54 }
55
56 // Calculate offset to read.
57 u64 off = (m_inodeData->block[blk] * sb->blockSize) +
58 (ent * sizeof(LinnDirectoryEntry));
59
60 // Get the next entry.
61 if (m_fs->getStorage()->read(off, &dent,
63 {
65 }
66
67 // Can we read another entry?
68 if (bytes + sizeof(Dirent) > size)
69 {
71 }
72
73 // Fill in the Dirent.
74 if (!(dInode = m_fs->getInode(dent.inode)))
75 {
77 }
79 tmp.type = (FileSystem::FileType) dInode->type;
80
81 // Copy to the buffer.
82 const FileSystem::Result result = buffer.write(&tmp, sizeof(Dirent), bytes);
83 if (result != FileSystem::Success)
84 {
85 return result;
86 }
87
88 bytes += sizeof(Dirent);
89 }
90
91 // All done.
92 size = bytes;
94}
95
96File * LinnDirectory::lookup(const char *name)
97{
99 LinnInode *inode;
100
101 // Try to find the given LinnDirectoryEntry.
102 if (!getLinnDirectoryEntry(&entry, name))
103 return ZERO;
104
105 // Then retrieve it's LinnInode.
106 if (!(inode = m_fs->getInode(entry.inode)))
107 return ZERO;
108
109 // Create the appropriate in-memory file.
110 switch ((FileSystem::FileType) inode->type)
111 {
113 LinnDirectory *dir = new LinnDirectory(m_fs, entry.inode, inode);
114 assert(dir != NULL);
115 return dir;
116 }
117
119 LinnFile *file = new LinnFile(m_fs, entry.inode, inode);
120 assert(file != NULL);
121 return file;
122 }
123
124 default:
125 return ZERO;
126 }
127}
128
130 const char *name)
131{
132 const String nameStr(name, false);
134 u64 offset;
135
136 // Loop all blocks.
137 for (u32 blk = 0; blk < LINN_INODE_NUM_BLOCKS(sb, m_inodeData); blk++)
138 {
139 // Read directory entries.
140 for (u32 ent = 0; ent < LINN_DIRENT_PER_BLOCK(sb); ent++)
141 {
142 // Calculate offset to read.
143 offset = (m_inodeData->block[blk] * sb->blockSize) +
144 (sizeof(LinnDirectoryEntry) * ent);
145
146 // Get the next entry.
147 if (m_fs->getStorage()->read(offset, dent,
149 {
150 return false;
151 }
152
153 // Is it the entry we are looking for?
154 if (nameStr.equals(dent->name))
155 {
156 return true;
157 }
158 }
159 }
160
161 // Not found.
162 return false;
163}
u32 entry[]
Definition IntelACPI.h:1
Directory File functionality.
Definition Directory.h:60
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
FileSystem::Result write(const void *buffer, const Size size, const Size offset=ZERO)
Write bytes to the I/O buffer.
Definition IOBuffer.cpp:180
Represents an directory on a LinnFS filesystem.
LinnFileSystem * m_fs
Filesystem pointer.
LinnDirectory(LinnFileSystem *fs, const u32 inode, LinnInode *inodeData)
Constructor function.
LinnInode * m_inodeData
Inode which describes the directory.
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read directory entries.
bool getLinnDirectoryEntry(LinnDirectoryEntry *dent, const char *name)
Retrieve a directory entry.
virtual File * lookup(const char *name)
Retrieves a File pointer for the given entry name.
Linnenbank FileSystem (LinnFS).
LinnSuperBlock * getSuperBlock()
Retrieve the superblock pointer.
Storage * getStorage()
Get the underlying Storage object.
LinnInode * getInode(u32 inodeNum)
Read an inode from the filesystem.
Represents a file on a mounted LinnFS filesystem.
Definition LinnFile.h:39
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
virtual FileSystem::Result read(const u64 offset, void *buffer, const Size size) const =0
Read a contiguous set of data.
Abstraction of strings.
Definition String.h:42
virtual bool equals(const String &str) const
Alias for compareTo().
Definition String.cpp:262
#define assert(exp)
Insert program diagnostics.
Definition assert.h:60
#define NULL
NULL means zero.
Definition Macros.h:39
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
#define ZERO
Zero value.
Definition Macros.h:43
unsigned long long u64
Unsigned 64-bit number.
Definition Types.h:50
#define LINN_INODE_DIR_BLOCKS
Direct blocks.
Definition LinnInode.h:49
#define LINN_DIRENT_NAME_LEN
Length of the name field in an directory entry.
#define LINN_INODE_NUM_BLOCKS(super, inode)
Calculate the number of blocks used in an LinnInode.
Definition LinnInode.h:80
#define LINN_DIRENT_PER_BLOCK(sb)
Calculates the number of LinnDirectoryEntry's fitting in one block.
FileType
All possible filetypes.
Definition FileSystem.h:71
Result
Result code for filesystem Actions.
Definition FileSystem.h:53
@ InvalidArgument
Definition FileSystem.h:55
@ PermissionDenied
Definition FileSystem.h:59
Describes an entry inside a Directory.
Definition Directory.h:40
FileSystem::FileType type
Type of file.
Definition Directory.h:45
char name[DIRENT_LEN]
Name of the file.
Definition Directory.h:42
Struct of an directory entry in LinnFS.
char name[LINN_DIRENT_NAME_LEN]
File name.
le32 inode
Inode number.
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
le32 block[LINN_INODE_BLOCKS]
Pointers to blocks.
Definition LinnInode.h:104
le16 type
Type of file, as an FileType.
Definition LinnInode.h:94
Linnenbank Filesystem (LinnFS) super block.
le32 blockSize
Size of each data block.