Fixing places where malloc() (and others) is not null checked
[tpg/acess2.git] / Kernel / vfs / open.c
1 /*
2  * Acess2 VFS
3  * - Open, Close and ChDir
4  */
5 #define DEBUG   0
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         // Save to shorter variable
241         mnt = longestMount;
242         
243         LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
244         
245         // Initialise String
246         if(TruePath)
247         {
248                 *TruePath = malloc( mnt->MountPointLen+1 );
249                 strcpy(*TruePath, mnt->MountPoint);
250                 retLength = mnt->MountPointLen;
251         }
252         
253         curNode = mnt->RootNode;
254         curNode->ReferenceCount ++;     
255         // Parse Path
256         ofs = mnt->MountPointLen+1;
257         for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
258         {
259                 char    pathEle[nextSlash+1];
260                 
261                 // Empty String
262                 if(nextSlash == 0)      continue;
263                 
264                 memcpy(pathEle, &Path[ofs], nextSlash);
265                 pathEle[nextSlash] = 0;
266         
267                 // Check permissions on root of filesystem
268                 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
269                         if(curNode->Close)      curNode->Close( curNode );
270                         if(TruePath) {
271                                 free(*TruePath);
272                                 *TruePath = NULL;
273                         }
274                         //Log("Permissions fail on '%s'", Path);
275                         LEAVE('n');
276                         return NULL;
277                 }
278                 
279                 // Check if the node has a FindDir method
280                 if( !curNode->FindDir )
281                 {
282                         if(curNode->Close)      curNode->Close(curNode);
283                         if(TruePath) {
284                                 free(*TruePath);
285                                 *TruePath = NULL;
286                         }
287                         //Log("FindDir fail on '%s'", Path);
288                         LEAVE('n');
289                         return NULL;
290                 }
291                 LOG("FindDir{=%p}(%p, '%s')", curNode->FindDir, curNode, pathEle);
292                 // Get Child Node
293                 tmpNode = curNode->FindDir(curNode, pathEle);
294                 LOG("tmpNode = %p", tmpNode);
295                 if(curNode->Close) {
296                         //LOG2("curNode->Close = %p", curNode->Close);
297                         curNode->Close(curNode);
298                 }
299                 curNode = tmpNode;
300                 
301                 // Error Check
302                 if(!curNode) {
303                         LOG("Node '%s' not found in dir '%s'", pathEle, Path);
304                         if(TruePath) {
305                                 free(*TruePath);
306                                 *TruePath = NULL;
307                         }
308                         //Log("Child fail on '%s' ('%s)", Path, pathEle);
309                         LEAVE('n');
310                         return NULL;
311                 }
312                 
313                 // Handle Symbolic Links
314                 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
315                         if(TruePath) {
316                                 free(*TruePath);
317                                 *TruePath = NULL;
318                         }
319                         if(!curNode->Read) {
320                                 Warning("VFS_ParsePath - Read of node %p is NULL (%s)",
321                                         curNode, Path);
322                                 if(curNode->Close)      curNode->Close(curNode);
323                                 // No need to free *TruePath, see above
324                                 LEAVE('n');
325                                 return NULL;
326                         }
327                         
328                         tmp = malloc( curNode->Size + 1 );
329                         if(!tmp) {
330                                 Log_Warning("VFS", "VFS_ParsePath - Malloc failure");
331                                 // No need to free *TruePath, see above
332                                 LEAVE('n');
333                                 return NULL;
334                         }
335                         curNode->Read( curNode, 0, curNode->Size, tmp );
336                         tmp[ curNode->Size ] = '\0';
337                         
338                         // Parse Symlink Path
339                         curNode = VFS_ParsePath(tmp, TruePath);
340                         if(TruePath)
341                                 LOG("VFS", "*TruePath='%s'", *TruePath);
342                         
343                         // Error Check
344                         if(!curNode) {
345                                 Log_Debug("VFS", "Symlink fail '%s'", tmp);
346                                 free(tmp);      // Free temp string
347                                 if(TruePath)    free(TruePath);
348                                 LEAVE('n');
349                                 return NULL;
350                         }
351                         
352                         // Free temp link
353                         free(tmp);
354                         
355                         // Set Path Variable
356                         if(TruePath) {
357                                 retLength = strlen(*TruePath);
358                         }
359                         
360                         continue;
361                 }
362                 
363                 // Handle Non-Directories
364                 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
365                 {
366                         Warning("VFS_ParsePath - File in directory context");
367                         if(TruePath)    free(*TruePath);
368                         LEAVE('n');
369                         return NULL;
370                 }
371                 
372                 // Check if path needs extending
373                 if(!TruePath)   continue;
374                 
375                 // Increase buffer space
376                 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
377                 // Check if allocation succeeded
378                 if(!tmp) {
379                         Warning("VFS_ParsePath -  Unable to reallocate true path buffer");
380                         free(*TruePath);
381                         *TruePath = NULL;
382                         if(curNode->Close)      curNode->Close(curNode);
383                         LEAVE('n');
384                         return NULL;
385                 }
386                 *TruePath = tmp;
387                 // Append to path
388                 (*TruePath)[retLength] = '/';
389                 strcpy(*TruePath+retLength+1, pathEle);
390                 
391                 LOG("*TruePath = '%s'", *TruePath);
392                 
393                 // - Extend Path
394                 retLength += nextSlash + 1;
395         }
396         
397         if( !curNode->FindDir ) {
398                 if(curNode->Close)      curNode->Close(curNode);
399                 if(TruePath) {
400                         free(*TruePath);
401                         *TruePath = NULL;
402                 }
403                 Log("FindDir fail on '%s'", Path);
404                 LEAVE('n');
405                 return NULL;
406         }
407         
408         // Get last node
409         LOG("VFS_ParsePath: FindDir(%p, '%s')", curNode, &Path[ofs]);
410         tmpNode = curNode->FindDir(curNode, &Path[ofs]);
411         LOG("tmpNode = %p", tmpNode);
412         if(curNode->Close)      curNode->Close(curNode);
413         // Check if file was found
414         if(!tmpNode) {
415                 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
416                 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
417                 if(TruePath)    free(*TruePath);
418                 if(curNode->Close)      curNode->Close(curNode);
419                 LEAVE('n');
420                 return NULL;
421         }
422         
423         if(TruePath)
424         {
425                 // Increase buffer space
426                 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
427                 // Check if allocation succeeded
428                 if(!tmp) {
429                         Warning("VFS_ParsePath -  Unable to reallocate true path buffer");
430                         free(*TruePath);
431                         if(tmpNode->Close)      tmpNode->Close(curNode);
432                         LEAVE('n');
433                         return NULL;
434                 }
435                 *TruePath = tmp;
436                 // Append to path
437                 (*TruePath)[retLength] = '/';
438                 strcpy(*TruePath + retLength + 1, &Path[ofs]);
439                 // - Extend Path
440                 //retLength += strlen(tmpNode->Name) + 1;
441         }
442         
443         LEAVE('p', tmpNode);
444         return tmpNode;
445 }
446
447 /**
448  * \fn int VFS_Open(char *Path, Uint Mode)
449  * \brief Open a file
450  */
451 int VFS_Open(char *Path, Uint Mode)
452 {
453         tVFS_Node       *node;
454         char    *absPath;
455          int    i;
456         
457         ENTER("sPath xMode", Path, Mode);
458         
459         // Get absolute path
460         absPath = VFS_GetAbsPath(Path);
461         if(absPath == NULL) {
462                 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
463                 return -1;
464         }
465         LOG("absPath = \"%s\"", absPath);
466         // Parse path and get mount point
467         node = VFS_ParsePath(absPath, NULL);
468         // Free generated path
469         free(absPath);
470         
471         if(!node) {
472                 LOG("Cannot find node");
473                 LEAVE('i', -1);
474                 return -1;
475         }
476         
477         // Check for symlinks
478         if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
479         {
480                 if( !node->Read ) {
481                         Warning("No read method on symlink");
482                         LEAVE('i', -1);
483                         return -1;
484                 }
485                 absPath = malloc(node->Size+1); // Allocate Buffer
486                 node->Read( node, 0, node->Size, absPath );     // Read Path
487                 
488                 absPath[ node->Size ] = '\0';   // End String
489                 if(node->Close) node->Close( node );    // Close old node
490                 node = VFS_ParsePath(absPath, NULL);    // Get new node
491                 free( absPath );        // Free allocated path
492         }
493         
494         if(!node) {
495                 LOG("Cannot find node");
496                 LEAVE('i', -1);
497                 return -1;
498         }
499         
500         i = 0;
501         i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
502         i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
503         i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
504         
505         LOG("i = 0b%b", i);
506         
507         // Permissions Check
508         if( !VFS_CheckACL(node, i) ) {
509                 if(node->Close) node->Close( node );
510                 Log("VFS_Open: Permissions Failed");
511                 LEAVE('i', -1);
512                 return -1;
513         }
514         
515         // Check for a user open
516         if(Mode & VFS_OPENFLAG_USER)
517         {
518                 // Allocate Buffer
519                 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
520                 {
521                         Uint    addr, size;
522                         size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
523                         for(addr = 0; addr < size; addr += 0x1000)
524                                 MM_Allocate( (Uint)gaUserHandles + addr );
525                         memset( gaUserHandles, 0, size );
526                 }
527                 // Get a handle
528                 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
529                 {
530                         if(gaUserHandles[i].Node)       continue;
531                         gaUserHandles[i].Node = node;
532                         gaUserHandles[i].Position = 0;
533                         gaUserHandles[i].Mode = Mode;
534                         LEAVE('i', i);
535                         return i;
536                 }
537         }
538         else
539         {
540                 // Allocate space if not already
541                 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
542                 {
543                         Uint    addr, size;
544                         size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
545                         for(addr = 0; addr < size; addr += 0x1000)
546                                 MM_Allocate( (Uint)gaKernelHandles + addr );
547                         memset( gaKernelHandles, 0, size );
548                 }
549                 // Get a handle
550                 for(i=0;i<MAX_KERNEL_FILES;i++)
551                 {
552                         if(gaKernelHandles[i].Node)     continue;
553                         gaKernelHandles[i].Node = node;
554                         gaKernelHandles[i].Position = 0;
555                         gaKernelHandles[i].Mode = Mode;
556                         LEAVE('x', i|VFS_KERNEL_FLAG);
557                         return i|VFS_KERNEL_FLAG;
558                 }
559         }
560         
561         Log("VFS_Open: Out of handles");
562         LEAVE('i', -1);
563         return -1;
564 }
565
566
567 /**
568  * \brief Open a file from an open directory
569  */
570 int VFS_OpenChild(Uint *Errno, int FD, char *Name, Uint Mode)
571 {
572         tVFS_Handle     *h;
573         tVFS_Node       *node;
574          int    i;
575         
576         // Get handle
577         h = VFS_GetHandle(FD);
578         if(h == NULL) {
579                 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
580                 if(Errno)       *Errno = EINVAL;
581                 LEAVE('i', -1);
582                 return -1;
583         }
584         
585         // Check for directory
586         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
587                 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
588                 if(Errno)       *Errno = ENOTDIR;
589                 LEAVE('i', -1);
590                 return -1;
591         }
592         
593         // Find Child
594         node = h->Node->FindDir(h->Node, Name);
595         if(!node) {
596                 if(Errno)       *Errno = ENOENT;
597                 LEAVE('i', -1);
598                 return -1;
599         }
600         
601         i = 0;
602         i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
603         i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
604         i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
605         
606         // Permissions Check
607         if( !VFS_CheckACL(node, i) ) {
608                 if(node->Close) node->Close( node );
609                 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
610                 if(Errno)       *Errno = EACCES;
611                 LEAVE('i', -1);
612                 return -1;
613         }
614         
615         // Check for a user open
616         if(Mode & VFS_OPENFLAG_USER)
617         {
618                 // Allocate Buffer
619                 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
620                 {
621                         Uint    addr, size;
622                         size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
623                         for(addr = 0; addr < size; addr += 0x1000)
624                                 MM_Allocate( (Uint)gaUserHandles + addr );
625                         memset( gaUserHandles, 0, size );
626                 }
627                 // Get a handle
628                 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
629                 {
630                         if(gaUserHandles[i].Node)       continue;
631                         gaUserHandles[i].Node = node;
632                         gaUserHandles[i].Position = 0;
633                         gaUserHandles[i].Mode = Mode;
634                         LEAVE('i', i);
635                         return i;
636                 }
637         }
638         else
639         {
640                 // Allocate space if not already
641                 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
642                 {
643                         Uint    addr, size;
644                         size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
645                         for(addr = 0; addr < size; addr += 0x1000)
646                                 MM_Allocate( (Uint)gaKernelHandles + addr );
647                         memset( gaKernelHandles, 0, size );
648                 }
649                 // Get a handle
650                 for(i=0;i<MAX_KERNEL_FILES;i++)
651                 {
652                         if(gaKernelHandles[i].Node)     continue;
653                         gaKernelHandles[i].Node = node;
654                         gaKernelHandles[i].Position = 0;
655                         gaKernelHandles[i].Mode = Mode;
656                         LEAVE('x', i|VFS_KERNEL_FLAG);
657                         return i|VFS_KERNEL_FLAG;
658                 }
659         }
660         
661         Log_Error("VFS", "VFS_OpenChild - Out of handles");
662         if(Errno)       *Errno = ENFILE;
663         LEAVE('i', -1);
664         return -1;
665 }
666
667 /**
668  * \fn void VFS_Close(int FD)
669  * \brief Closes an open file handle
670  */
671 void VFS_Close(int FD)
672 {
673         tVFS_Handle     *h;
674         
675         // Get handle
676         h = VFS_GetHandle(FD);
677         if(h == NULL) {
678                 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x\n", FD);
679                 return;
680         }
681         
682         #if VALIDATE_VFS_FUNCTIPONS
683         if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
684                 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
685                         h->Node, h->Node->Close);
686                 return ;
687         }
688         #endif
689         
690         if(h->Node->Close)
691                 h->Node->Close( h->Node );
692         
693         h->Node = NULL;
694 }
695
696 /**
697  * \brief Change current working directory
698  */
699 int VFS_ChDir(char *Dest)
700 {
701         char    *buf;
702          int    fd;
703         tVFS_Handle     *h;
704         
705         // Create Absolute
706         buf = VFS_GetAbsPath(Dest);
707         if(buf == NULL) {
708                 Log("VFS_ChDir: Path expansion failed");
709                 return -1;
710         }
711         
712         // Check if path exists
713         fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
714         if(fd == -1) {
715                 Log("VFS_ChDir: Path is invalid");
716                 return -1;
717         }
718         
719         // Get node so we can check for directory
720         h = VFS_GetHandle(fd);
721         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
722                 Log("VFS_ChDir: Path is not a directory");
723                 VFS_Close(fd);
724                 return -1;
725         }
726         
727         // Close file
728         VFS_Close(fd);
729         
730         // Free old working directory
731         if( CFGPTR(CFG_VFS_CWD) )
732                 free( CFGPTR(CFG_VFS_CWD) );
733         // Set new
734         CFGPTR(CFG_VFS_CWD) = buf;
735         
736         Log("Updated CWD to '%s'", buf);
737         
738         return 1;
739 }
740
741 /**
742  * \fn int VFS_ChRoot(char *New)
743  * \brief Change current root directory
744  */
745 int VFS_ChRoot(char *New)
746 {
747         char    *buf;
748          int    fd;
749         tVFS_Handle     *h;
750         
751         if(New[0] == '/' && New[1] == '\0')
752                 return 1;       // What a useless thing to ask!
753         
754         // Create Absolute
755         buf = VFS_GetAbsPath(New);
756         if(buf == NULL) {
757                 LOG("Path expansion failed");
758                 return -1;
759         }
760         
761         // Check if path exists
762         fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
763         if(fd == -1) {
764                 LOG("Path is invalid");
765                 return -1;
766         }
767         
768         // Get node so we can check for directory
769         h = VFS_GetHandle(fd);
770         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
771                 LOG("Path is not a directory");
772                 VFS_Close(fd);
773                 return -1;
774         }
775         
776         // Close file
777         VFS_Close(fd);
778         
779         // Free old working directory
780         if( CFGPTR(CFG_VFS_CHROOT) )
781                 free( CFGPTR(CFG_VFS_CHROOT) );
782         // Set new
783         CFGPTR(CFG_VFS_CHROOT) = buf;
784         
785         LOG("Updated Root to '%s'", buf);
786         
787         return 1;
788 }
789
790 /**
791  * \fn tVFS_Handle *VFS_GetHandle(int FD)
792  * \brief Gets a pointer to the handle information structure
793  */
794 tVFS_Handle *VFS_GetHandle(int FD)
795 {
796         tVFS_Handle     *h;
797         
798         //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
799         
800         if(FD < 0)      return NULL;
801         
802         if(FD & VFS_KERNEL_FLAG) {
803                 FD &= (VFS_KERNEL_FLAG - 1);
804                 if(FD >= MAX_KERNEL_FILES)      return NULL;
805                 h = &gaKernelHandles[ FD ];
806         } else {
807                 if(FD >= CFGINT(CFG_VFS_MAXFILES))      return NULL;
808                 h = &gaUserHandles[ FD ];
809         }
810         
811         if(h->Node == NULL)     return NULL;
812         //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
813         return h;
814 }
815
816 // === EXPORTS ===
817 EXPORT(VFS_Open);
818 EXPORT(VFS_Close);

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