Buglocating: Some random corruption in VFS, FDD lockups
[tpg/acess2.git] / Kernel / vfs / open.c
1 /*
2  * Acess2 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         // 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         LOG("absPath = \"%s\"", absPath);
462         // Parse path and get mount point
463         node = VFS_ParsePath(absPath, NULL);
464         // Free generated path
465         free(absPath);
466         
467         if(!node) {
468                 LOG("Cannot find node");
469                 LEAVE('i', -1);
470                 return -1;
471         }
472         
473         // Check for symlinks
474         if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
475         {
476                 if( !node->Read ) {
477                         Warning("No read method on symlink");
478                         LEAVE('i', -1);
479                         return -1;
480                 }
481                 absPath = malloc(node->Size+1); // Allocate Buffer
482                 node->Read( node, 0, node->Size, absPath );     // Read Path
483                 
484                 absPath[ node->Size ] = '\0';   // End String
485                 if(node->Close) node->Close( node );    // Close old node
486                 node = VFS_ParsePath(absPath, NULL);    // Get new node
487                 free( absPath );        // Free allocated path
488         }
489         
490         if(!node) {
491                 LOG("Cannot find node");
492                 LEAVE('i', -1);
493                 return -1;
494         }
495         
496         i = 0;
497         i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
498         i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
499         i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
500         
501         LOG("i = 0b%b", i);
502         
503         // Permissions Check
504         if( !VFS_CheckACL(node, i) ) {
505                 if(node->Close) node->Close( node );
506                 Log("VFS_Open: Permissions Failed");
507                 LEAVE('i', -1);
508                 return -1;
509         }
510         
511         // Check for a user open
512         if(Mode & VFS_OPENFLAG_USER)
513         {
514                 // Allocate Buffer
515                 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
516                 {
517                         Uint    addr, size;
518                         size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
519                         for(addr = 0; addr < size; addr += 0x1000)
520                                 MM_Allocate( (Uint)gaUserHandles + addr );
521                         memset( gaUserHandles, 0, size );
522                 }
523                 // Get a handle
524                 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
525                 {
526                         if(gaUserHandles[i].Node)       continue;
527                         gaUserHandles[i].Node = node;
528                         gaUserHandles[i].Position = 0;
529                         gaUserHandles[i].Mode = Mode;
530                         LEAVE('i', i);
531                         return i;
532                 }
533         }
534         else
535         {
536                 // Allocate space if not already
537                 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
538                 {
539                         Uint    addr, size;
540                         size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
541                         for(addr = 0; addr < size; addr += 0x1000)
542                                 MM_Allocate( (Uint)gaKernelHandles + addr );
543                         memset( gaKernelHandles, 0, size );
544                 }
545                 // Get a handle
546                 for(i=0;i<MAX_KERNEL_FILES;i++)
547                 {
548                         if(gaKernelHandles[i].Node)     continue;
549                         gaKernelHandles[i].Node = node;
550                         gaKernelHandles[i].Position = 0;
551                         gaKernelHandles[i].Mode = Mode;
552                         LEAVE('x', i|VFS_KERNEL_FLAG);
553                         return i|VFS_KERNEL_FLAG;
554                 }
555         }
556         
557         Log("VFS_Open: Out of handles");
558         LEAVE('i', -1);
559         return -1;
560 }
561
562
563 /**
564  * \brief Open a file from an open directory
565  */
566 int VFS_OpenChild(Uint *Errno, int FD, char *Name, Uint Mode)
567 {
568         tVFS_Handle     *h;
569         tVFS_Node       *node;
570          int    i;
571         
572         // Get handle
573         h = VFS_GetHandle(FD);
574         if(h == NULL) {
575                 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
576                 if(Errno)       *Errno = EINVAL;
577                 LEAVE('i', -1);
578                 return -1;
579         }
580         
581         // Check for directory
582         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
583                 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
584                 if(Errno)       *Errno = ENOTDIR;
585                 LEAVE('i', -1);
586                 return -1;
587         }
588         
589         // Find Child
590         node = h->Node->FindDir(h->Node, Name);
591         if(!node) {
592                 if(Errno)       *Errno = ENOENT;
593                 LEAVE('i', -1);
594                 return -1;
595         }
596         
597         i = 0;
598         i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
599         i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
600         i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
601         
602         // Permissions Check
603         if( !VFS_CheckACL(node, i) ) {
604                 if(node->Close) node->Close( node );
605                 Log_Notice("VFS", "VFS_OpenChild - Permissions Failed");
606                 if(Errno)       *Errno = EACCES;
607                 LEAVE('i', -1);
608                 return -1;
609         }
610         
611         // Check for a user open
612         if(Mode & VFS_OPENFLAG_USER)
613         {
614                 // Allocate Buffer
615                 if( MM_GetPhysAddr( (Uint)gaUserHandles ) == 0 )
616                 {
617                         Uint    addr, size;
618                         size = CFGINT(CFG_VFS_MAXFILES) * sizeof(tVFS_Handle);
619                         for(addr = 0; addr < size; addr += 0x1000)
620                                 MM_Allocate( (Uint)gaUserHandles + addr );
621                         memset( gaUserHandles, 0, size );
622                 }
623                 // Get a handle
624                 for(i=0;i<CFGINT(CFG_VFS_MAXFILES);i++)
625                 {
626                         if(gaUserHandles[i].Node)       continue;
627                         gaUserHandles[i].Node = node;
628                         gaUserHandles[i].Position = 0;
629                         gaUserHandles[i].Mode = Mode;
630                         LEAVE('i', i);
631                         return i;
632                 }
633         }
634         else
635         {
636                 // Allocate space if not already
637                 if( MM_GetPhysAddr( (Uint)gaKernelHandles ) == 0 )
638                 {
639                         Uint    addr, size;
640                         size = MAX_KERNEL_FILES * sizeof(tVFS_Handle);
641                         for(addr = 0; addr < size; addr += 0x1000)
642                                 MM_Allocate( (Uint)gaKernelHandles + addr );
643                         memset( gaKernelHandles, 0, size );
644                 }
645                 // Get a handle
646                 for(i=0;i<MAX_KERNEL_FILES;i++)
647                 {
648                         if(gaKernelHandles[i].Node)     continue;
649                         gaKernelHandles[i].Node = node;
650                         gaKernelHandles[i].Position = 0;
651                         gaKernelHandles[i].Mode = Mode;
652                         LEAVE('x', i|VFS_KERNEL_FLAG);
653                         return i|VFS_KERNEL_FLAG;
654                 }
655         }
656         
657         Log_Error("VFS", "VFS_OpenChild - Out of handles");
658         if(Errno)       *Errno = ENFILE;
659         LEAVE('i', -1);
660         return -1;
661 }
662
663 /**
664  * \fn void VFS_Close(int FD)
665  * \brief Closes an open file handle
666  */
667 void VFS_Close(int FD)
668 {
669         tVFS_Handle     *h;
670         
671         // Get handle
672         h = VFS_GetHandle(FD);
673         if(h == NULL) {
674                 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x\n", FD);
675                 return;
676         }
677         
678         #if VALIDATE_VFS_FUNCTIPONS
679         if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
680                 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
681                         h->Node, h->Node->Close);
682                 return ;
683         }
684         #endif
685         
686         if(h->Node->Close)
687                 h->Node->Close( h->Node );
688         
689         h->Node = NULL;
690 }
691
692 /**
693  * \brief Change current working directory
694  */
695 int VFS_ChDir(char *Dest)
696 {
697         char    *buf;
698          int    fd;
699         tVFS_Handle     *h;
700         
701         // Create Absolute
702         buf = VFS_GetAbsPath(Dest);
703         if(buf == NULL) {
704                 Log("VFS_ChDir: Path expansion failed");
705                 return -1;
706         }
707         
708         // Check if path exists
709         fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
710         if(fd == -1) {
711                 Log("VFS_ChDir: Path is invalid");
712                 return -1;
713         }
714         
715         // Get node so we can check for directory
716         h = VFS_GetHandle(fd);
717         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
718                 Log("VFS_ChDir: Path is not a directory");
719                 VFS_Close(fd);
720                 return -1;
721         }
722         
723         // Close file
724         VFS_Close(fd);
725         
726         // Free old working directory
727         if( CFGPTR(CFG_VFS_CWD) )
728                 free( CFGPTR(CFG_VFS_CWD) );
729         // Set new
730         CFGPTR(CFG_VFS_CWD) = buf;
731         
732         Log("Updated CWD to '%s'", buf);
733         
734         return 1;
735 }
736
737 /**
738  * \fn int VFS_ChRoot(char *New)
739  * \brief Change current root directory
740  */
741 int VFS_ChRoot(char *New)
742 {
743         char    *buf;
744          int    fd;
745         tVFS_Handle     *h;
746         
747         if(New[0] == '/' && New[1] == '\0')
748                 return 1;       // What a useless thing to ask!
749         
750         // Create Absolute
751         buf = VFS_GetAbsPath(New);
752         if(buf == NULL) {
753                 LOG("Path expansion failed");
754                 return -1;
755         }
756         
757         // Check if path exists
758         fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
759         if(fd == -1) {
760                 LOG("Path is invalid");
761                 return -1;
762         }
763         
764         // Get node so we can check for directory
765         h = VFS_GetHandle(fd);
766         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
767                 LOG("Path is not a directory");
768                 VFS_Close(fd);
769                 return -1;
770         }
771         
772         // Close file
773         VFS_Close(fd);
774         
775         // Free old working directory
776         if( CFGPTR(CFG_VFS_CHROOT) )
777                 free( CFGPTR(CFG_VFS_CHROOT) );
778         // Set new
779         CFGPTR(CFG_VFS_CHROOT) = buf;
780         
781         LOG("Updated Root to '%s'", buf);
782         
783         return 1;
784 }
785
786 /**
787  * \fn tVFS_Handle *VFS_GetHandle(int FD)
788  * \brief Gets a pointer to the handle information structure
789  */
790 tVFS_Handle *VFS_GetHandle(int FD)
791 {
792         tVFS_Handle     *h;
793         
794         //Log_Debug("VFS", "VFS_GetHandle: (FD=0x%x)", FD);
795         
796         if(FD < 0)      return NULL;
797         
798         if(FD & VFS_KERNEL_FLAG) {
799                 FD &= (VFS_KERNEL_FLAG - 1);
800                 if(FD >= MAX_KERNEL_FILES)      return NULL;
801                 h = &gaKernelHandles[ FD ];
802         } else {
803                 if(FD >= CFGINT(CFG_VFS_MAXFILES))      return NULL;
804                 h = &gaUserHandles[ FD ];
805         }
806         
807         if(h->Node == NULL)     return NULL;
808         //Log_Debug("VFS", "VFS_GetHandle: RETURN %p", h);
809         return h;
810 }
811
812 // === EXPORTS ===
813 EXPORT(VFS_Open);
814 EXPORT(VFS_Close);

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