cc546f48b0af834ac8005cb58a79da8e81462802
[tpg/acess2.git] / Kernel / vfs / open.c
1 /*
2  * AcessMicro VFS
3  * - Open, Close and ChDir
4  */
5 #define DEBUG   1
6 #include <acess.h>
7 #include <mm_virt.h>
8 #include "vfs.h"
9 #include "vfs_int.h"
10 #include "vfs_ext.h"
11
12 // === CONSTANTS ===
13 #define OPEN_MOUNT_ROOT 1
14 #define MAX_KERNEL_FILES        128
15 #define MAX_PATH_SLASHES        256
16
17 // === IMPORTS ===
18 extern tVFS_Node        gVFS_MemRoot;
19 extern tVFS_Mount       *gVFS_RootMount;
20
21 // === GLOBALS ===
22 tVFS_Handle     *gaUserHandles = (void*)MM_PPD_VFS;
23 tVFS_Handle     *gaKernelHandles = (void*)MM_KERNEL_VFS;
24
25 // === CODE ===
26 /**
27  * \fn char *VFS_GetAbsPath(const char *Path)
28  * \brief Create an absolute path from a relative one
29  */
30 char *VFS_GetAbsPath(const char *Path)
31 {
32         char    *ret;
33          int    pathLen = strlen(Path);
34         char    *pathComps[MAX_PATH_SLASHES];
35         char    *tmpStr;
36         int             iPos = 0;
37         int             iPos2 = 0;
38         char    *chroot = CFGPTR(CFG_VFS_CHROOT);
39          int    chrootLen;
40         char    *cwd = CFGPTR(CFG_VFS_CWD);
41          int    cwdLen;
42         
43         ENTER("sPath", Path);
44         
45         // Memory File
46         if(Path[0] == '$') {
47                 ret = malloc(strlen(Path)+1);
48                 if(!ret) {
49                         Warning("VFS_GetAbsPath - malloc() returned NULL");
50                         return NULL;
51                 }
52                 strcpy(ret, Path);
53                 LEAVE('p', ret);
54                 return ret;
55         }
56         
57         // - Fetch ChRoot
58         if( chroot == NULL ) {
59                 chroot = "";
60                 chrootLen = 0;
61         } else {
62                 chrootLen = strlen(chroot);
63         }
64         
65         // Check if the path is already absolute
66         if(Path[0] == '/') {
67                 ret = malloc(pathLen + 1);
68                 if(!ret) {
69                         Warning("VFS_GetAbsPath - malloc() returned NULL");
70                         return NULL;
71                 }
72                 strcpy(ret, Path);
73         } else {
74                 if(cwd == NULL) {
75                         cwd = "/";
76                         cwdLen = 1;
77                 }
78                 else {
79                         cwdLen = strlen(cwd);
80                 }
81                 // Prepend the current directory
82                 ret = malloc( cwdLen + 1 + pathLen + 1 );
83                 strcpy(ret, cwd);
84                 ret[cwdLen] = '/';
85                 strcpy(&ret[cwdLen+1], Path);
86                 //Log("ret = '%s'\n", ret);
87         }
88         
89         // Parse Path
90         pathComps[iPos++] = tmpStr = ret+1;
91         while(*tmpStr)
92         {
93                 if(*tmpStr++ == '/')
94                 {
95                         pathComps[iPos++] = tmpStr;
96                         if(iPos == MAX_PATH_SLASHES) {
97                                 LOG("Path '%s' has too many elements", Path);
98                                 free(ret);
99                                 LEAVE('n');
100                                 return NULL;
101                         }
102                 }
103         }
104         pathComps[iPos] = NULL;
105         
106         // Cleanup
107         iPos2 = iPos = 0;
108         while(pathComps[iPos])
109         {
110                 tmpStr = pathComps[iPos];
111                 // Always Increment iPos
112                 iPos++;
113                 // ..
114                 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )
115                 {
116                         if(iPos2 != 0)
117                                 iPos2 --;
118                         continue;
119                 }
120                 // .
121                 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )
122                 {
123                         continue;
124                 }
125                 // Empty
126                 if(tmpStr[0] == '/' || tmpStr[0] == '\0')
127                 {
128                         continue;
129                 }
130                 
131                 // Set New Position
132                 pathComps[iPos2] = tmpStr;
133                 iPos2++;
134         }
135         pathComps[iPos2] = NULL;
136         
137         // Build New Path
138         iPos2 = 1;      iPos = 0;
139         ret[0] = '/';
140         while(pathComps[iPos])
141         {
142                 tmpStr = pathComps[iPos];
143                 while(*tmpStr && *tmpStr != '/')
144                 {
145                         ret[iPos2++] = *tmpStr;
146                         tmpStr++;
147                 }
148                 ret[iPos2++] = '/';
149                 iPos++;
150         }
151         if(iPos2 > 1)
152                 ret[iPos2-1] = 0;
153         else
154                 ret[iPos2] = 0;
155         
156         
157         // Prepend the chroot
158         tmpStr = malloc(chrootLen + strlen(ret) + 1);
159         strcpy( tmpStr, chroot );
160         strcpy( tmpStr+chrootLen, ret );
161         free(ret);
162         ret = tmpStr;
163         
164         LEAVE('s', ret);
165         //Log("VFS_GetAbsPath: RETURN '%s'", ret);
166         return ret;
167 }
168
169 /**
170  * \fn char *VFS_ParsePath(const char *Path, char **TruePath)
171  * \brief Parses a path, resolving sysmlinks and applying permissions
172  */
173 tVFS_Node *VFS_ParsePath(const char *Path, char **TruePath)
174 {
175         tVFS_Mount      *mnt;
176         tVFS_Mount      *longestMount = gVFS_RootMount; // Root is first
177          int    cmp, retLength = 0;
178          int    ofs, nextSlash;
179         tVFS_Node       *curNode, *tmpNode;
180         char    *tmp;
181         
182         ENTER("sPath pTruePath", Path, TruePath);
183         
184         // Memory File
185         if(Path[0] == '$') {
186                 if(TruePath) {
187                         *TruePath = malloc(strlen(Path)+1);
188                         strcpy(*TruePath, Path);
189                 }
190                 curNode = gVFS_MemRoot.FindDir(&gVFS_MemRoot, Path);
191                 LEAVE('p', curNode);
192                 return curNode;
193         }
194         
195         // For root we always fast return
196         if(Path[0] == '/' && Path[1] == '\0') {
197                 if(TruePath) {
198                         *TruePath = malloc( gVFS_RootMount->MountPointLen+1 );
199                         strcpy(*TruePath, gVFS_RootMount->MountPoint);
200                 }
201                 LEAVE('p', gVFS_RootMount->RootNode);
202                 return gVFS_RootMount->RootNode;
203         }
204         
205         // Check if there is an`ything mounted
206         if(!gVFS_Mounts) {
207                 Warning("WTF! There's nothing mounted?");
208                 return NULL;
209         }
210         
211         // Find Mountpoint
212         for(mnt = gVFS_Mounts;
213                 mnt;
214                 mnt = mnt->Next)
215         {
216                 // Quick Check
217                 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
218                         continue;
219                 // Length Check - If the length is smaller than the longest match sofar
220                 if(mnt->MountPointLen < longestMount->MountPointLen)    continue;
221                 // String Compare
222                 cmp = strcmp(Path, mnt->MountPoint);
223                 
224                 #if OPEN_MOUNT_ROOT
225                 // Fast Break - Request Mount Root
226                 if(cmp == 0) {
227                         if(TruePath) {
228                                 *TruePath = malloc( mnt->MountPointLen+1 );
229                                 strcpy(*TruePath, mnt->MountPoint);
230                         }
231                         LEAVE('p', mnt->RootNode);
232                         return mnt->RootNode;
233                 }
234                 #endif
235                 // Not a match, continue
236                 if(cmp != '/')  continue;
237                 longestMount = mnt;
238         }
239         
240         // Sanity Check
241         /*if(!longestMount) {
242                 Log("VFS_ParsePath - ERROR: No Root Node\n");
243                 return NULL;
244         }*/
245         
246         // Save to shorter variable
247         mnt = longestMount;
248         
249         LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
250         
251         // Initialise String
252         if(TruePath)
253         {
254                 *TruePath = malloc( mnt->MountPointLen+1 );
255                 strcpy(*TruePath, mnt->MountPoint);
256                 retLength = mnt->MountPointLen;
257         }
258         
259         curNode = mnt->RootNode;
260         curNode->ReferenceCount ++;     
261         // Parse Path
262         ofs = mnt->MountPointLen+1;
263         for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
264         {
265                 char    pathEle[nextSlash+1];
266                 
267                 // Empty String
268                 if(nextSlash == 0)      continue;
269                 
270                 memcpy(pathEle, &Path[ofs], nextSlash);
271                 pathEle[nextSlash] = 0;
272         
273                 // Check permissions on root of filesystem
274                 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
275                         if(curNode->Close)      curNode->Close( curNode );
276                         if(TruePath) {
277                                 free(*TruePath);
278                                 *TruePath = NULL;
279                         }
280                         //Log("Permissions fail on '%s'", Path);
281                         LEAVE('n');
282                         return NULL;
283                 }
284                 
285                 // Check if the node has a FindDir method
286                 if( !curNode->FindDir )
287                 {
288                         if(curNode->Close)      curNode->Close(curNode);
289                         if(TruePath) {
290                                 free(*TruePath);
291                                 *TruePath = NULL;
292                         }
293                         //Log("FindDir fail on '%s'", Path);
294                         LEAVE('n');
295                         return NULL;
296                 }
297                 LOG("FindDir(%p, '%s')", curNode, pathEle);
298                 // Get Child Node
299                 tmpNode = curNode->FindDir(curNode, pathEle);
300                 LOG("tmpNode = %p", tmpNode);
301                 if(curNode->Close)      curNode->Close(curNode);
302                 curNode = tmpNode;
303                 
304                 // Error Check
305                 if(!curNode) {
306                         LOG("Node '%s' not found in dir '%s'", pathEle, Path);
307                         if(TruePath) {
308                                 free(*TruePath);
309                                 *TruePath = NULL;
310                         }
311                         //Log("Child fail on '%s' ('%s)", Path, pathEle);
312                         LEAVE('n');
313                         return NULL;
314                 }
315                 
316                 // Handle Symbolic Links
317                 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
318                         if(TruePath) {
319                                 free(*TruePath);
320                                 *TruePath = NULL;
321                         }
322                         if(!curNode->Read) {
323                                 Warning("VFS_ParsePath - Read of node %p is NULL (%s)",
324                                         curNode, Path);
325                                 if(curNode->Close)      curNode->Close(curNode);
326                                 // No need to free *TruePath, see above
327                                 LEAVE('n');
328                                 return NULL;
329                         }
330                         
331                         tmp = malloc( curNode->Size + 1 );
332                         if(!tmp) {
333                                 Log_Warning("VFS", "VFS_ParsePath - Malloc failure");
334                                 // No need to free *TruePath, see above
335                                 LEAVE('n');
336                                 return NULL;
337                         }
338                         curNode->Read( curNode, 0, curNode->Size, tmp );
339                         tmp[ curNode->Size ] = '\0';
340                         
341                         // Parse Symlink Path
342                         curNode = VFS_ParsePath(tmp, TruePath);
343                         if(TruePath)
344                                 LOG("VFS", "*TruePath='%s'", *TruePath);
345                         
346                         // Error Check
347                         if(!curNode) {
348                                 Log_Debug("VFS", "Symlink fail '%s'", tmp);
349                                 free(tmp);      // Free temp string
350                                 if(TruePath)    free(TruePath);
351                                 LEAVE('n');
352                                 return NULL;
353                         }
354                         
355                         // Free temp link
356                         free(tmp);
357                         
358                         // Set Path Variable
359                         if(TruePath) {
360                                 retLength = strlen(*TruePath);
361                         }
362                         
363                         continue;
364                 }
365                 
366                 // Handle Non-Directories
367                 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
368                 {
369                         Warning("VFS_ParsePath - File in directory context");
370                         if(TruePath)    free(*TruePath);
371                         LEAVE('n');
372                         return NULL;
373                 }
374                 
375                 // Check if path needs extending
376                 if(!TruePath)   continue;
377                 
378                 // Increase buffer space
379                 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
380                 // Check if allocation succeeded
381                 if(!tmp) {
382                         Warning("VFS_ParsePath -  Unable to reallocate true path buffer");
383                         free(*TruePath);
384                         *TruePath = NULL;
385                         if(curNode->Close)      curNode->Close(curNode);
386                         LEAVE('n');
387                         return NULL;
388                 }
389                 *TruePath = tmp;
390                 // Append to path
391                 (*TruePath)[retLength] = '/';
392                 strcpy(*TruePath+retLength+1, pathEle);
393                 
394                 LOG("*TruePath = '%s'", *TruePath);
395                 
396                 // - Extend Path
397                 retLength += nextSlash + 1;
398         }
399         
400         if( !curNode->FindDir ) {
401                 if(curNode->Close)      curNode->Close(curNode);
402                 if(TruePath) {
403                         free(*TruePath);
404                         *TruePath = NULL;
405                 }
406                 Log("FindDir fail on '%s'", Path);
407                 LEAVE('n');
408                 return NULL;
409         }
410         
411         // Get last node
412         LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
413         tmpNode = curNode->FindDir(curNode, &Path[ofs]);
414         LOG("tmpNode = %p", tmpNode);
415         if(curNode->Close)      curNode->Close(curNode);
416         // Check if file was found
417         if(!tmpNode) {
418                 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
419                 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
420                 if(TruePath)    free(*TruePath);
421                 if(curNode->Close)      curNode->Close(curNode);
422                 LEAVE('n');
423                 return NULL;
424         }
425         
426         if(TruePath)
427         {
428                 // Increase buffer space
429                 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
430                 // Check if allocation succeeded
431                 if(!tmp) {
432                         Warning("VFS_ParsePath -  Unable to reallocate true path buffer");
433                         free(*TruePath);
434                         if(tmpNode->Close)      tmpNode->Close(curNode);
435                         LEAVE('n');
436                         return NULL;
437                 }
438                 *TruePath = tmp;
439                 // Append to path
440                 (*TruePath)[retLength] = '/';
441                 strcpy(*TruePath + retLength + 1, &Path[ofs]);
442                 // - Extend Path
443                 //retLength += strlen(tmpNode->Name) + 1;
444         }
445         
446         LEAVE('p', tmpNode);
447         return tmpNode;
448 }
449
450 /**
451  * \fn int VFS_Open(char *Path, Uint Mode)
452  * \brief Open a file
453  */
454 int VFS_Open(char *Path, Uint Mode)
455 {
456         tVFS_Node       *node;
457         char    *absPath;
458          int    i;
459         
460         ENTER("sPath xMode", Path, Mode);
461         
462         // Get absolute path
463         absPath = VFS_GetAbsPath(Path);
464         LOG("absPath = \"%s\"", absPath);
465         // Parse path and get mount point
466         node = VFS_ParsePath(absPath, NULL);
467         // Free generated path
468         free(absPath);
469         
470         if(!node) {
471                 LOG("Cannot find node");
472                 LEAVE('i', -1);
473                 return -1;
474         }
475         
476         // Check for symlinks
477         if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
478         {
479                 if( !node->Read ) {
480                         Warning("No read method on symlink");
481                         LEAVE('i', -1);
482                         return -1;
483                 }
484                 absPath = malloc(node->Size+1); // Allocate Buffer
485                 node->Read( node, 0, node->Size, absPath );     // Read Path
486                 
487                 absPath[ node->Size ] = '\0';   // End String
488                 if(node->Close) node->Close( node );    // Close old node
489                 node = VFS_ParsePath(absPath, NULL);    // Get new node
490                 free( absPath );        // Free allocated path
491         }
492         
493         if(!node) {
494                 LOG("Cannot find node");
495                 LEAVE('i', -1);
496                 return -1;
497         }
498         
499         i = 0;
500         i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
501         i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
502         i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
503         
504         LOG("i = 0b%b", i);
505         
506         // Permissions Check
507         if( !VFS_CheckACL(node, i) ) {
508                 if(node->Close) node->Close( node );
509                 Log("VFS_Open: Permissions Failed");
510                 LEAVE('i', -1);
511                 return -1;
512         }
513         
514         // Check for a user open
515         if(Mode & VFS_OPENFLAG_USER)
516         {
517                 // Allocate Buffer
518                 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
519                 {
520                         Uint    addr, size;
521                         size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
522                         for(addr = 0; addr < size; addr += 0x1000)
523                                 MM_Allocate( (Uint)gaUserHandles + addr );
524                         memset( gaUserHandles, 0, size );
525                 }
526                 // Get a handle
527                 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
528                 {
529                         if(gaUserHandles[i].Node)       continue;
530                         gaUserHandles[i].Node = node;
531                         gaUserHandles[i].Position = 0;
532                         gaUserHandles[i].Mode = Mode;
533                         LEAVE('i', i);
534                         return i;
535                 }
536         }
537         else
538         {
539                 // Allocate space if not already
540                 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
541                 {
542                         Uint    addr, size;
543                         size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
544                         for(addr = 0; addr < size; addr += 0x1000)
545                                 MM_Allocate( (Uint)gaKernelHandles + addr );
546                         memset( gaKernelHandles, 0, size );
547                 }
548                 // Get a handle
549                 for(i=0;i<MAX_KERNEL_FILES;i++)
550                 {
551                         if(gaKernelHandles[i].Node)     continue;
552                         gaKernelHandles[i].Node = node;
553                         gaKernelHandles[i].Position = 0;
554                         gaKernelHandles[i].Mode = Mode;
555                         LEAVE('x', i|VFS_KERNEL_FLAG);
556                         return i|VFS_KERNEL_FLAG;
557                 }
558         }
559         
560         Log("VFS_Open: Out of handles");
561         LEAVE('i', -1);
562         return -1;
563 }
564
565
566 /**
567  * \brief Open a file from an open directory
568  */
569 int VFS_OpenChild(Uint *Errno, int FD, char *Name, Uint Mode)
570 {
571         tVFS_Handle     *h;
572         tVFS_Node       *node;
573          int    i;
574         
575         // Get handle
576         h = VFS_GetHandle(FD);
577         if(h == NULL) {
578                 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
579                 if(Errno)       *Errno = EINVAL;
580                 LEAVE('i', -1);
581                 return -1;
582         }
583         
584         // Check for directory
585         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
586                 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
587                 if(Errno)       *Errno = ENOTDIR;
588                 LEAVE('i', -1);
589                 return -1;
590         }
591         
592         // Find Child
593         node = h->Node->FindDir(h->Node, Name);
594         if(!node) {
595                 if(Errno)       *Errno = ENOENT;
596                 LEAVE('i', -1);
597                 return -1;
598         }
599         
600         i = 0;
601         i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
602         i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
603         i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
604         
605         // Permissions Check
606         if( !VFS_CheckACL(node, i) ) {
607                 if(node->Close) node->Close( node );
608                 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
609                 if(Errno)       *Errno = EACCES;
610                 LEAVE('i', -1);
611                 return -1;
612         }
613         
614         // Check for a user open
615         if(Mode & VFS_OPENFLAG_USER)
616         {
617                 // Allocate Buffer
618                 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
619                 {
620                         Uint    addr, size;
621                         size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
622                         for(addr = 0; addr < size; addr += 0x1000)
623                                 MM_Allocate( (Uint)gaUserHandles + addr );
624                         memset( gaUserHandles, 0, size );
625                 }
626                 // Get a handle
627                 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
628                 {
629                         if(gaUserHandles[i].Node)       continue;
630                         gaUserHandles[i].Node = node;
631                         gaUserHandles[i].Position = 0;
632                         gaUserHandles[i].Mode = Mode;
633                         LEAVE('i', i);
634                         return i;
635                 }
636         }
637         else
638         {
639                 // Allocate space if not already
640                 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
641                 {
642                         Uint    addr, size;
643                         size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
644                         for(addr = 0; addr < size; addr += 0x1000)
645                                 MM_Allocate( (Uint)gaKernelHandles + addr );
646                         memset( gaKernelHandles, 0, size );
647                 }
648                 // Get a handle
649                 for(i=0;i<MAX_KERNEL_FILES;i++)
650                 {
651                         if(gaKernelHandles[i].Node)     continue;
652                         gaKernelHandles[i].Node = node;
653                         gaKernelHandles[i].Position = 0;
654                         gaKernelHandles[i].Mode = Mode;
655                         LEAVE('x', i|VFS_KERNEL_FLAG);
656                         return i|VFS_KERNEL_FLAG;
657                 }
658         }
659         
660         Log_Error("VFS", "VFS_OpenChild - Out of handles");
661         if(Errno)       *Errno = ENFILE;
662         LEAVE('i', -1);
663         return -1;
664 }
665
666 /**
667  * \fn void VFS_Close(int FD)
668  * \brief Closes an open file handle
669  */
670 void VFS_Close(int FD)
671 {
672         tVFS_Handle     *h;
673         
674         // Get handle
675         h = VFS_GetHandle(FD);
676         if(h == NULL) {
677                 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x\n", FD);
678                 return;
679         }
680         
681         #if VALIDATE_VFS_FUNCTIPONS
682         if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
683                 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
684                         h->Node, h->Node->Close);
685                 return ;
686         }
687         #endif
688         
689         if(h->Node->Close)
690                 h->Node->Close( h->Node );
691         
692         h->Node = NULL;
693 }
694
695 /**
696  * \brief Change current working directory
697  */
698 int VFS_ChDir(char *Dest)
699 {
700         char    *buf;
701          int    fd;
702         tVFS_Handle     *h;
703         
704         // Create Absolute
705         buf = VFS_GetAbsPath(Dest);
706         if(buf == NULL) {
707                 Log("VFS_ChDir: Path expansion failed");
708                 return -1;
709         }
710         
711         // Check if path exists
712         fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
713         if(fd == -1) {
714                 Log("VFS_ChDir: Path is invalid");
715                 return -1;
716         }
717         
718         // Get node so we can check for directory
719         h = VFS_GetHandle(fd);
720         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
721                 Log("VFS_ChDir: Path is not a directory");
722                 VFS_Close(fd);
723                 return -1;
724         }
725         
726         // Close file
727         VFS_Close(fd);
728         
729         // Free old working directory
730         if( CFGPTR(CFG_VFS_CWD) )
731                 free( CFGPTR(CFG_VFS_CWD) );
732         // Set new
733         CFGPTR(CFG_VFS_CWD) = buf;
734         
735         Log("Updated CWD to '%s'", buf);
736         
737         return 1;
738 }
739
740 /**
741  * \fn int VFS_ChRoot(char *New)
742  * \brief Change current root directory
743  */
744 int VFS_ChRoot(char *New)
745 {
746         char    *buf;
747          int    fd;
748         tVFS_Handle     *h;
749         
750         if(New[0] == '/' && New[1] == '\0')
751                 return 1;       // What a useless thing to ask!
752         
753         // Create Absolute
754         buf = VFS_GetAbsPath(New);
755         if(buf == NULL) {
756                 LOG("Path expansion failed");
757                 return -1;
758         }
759         
760         // Check if path exists
761         fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
762         if(fd == -1) {
763                 LOG("Path is invalid");
764                 return -1;
765         }
766         
767         // Get node so we can check for directory
768         h = VFS_GetHandle(fd);
769         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
770                 LOG("Path is not a directory");
771                 VFS_Close(fd);
772                 return -1;
773         }
774         
775         // Close file
776         VFS_Close(fd);
777         
778         // Free old working directory
779         if( CFGPTR(CFG_VFS_CHROOT) )
780                 free( CFGPTR(CFG_VFS_CHROOT) );
781         // Set new
782         CFGPTR(CFG_VFS_CHROOT) = buf;
783         
784         LOG("Updated Root to '%s'", buf);
785         
786         return 1;
787 }
788
789 /**
790  * \fn tVFS_Handle *VFS_GetHandle(int FD)
791  * \brief Gets a pointer to the handle information structure
792  */
793 tVFS_Handle *VFS_GetHandle(int FD)
794 {
795         tVFS_Handle     *h;
796         
797         //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
798         
799         if(FD < 0)      return NULL;
800         
801         if(FD & VFS_KERNEL_FLAG) {
802                 FD &= (VFS_KERNEL_FLAG - 1);
803                 if(FD >= MAX_KERNEL_FILES)      return NULL;
804                 h = &gaKernelHandles[ FD ];
805         } else {
806                 if(FD >= CFGINT(CFG_VFS_MAXFILES))      return NULL;
807                 h = &gaUserHandles[ FD ];
808         }
809         
810         if(h->Node == NULL)     return NULL;
811         //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
812         return h;
813 }
814
815 // === EXPORTS ===
816 EXPORT(VFS_Open);
817 EXPORT(VFS_Close);

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