Rev 2445 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2445 | Rev 2474 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2007 Michal Konopa |
2 | * Copyright (c) 2007 Michal Konopa |
3 | * Copyright (c) 2007 Martin Jelen |
3 | * Copyright (c) 2007 Martin Jelen |
4 | * Copyright (c) 2007 Peter Majer |
4 | * Copyright (c) 2007 Peter Majer |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
9 | * are met: |
10 | * |
10 | * |
11 | * - Redistributions of source code must retain the above copyright |
11 | * - Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * - Redistributions in binary form must reproduce the above copyright |
13 | * - Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
16 | * - The name of the author may not be used to endorse or promote products |
16 | * - The name of the author may not be used to endorse or promote products |
17 | * derived from this software without specific prior written permission. |
17 | * derived from this software without specific prior written permission. |
18 | * |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
29 | */ |
30 | 30 | ||
31 | /** @addtogroup libc |
31 | /** @addtogroup libc |
32 | * @{ |
32 | * @{ |
33 | */ |
33 | */ |
34 | 34 | ||
35 | /** |
35 | /** |
36 | * @file file.c |
36 | * @file file.c |
37 | * @brief The user library for working with the file system |
37 | * @brief The user library for working with the file system |
38 | */ |
38 | */ |
39 | 39 | ||
40 | #include <stdio.h> |
40 | #include <stdio.h> |
41 | #include <stdlib.h> |
41 | #include <stdlib.h> |
42 | #include <string.h> |
42 | #include <string.h> |
43 | #include <unistd.h> |
43 | #include <unistd.h> |
44 | #include <async.h> |
44 | #include <async.h> |
45 | #include <ipc/services.h> |
45 | #include <ipc/services.h> |
46 | #include <ipc/ipc.h> |
46 | #include <ipc/ipc.h> |
47 | #include <sys/mman.h> |
47 | #include <sys/mman.h> |
48 | #include <io/file.h> |
48 | #include <io/file.h> |
49 | #include <bool.h> |
49 | #include <bool.h> |
50 | #include <err.h> |
50 | #include <err.h> |
51 | #include <align.h> |
51 | #include <align.h> |
52 | #include "../../../fs/dir.h" |
52 | #include "../../../fs/dir.h" |
53 | #include "../../../share/message.h" |
53 | #include "../../../share/message.h" |
54 | #include "../../../share/shared_proto.h" |
54 | #include "../../../share/shared_proto.h" |
55 | 55 | ||
56 | #define CONNECT_SLEEP_INTERVAL 10000 |
56 | #define CONNECT_SLEEP_INTERVAL 10000 |
57 | #define CONNECT_SLEEP_TIMEOUT 100000 |
57 | #define CONNECT_SLEEP_TIMEOUT 100000 |
58 | 58 | ||
59 | /** |
59 | /** |
60 | * |
60 | * |
61 | */ |
61 | */ |
62 | static int fs_phone; |
62 | static int fs_phone; |
63 | 63 | ||
64 | static file_t *file_connect(); |
64 | static file_t *file_connect(); |
65 | static int file_disconnect(file_t *file); |
65 | static int file_disconnect(file_t *file); |
66 | 66 | ||
67 | /** |
67 | /** |
68 | * Connect to the FS task and share memory with it for further data and |
68 | * Connect to the FS task and share memory with it for further data and |
69 | * extended memory transfers |
69 | * extended memory transfers |
70 | */ |
70 | */ |
71 | file_t *file_connect() { |
71 | file_t *file_connect() { |
72 | file_t *result; |
72 | file_t *result; |
73 | 73 | ||
74 | size_t size; |
74 | size_t size; |
75 | void *share = NULL; |
75 | void *share = NULL; |
76 | int retval; |
76 | int retval; |
77 | 77 | ||
78 | size = ALIGN_UP(BLOCK_SIZE * sizeof(char), PAGE_SIZE); |
78 | size = ALIGN_UP(BLOCK_SIZE * sizeof(char), PAGE_SIZE); |
79 | share = mmap(share, size, AS_AREA_READ | AS_AREA_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0); |
79 | share = mmap(share, size, AS_AREA_READ | AS_AREA_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0); |
80 | if ((int) share < 0) { |
80 | if ((int) share < 0) { |
81 | f_err = F_MMAP_FAILURE; |
81 | f_err = F_MMAP_FAILURE; |
82 | return NULL; |
82 | return NULL; |
83 | } |
83 | } |
84 | 84 | ||
85 | retval = async_req_2(fs_phone, FS_NEW_CONSUMER, task_get_id(), 0, NULL, NULL); |
85 | retval = async_req_2(fs_phone, FS_NEW_CONSUMER, task_get_id(), 0, NULL, NULL); |
86 | if (retval < 0) { |
86 | if (retval < 0) { |
87 | f_err = F_COMM_FAILURE; |
87 | f_err = F_COMM_FAILURE; |
88 | return NULL; |
88 | return NULL; |
89 | } |
89 | } |
90 | 90 | ||
91 | int flags = 0; |
91 | int flags = 0; |
92 | flags = AS_AREA_READ | AS_AREA_WRITE; |
92 | flags = AS_AREA_READ | AS_AREA_WRITE; |
93 | retval = async_req_3(fs_phone, IPC_M_AS_AREA_SEND, (uintptr_t)share, size, flags, NULL, NULL, NULL); |
93 | retval = async_req_3(fs_phone, IPC_M_AS_AREA_SEND, (uintptr_t)share, size, flags, NULL, NULL, NULL); |
94 | if (retval < 0) { |
94 | if (retval < 0) { |
95 | f_err = F_COMM_FAILURE; |
95 | f_err = F_COMM_FAILURE; |
96 | return NULL; |
96 | return NULL; |
97 | } |
97 | } |
98 | 98 | ||
99 | /* Allocating structure for extended message. */ |
99 | /* Allocating structure for extended message. */ |
100 | message_params_t *params = malloc(sizeof(message_params_t)); |
100 | message_params_t *params = malloc(sizeof(message_params_t)); |
101 | memset((void*) params, 0, sizeof(message_params_t)); |
101 | memset((void*) params, 0, sizeof(message_params_t)); |
102 | 102 | ||
103 | result = malloc(sizeof(file_t)); |
103 | result = malloc(sizeof(file_t)); |
104 | result->share = share; |
104 | result->share = share; |
105 | result->size = size; |
105 | result->size = size; |
106 | result->params = params; |
106 | result->params = params; |
107 | 107 | ||
108 | f_err = F_OK; |
108 | f_err = F_OK; |
109 | return result; |
109 | return result; |
110 | } |
110 | } |
111 | 111 | ||
112 | /** |
112 | /** |
113 | * Disconnect from the FS task, unsharing memory and freeing the file data structure |
113 | * Disconnect from the FS task, unsharing memory and freeing the file data structure |
114 | */ |
114 | */ |
115 | int file_disconnect(file_t *file) { |
115 | int file_disconnect(file_t *file) { |
116 | int retval = send_request(fs_phone, FS_DROP_CONSUMER, file->params, file->share); |
116 | int retval = send_request(fs_phone, FS_DROP_CONSUMER, file->params, file->share); |
117 | if (retval < 0) |
117 | if (retval < 0) |
118 | return -1; |
118 | return -1; |
119 | 119 | ||
120 | /* Unmapping share area. */ |
120 | /* Unmapping share area. */ |
121 | retval = munmap(file->share, file->size); |
121 | retval = munmap(file->share, file->size); |
122 | if (retval < 0) |
122 | if (retval < 0) |
123 | return -1; |
123 | return -1; |
124 | 124 | ||
125 | file->size = ALIGN_UP(BLOCK_SIZE * sizeof(char), PAGE_SIZE); |
125 | file->size = ALIGN_UP(BLOCK_SIZE * sizeof(char), PAGE_SIZE); |
126 | file->share = mmap(file->share, file->size, AS_AREA_READ | AS_AREA_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0); |
126 | file->share = mmap(file->share, file->size, AS_AREA_READ | AS_AREA_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0); |
127 | if ((int) (file->share) < 0) |
127 | if ((int) (file->share) < 0) |
128 | return -1; |
128 | return -1; |
129 | 129 | ||
130 | free(file); |
130 | free(file); |
131 | f_err = F_OK; |
131 | f_err = F_OK; |
132 | return F_OK; |
132 | return F_OK; |
133 | } |
133 | } |
134 | 134 | ||
135 | /** |
135 | /** |
136 | * List contents of the current directory |
136 | * List contents of the current directory |
137 | */ |
137 | */ |
138 | dir_item_t *ls(unsigned int *length) { |
138 | dir_item_t *ls(unsigned int *length) { |
139 | dir_item_t *result; |
139 | dir_item_t *result; |
140 | unsigned short entries_num; |
140 | unsigned short entries_num; |
141 | 141 | ||
142 | file_t *shared_file = file_connect(); |
142 | file_t *shared_file = file_connect(); |
143 | if (shared_file == NULL) |
143 | if (shared_file == NULL) |
144 | return NULL; |
144 | return NULL; |
145 | 145 | ||
146 | /* We want lookup our work directory. */ |
146 | /* We want lookup our work directory. */ |
147 | retval = send_request(fs_phone, FS_DSUM, shared_file->params, shared_file->share); |
147 | retval = send_request(fs_phone, FS_DSUM, shared_file->params, shared_file->share); |
148 | if (retval < 0) { |
148 | if (retval < 0) { |
149 | f_err = F_READ_ERROR; |
149 | f_err = F_READ_ERROR; |
150 | return NULL; |
150 | return NULL; |
151 | } |
151 | } |
152 | 152 | ||
153 | entries_num = retval; |
153 | entries_num = retval; |
154 | *length = entries_num; |
154 | *length = entries_num; |
155 | 155 | ||
156 | result = malloc(entries_num * sizeof (dir_item_t)); |
156 | result = malloc(entries_num * sizeof (dir_item_t)); |
157 | 157 | ||
158 | int entry; |
158 | int entry; |
159 | for (entry = 0; entry < entries_num; entry++) { |
159 | for (entry = 0; entry < entries_num; entry++) { |
160 | shared_file->params->entry_number = entry; |
160 | shared_file->params->entry_number = entry; |
161 | retval = send_request(fs_phone, FS_READENTRY, shared_file->params, shared_file->share); |
161 | retval = send_request(fs_phone, FS_READENTRY, shared_file->params, shared_file->share); |
162 | if (retval < 0) { |
162 | if (retval < 0) { |
163 | f_err = F_READ_ERROR; |
163 | f_err = F_READ_ERROR; |
164 | return NULL; |
164 | return NULL; |
165 | } |
165 | } |
166 | 166 | ||
167 | memcpy(&(result[entry].inode_num), shared_file->share, sizeof(unsigned short)); |
167 | memcpy(&(result[entry].inode_num), shared_file->share, sizeof(unsigned short)); |
168 | memcpy(result[entry].name, (void *)(shared_file->share+sizeof(unsigned short)), retval-sizeof(unsigned short)); |
168 | memcpy(result[entry].name, (void *)(shared_file->share+sizeof(unsigned short)), retval-sizeof(unsigned short)); |
169 | 169 | ||
170 | /* Do not show empty entries. */ |
170 | /* Do not show empty entries. */ |
171 | if (!result[entry].inode_num) |
171 | if (!result[entry].inode_num) |
172 | continue; |
172 | continue; |
173 | 173 | ||
174 | } |
174 | } |
175 | return result; |
175 | return result; |
176 | } |
176 | } |
177 | 177 | ||
178 | /** |
178 | /** |
179 | * Change the current working directory for the task |
179 | * Change the current working directory for the task |
180 | */ |
180 | */ |
181 | int chdir(char * new_dir) |
181 | int chdir(char * new_dir) |
182 | { |
182 | { |
183 | file_t *shared_file = file_connect(); |
183 | file_t *shared_file = file_connect(); |
184 | 184 | ||
185 | if (shared_file == NULL) { |
185 | if (shared_file == NULL) { |
186 | f_err = F_READ_ERROR; |
186 | f_err = F_READ_ERROR; |
187 | return F_READ_ERROR; |
187 | return F_READ_ERROR; |
188 | } |
188 | } |
189 | memcpy(shared_file->params->fname, new_dir, 30); |
189 | memcpy(shared_file->params->fname, new_dir, 30); |
190 | 190 | ||
191 | int retval = send_request(fs_phone, FS_CHDIR, shared_file->params, shared_file->share); |
191 | int retval = send_request(fs_phone, FS_CHDIR, shared_file->params, shared_file->share); |
192 | if (retval < 0) { |
192 | if (retval < 0) { |
193 | f_err = F_READ_ERROR; |
193 | f_err = F_READ_ERROR; |
194 | return F_READ_ERROR; |
194 | return F_READ_ERROR; |
195 | } |
195 | } |
196 | 196 | ||
197 | retval = file_disconnect(shared_file); |
197 | retval = file_disconnect(shared_file); |
198 | f_err = F_OK; |
198 | f_err = F_OK; |
199 | return F_OK; |
199 | return F_OK; |
200 | } |
200 | } |
201 | 201 | ||
202 | /** |
202 | /** |
203 | * Open a file for reading and/or writing |
203 | * Open a file for reading and/or writing |
204 | */ |
204 | */ |
205 | file_t *fopen(char *name, int mode) |
205 | file_t *fopen(char *name, int mode) |
206 | { |
206 | { |
207 | file_t *file = file_connect(); |
207 | file_t *file = file_connect(); |
208 | 208 | ||
209 | /* We want to work with the specified file. */ |
209 | /* We want to work with the specified file. */ |
210 | memcpy(file->params->fname, name, 30); |
210 | memcpy(file->params->fname, name, 30); |
211 | 211 | ||
212 | int retval = send_request(fs_phone, FS_OPEN, file->params, file->share); |
212 | int retval = send_request(fs_phone, FS_OPEN, file->params, file->share); |
213 | if (retval < 0) |
213 | if (retval < 0) |
214 | return NULL; |
214 | return NULL; |
215 | 215 | ||
216 | file->handle = retval; |
216 | file->handle = retval; |
217 | 217 | ||
218 | return file; |
218 | return file; |
219 | } |
219 | } |
220 | 220 | ||
221 | /** |
221 | /** |
222 | * Read status information about a file |
222 | * Read status information about a file |
223 | */ |
223 | */ |
224 | int fstat(file_t *file) |
224 | int fstat(file_t *file) |
225 | { |
225 | { |
226 | memcpy(file->params->fname, file->base_info.name, 30); |
226 | memcpy(file->params->fname, file->base_info.name, 30); |
227 | file->params->fd = file->handle; |
227 | file->params->fd = file->handle; |
228 | 228 | ||
229 | int retval = send_request(fs_phone, FS_FSTAT, file->params, file->share); |
229 | int retval = send_request(fs_phone, FS_FSTAT, file->params, file->share); |
230 | if (retval < 0) |
230 | if (retval < 0) |
231 | return -1; |
231 | return -1; |
232 | 232 | ||
233 | memcpy((void *)(&file->stat), file->share, sizeof(stat_t)); |
233 | memcpy((void *)(&file->stat), file->share, sizeof(stat_t)); |
234 | 234 | ||
235 | f_err = F_OK; |
235 | f_err = F_OK; |
236 | return F_OK; |
236 | return F_OK; |
237 | } |
237 | } |
238 | 238 | ||
239 | /** |
239 | /** |
240 | * Read data from a file |
240 | * Read data from a file |
241 | */ |
241 | */ |
242 | int fread(file_t *file, void* buffer, unsigned int size) |
242 | int fread(file_t *file, void* buffer, unsigned int size) |
243 | { |
243 | { |
244 | file->params->nbytes = size; |
244 | file->params->nbytes = size; |
245 | 245 | ||
246 | int retval = send_request(fs_phone, FS_READ, file->params, file->share); |
246 | int retval = send_request(fs_phone, FS_READ, file->params, file->share); |
247 | if (retval < 0) |
247 | if (retval < 0) |
248 | return -1; |
248 | return -1; |
249 | 249 | ||
250 | f_err = F_OK; |
250 | f_err = F_OK; |
251 | return F_OK; |
251 | return F_OK; |
252 | } |
252 | } |
253 | 253 | ||
254 | /** |
254 | /** |
255 | * Seek to a position within a file |
255 | * Seek to a position within a file |
256 | */ |
256 | */ |
257 | int fseek(file_t *file, int offset, int whence) |
257 | int fseek(file_t *file, int offset, int whence) |
258 | { |
258 | { |
259 | file->params->offset = 0; |
259 | file->params->offset = 0; |
260 | file->params->whence = 0; /* from beginning of the file */ |
260 | file->params->whence = 0; /* from beginning of the file */ |
261 | 261 | ||
262 | int retval = send_request(fs_phone, FS_SEEK, file->params, file->share); |
262 | int retval = send_request(fs_phone, FS_SEEK, file->params, file->share); |
263 | if (retval < 0) |
263 | if (retval < 0) |
264 | return -1; |
264 | return -1; |
265 | 265 | ||
266 | f_err = F_OK; |
266 | f_err = F_OK; |
267 | return F_OK; |
267 | return F_OK; |
268 | } |
268 | } |
269 | 269 | ||
270 | /** |
270 | /** |
271 | * Close a file |
271 | * Close a file |
272 | */ |
272 | */ |
273 | int fclose(file_t *file) |
273 | int fclose(file_t *file) |
274 | { |
274 | { |
275 | int retval = send_request(fs_phone, FS_CLOSE, file->params, file->share); |
275 | int retval = send_request(fs_phone, FS_CLOSE, file->params, file->share); |
276 | if (retval < 0) |
276 | if (retval < 0) |
277 | return -1; |
277 | return -1; |
278 | 278 | ||
279 | if (file != NULL) |
279 | if (file != NULL) |
280 | file_disconnect(file); |
280 | file_disconnect(file); |
281 | 281 | ||
282 | f_err = F_OK; |
282 | f_err = F_OK; |
283 | return F_OK; |
283 | return F_OK; |
284 | } |
284 | } |
285 | 285 | ||
286 | /** |
286 | /** |
287 | *@} |
287 | *@} |
288 | */ |
288 | */ |
289 | 289 |