Kernel/VFS - Better error reporting in open
[tpg/acess2.git] / KernelLand / Kernel / vfs / memfile.c
1 /* 
2  * Acess 2
3  * Virtual File System
4  * - Memory Pseudo Files
5  */
6 #include <acess.h>
7 #include <vfs.h>
8
9 // === PROTOTYPES ===
10 tVFS_Node       *VFS_MemFile_Create(const char *Path);
11 void    VFS_MemFile_Close(tVFS_Node *Node);
12 size_t  VFS_MemFile_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags);
13 size_t  VFS_MemFile_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags);
14
15 // === GLOBALS ===
16 tVFS_NodeType   gVFS_MemFileType = {
17         .Close = VFS_MemFile_Close,
18         .Read = VFS_MemFile_Read,
19         .Write = VFS_MemFile_Write
20         };
21
22 // === CODE ===
23 /**
24  * \fn tVFS_Node *VFS_MemFile_Create(const char *Path)
25  */
26 tVFS_Node *VFS_MemFile_Create(const char *Path)
27 {
28         Uint    base, size;
29         const char      *str = Path;
30         tVFS_Node       *ret;
31         
32         str++;  // Eat '$'
33         
34         // Read Base address
35         base = 0;
36         for( ; ('0' <= *str && *str <= '9') || ('A' <= *str && *str <= 'F'); str++ )
37         {
38                 base *= 16;
39                 if('A' <= *str && *str <= 'F')
40                         base += *str - 'A' + 10;
41                 else
42                         base += *str - '0';
43         }
44         
45         // Check separator
46         if(*str++ != ':')       return NULL;
47         
48         // Read buffer size
49         size = 0;
50         for( ; ('0' <= *str && *str <= '9') || ('A' <= *str && *str <= 'F'); str++ )
51         {
52                 size *= 16;
53                 if('A' <= *str && *str <= 'F')
54                         size += *str - 'A' + 10;
55                 else
56                         size += *str - '0';
57         }
58         
59         // Check for NULL byte
60         if(*str != '\0')        return NULL;
61         
62         // Allocate and fill node
63         ret = malloc(sizeof(tVFS_Node));
64         memset(ret, 0, sizeof(tVFS_Node));
65         
66         // State
67         ret->Inode = base;
68         ret->ImplPtr = (void*)base;
69         ret->Size = size;
70         
71         // ACLs
72         ret->NumACLs = 1;
73         ret->ACLs = &gVFS_ACL_EveryoneRWX;
74         
75         // Functions
76         ret->Type = &gVFS_MemFileType;
77         
78         return ret;
79 }
80
81 /**
82  * \fn void VFS_MemFile_Close(tVFS_Node *Node)
83  * \brief Dereference and clean up a memory file
84  */
85 void VFS_MemFile_Close(tVFS_Node *Node)
86 {
87         Node->ReferenceCount --;
88         if( Node->ReferenceCount == 0 ) {
89                 Node->ImplPtr = NULL;
90                 free(Node);
91         }
92 }
93
94 /**
95  * \brief Read from a memory file
96  */
97 size_t VFS_MemFile_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
98 {
99         // Check for use of free'd file
100         if(Node->ImplPtr == NULL)       return 0;
101         
102         // Check for out of bounds read
103         if(Offset > Node->Size) return 0;
104         
105         // Truncate data read if needed
106         if(Length > Node->Size)
107                 Length = Node->Size;
108         if(Offset + Length > Node->Size)
109                 Length = Node->Size - Offset;
110         
111         // Copy Data
112         memcpy(Buffer, (Uint8*)Node->ImplPtr + Offset, Length);
113         
114         return Length;
115 }
116
117 /**
118  * \brief Write to a memory file
119  */
120 size_t VFS_MemFile_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
121 {
122         // Check for use of free'd file
123         if(Node->ImplPtr == NULL)       return 0;
124         
125         // Check for out of bounds read
126         if(Offset > Node->Size) return 0;
127         
128         // Truncate data read if needed
129         if(Length > Node->Size)
130                 Length = Node->Size;
131         if(Offset + Length > Node->Size)
132                 Length = Node->Size - Offset;
133         
134         // Copy Data
135         memcpy((Uint8*)Node->ImplPtr + Offset, Buffer, Length);
136         
137         return Length;
138 }

UCC git Repository :: git.ucc.asn.au