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

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