FreeNOS
FileDescriptor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 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 "FileDescriptor.h"
19 
21  : m_array(ZERO)
22 {
23 }
24 
26 {
27  count = m_count;
28  return m_array;
29 }
30 
32  const Size count)
33 {
34  m_array = array;
35  m_count = count;
36 }
37 
39  const ProcessID filesystem,
40  Size & index)
41 {
42  // Insert into file descriptor table
43  for (index = 0; index < m_count; index++)
44  {
45  if (!m_array[index].open)
46  {
47  m_array[index].open = true;
48  m_array[index].position = 0;
49  m_array[index].inode = inode;
50  m_array[index].pid = filesystem;
52  }
53  }
54 
56 }
57 
59 {
60  if (index >= m_count)
61  {
62  return ZERO;
63  }
64 
65  return &m_array[index];
66 }
67 
69 {
70  if (index >= m_count)
71  {
73  }
74 
75  m_array[index].open = false;
77 }
FileDescriptor::setArray
void setArray(Entry *array, const Size count)
Assign entry table.
Definition: FileDescriptor.cpp:31
FileDescriptor::Entry::position
Size position
< Process identifier of the filesystem
Definition: FileDescriptor.h:50
FileDescriptor::getArray
Entry * getArray(Size &count)
Get entry table.
Definition: FileDescriptor.cpp:25
FileDescriptor::Entry::open
bool open
< Current position indicator.
Definition: FileDescriptor.h:51
FileDescriptor::m_count
Size m_count
Number of entries in the array.
Definition: FileDescriptor.h:126
FileDescriptor::InvalidArgument
@ InvalidArgument
Definition: FileDescriptor.h:60
ProcessID
u32 ProcessID
Process Identification Number.
Definition: Types.h:140
open
int open(const char *path, int oflag,...)
Open file relative to directory file descriptor.
Definition: open.cpp:26
FileDescriptor.h
FileDescriptor::getEntry
Entry * getEntry(const Size index)
Retrieve a file descriptor Entry.
Definition: FileDescriptor.cpp:58
FileDescriptor::Entry::inode
u32 inode
Definition: FileDescriptor.h:48
FileDescriptor::Entry
Describes a single file opened by a user process.
Definition: FileDescriptor.h:46
FileDescriptor::Result
Result
Result code.
Definition: FileDescriptor.h:57
FileDescriptor::closeEntry
Result closeEntry(const Size index)
Remove file descriptor entry.
Definition: FileDescriptor.cpp:68
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
FileDescriptor::FileDescriptor
FileDescriptor()
Constructor.
Definition: FileDescriptor.cpp:20
FileDescriptor::Success
@ Success
Definition: FileDescriptor.h:59
FileDescriptor::openEntry
Result openEntry(const u32 inode, const ProcessID filesystem, Size &index)
Add new file descriptor entry.
Definition: FileDescriptor.cpp:38
FileDescriptor::OutOfFiles
@ OutOfFiles
Definition: FileDescriptor.h:61
FileDescriptor::Entry::pid
ProcessID pid
< Inode number of the file
Definition: FileDescriptor.h:49
FileDescriptor::m_array
Entry * m_array
Pointer to array of entries.
Definition: FileDescriptor.h:123
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43