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 {
41  LinnDirectoryEntry dent;
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  {
76  return FileSystem::NotFound;
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;
93  return FileSystem::Success;
94 }
95 
96 File * 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 }
MemoryBlock::copy
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Definition: MemoryBlock.cpp:36
Dirent::name
char name[DIRENT_LEN]
Name of the file.
Definition: Directory.h:42
LINN_INODE_NUM_BLOCKS
#define LINN_INODE_NUM_BLOCKS(super, inode)
Calculate the number of blocks used in an LinnInode.
Definition: LinnInode.h:80
LINN_DIRENT_NAME_LEN
#define LINN_DIRENT_NAME_LEN
Length of the name field in an directory entry.
Definition: LinnDirectoryEntry.h:39
LinnDirectory::read
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read directory entries.
Definition: LinnDirectory.cpp:36
FileSystem::FileType
FileType
All possible filetypes.
Definition: FileSystem.h:70
LinnFileSystem
Linnenbank FileSystem (LinnFS).
Definition: LinnFileSystem.h:73
Dirent
struct Dirent Dirent
Describes an entry inside a Directory.
String
Abstraction of strings.
Definition: String.h:41
Directory
Directory File functionality.
Definition: Directory.h:59
LinnDirectory::LinnDirectory
LinnDirectory(LinnFileSystem *fs, const u32 inode, LinnInode *inodeData)
Constructor function.
Definition: LinnDirectory.cpp:25
LinnInode::block
le32 block[LINN_INODE_BLOCKS]
Pointers to blocks.
Definition: LinnInode.h:104
FileSystem::InvalidArgument
@ InvalidArgument
Definition: FileSystem.h:55
Assert.h
LinnDirectoryEntry
Struct of an directory entry in LinnFS.
Definition: LinnDirectoryEntry.h:44
LinnDirectory::lookup
virtual File * lookup(const char *name)
Retrieves a File pointer for the given entry name.
Definition: LinnDirectory.cpp:96
MemoryBlock.h
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
LinnDirectory::m_fs
LinnFileSystem * m_fs
Filesystem pointer.
Definition: LinnDirectory.h:108
Dirent::type
FileSystem::FileType type
Type of file.
Definition: Directory.h:45
LinnFileSystem::getInode
LinnInode * getInode(u32 inodeNum)
Read an inode from the filesystem.
Definition: LinnFileSystem.cpp:83
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
LinnDirectory::m_inodeData
LinnInode * m_inodeData
Inode which describes the directory.
Definition: LinnDirectory.h:111
File::m_size
Size m_size
Size of the file, in bytes.
Definition: File.h:148
LinnDirectory.h
LinnSuperBlock
Linnenbank Filesystem (LinnFS) super block.
Definition: LinnSuperBlock.h:113
LinnFile
Represents a file on a mounted LinnFS filesystem.
Definition: LinnFile.h:38
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
LinnInode
Structure of an inode on the disk in the LinnFS filesystem.
Definition: LinnInode.h:92
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
FileSystem::RegularFile
@ RegularFile
Definition: FileSystem.h:72
FileSystem::DirectoryFile
@ DirectoryFile
Definition: FileSystem.h:73
LinnDirectory
Represents an directory on a LinnFS filesystem.
Definition: LinnDirectory.h:44
LINN_INODE_DIR_BLOCKS
#define LINN_INODE_DIR_BLOCKS
Direct blocks.
Definition: LinnInode.h:49
LinnDirectory::getLinnDirectoryEntry
bool getLinnDirectoryEntry(LinnDirectoryEntry *dent, const char *name)
Retrieve a directory entry.
Definition: LinnDirectory.cpp:129
LinnInode::mode
le16 mode
Access permissions, as an FileMode.
Definition: LinnInode.h:95
assert
#define assert(exp)
Insert program diagnostics.
Definition: assert.h:60
entry
u32 entry[]
Definition: IntelACPI.h:64
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
String.h
LinnInode::type
le16 type
Type of file, as an FileType.
Definition: LinnInode.h:94
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.
FileSystem::NotFound
@ NotFound
Definition: FileSystem.h:56
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
LinnFile.h
LinnDirectoryEntry::name
char name[LINN_DIRENT_NAME_LEN]
File name.
Definition: LinnDirectoryEntry.h:53
LinnInode::size
le32 size
Size in bytes.
Definition: LinnInode.h:98
LinnDirectoryEntry::inode
le32 inode
Inode number.
Definition: LinnDirectoryEntry.h:47
LinnFileSystem::getSuperBlock
LinnSuperBlock * getSuperBlock()
Retrieve the superblock pointer.
Definition: LinnFileSystem.h:92
String::equals
virtual bool equals(const String &str) const
Alias for compareTo().
Definition: String.cpp:262
Dirent
Describes an entry inside a Directory.
Definition: Directory.h:39
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
LinnDirectoryEntry
struct LinnDirectoryEntry LinnDirectoryEntry
Struct of an directory entry in LinnFS.
FileSystem::PermissionDenied
@ PermissionDenied
Definition: FileSystem.h:59
LINN_DIRENT_PER_BLOCK
#define LINN_DIRENT_PER_BLOCK(sb)
Calculates the number of LinnDirectoryEntry's fitting in one block.
Definition: LinnDirectoryEntry.h:35