Fixed places where char* was used in place of const char*
[tpg/acess2.git] / Kernel / vfs / io.c
1 /*
2  * AcessMicro VFS
3  * - File IO Passthru's
4  */
5 #include <acess.h>
6 #include "vfs.h"
7 #include "vfs_int.h"
8
9 #define DEBUG   0
10
11 #if DEBUG
12 #else
13 # undef ENTER
14 # undef LOG
15 # undef LEAVE
16 # define ENTER(...)
17 # define LOG(...)
18 # define LEAVE(...)
19 #endif
20
21 // === CODE ===
22 /**
23  * \fn Uint64 VFS_Read(int FD, Uint64 Length, void *Buffer)
24  * \brief Read data from a node (file)
25  */
26 Uint64 VFS_Read(int FD, Uint64 Length, void *Buffer)
27 {
28         tVFS_Handle     *h;
29         Uint64  ret;
30         
31         ENTER("iFD XLength pBuffer", FD, Length, Buffer);
32         
33         h = VFS_GetHandle(FD);
34         if(!h)  return -1;
35         
36         if( !(h->Mode & VFS_OPENFLAG_READ) || h->Node->Flags & VFS_FFLAG_DIRECTORY ) {
37                 LEAVE('i', -1);
38                 return -1;
39         }
40
41         if(!h->Node->Read) {
42                 LEAVE('i', 0);
43                 return 0;
44         }
45         
46         ret = h->Node->Read(h->Node, h->Position, Length, Buffer);
47         if(ret == -1) {
48                 LEAVE('i', -1);
49                 return -1;
50         }
51         
52         h->Position += ret;
53         LEAVE('X', ret);
54         return ret;
55 }
56
57 /**
58  * \fn Uint64 VFS_ReadAt(int FD, Uint64 Offset, Uint64 Length, void *Buffer)
59  * \brief Read data from a given offset (atomic)
60  */
61 Uint64 VFS_ReadAt(int FD, Uint64 Offset, Uint64 Length, void *Buffer)
62 {
63         tVFS_Handle     *h;
64         Uint64  ret;
65         
66         h = VFS_GetHandle(FD);
67         if(!h)  return -1;
68         
69         if( !(h->Mode & VFS_OPENFLAG_READ) )    return -1;
70         if( h->Node->Flags & VFS_FFLAG_DIRECTORY )      return -1;
71
72         if(!h->Node->Read) {
73                 Warning("VFS_ReadAt - Node %p, does not have a read method", h->Node);
74                 return 0;
75         }
76         ret = h->Node->Read(h->Node, Offset, Length, Buffer);
77         if(ret == -1)   return -1;
78         return ret;
79 }
80
81 /**
82  * \fn Uint64 VFS_Write(int FD, Uint64 Length, const void *Buffer)
83  * \brief Read data from a node (file)
84  */
85 Uint64 VFS_Write(int FD, Uint64 Length, const void *Buffer)
86 {
87         tVFS_Handle     *h;
88         Uint64  ret;
89         
90         h = VFS_GetHandle(FD);
91         if(!h)  return -1;
92         
93         if( !(h->Mode & VFS_OPENFLAG_WRITE) )   return -1;
94         if( h->Node->Flags & VFS_FFLAG_DIRECTORY )      return -1;
95
96         if(!h->Node->Write)     return 0;
97         
98         // TODO: This is a hack, I need to change VFS_Node to have "const void*"
99         ret = h->Node->Write(h->Node, h->Position, Length, (void*)Buffer);
100         if(ret == -1)   return -1;
101         h->Position += ret;
102         return ret;
103 }
104
105 /**
106  * \fn Uint64 VFS_WriteAt(int FD, Uint64 Offset, Uint64 Length, const void *Buffer)
107  * \brief Write data to a file at a given offset
108  */
109 Uint64 VFS_WriteAt(int FD, Uint64 Offset, Uint64 Length, const void *Buffer)
110 {
111         tVFS_Handle     *h;
112         Uint64  ret;
113         
114         h = VFS_GetHandle(FD);
115         if(!h)  return -1;
116         
117         if( !(h->Mode & VFS_OPENFLAG_WRITE) )   return -1;
118         if( h->Node->Flags & VFS_FFLAG_DIRECTORY )      return -1;
119
120         if(!h->Node->Write)     return 0;
121         // TODO: This is a hack, I need to change VFS_Node to have "const void*"
122         ret = h->Node->Write(h->Node, Offset, Length, (void*)Buffer);
123         if(ret == -1)   return -1;
124         return ret;
125 }
126
127 /**
128  * \fn Uint64 VFS_Tell(int FD)
129  * \brief Returns the current file position
130  */
131 Uint64 VFS_Tell(int FD)
132 {
133         tVFS_Handle     *h;
134         
135         h = VFS_GetHandle(FD);
136         if(!h)  return -1;
137         
138         return h->Position;
139 }
140
141 /**
142  * \fn int VFS_Seek(int FD, Sint64 Offset, int Whence)
143  * \brief Seek to a new location
144  * \param FD    File descriptor
145  * \param Offset        Where to go
146  * \param Whence        From where
147  */
148 int VFS_Seek(int FD, Sint64 Offset, int Whence)
149 {
150         tVFS_Handle     *h;
151         
152         h = VFS_GetHandle(FD);
153         if(!h)  return -1;
154         
155         //Log_Debug("VFS", "VFS_Seek: (fd=0x%x, Offset=0x%llx, Whence=%i)",
156         //      FD, Offset, Whence);
157         
158         // Set relative to current position
159         if(Whence == 0) {
160                 h->Position += Offset;
161                 return 0;
162         }
163         
164         // Set relative to end of file
165         if(Whence < 0) {
166                 h->Position = h->Node->Size - Offset;
167                 return 0;
168         }
169         
170         // Set relative to start of file
171         h->Position = Offset;
172         return 0;
173 }
174
175 /**
176  * \fn int VFS_IOCtl(int FD, int ID, void *Buffer)
177  * \brief Call an IO Control on a file
178  */
179 int VFS_IOCtl(int FD, int ID, void *Buffer)
180 {
181         tVFS_Handle     *h;
182         
183         h = VFS_GetHandle(FD);
184         if(!h)  return -1;
185
186         if(!h->Node->IOCtl)     return -1;
187         return h->Node->IOCtl(h->Node, ID, Buffer);
188 }
189
190 /**
191  * \fn int VFS_FInfo(int FD, tFInfo *Dest, int MaxACLs)
192  * \brief Retrieve file information
193  * \return Number of ACLs stored
194  */
195 int VFS_FInfo(int FD, tFInfo *Dest, int MaxACLs)
196 {
197         tVFS_Handle     *h;
198          int    max;
199         
200         h = VFS_GetHandle(FD);
201         if(!h)  return -1;
202         
203         Dest->uid = h->Node->UID;
204         Dest->gid = h->Node->GID;
205         Dest->size = h->Node->Size;
206         Dest->atime = h->Node->ATime;
207         Dest->ctime = h->Node->MTime;
208         Dest->mtime = h->Node->CTime;
209         Dest->numacls = h->Node->NumACLs;
210         
211         Dest->flags = 0;
212         if(h->Node->Flags & VFS_FFLAG_DIRECTORY)        Dest->flags |= 0x10;
213         if(h->Node->Flags & VFS_FFLAG_SYMLINK)  Dest->flags |= 0x20;
214         
215         max = (MaxACLs < h->Node->NumACLs) ? MaxACLs : h->Node->NumACLs;
216         memcpy(&Dest->acls, h->Node->ACLs, max*sizeof(tVFS_ACL));
217         
218         return max;
219 }
220
221 // === EXPORTS ===
222 EXPORT(VFS_Read);
223 EXPORT(VFS_Write);
224 EXPORT(VFS_ReadAt);
225 EXPORT(VFS_WriteAt);
226 EXPORT(VFS_IOCtl);
227 EXPORT(VFS_Seek);
228 EXPORT(VFS_Tell);

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