Kernel/VFS - Added unmount support
[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                 if(MountPoint)  *MountPoint = gVFS_RootMount;
205                 LEAVE('p', gVFS_RootMount->RootNode);
206                 return gVFS_RootMount->RootNode;
207         }
208         
209         // Check if there is anything mounted
210         if(!gVFS_Mounts) {
211                 Log_Error("VFS", "VFS_ParsePath - No filesystems mounted");
212                 return NULL;
213         }
214         
215         // Find Mountpoint
216         longestMount = gVFS_RootMount;
217         RWLock_AcquireRead( &glVFS_MountList );
218         for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
219         {
220                 // Quick Check
221                 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
222                         continue;
223                 // Length Check - If the length is smaller than the longest match sofar
224                 if(mnt->MountPointLen < longestMount->MountPointLen)    continue;
225                 // String Compare
226                 cmp = strncmp(Path, mnt->MountPoint, mnt->MountPointLen);
227                 // Not a match, continue
228                 if(cmp != 0)    continue;
229                 
230                 #if OPEN_MOUNT_ROOT
231                 // Fast Break - Request Mount Root
232                 if(Path[mnt->MountPointLen] == '\0') {
233                         if(TruePath) {
234                                 *TruePath = malloc( mnt->MountPointLen+1 );
235                                 strcpy(*TruePath, mnt->MountPoint);
236                         }
237                         if(MountPoint)
238                                 *MountPoint = mnt;
239                         RWLock_Release( &glVFS_MountList );
240                         LOG("Mount %p root", mnt);
241                         LEAVE('p', mnt->RootNode);
242                         return mnt->RootNode;
243                 }
244                 #endif
245                 longestMount = mnt;
246         }
247         longestMount->OpenHandleCount ++;       // Increment assuimg it worked
248         RWLock_Release( &glVFS_MountList );
249         
250         // Save to shorter variable
251         mnt = longestMount;
252         
253         LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
254         
255         // Initialise String
256         if(TruePath)
257         {
258                 // Assumes that the resultant path (here) will not be > strlen(Path) + 1
259                 *TruePath = malloc( strlen(Path) + 1 );
260                 strcpy(*TruePath, mnt->MountPoint);
261                 retLength = mnt->MountPointLen;
262         }
263         
264         curNode = mnt->RootNode;
265         curNode->ReferenceCount ++;     
266         // Parse Path
267         ofs = mnt->MountPointLen+1;
268         for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
269         {
270                 char    pathEle[nextSlash+1];
271                 
272                 // Empty String
273                 if(nextSlash == 0)      continue;
274                 
275                 memcpy(pathEle, &Path[ofs], nextSlash);
276                 pathEle[nextSlash] = 0;
277         
278                 // Check permissions on root of filesystem
279                 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
280                         LOG("Permissions failure on '%s'", Path);
281                         goto _error;
282                 }
283                 
284                 // Check if the node has a FindDir method
285                 if( !curNode->Type->FindDir )
286                 {
287                         LOG("Finddir failure on '%s'", Path);
288                         goto _error;
289                 }
290                 LOG("FindDir{=%p}(%p, '%s')", curNode->Type->FindDir, curNode, pathEle);
291                 // Get Child Node
292                 tmpNode = curNode->Type->FindDir(curNode, pathEle);
293                 LOG("tmpNode = %p", tmpNode);
294                 _CloseNode( curNode );
295                 curNode = tmpNode;
296                 
297                 // Error Check
298                 if(!curNode) {
299                         LOG("Node '%s' not found in dir '%s'", pathEle, Path);
300                         goto _error;
301                 }
302                 
303                 // Handle Symbolic Links
304                 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
305                         if(TruePath) {
306                                 free(*TruePath);
307                                 *TruePath = NULL;
308                         }
309                         if(!curNode->Type || !curNode->Type->Read) {
310                                 Log_Warning("VFS", "VFS_ParsePath - Read of symlink node %p'%s' is NULL",
311                                         curNode, Path);
312                                 goto _error;
313                         }
314                         
315                         if(iNestedLinks > MAX_NESTED_LINKS) {
316                                 Log_Notice("VFS", "VFS_ParsePath - Nested link limit exceeded");
317                                 goto _error;
318                         }
319                         
320                         // Parse Symlink Path
321                         // - Just update the path variable and restart the function
322                         // > Count nested symlinks and limit to some value (counteracts loops)
323                         {
324                                  int    remlen = strlen(Path) - (ofs + nextSlash);
325                                 if( curNode->Size + remlen > MAX_PATH_LEN ) {
326                                         Log_Warning("VFS", "VFS_ParsePath - Symlinked path too long");
327                                         goto _error;
328                                 }
329                                 curNode->Type->Read( curNode, 0, curNode->Size, path_buffer );
330                                 path_buffer[ curNode->Size ] = '\0';
331                                 LOG("path_buffer = '%s'", path_buffer);
332                                 strcat(path_buffer, Path + ofs+nextSlash);
333                                 
334                                 Path = path_buffer;
335 //                              Log_Debug("VFS", "VFS_ParsePath: Symlink translated to '%s'", Path);
336                                 iNestedLinks ++;
337                         }
338
339                         // EVIL: Goto :)
340                         LOG("Symlink -> '%s', restart", Path);
341                         mnt->OpenHandleCount --;        // Not in this mountpoint
342                         goto restart_parse;
343                 }
344                 
345                 // Handle Non-Directories
346                 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
347                 {
348                         Log_Warning("VFS", "VFS_ParsePath - Path segment is not a directory");
349                         goto _error;
350                 }
351                 
352                 // Check if path needs extending
353                 if(!TruePath)   continue;
354                 
355                 // Increase buffer space
356                 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
357                 // Check if allocation succeeded
358                 if(!tmp) {
359                         Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
360                         goto _error;
361                 }
362                 *TruePath = tmp;
363                 // Append to path
364                 (*TruePath)[retLength] = '/';
365                 strcpy(*TruePath+retLength+1, pathEle);
366                 
367                 LOG("*TruePath = '%s'", *TruePath);
368                 
369                 // - Extend Path
370                 retLength += nextSlash + 1;
371         }
372
373         // Check final finddir call     
374         if( !curNode->Type || !curNode->Type->FindDir ) {
375                 Log_Warning("VFS", "VFS_ParsePath - FindDir doesn't exist for element of '%s'", Path);
376                 goto _error;
377         }
378         
379         // Get last node
380         LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
381         tmpNode = curNode->Type->FindDir(curNode, &Path[ofs]);
382         LOG("tmpNode = %p", tmpNode);
383         // Check if file was found
384         if(!tmpNode) {
385                 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
386                 goto _error;
387         }
388         _CloseNode( curNode );
389         
390         if(TruePath)
391         {
392                 // Increase buffer space
393                 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
394                 // Check if allocation succeeded
395                 if(!tmp) {
396                         Log_Warning("VFS", "VFS_ParsePath -  Unable to reallocate true path buffer");
397                         goto _error;
398                 }
399                 *TruePath = tmp;
400                 // Append to path
401                 (*TruePath)[retLength] = '/';
402                 strcpy(*TruePath + retLength + 1, &Path[ofs]);
403                 // - Extend Path
404                 //retLength += strlen(tmpNode->Name) + 1;
405         }
406
407         if( MountPoint ) {
408                 *MountPoint = mnt;
409         }
410         
411         // Leave the mointpoint's count increased
412         
413         LEAVE('p', tmpNode);
414         return tmpNode;
415
416 _error:
417         _CloseNode( curNode );
418         
419         if(TruePath && *TruePath) {
420                 free(*TruePath);
421                 *TruePath = NULL;
422         }
423         // Open failed, so decrement the open handle count
424         mnt->OpenHandleCount --;
425         
426         LEAVE('n');
427         return NULL;
428 }
429
430 /**
431  * \brief Create and return a handle number for the given node and mode
432  */
433 int VFS_int_CreateHandle( tVFS_Node *Node, tVFS_Mount *Mount, int Mode )
434 {
435          int    i;
436         
437         ENTER("pNode pMount xMode", Node, Mount, Mode);
438
439         i = 0;
440         i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
441         i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
442         i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
443         
444         LOG("i = 0b%b", i);
445         
446         // Permissions Check
447         if( !VFS_CheckACL(Node, i) ) {
448                 _CloseNode( Node );
449                 Log_Log("VFS", "VFS_int_CreateHandle: Permissions Failed");
450                 errno = EACCES;
451                 LEAVE_RET('i', -1);
452         }
453         
454         i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), Node, Mode );
455         if( i < 0 ) {
456                 Log_Notice("VFS", "VFS_int_CreateHandle: Out of handles");
457                 errno = ENFILE;
458                 LEAVE_RET('i', -1);
459         }
460
461         VFS_GetHandle(i)->Mount = Mount;
462
463         LEAVE_RET('x', i);
464 }
465
466 /**
467  * \fn int VFS_Open(const char *Path, Uint Mode)
468  * \brief Open a file
469  */
470 int VFS_Open(const char *Path, Uint Flags)
471 {
472         return VFS_OpenEx(Path, Flags, 0);
473 }
474
475 int VFS_OpenEx(const char *Path, Uint Flags, Uint Mode)
476 {
477         tVFS_Node       *node;
478         tVFS_Mount      *mnt;
479         char    *absPath;
480         
481         ENTER("sPath xFlags oMode", Path, Flags);
482         
483         // Get absolute path
484         absPath = VFS_GetAbsPath(Path);
485         if(absPath == NULL) {
486                 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
487                 LEAVE_RET('i', -1);
488         }
489         LOG("absPath = \"%s\"", absPath);
490         
491         // Parse path and get mount point
492         node = VFS_ParsePath(absPath, NULL, &mnt);
493         
494         // Create file if requested and it doesn't exist
495         if( !node && (Flags & VFS_OPENFLAG_CREATE) )
496         {
497                 // TODO: Translate `Mode` into ACL and node flags
498                 // Get parent, create node
499                 if( VFS_MkNod(absPath, 0) ) {
500                         free(absPath);
501                         return -1;
502                 }
503                 node = VFS_ParsePath(absPath, NULL, &mnt);
504         }
505         
506         // Free generated path
507         free(absPath);
508         
509         // Check for error
510         if(!node)
511         {
512                 LOG("Cannot find node");
513                 errno = ENOENT;
514                 LEAVE_RET('i', -1);
515         }
516         
517         // Check for symlinks
518         if( !(Flags & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
519         {
520                 char    tmppath[node->Size+1];
521                 if( node->Size > MAX_PATH_LEN ) {
522                         Log_Warning("VFS", "VFS_Open - Symlink is too long (%i)", node->Size);
523                         LEAVE_RET('i', -1);
524                 }
525                 if( !node->Type || !node->Type->Read ) {
526                         Log_Warning("VFS", "VFS_Open - No read method on symlink");
527                         LEAVE_RET('i', -1);
528                 }
529                 // Read symlink's path
530                 node->Type->Read( node, 0, node->Size, tmppath );
531                 tmppath[ node->Size ] = '\0';
532                 _CloseNode( node );
533                 // Open the target
534                 node = VFS_ParsePath(tmppath, NULL, &mnt);
535                 if(!node) {
536                         LOG("Cannot find symlink target node (%s)", tmppath);
537                         errno = ENOENT;
538                         LEAVE_RET('i', -1);
539                 }
540         }
541
542         LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Flags));
543 }
544
545
546 /**
547  * \brief Open a file from an open directory
548  */
549 int VFS_OpenChild(int FD, const char *Name, Uint Mode)
550 {
551         tVFS_Handle     *h;
552         tVFS_Node       *node;
553         
554         ENTER("xFD sName xMode", FD, Name, Mode);
555
556         // Get handle
557         h = VFS_GetHandle(FD);
558         if(h == NULL) {
559                 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
560                 errno = EINVAL;
561                 LEAVE_RET('i', -1);
562         }
563         
564         // Check for directory
565         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
566                 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory");
567                 errno = ENOTDIR;
568                 LEAVE_RET('i', -1);
569         }
570
571         // Sanity check
572         if( !h->Node->Type || !h->Node->Type->FindDir ) {
573                 Log_Error("VFS", "VFS_OpenChild - Node does not have a type/is missing FindDir");
574                 errno = ENOTDIR;
575                 LEAVE_RET('i', -1);
576         }
577         
578         // Find Child
579         node = h->Node->Type->FindDir(h->Node, Name);
580         if(!node) {
581                 errno = ENOENT;
582                 LEAVE_RET('i', -1);
583         }
584
585         // Increment open handle count, no problems with the mount going away as `h` is already open on it
586         h->Mount->OpenHandleCount ++;
587
588         LEAVE_RET('x', VFS_int_CreateHandle(node, h->Mount, Mode));
589 }
590
591 int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode)
592 {
593         tVFS_Mount      *mnt;
594         tVFS_Node       *node;
595
596         ENTER("iMount XInode xMode", Mount, Inode, Mode);
597         
598         // Get mount point
599         mnt = VFS_GetMountByIdent(Mount);
600         if( !mnt ) {
601                 LOG("Mount point ident invalid");
602                 errno = ENOENT;
603                 LEAVE_RET('i', -1);
604         }
605         
606         // Does the filesystem support this?
607         if( !mnt->Filesystem->GetNodeFromINode ) {
608                 LOG("Filesystem does not support inode accesses");
609                 errno = ENOENT;
610                 LEAVE_RET('i', -1);
611         }
612
613         // Get node
614         node = mnt->Filesystem->GetNodeFromINode(mnt->RootNode, Inode);
615         if( !node ) {
616                 LOG("Unable to find inode");
617                 errno = ENOENT;
618                 LEAVE_RET('i', -1);
619         }
620         
621         LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
622 }
623
624 /**
625  * \fn void VFS_Close(int FD)
626  * \brief Closes an open file handle
627  */
628 void VFS_Close(int FD)
629 {
630         tVFS_Handle     *h;
631         
632         // Get handle
633         h = VFS_GetHandle(FD);
634         if(h == NULL) {
635                 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x", FD);
636                 return;
637         }
638         
639         #if VALIDATE_VFS_FUNCTIPONS
640         if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
641                 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
642                         h->Node, h->Node->Close);
643                 return ;
644         }
645         #endif
646         
647         _CloseNode(h->Node);
648
649         h->Mount->OpenHandleCount --;   
650
651         h->Node = NULL;
652 }
653
654 /**
655  * \brief Change current working directory
656  */
657 int VFS_ChDir(const char *Dest)
658 {
659         char    *buf;
660          int    fd;
661         tVFS_Handle     *h;
662         
663         // Create Absolute
664         buf = VFS_GetAbsPath(Dest);
665         if(buf == NULL) {
666                 Log_Notice("VFS", "VFS_ChDir: Path expansion failed");
667                 return -1;
668         }
669         
670         // Check if path exists
671         fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
672         if(fd == -1) {
673                 Log_Notice("VFS", "VFS_ChDir: Path is invalid");
674                 return -1;
675         }
676         
677         // Get node so we can check for directory
678         h = VFS_GetHandle(fd);
679         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
680                 Log("VFS_ChDir: Path is not a directory");
681                 VFS_Close(fd);
682                 return -1;
683         }
684         
685         // Close file
686         VFS_Close(fd);
687         
688         {
689                 char    **cwdptr = Threads_GetCWD();
690                 // Free old working directory
691                 if( *cwdptr )   free( *cwdptr );
692                 // Set new
693                 *cwdptr = buf;
694         }
695         
696         Log("Updated CWD to '%s'", buf);
697         
698         return 1;
699 }
700
701 /**
702  * \fn int VFS_ChRoot(char *New)
703  * \brief Change current root directory
704  */
705 int VFS_ChRoot(const char *New)
706 {
707         char    *buf;
708          int    fd;
709         tVFS_Handle     *h;
710         
711         if(New[0] == '/' && New[1] == '\0')
712                 return 1;       // What a useless thing to ask!
713         
714         // Create Absolute
715         buf = VFS_GetAbsPath(New);
716         if(buf == NULL) {
717                 LOG("Path expansion failed");
718                 return -1;
719         }
720         
721         // Check if path exists
722         fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
723         if(fd == -1) {
724                 LOG("Path is invalid");
725                 return -1;
726         }
727         
728         // Get node so we can check for directory
729         h = VFS_GetHandle(fd);
730         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
731                 LOG("Path is not a directory");
732                 VFS_Close(fd);
733                 return -1;
734         }
735         
736         // Close file
737         VFS_Close(fd);
738
739         // Update       
740         {
741                 char    **chroot_ptr = Threads_GetChroot();
742                 if( *chroot_ptr )       free( *chroot_ptr );
743                 *chroot_ptr = buf;
744         }
745         
746         LOG("Updated Root to '%s'", buf);
747         
748         return 1;
749 }
750
751 // === EXPORTS ===
752 EXPORT(VFS_Open);
753 EXPORT(VFS_Close);

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