3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 extern mount_t mount_list
;
29 extern unsigned int fd_count
;
31 vfs_t
*vfs_list_findbymp (char *mountpoint
)
34 for (vfs
= vfs_list
.next
; vfs
!= &vfs_list
; vfs
= vfs
->next
) {
35 if (!strcmp (vfs
->mountpoint
, mountpoint
))
42 bool vfs_list_add (char *name
, unsigned attrib
, char *mountpoint
)
44 unsigned name_len
= strlen (name
);
45 unsigned mp_len
= strlen (mountpoint
);
49 /* alloc and init context */
50 vfs
= (vfs_t
*) kmalloc (sizeof (vfs_t
));
54 memset (vfs
, 0, sizeof (vfs_t
));
56 vfs
->name
= (char *) kmalloc (sizeof (char) * VFS_FILENAME_LEN
+ 2);
62 memset (vfs
->name
, 0, VFS_FILENAME_LEN
);
63 memcpy (vfs
->name
, name
, name_len
);
64 vfs
->name
[name_len
] = '\0';
65 //printf ("dd: '%s' '%s'", name, vfs->name);
66 memset (vfs
->mountpoint
, 0, VFS_MOUNTPOINT_LEN
);
67 memcpy (vfs
->mountpoint
, mountpoint
, mp_len
);
68 vfs
->mountpoint
[mp_len
] = '\0';
75 vfs
->next
= &vfs_list
;
76 vfs
->prev
= vfs_list
.prev
;
77 vfs
->prev
->next
= vfs
;
78 vfs
->next
->prev
= vfs
;
83 bool vfs_list_del (char *mountpoint
)
87 for (vfs
= vfs_list
.next
; vfs
!= &vfs_list
; vfs
= vfs
->next
) {
88 if (!strncmp (vfs
->mountpoint
, mountpoint
, strlen (mountpoint
))) {
95 vfs
->next
->prev
= vfs
->prev
;
96 vfs
->prev
->next
= vfs
->next
;
105 bool vfs_ls (char *file
, unsigned file_len
)
108 strcpy (pwd
, (char *) env_get ("PWD"));
110 // puts ("name rwhsdb\n");
116 for (vfs
= vfs_list
.next
; vfs
!= &vfs_list
; vfs
= vfs
->next
)
117 if (!strcmp (vfs
->mountpoint
, pwd
)) {
118 printf ("%s\t", vfs
->name
);
122 for (vfs
= vfs_list
.next
; vfs
!= &vfs_list
; vfs
= vfs
->next
)
123 if (!strcmp (vfs
->mountpoint
, file
) ||
124 !strcmp (vfs
->mountpoint
+1, file
)) {
125 printf ("%s\t", vfs
->name
);
130 /*printf ("%s\t%u%u%u%u%u%u%u\n", vfs->name,
131 vfs->attrib & VFS_FILEATTR_READ,
132 vfs->attrib & VFS_FILEATTR_WRITE,
133 vfs->attrib & VFS_FILEATTR_HIDDEN,
134 vfs->attrib & VFS_FILEATTR_SYSTEM,
135 vfs->attrib & VFS_FILEATTR_DIR,
136 vfs->attrib & VFS_FILEATTR_BIN);*/
144 bool vfs_cd (char *file
, unsigned file_len
)
147 if (!strcmp (file
, "..")) {
149 strcpy (pwd
, (char *) env_get ("PWD"));
151 unsigned pwd_len
= strlen (pwd
);
153 if (!strcmp (pwd
, "/"))
156 /* vymaze kus retezce - az po predposledni / */
157 pwd_len
--; // preskoci / na konci retezce
160 if (pwd
[pwd_len
] == '/') {
161 pwd
[pwd_len
+1] = '\0';
167 for (vfs
= vfs_list
.next
; vfs
!= &vfs_list
; vfs
= vfs
->next
)
168 if (!strcmp (vfs
->mountpoint
, pwd
)) {
169 if (vfs
->attrib
& VFS_FILEATTR_MOUNTED
) {
170 //printf ("vfs->mountpoint: %s | pwd: %s\n", vfs->mountpoint, pwd);
171 partition_t
*p
= mount_find (pwd
);
174 mount (p
, file
, ""); // first return to last directory
176 umount (p
, (char *) env_get ("PWD")); // then umount old directory
178 while (vfs_list_del ((char *) env_get ("PWD")));
181 printf ("ERROR -> device not respond\n");
187 env_set ("PWD", pwd
); // set new directory
193 if (!strcmp (file
, "."))
197 /* go to root fs dir */
198 if (!strcmp (file
, "/")) {
200 env_set ("PWD", file
);
202 for (vfs
= vfs_list
.next
; vfs
!= &vfs_list
; vfs
= vfs
->next
) {
203 if (vfs
->attrib
& VFS_FILEATTR_MOUNTED
) {
204 partition_t
*p
= mount_find ((char *) env_get ("PWD"));
214 /* go to another directory */
216 strcpy (pwd
, (char *) env_get ("PWD"));
220 unsigned pwd_len
= strlen (pwd
);
222 unsigned i
= file
[0] == '/' ? 1 : 0;
225 memcpy (buf
, pwd
, pwd_len
);
226 memcpy (buf
+pwd_len
, file
+i
, file_len
-i
);
227 buf
[pwd_len
+file_len
-i
] = '\0';
229 memcpy (buf
, file
, file_len
);
230 buf
[file_len
] = '\0';
233 if (buf
[pwd_len
+file_len
-i
-1] == '/')
234 buf
[pwd_len
+file_len
-i
-1] = '\0';
237 for (vfs
= vfs_list
.next
; vfs
!= &vfs_list
; vfs
= vfs
->next
) {
238 unsigned mp_len
= strlen (vfs
->mountpoint
);
239 unsigned nm_len
= strlen (vfs
->name
);
242 memcpy (buf2
, vfs
->mountpoint
, mp_len
);
243 memcpy (buf2
+mp_len
, vfs
->name
, nm_len
);
244 buf2
[mp_len
+nm_len
] = '\0';
246 if (!strcmp (buf
, buf2
)) {
247 if (vfs
->attrib
& VFS_FILEATTR_FILE
) {
248 printf ("ERROR -> this is a file, not an directory\n");
253 if (vfs
->attrib
& VFS_FILEATTR_SYSTEM
&& strcmp ((char *) env_get ("USER"), "root")) {
254 printf ("ERROR -> only root can do that\n");
258 memcpy (pwd
, buf2
, mp_len
+nm_len
);
259 pwd
[mp_len
+nm_len
] = '/';
260 pwd
[mp_len
+nm_len
+1] = '\0';
262 if (vfs
->attrib
& VFS_FILEATTR_MOUNTED
) {
263 partition_t
*p
= mount_find ((char *) env_get ("PWD"));
266 mount (p
, file
, pwd
);
268 printf ("ERROR -> device not respond\n");
273 env_set ("PWD", pwd
);
282 bool vfs_mkdir (char *file
, unsigned file_len
)
286 strcpy (pwd
, (char *) env_get ("PWD"));
288 vfs_list_add (file
, VFS_FILEATTR_DIR
| VFS_FILEATTR_READ
| VFS_FILEATTR_MOUNTED
, (char *) pwd
);
290 partition_t
*p
= mount_find ((char *) pwd
);
293 p
->fs
->handler (FS_ACT_WRITE
, FS_ARG_DIR
, file
, 0, file_len
);
295 printf ("ERROR -> device not respond\n");
299 DPRINT ("vfs_mkdir ()\n");
306 vfs_dirent_t
*vfs_dirent ()
309 strcpy (pwd
, (char *) env_get ("PWD"));
314 unsigned dirents
= 0;
316 for (vfs
= vfs_list
.next
; vfs
!= &vfs_list
; vfs
= vfs
->next
)
317 if (!strcmp (vfs
->mountpoint
, pwd
))
323 vfs_dirent_t
*dirent
= (vfs_dirent_t
*) kmalloc (sizeof (vfs_dirent_t
) * (dirents
+1));
329 for (vfs
= vfs_list
.next
; vfs
!= &vfs_list
; vfs
= vfs
->next
)
330 if (!strcmp (vfs
->mountpoint
, pwd
)) {
331 /*unsigned len = strlen (vfs->name);
332 dirent[i].name = (char *) kmalloc (sizeof (char) * (len + 1));
337 memcpy (dirent[i].name, vfs->name, len);
338 dirent[i].name[len] = '\0';*/
339 dirent
[i
].name
= vfs
->name
;
341 dirent
[i
].attrib
= vfs
->attrib
;
343 if ((i
+1) == dirents
)
353 dirent
[dirents
].next
= 0;
357 unsigned int init_vfs ()
361 fd_list
.next
= &fd_list
;
362 fd_list
.prev
= &fd_list
;
364 mount_list
.next
= &mount_list
;
365 mount_list
.prev
= &mount_list
;
367 vfs_list
.next
= &vfs_list
;
368 vfs_list
.prev
= &vfs_list
;
370 vfs_list_add ("bin", VFS_FILEATTR_DIR
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
, "/");
371 vfs_list_add ("cd", VFS_FILEATTR_FILE
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
| VFS_FILEATTR_BIN
, "/bin/");
372 vfs_list_add ("ls", VFS_FILEATTR_FILE
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
| VFS_FILEATTR_BIN
, "/bin/");
373 vfs_list_add ("exec", VFS_FILEATTR_FILE
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
| VFS_FILEATTR_BIN
, "/bin/");
374 vfs_list_add ("dev", VFS_FILEATTR_DIR
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
, "/");
375 vfs_list_add ("etc", VFS_FILEATTR_DIR
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
, "/");
376 vfs_list_add ("mnt", VFS_FILEATTR_DIR
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
, "/");
377 vfs_list_add ("floppy", VFS_FILEATTR_DIR
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
, "/mnt/");
378 vfs_list_add ("cdrom", VFS_FILEATTR_DIR
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
, "/mnt/");
379 vfs_list_add ("hdd", VFS_FILEATTR_DIR
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
, "/mnt/");
380 vfs_list_add ("usr", VFS_FILEATTR_DIR
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
, "/");
381 vfs_list_add ("root", VFS_FILEATTR_DIR
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
, "/usr/");
382 vfs_list_add ("proc", VFS_FILEATTR_DIR
| VFS_FILEATTR_SYSTEM
| VFS_FILEATTR_READ
, "/");
384 /*dev_t *dev = dev_find ("/dev/fd0");
387 mount (dev, "", "/mnt/floppy/");*/