Kernel 0.5.9-r7; New iso9660 filesystem support, better exec () function, some change...
[ZeXOS.git] / kernel / core / partition.c
blob2d2d92310cb4daa7f7d46d934e7b40ee4c8c5432
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 <dev.h>
23 #include <partition.h>
24 #include <fs.h>
25 #include <vfs.h>
27 partition_t partition_list;
28 extern vfs_t vfs_list;
30 partition_t *partition_find (char *name)
32 partition_t *partition;
33 for (partition = partition_list.next; partition != &partition_list; partition = partition->next) {
34 if (!strcmp (partition->name, name))
35 return partition;
38 return 0;
41 /* limited only for _one_ partition per device */
42 partition_t *partition_findbydev (dev_t *dev)
44 unsigned l = strlen (dev->devname);
46 partition_t *partition;
47 for (partition = partition_list.next; partition != &partition_list; partition = partition->next) {
48 if (!strncmp (partition->name, dev->devname, l))
49 return partition;
52 return 0;
55 /* find partition by file in current directory */
56 partition_t *partition_findbyfile (char *file)
58 char pwd[64];
59 strcpy (pwd, (char *) env_get ("PWD"));
61 vfs_t *vfs;
62 for (vfs = vfs_list.next; vfs != &vfs_list; vfs = vfs->next)
63 if (!strcmp (vfs->mountpoint, pwd) &&
64 !cstrcmp (file, vfs->name)) {
65 if (vfs->attrib & VFS_FILEATTR_DIR) {
66 printf ("ERROR -> this is a directory, not an file\n");
67 return 0;
70 /* check permissions */
71 if (vfs->attrib & VFS_FILEATTR_SYSTEM && strcmp ((char *) env_get ("USER"), "root")) {
72 printf ("ERROR -> only root can do that\n");
73 return 0;
77 if (vfs->attrib & VFS_FILEATTR_MOUNTED) {
78 partition_t *p = (partition_t *) mount_find ((char *) env_get ("PWD"));
80 if (p)
81 return p;
82 else {
83 printf ("ERROR -> device not respond\n");
84 return 0;
88 env_set ("PWD", pwd);
90 return 0;
93 printf ("No such file : %s\n", file);
95 return 0;
98 partition_t *partition_add (dev_t *dev, fs_t *fs)
100 if (!dev)
101 return 0;
103 char name[20];
104 unsigned i;
105 for (i = 0; i < 256; i ++) {
106 sprintf (name, "%s%u", dev->devname, i);
107 printf ("#name: %s\n", name);
109 if (partition_find (name) == 0)
110 break;
113 partition_t *partition;
115 /* alloc and init context */
116 partition = (partition_t *) kmalloc (sizeof (partition_t));
117 memset (partition, 0, sizeof (partition_t));
119 strcpy (partition->name, name);
121 partition->id = i;
123 if (!fs)
124 partition->fs = (fs_t *) fs_detect (partition);
125 else
126 partition->fs = fs;
128 if (!partition->fs)
129 return 0;
131 /* add into list */
132 partition->next = &partition_list;
133 partition->prev = partition_list.prev;
134 partition->prev->next = partition;
135 partition->next->prev = partition;
137 return partition;
140 bool partition_del (partition_t *partition)
142 if (partition) {
143 partition->next->prev = partition->prev;
144 partition->prev->next = partition->next;
146 //kfree (vfs);
147 return 1;
150 return 0;
153 unsigned int init_partition ()
155 partition_list.next = &partition_list;
156 partition_list.prev = &partition_list;
158 return 1;