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 {
32  for (ListIterator<Dirent *> i(entries); i.hasCurrent(); i++)
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;
58  return FileSystem::Success;
59 }
60 
61 File * 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 
83 void 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 
107 Dirent * 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 }
MemoryBlock::copy
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Definition: MemoryBlock.cpp:36
List::clear
virtual void clear()
Clears the entire List.
Definition: List.h:232
Dirent::name
char name[DIRENT_LEN]
Name of the file.
Definition: Directory.h:42
Directory::lookup
virtual File * lookup(const char *name)
Retrieve a File from storage.
Definition: Directory.cpp:61
FileSystem::FileType
FileType
All possible filetypes.
Definition: FileSystem.h:70
Dirent
struct Dirent Dirent
Describes an entry inside a Directory.
String
Abstraction of strings.
Definition: String.h:41
Directory::insert
void insert(FileSystem::FileType type, const char *name)
Insert a new directory entry.
Definition: Directory.cpp:66
Directory::get
Dirent * get(const char *name)
Retrieve a directory entry by it's name.
Definition: Directory.cpp:107
MemoryBlock.h
FileSystem::Success
@ Success
Definition: FileSystem.h:54
String::compareTo
virtual int compareTo(const String &str) const
Compares this String to the given String.
Definition: String.cpp:231
Directory::~Directory
virtual ~Directory()
Destructor.
Definition: Directory.cpp:30
File
Represents a file present on a FileSystem.
Definition: File.h:39
Directory::read
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read directory entries.
Definition: Directory.cpp:38
Dirent::type
FileSystem::FileType type
Type of file.
Definition: Directory.h:45
File::m_size
Size m_size
Size of the file, in bytes.
Definition: File.h:148
ListIterator::hasCurrent
virtual bool hasCurrent() const
Check if there is a current item on the List.
Definition: ListIterator.h:104
Directory::Directory
Directory(const u32 inode)
Constructor.
Definition: Directory.cpp:23
List::append
void append(T t)
Insert an item at the end of the list.
Definition: List.h:139
IOBuffer
Abstract Input/Output buffer.
Definition: IOBuffer.h:37
Directory::entries
List< Dirent * > entries
List of directory entries.
Definition: Directory.h:176
NULL
#define NULL
NULL means zero.
Definition: Macros.h:39
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
FileSystem::DirectoryFile
@ DirectoryFile
Definition: FileSystem.h:73
Directory::clear
void clear()
Clears the internal list of entries.
Definition: Directory.cpp:99
ListIterator.h
assert
#define assert(exp)
Insert program diagnostics.
Definition: assert.h:60
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
String.h
FileSystem
Definition: FileSystem.h:31
Directory.h
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
DIRENT_LEN
#define DIRENT_LEN
Maximum length of a filename.
Definition: Directory.h:34
type
u8 type
Definition: IntelACPI.h:63
Dirent
Describes an entry inside a Directory.
Definition: Directory.h:39
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
Directory::remove
void remove(const char *name)
Remove a directory entry.
Definition: Directory.cpp:83
ListIterator
Iterate through a List.
Definition: ListIterator.h:37