Kernel/vfs - Fixed bug in VFS_ParsePath
[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                 if(MountPoint)  *MountPoint = gVFS_RootMount;
203                 LEAVE('p', gVFS_RootMount->RootNode);
204                 return gVFS_RootMount->RootNode;
205         }
206         
207         // Check if there is anything mounted
208         if(!gVFS_Mounts) {
209                 Log_Error("VFS", "VFS_ParsePath - No filesystems mounted");
210                 return NULL;
211         }
212         
213         // Find Mountpoint
214         longestMount = gVFS_RootMount;
215         for(mnt = gVFS_Mounts; mnt; mnt = mnt->Next)
216         {
217                 // Quick Check
218                 if( Path[mnt->MountPointLen] != '/' && Path[mnt->MountPointLen] != '\0')
219                         continue;
220                 // Length Check - If the length is smaller than the longest match sofar
221                 if(mnt->MountPointLen < longestMount->MountPointLen)    continue;
222                 // String Compare
223                 cmp = strcmp(Path, mnt->MountPoint);
224                 
225                 #if OPEN_MOUNT_ROOT
226                 // Fast Break - Request Mount Root
227                 if(cmp == 0) {
228                         if(TruePath) {
229                                 *TruePath = malloc( mnt->MountPointLen+1 );
230                                 strcpy(*TruePath, mnt->MountPoint);
231                         }
232                         if(MountPoint)
233                                 *MountPoint = mnt;
234                         LEAVE('p', mnt->RootNode);
235                         return mnt->RootNode;
236                 }
237                 #endif
238                 // Not a match, continue
239                 if(cmp != '/')  continue;
240                 longestMount = mnt;
241         }
242         
243         // Save to shorter variable
244         mnt = longestMount;
245         
246         LOG("mnt = {MountPoint:\"%s\"}", mnt->MountPoint);
247         
248         // Initialise String
249         if(TruePath)
250         {
251                 // Assumes that the resultant path (here) will not be > strlen(Path) + 1
252                 *TruePath = malloc( strlen(Path) + 1 );
253                 strcpy(*TruePath, mnt->MountPoint);
254                 retLength = mnt->MountPointLen;
255         }
256         
257         curNode = mnt->RootNode;
258         curNode->ReferenceCount ++;     
259         // Parse Path
260         ofs = mnt->MountPointLen+1;
261         for(; (nextSlash = strpos(&Path[ofs], '/')) != -1; ofs += nextSlash + 1)
262         {
263                 char    pathEle[nextSlash+1];
264                 
265                 // Empty String
266                 if(nextSlash == 0)      continue;
267                 
268                 memcpy(pathEle, &Path[ofs], nextSlash);
269                 pathEle[nextSlash] = 0;
270         
271                 // Check permissions on root of filesystem
272                 if( !VFS_CheckACL(curNode, VFS_PERM_EXECUTE) ) {
273                         if(curNode->Close)      curNode->Close( curNode );
274                         if(TruePath) {
275                                 free(*TruePath);
276                                 *TruePath = NULL;
277                         }
278                         //Log("Permissions fail on '%s'", Path);
279                         LEAVE('n');
280                         return NULL;
281                 }
282                 
283                 // Check if the node has a FindDir method
284                 if( !curNode->FindDir )
285                 {
286                         if(curNode->Close)      curNode->Close(curNode);
287                         if(TruePath) {
288                                 free(*TruePath);
289                                 *TruePath = NULL;
290                         }
291                         //Log("FindDir fail on '%s'", Path);
292                         LEAVE('n');
293                         return NULL;
294                 }
295                 LOG("FindDir{=%p}(%p, '%s')", curNode->FindDir, curNode, pathEle);
296                 // Get Child Node
297                 tmpNode = curNode->FindDir(curNode, pathEle);
298                 LOG("tmpNode = %p", tmpNode);
299                 if(curNode->Close) {
300                         //LOG2("curNode->Close = %p", curNode->Close);
301                         curNode->Close(curNode);
302                 }
303                 curNode = tmpNode;
304                 
305                 // Error Check
306                 if(!curNode) {
307                         LOG("Node '%s' not found in dir '%s'", pathEle, Path);
308                         if(TruePath) {
309                                 free(*TruePath);
310                                 *TruePath = NULL;
311                         }
312                         //Log("Child fail on '%s' ('%s)", Path, pathEle);
313                         LEAVE('n');
314                         return NULL;
315                 }
316                 
317                 // Handle Symbolic Links
318                 if(curNode->Flags & VFS_FFLAG_SYMLINK) {
319                         if(TruePath) {
320                                 free(*TruePath);
321                                 *TruePath = NULL;
322                         }
323                         if(!curNode->Read) {
324                                 Log_Warning("VFS", "VFS_ParsePath - Read of symlink node %p'%s' is NULL",
325                                         curNode, Path);
326                                 if(curNode->Close)      curNode->Close(curNode);
327                                 // No need to free *TruePath, it should already be NULL
328                                 LEAVE('n');
329                                 return NULL;
330                         }
331                         
332                         if(iNestedLinks > MAX_NESTED_LINKS) {
333                                 if(curNode->Close)      curNode->Close(curNode);
334                                 LEAVE('n');
335                                 return NULL;
336                         }
337                         
338                         // Parse Symlink Path
339                         // - Just update the path variable and restart the function
340                         // > Count nested symlinks and limit to some value (counteracts loops)
341                         {
342                                  int    remlen = strlen(Path) - (ofs + nextSlash);
343                                 if( curNode->Size + remlen > MAX_PATH_LEN ) {
344                                         if(curNode->Close)      curNode->Close(curNode);
345                                         Log_Warning("VFS", "VFS_ParsePath - Symlinked path too long");
346                                         LEAVE('n');
347                                         return NULL;
348                                 }
349                                 curNode->Read( curNode, 0, curNode->Size, path_buffer );
350                                 path_buffer[ curNode->Size ] = '\0';
351                                 strcat(path_buffer, Path + ofs+nextSlash);
352                                 
353                                 Path = path_buffer;
354 //                              Log_Debug("VFS", "VFS_ParsePath: Symlink translated to '%s'", Path);
355                                 iNestedLinks ++;
356                         }
357
358                         // EVIL: Goto :)
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 iMode", 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