+ libc: file with doxygen groups
[lightOS.git] / libserver / fs.c
blobede0eb5b9e3358f2d0b108515cccd05193e08503
1 /*
2 lightOS libserver
3 Copyright (C) 2006-2009 Jörg Pfähler
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (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, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <libkernel/kernel.h>
20 #include <libkernel/type_noprefix.h>
21 #include <libkernel/message_noprefix.h>
22 #include <libserver/fs_noprefix.h>
24 // TODO: This is a HACK
25 size_t strlen(const char * s)
27 size_t rc = 0;
28 while (s[rc])
30 ++rc;
32 return rc;
34 char *strcpy(char *s1, const char *s2)
36 char * rc = s1;
37 while ((*s1++ = *s2++));
38 return rc;
40 char *strncpy(char *s1, const char *s2, size_t n)
42 char * rc = s1;
43 while (( n > 0) && (*s1++ = *s2++))
45 --n;
47 if (n > 0)
49 while (--n)
51 *s1++ = '\0';
54 return rc;
58 port_id_t _LIBSERVER_get_fs_from_name(port_id_t port,
59 const char *name,
60 char **fsname,
61 void *(*malloc)(size_t))
63 shared_memory_t shm = _LIBKERNEL_create_shared_memory(sizeof(file_name_info));
64 file_name_info *info = shm.address;
65 strncpy(info->name, name, FILENAME_MAX);
67 message_t msg = _LIBKERNEL_create_message_shm(PORT_VFS,
68 MSG_VFS_FIND_FS,
70 &shm,
71 SHM_TRANSFER_OWNERSHIP);
72 _LIBKERNEL_send_message(port, &msg);
73 message_t reply = _LIBKERNEL_create_message(PORT_VFS,
74 MSG_VFS_FIND_FS,
77 0);
78 _LIBKERNEL_add_wait_message(port, &reply);
79 _LIBKERNEL_wait_message(&reply);
81 if (_LIBKERNEL_get_message_attribute(&reply) == SHM_TRANSFER_OWNERSHIP)
83 shared_memory_t shared = _LIBKERNEL_get_shared_memory(&reply);
84 file_name_info *tmp = shared.address;
85 *fsname = malloc(strlen(tmp->name) + 1);
86 strcpy(*fsname, tmp->name);
87 _LIBKERNEL_destroy_shared_memory(&shared);
89 else
91 *fsname = malloc(strlen(&name[reply.param2]) + 1);
92 strcpy(*fsname, &name[reply.param2]);
94 _LIBKERNEL_destroy_shared_memory(&shm);
95 return reply.param1;
98 bool _LIBSERVER_file_exists(port_id_t port,
99 port_id_t fsport,
100 const char *relname)
102 shared_memory_t shm = _LIBKERNEL_create_shared_memory(sizeof(file_name_info));
103 strncpy(shm.address, relname, FILENAME_MAX);
105 message_t msg = _LIBKERNEL_create_message_shm(fsport,
106 MSG_FS_EXISTS_FILE,
108 &shm,
109 SHM_TRANSFER_OWNERSHIP);
110 _LIBKERNEL_send_message(port, &msg);
111 message_t reply = _LIBKERNEL_create_message(fsport,
112 MSG_FS_EXISTS_FILE,
116 _LIBKERNEL_add_wait_message(port, &reply);
117 _LIBKERNEL_wait_message(&reply);
118 return reply.param1;
121 bool _LIBSERVER_create_file(port_id_t port,
122 port_id_t fsport,
123 const char *relname,
124 message_param_t type,
125 message_param_t flags,
126 message_param_t blocksize,
127 message_param_t blockcount)
129 shared_memory_t shm = _LIBKERNEL_create_shared_memory(sizeof(file_info));
130 file_info *info = shm.address;
131 strncpy(info->name, relname, FILENAME_MAX);
132 info->type = type;
133 info->blocksize = blocksize;
134 info->blockcount = blockcount;
136 message_t msg = _LIBKERNEL_create_message_shm(fsport,
137 MSG_FS_CREATE_FILE,
138 flags,
139 &shm,
140 SHM_TRANSFER_OWNERSHIP);
141 _LIBKERNEL_send_message(port, &msg);
142 message_t reply = _LIBKERNEL_create_message(fsport,
143 MSG_FS_CREATE_FILE,
147 _LIBKERNEL_add_wait_message(port, &reply);
148 _LIBKERNEL_wait_message(&reply);
149 return reply.param1;
152 bool _LIBSERVER_open_file(port_id_t port,
153 port_id_t fsport,
154 const char *relname,
155 message_param_t flags)
157 shared_memory_t shm = _LIBKERNEL_create_shared_memory(sizeof(file_info));
158 _LIBSERVER_file_info *info = shm.address;
159 strncpy(info->name, relname, FILENAME_MAX);
161 message_t msg = _LIBKERNEL_create_message_shm(fsport,
162 MSG_FS_OPEN_FILE,
163 flags,
164 &shm,
165 SHM_TRANSFER_OWNERSHIP);
166 _LIBKERNEL_send_message(port, &msg);
167 message_t reply = _LIBKERNEL_create_message(fsport,
168 MSG_FS_OPEN_FILE,
172 _LIBKERNEL_add_wait_message(port, &reply);
173 _LIBKERNEL_wait_message(&reply);
174 if (reply.param1 == 0)
175 return false;
176 return true;