Fixed possible memory corruption in commands exec and netexec; fixed command netcp...
[ZeXOS.git] / kernel / utils / fs / mount.c
blob82d52c250ba10a06510b1a9099adf6472a5e97d4
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
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 if (!strcmp (mnt->mountpoint, mountpoint))
41 return mnt->p;
43 return 0;
46 unsigned mount_verify (partition_t *p, char *mountpoint)
48 unsigned mp_len = strlen (mountpoint);
50 if (!mp_len) // HACK
51 return 2;
53 if (mount_find (mountpoint))
54 return 1;
56 if (!vfs_find (mountpoint, mp_len))
57 return 3;
59 return 0;
62 unsigned mount_register (partition_t *p, char *mountpoint)
64 unsigned mp_len = strlen (mountpoint);
66 mount_t *mnt;
68 /* alloc and init context */
69 mnt = (mount_t *) kmalloc (sizeof (mount_t));
71 if (!mnt)
72 return 0;
74 memcpy (mnt->mountpoint, mountpoint, mp_len);
75 mnt->mountpoint[mp_len] = '\0';
77 mnt->p = p;
79 /* add into list */
80 mnt->next = &mount_list;
81 mnt->prev = mount_list.prev;
82 mnt->prev->next = mnt;
83 mnt->next->prev = mnt;
85 return 1;
88 bool mount (partition_t *p, char *dir, char *mountpoint)
90 unsigned err = mount_verify (p, mountpoint);
92 if (err == 1) {
93 kprintf ("ERROR -> partition %s is already mounted to %s\n", p->name, mountpoint);
94 return 0;
96 if (err == 3) {
97 kprintf ("ERROR -> mountpoint %s does not exists\n", mountpoint);
98 return 0;
101 dev_t *dev = (dev_t *) dev_findbypartition (p);
103 if (!dev)
104 return 0;
106 if (!dev->handler (DEV_ACT_MOUNT, p, mountpoint, dir, NULL)) {
107 kprintf ("ERROR -> mount failed: %s\n", p->name);
108 return 0;
111 return mount_register (p, mountpoint);
114 bool umount (partition_t *p, char *mountpoint)
116 bool f = 0;
118 char mdir[64];
119 unsigned mdir_len = strlen (mountpoint);
121 /* FIXME: you can umount wrong local directory */
122 if (mountpoint[0] != '/') {
123 mdir[0] = '/';
124 memcpy (mdir+1, mountpoint, mdir_len);
125 mdir_len ++;
126 } else
127 memcpy (mdir, mountpoint, mdir_len);
129 mdir[mdir_len] = '\0';
131 mount_t *mnt;
132 for (mnt = mount_list.next; mnt != &mount_list; mnt = mnt->next) {
133 if (!strcmp (mnt->mountpoint, mdir)) {
134 if (mnt->p == p) {
135 f = 1;
136 break;
141 if (f) {
142 /*dev_t *dev = (dev_t *) dev_findbypartition (p);
144 if (!dev)
145 return 0;
147 if (!dev->handler (DEV_ACT_UMOUNT, p, mnt->mountpoint, dir, NULL)) {
148 kprintf ("ERROR -> umount failed: %s\n", p->name);
149 return 0;
152 while (vfs_list_delbymp (mdir))
153 schedule ();
155 mnt->next->prev = mnt->prev;
156 mnt->prev->next = mnt->next;
158 //kfree (mnt);
159 return 1;
162 return 0;