Kernel/vfs - TODO in open.c for relative symlinks
[tpg/acess2.git] / KernelLand / Kernel / vfs / dir.c
1 /*
2  * Acess2 VFS
3  * - Directory Management Functions
4  */
5 #define DEBUG   0
6 #include <acess.h>
7 #include <vfs.h>
8 #include <vfs_int.h>
9
10 // === IMPORTS ===
11 extern tVFS_Mount       *gRootMount;
12
13 // === PROTOTYPES ===
14 #if 0
15  int    VFS_MkDir(const char *Path);
16  int    VFS_MkNod(const char *Path, Uint Flags);
17  int    VFS_Symlink(const char *Name, const char *Link);
18 #endif
19
20 // === CODE ===
21 /**
22  * \fn int VFS_MkDir(char *Path)
23  * \brief Create a new node
24  * \param Path  Path of directory to create
25  */
26 int VFS_MkDir(const char *Path)
27 {
28         return VFS_MkNod(Path, VFS_FFLAG_DIRECTORY);
29 }
30
31 /**
32  * \fn int VFS_MkNod(char *Path, Uint Flags)
33  * \brief Create a new node in a directory
34  * \param Path  Path of new node
35  * \param Flags Flags to apply to the node
36  */
37 int VFS_MkNod(const char *Path, Uint Flags)
38 {
39         tVFS_Mount      *mountpt;
40         char    *absPath, *name;
41          int    pos = 0, oldpos = 0;
42          int    next = 0;
43         tVFS_Node       *parent;
44          int    ret;
45         
46         ENTER("sPath xFlags", Path, Flags);
47         
48         absPath = VFS_GetAbsPath(Path);
49         LOG("absPath = '%s'", absPath);
50         
51         while( (next = strpos(&absPath[pos+1], '/')) != -1 ) {
52                 LOG("next = %i", next);
53                 pos += next+1;
54                 LOG("pos = %i", pos);
55                 oldpos = pos;
56         }
57         absPath[oldpos] = '\0'; // Mutilate path
58         name = &absPath[oldpos+1];
59         
60         LOG("absPath = '%s', name = '%s'", absPath, name);
61         
62         // Check for root
63         if(absPath[0] == '\0')
64                 parent = VFS_ParsePath("/", NULL, &mountpt);
65         else
66                 parent = VFS_ParsePath(absPath, NULL, &mountpt);
67         
68         LOG("parent = %p", parent);
69         
70         if(!parent) {
71                 LEAVE('i', -1);
72                 return -1;      // Error Check
73         }
74
75         // Permissions Check
76         if( !VFS_CheckACL(parent, VFS_PERM_EXECUTE|VFS_PERM_WRITE) ) {
77                 _CloseNode(parent);
78                 mountpt->OpenHandleCount --;
79                 free(absPath);
80                 LEAVE('i', -1);
81                 return -1;
82         }
83         
84         LOG("parent = %p", parent);
85         
86         if(!parent->Type || !parent->Type->MkNod) {
87                 Log_Warning("VFS", "VFS_MkNod - Directory has no MkNod method");
88                 mountpt->OpenHandleCount --;
89                 LEAVE('i', -1);
90                 return -1;
91         }
92         
93         // Create node
94         ret = parent->Type->MkNod(parent, name, Flags);
95         
96         // Free allocated string
97         free(absPath);
98         
99         // Free Parent
100         mountpt->OpenHandleCount --;
101         _CloseNode(parent);
102
103         // Return whatever the driver said      
104         LEAVE('i', ret);
105         return ret;
106 }
107
108 /**
109  * \fn int VFS_Symlink(const char *Name, const char *Link)
110  * \brief Creates a symlink called \a Name to \a Link
111  * \param Name  Name of symbolic link
112  * \param Link  Destination of symbolic link
113  */
114 int VFS_Symlink(const char *Name, const char *Link)
115 {
116         char    *realLink;
117          int    fp;
118         
119         ENTER("sName sLink", Name, Link);
120         
121         // Get absolue path name
122         realLink = VFS_GetAbsPath( Link );
123         if(!realLink) {
124                 Log_Warning("VFS", "Path '%s' is badly formed", Link);
125                 LEAVE('i', -1);
126                 return -1;
127         }
128
129         LOG("realLink = '%s'", realLink);
130
131         // Make node
132         if( VFS_MkNod(Name, VFS_FFLAG_SYMLINK) != 0 ) {
133                 Log_Warning("VFS", "Unable to create link node '%s'", Name);
134                 free(realLink);
135                 LEAVE('i', -2);
136                 return -2;      // Make link node
137         }
138         
139         // Write link address
140         fp = VFS_Open(Name, VFS_OPENFLAG_WRITE|VFS_OPENFLAG_NOLINK);
141         VFS_Write(fp, strlen(realLink), realLink);
142         VFS_Close(fp);
143         
144         free(realLink);
145         
146         LEAVE('i', 1);
147         return 1;
148 }
149
150 /**
151  * \fn int VFS_ReadDir(int FD, char *Dest)
152  * \brief Read from a directory
153  */
154 int VFS_ReadDir(int FD, char *Dest)
155 {
156         tVFS_Handle     *h = VFS_GetHandle(FD);
157         char    *tmp;
158         
159         //ENTER("ph pDest", h, Dest);
160         
161         if(!h || !h->Node->Type || !h->Node->Type->ReadDir) {
162                 //LEAVE('i', 0);
163                 return 0;
164         }
165         
166         if(h->Node->Size != -1 && h->Position >= h->Node->Size) {
167                 //LEAVE('i', 0);
168                 return 0;
169         }
170         
171         do {
172                 tmp = h->Node->Type->ReadDir(h->Node, h->Position);
173                 if((Uint)tmp < (Uint)VFS_MAXSKIP)
174                         h->Position += (Uint)tmp;
175                 else
176                         h->Position ++;
177         } while(tmp != NULL && (Uint)tmp < (Uint)VFS_MAXSKIP);
178         
179         //LOG("tmp = '%s'", tmp);
180         
181         if(!tmp) {
182                 //LEAVE('i', 0);
183                 return 0;
184         }
185         
186         strcpy(Dest, tmp);
187         free(tmp);
188         
189         //LEAVE('i', 1);
190         return 1;
191 }

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