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

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