Kernel - Disabled -fno-builtins, removed some unneeded memcpy calls
[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                                 strcat(path_buffer, Path + ofs+nextSlash);
350                                 
351                                 Path = path_buffer;
352 //                              Log_Debug("VFS", "VFS_ParsePath: Symlink translated to '%s'", Path);
353                                 iNestedLinks ++;
354                         }
355
356                         // EVIL: Goto :)
357                         goto restart_parse;
358                 }
359                 
360                 // Handle Non-Directories
361                 if( !(curNode->Flags & VFS_FFLAG_DIRECTORY) )
362                 {
363                         Log_Warning("VFS", "VFS_ParsePath - Path segment is not a directory");
364                         if(TruePath)    free(*TruePath);
365                         LEAVE('n');
366                         return NULL;
367                 }
368                 
369                 // Check if path needs extending
370                 if(!TruePath)   continue;
371                 
372                 // Increase buffer space
373                 tmp = realloc( *TruePath, retLength + strlen(pathEle) + 1 + 1 );
374                 // Check if allocation succeeded
375                 if(!tmp) {
376                         Log_Warning("VFS", "VFS_ParsePath - Unable to reallocate true path buffer");
377                         free(*TruePath);
378                         *TruePath = NULL;
379                         if(curNode->Close)      curNode->Close(curNode);
380                         LEAVE('n');
381                         return NULL;
382                 }
383                 *TruePath = tmp;
384                 // Append to path
385                 (*TruePath)[retLength] = '/';
386                 strcpy(*TruePath+retLength+1, pathEle);
387                 
388                 LOG("*TruePath = '%s'", *TruePath);
389                 
390                 // - Extend Path
391                 retLength += nextSlash + 1;
392         }
393         
394         if( !curNode->FindDir ) {
395                 if(curNode->Close)      curNode->Close(curNode);
396                 if(TruePath) {
397                         free(*TruePath);
398                         *TruePath = NULL;
399                 }
400                 Log_Warning("VFS", "VFS_ParsePath - FindDir doesn't exist for element of '%s'", Path);
401                 LEAVE('n');
402                 return NULL;
403         }
404         
405         // Get last node
406         LOG("FindDir(%p, '%s')", curNode, &Path[ofs]);
407         tmpNode = curNode->FindDir(curNode, &Path[ofs]);
408         LOG("tmpNode = %p", tmpNode);
409         if(curNode->Close)      curNode->Close(curNode);
410         // Check if file was found
411         if(!tmpNode) {
412                 LOG("Node '%s' not found in dir '%s'", &Path[ofs], Path);
413                 //Log("Child fail '%s' ('%s')", Path, &Path[ofs]);
414                 if(TruePath)    free(*TruePath);
415                 if(curNode->Close)      curNode->Close(curNode);
416                 LEAVE('n');
417                 return NULL;
418         }
419         
420         if(TruePath)
421         {
422                 // Increase buffer space
423                 tmp = realloc(*TruePath, retLength + strlen(&Path[ofs]) + 1 + 1);
424                 // Check if allocation succeeded
425                 if(!tmp) {
426                         Log_Warning("VFS", "VFS_ParsePath -  Unable to reallocate true path buffer");
427                         free(*TruePath);
428                         if(tmpNode->Close)      tmpNode->Close(curNode);
429                         LEAVE('n');
430                         return NULL;
431                 }
432                 *TruePath = tmp;
433                 // Append to path
434                 (*TruePath)[retLength] = '/';
435                 strcpy(*TruePath + retLength + 1, &Path[ofs]);
436                 // - Extend Path
437                 //retLength += strlen(tmpNode->Name) + 1;
438         }
439
440         if( MountPoint ) {
441                 *MountPoint = mnt;
442         }
443         
444         LEAVE('p', tmpNode);
445         return tmpNode;
446 }
447
448 /**
449  * \brief Create and return a handle number for the given node and mode
450  */
451 int VFS_int_CreateHandle( tVFS_Node *Node, tVFS_Mount *Mount, int Mode )
452 {
453          int    i;
454         
455         ENTER("pNode pMount iMode", Node, Mount, Mode);
456
457         i = 0;
458         i |= (Mode & VFS_OPENFLAG_EXEC) ? VFS_PERM_EXECUTE : 0;
459         i |= (Mode & VFS_OPENFLAG_READ) ? VFS_PERM_READ : 0;
460         i |= (Mode & VFS_OPENFLAG_WRITE) ? VFS_PERM_WRITE : 0;
461         
462         LOG("i = 0b%b", i);
463         
464         // Permissions Check
465         if( !VFS_CheckACL(Node, i) ) {
466                 if(Node->Close) Node->Close( Node );
467                 Log_Log("VFS", "VFS_int_CreateHandle: Permissions Failed");
468                 errno = EACCES;
469                 LEAVE_RET('i', -1);
470         }
471         
472         i = VFS_AllocHandle( !!(Mode & VFS_OPENFLAG_USER), Node, Mode );
473         if( i < 0 ) {
474                 Log_Notice("VFS", "VFS_int_CreateHandle: Out of handles");
475                 errno = ENFILE;
476                 LEAVE_RET('i', -1);
477         }
478
479         VFS_GetHandle(i)->Mount = Mount;
480
481         LEAVE_RET('x', i);
482 }
483
484 /**
485  * \fn int VFS_Open(const char *Path, Uint Mode)
486  * \brief Open a file
487  */
488 int VFS_Open(const char *Path, Uint Mode)
489 {
490         tVFS_Node       *node;
491         tVFS_Mount      *mnt;
492         char    *absPath;
493         
494         ENTER("sPath xMode", Path, Mode);
495         
496         // Get absolute path
497         absPath = VFS_GetAbsPath(Path);
498         if(absPath == NULL) {
499                 Log_Warning("VFS", "VFS_Open: Path expansion failed '%s'", Path);
500                 LEAVE_RET('i', -1);
501         }
502         LOG("absPath = \"%s\"", absPath);
503         // Parse path and get mount point
504         node = VFS_ParsePath(absPath, NULL, &mnt);
505         // Free generated path
506         free(absPath);
507         
508         if(!node) {
509                 LOG("Cannot find node");
510                 errno = ENOENT;
511                 LEAVE_RET('i', -1);
512         }
513         
514         // Check for symlinks
515         if( !(Mode & VFS_OPENFLAG_NOLINK) && (node->Flags & VFS_FFLAG_SYMLINK) )
516         {
517                 char    tmppath[node->Size+1];
518                 if( node->Size > MAX_PATH_LEN ) {
519                         Log_Warning("VFS", "VFS_Open - Symlink is too long (%i)", node->Size);
520                         LEAVE_RET('i', -1);
521                 }
522                 if( !node->Read ) {
523                         Log_Warning("VFS", "VFS_Open - No read method on symlink");
524                         LEAVE_RET('i', -1);
525                 }
526                 // Read symlink's path
527                 node->Read( node, 0, node->Size, tmppath );
528                 tmppath[ node->Size ] = '\0';
529                 if(node->Close) node->Close( node );
530                 // Open the target
531                 node = VFS_ParsePath(tmppath, NULL, &mnt);
532                 if(!node) {
533                         LOG("Cannot find symlink target node (%s)", tmppath);
534                         errno = ENOENT;
535                         LEAVE_RET('i', -1);
536                 }
537         }
538
539         LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
540 }
541
542
543 /**
544  * \brief Open a file from an open directory
545  */
546 int VFS_OpenChild(int FD, const char *Name, Uint Mode)
547 {
548         tVFS_Handle     *h;
549         tVFS_Node       *node;
550         
551         ENTER("xFD sName xMode", FD, Name, Mode);
552
553         // Get handle
554         h = VFS_GetHandle(FD);
555         if(h == NULL) {
556                 Log_Warning("VFS", "VFS_OpenChild - Invalid file handle 0x%x", FD);
557                 errno = EINVAL;
558                 LEAVE_RET('i', -1);
559         }
560         
561         // Check for directory
562         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
563                 Log_Warning("VFS", "VFS_OpenChild - Passed handle is not a directory", FD);
564                 errno = ENOTDIR;
565                 LEAVE_RET('i', -1);
566         }
567         
568         // Find Child
569         node = h->Node->FindDir(h->Node, Name);
570         if(!node) {
571                 errno = ENOENT;
572                 LEAVE_RET('i', -1);
573         }
574
575         LEAVE_RET('x', VFS_int_CreateHandle(node, h->Mount, Mode));
576 }
577
578 int VFS_OpenInode(Uint32 Mount, Uint64 Inode, int Mode)
579 {
580         tVFS_Mount      *mnt;
581         tVFS_Node       *node;
582
583         ENTER("iMount XInode xMode", Mount, Inode, Mode);
584         
585         // Get mount point
586         mnt = VFS_GetMountByIdent(Mount);
587         if( !mnt ) {
588                 LOG("Mount point ident invalid");
589                 errno = ENOENT;
590                 LEAVE_RET('i', -1);
591         }
592         
593         // Does the filesystem support this?
594         if( !mnt->Filesystem->GetNodeFromINode ) {
595                 LOG("Filesystem does not support inode accesses");
596                 errno = ENOENT;
597                 LEAVE_RET('i', -1);
598         }
599
600         // Get node
601         node = mnt->Filesystem->GetNodeFromINode(mnt->RootNode, Inode);
602         if( !node ) {
603                 LOG("Unable to find inode");
604                 errno = ENOENT;
605                 LEAVE_RET('i', -1);
606         }
607         
608         LEAVE_RET('x', VFS_int_CreateHandle(node, mnt, Mode));
609 }
610
611 /**
612  * \fn void VFS_Close(int FD)
613  * \brief Closes an open file handle
614  */
615 void VFS_Close(int FD)
616 {
617         tVFS_Handle     *h;
618         
619         // Get handle
620         h = VFS_GetHandle(FD);
621         if(h == NULL) {
622                 Log_Warning("VFS", "Invalid file handle passed to VFS_Close, 0x%x", FD);
623                 return;
624         }
625         
626         #if VALIDATE_VFS_FUNCTIPONS
627         if(h->Node->Close && !MM_GetPhysAddr(h->Node->Close)) {
628                 Log_Warning("VFS", "Node %p's ->Close method is invalid (%p)",
629                         h->Node, h->Node->Close);
630                 return ;
631         }
632         #endif
633         
634         if(h->Node->Close)
635                 h->Node->Close( h->Node );
636         
637         h->Node = NULL;
638 }
639
640 /**
641  * \brief Change current working directory
642  */
643 int VFS_ChDir(const char *Dest)
644 {
645         char    *buf;
646          int    fd;
647         tVFS_Handle     *h;
648         
649         // Create Absolute
650         buf = VFS_GetAbsPath(Dest);
651         if(buf == NULL) {
652                 Log_Notice("VFS", "VFS_ChDir: Path expansion failed");
653                 return -1;
654         }
655         
656         // Check if path exists
657         fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
658         if(fd == -1) {
659                 Log_Notice("VFS", "VFS_ChDir: Path is invalid");
660                 return -1;
661         }
662         
663         // Get node so we can check for directory
664         h = VFS_GetHandle(fd);
665         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
666                 Log("VFS_ChDir: Path is not a directory");
667                 VFS_Close(fd);
668                 return -1;
669         }
670         
671         // Close file
672         VFS_Close(fd);
673         
674         // Free old working directory
675         if( CFGPTR(CFG_VFS_CWD) )
676                 free( CFGPTR(CFG_VFS_CWD) );
677         // Set new
678         CFGPTR(CFG_VFS_CWD) = buf;
679         
680         Log("Updated CWD to '%s'", buf);
681         
682         return 1;
683 }
684
685 /**
686  * \fn int VFS_ChRoot(char *New)
687  * \brief Change current root directory
688  */
689 int VFS_ChRoot(const char *New)
690 {
691         char    *buf;
692          int    fd;
693         tVFS_Handle     *h;
694         
695         if(New[0] == '/' && New[1] == '\0')
696                 return 1;       // What a useless thing to ask!
697         
698         // Create Absolute
699         buf = VFS_GetAbsPath(New);
700         if(buf == NULL) {
701                 LOG("Path expansion failed");
702                 return -1;
703         }
704         
705         // Check if path exists
706         fd = VFS_Open(buf, VFS_OPENFLAG_EXEC);
707         if(fd == -1) {
708                 LOG("Path is invalid");
709                 return -1;
710         }
711         
712         // Get node so we can check for directory
713         h = VFS_GetHandle(fd);
714         if( !(h->Node->Flags & VFS_FFLAG_DIRECTORY) ) {
715                 LOG("Path is not a directory");
716                 VFS_Close(fd);
717                 return -1;
718         }
719         
720         // Close file
721         VFS_Close(fd);
722         
723         // Free old working directory
724         if( CFGPTR(CFG_VFS_CHROOT) )
725                 free( CFGPTR(CFG_VFS_CHROOT) );
726         // Set new
727         CFGPTR(CFG_VFS_CHROOT) = buf;
728         
729         LOG("Updated Root to '%s'", buf);
730         
731         return 1;
732 }
733
734 // === EXPORTS ===
735 EXPORT(VFS_Open);
736 EXPORT(VFS_Close);

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