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

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