Kernel 0.5.0 ! Lot of fixes, fast, no few new features, etc ..
[ZeXOS.git] / kernel / apps / fs / mount.c
blobb75216e8ef1d67788ce475290572f270c41ec7d7
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <system.h>
21 #include <string.h>
22 #include <mount.h>
23 #include <partition.h>
24 #include <dev.h>
25 #include <vfs.h>
27 mount_t mount_list;
29 void mount_display ()
31 mount_t *mnt;
32 for (mnt = mount_list.next; mnt != &mount_list; mnt = mnt->next)
33 kprintf ("%s on %s\n", mnt->p->name, mnt->mountpoint);
36 partition_t *mount_find (char *mountpoint)
38 mount_t *mnt;
39 for (mnt = mount_list.next; mnt != &mount_list; mnt = mnt->next) {
40 printf ("mnt: '%s' '%s'\n", mnt->mountpoint, mountpoint);
41 if (!strcmp (mnt->mountpoint, mountpoint))
42 return mnt->p;
45 return 0;
48 unsigned mount_list_add (partition_t *p, char *mountpoint)
50 unsigned mp_len = strlen (mountpoint);
52 if (!mp_len) // HACK
53 return 2;
55 if (mount_find (mountpoint))
56 return 1;
58 if (vfs_list_findbymp (mountpoint))
59 return 3;
61 mount_t *mnt;
63 /* alloc and init context */
64 mnt = (mount_t *) kmalloc (sizeof (mount_t));
66 memcpy (mnt->mountpoint, mountpoint, mp_len);
67 mnt->mountpoint[mp_len] = '\0';
69 mnt->p = p;
71 /* add into list */
72 mnt->next = &mount_list;
73 mnt->prev = mount_list.prev;
74 mnt->prev->next = mnt;
75 mnt->next->prev = mnt;
77 return 0;
80 bool mount (partition_t *p, char *dir, char *mountpoint)
82 unsigned err = mount_list_add (p, mountpoint);
84 if (err == 1) {
85 kprintf ("ERROR -> partition %s is already mounted to %s\n", p->name, mountpoint);
86 return 0;
88 if (err == 3) {
89 kprintf ("ERROR -> mountpoint %s does not exists\n", mountpoint);
90 return 0;
93 dev_t *dev = dev_findbypartition (p);
95 if (!dev)
96 return 0;
98 if (!dev->handler (DEV_ACT_MOUNT, p, mountpoint, dir, NULL)) {
99 kprintf ("ERROR -> mount failed: %s\n", p->name);
100 return 0;
103 return 1;
106 bool umount (partition_t *p, char *mountpoint)
108 bool f = 0;
109 mount_t *mnt;
110 for (mnt = mount_list.next; mnt != &mount_list; mnt = mnt->next) {
111 if (!strcmp (mnt->mountpoint, mountpoint) &&
112 mnt->p == p) {
113 f = 1;
114 break;
118 if (f) {
119 mnt->next->prev = mnt->prev;
120 mnt->prev->next = mnt->next;
122 //kfree (mnt);
123 return 1;
126 return 0;