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

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