FreeNOS
Directory.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 <String.h>
19#include <ListIterator.h>
20#include <MemoryBlock.h>
21#include "Directory.h"
22
24 : File(inode, FileSystem::DirectoryFile)
25{
28}
29
31{
33 delete i.current();
34
35 entries.clear();
36}
37
39 Size & size,
40 const Size offset)
41{
42 Size bytes = 0;
43
44 // Loop our list of Dirents
45 for (ListIterator<Dirent *> i(&entries); i.hasCurrent(); i++)
46 {
47 // Can we read another entry?
48 if (bytes + sizeof(Dirent) <= size)
49 {
50 buffer.write(i.current(), sizeof(Dirent), bytes);
51 bytes += sizeof(Dirent);
52 }
53 else break;
54 }
55
56 // Report results
57 size = bytes;
59}
60
61File * Directory::lookup(const char *name)
62{
63 return ZERO;
64}
65
67{
68 Dirent *d;
69
70 // Only insert if not already in
71 if (!get(name))
72 {
73 // Create an fill entry object
74 d = new Dirent;
75 assert(d != NULL);
76 MemoryBlock::copy(d->name, (char *)name, DIRENT_LEN);
77 d->type = type;
78 entries.append(d);
79 m_size += sizeof(*d);
80 }
81}
82
83void Directory::remove(const char *name)
84{
85 const String str(name, false);
86
87 for (ListIterator<Dirent *> i(&entries); i.hasCurrent(); i++)
88 {
89 if (str.compareTo(i.current()->name) == 0)
90 {
91 delete i.current();
92 i.remove();
93 m_size -= sizeof(Dirent);
94 return;
95 }
96 }
97}
98
100{
101 for (ListIterator<Dirent *> i(entries); i.hasCurrent(); i++)
102 delete i.current();
103
104 entries.clear();
105}
106
107Dirent * Directory::get(const char *name)
108{
109 const String str(name, false);
110
111 for (ListIterator<Dirent *> i(&entries); i.hasCurrent(); i++)
112 {
113 if (str.compareTo(i.current()->name) == 0)
114 {
115 return i.current();
116 }
117 }
118 return (Dirent *) ZERO;
119}
u8 type
Definition IntelACPI.h:0
virtual File * lookup(const char *name)
Retrieve a File from storage.
Definition Directory.cpp:61
void remove(const char *name)
Remove a directory entry.
Definition Directory.cpp:83
void clear()
Clears the internal list of entries.
Definition Directory.cpp:99
Dirent * get(const char *name)
Retrieve a directory entry by it's name.
List< Dirent * > entries
List of directory entries.
Definition Directory.h:176
void insert(FileSystem::FileType type, const char *name)
Insert a new directory entry.
Definition Directory.cpp:66
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read directory entries.
Definition Directory.cpp:38
Directory(const u32 inode)
Constructor.
Definition Directory.cpp:23
virtual ~Directory()
Destructor.
Definition Directory.cpp:30
Represents a file present on a FileSystem.
Definition File.h:40
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
Iterate through a List.
virtual bool hasCurrent() const
Check if there is a current item on the List.
void append(T t)
Insert an item at the end of the list.
Definition List.h:139
virtual void clear()
Clears the entire List.
Definition List.h:232
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Abstraction of strings.
Definition String.h:42
virtual int compareTo(const String &str) const
Compares this String to the given String.
Definition String.cpp:231
#define DIRENT_LEN
Maximum length of a filename.
Definition Directory.h:34
#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
FileType
All possible filetypes.
Definition FileSystem.h:71
Result
Result code for filesystem Actions.
Definition FileSystem.h:53
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