1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief LibIO Internal Interface |
---|
5 | * |
---|
6 | * This file is the libio internal interface. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2011. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * Modifications to support reference counting in the file system are |
---|
14 | * Copyright (c) 2012 embedded brains GmbH. |
---|
15 | * |
---|
16 | * The license and distribution terms for this file may be |
---|
17 | * found in the file LICENSE in this distribution or at |
---|
18 | * http://www.rtems.com/license/LICENSE. |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef _RTEMS_RTEMS_LIBIO__H |
---|
22 | #define _RTEMS_RTEMS_LIBIO__H |
---|
23 | |
---|
24 | #include <errno.h> |
---|
25 | |
---|
26 | #include <rtems.h> |
---|
27 | #include <rtems/libio.h> |
---|
28 | #include <rtems/seterr.h> |
---|
29 | |
---|
30 | #ifdef __cplusplus |
---|
31 | extern "C" { |
---|
32 | #endif |
---|
33 | |
---|
34 | /** |
---|
35 | * @defgroup LibIOInternal IO Internal Library |
---|
36 | * |
---|
37 | * @brief Internal IO library API and implementation. |
---|
38 | * |
---|
39 | */ |
---|
40 | /**@{**/ |
---|
41 | |
---|
42 | #define RTEMS_FILESYSTEM_SYMLOOP_MAX 32 |
---|
43 | |
---|
44 | /* |
---|
45 | * Not defined in newlib so provide here. Users should use dup2 and |
---|
46 | * not this non-portable fcntl command. Provided here to allow the |
---|
47 | * RTEMS implementation to work. |
---|
48 | */ |
---|
49 | #define F_DUP2FD 20 |
---|
50 | |
---|
51 | /* |
---|
52 | * Semaphore to protect the io table |
---|
53 | */ |
---|
54 | |
---|
55 | #define RTEMS_LIBIO_SEM rtems_build_name('L', 'B', 'I', 'O') |
---|
56 | #define RTEMS_LIBIO_IOP_SEM(n) rtems_build_name('L', 'B', 'I', n) |
---|
57 | |
---|
58 | extern rtems_id rtems_libio_semaphore; |
---|
59 | |
---|
60 | /* |
---|
61 | * File descriptor Table Information |
---|
62 | */ |
---|
63 | |
---|
64 | extern uint32_t rtems_libio_number_iops; |
---|
65 | extern rtems_libio_t *rtems_libio_iops; |
---|
66 | extern rtems_libio_t *rtems_libio_last_iop; |
---|
67 | extern rtems_libio_t *rtems_libio_iop_freelist; |
---|
68 | |
---|
69 | extern const rtems_filesystem_file_handlers_r rtems_filesystem_null_handlers; |
---|
70 | |
---|
71 | extern rtems_filesystem_mount_table_entry_t rtems_filesystem_null_mt_entry; |
---|
72 | |
---|
73 | /** |
---|
74 | * @brief The global null location. |
---|
75 | * |
---|
76 | * Every operation and the open and fstat handlers of this location returns an |
---|
77 | * error status. The errno is not touched by these operations and handlers. |
---|
78 | * The purpose of this location is to deliver the error return status for a |
---|
79 | * previous error condition which must set the errno accordingly. |
---|
80 | * |
---|
81 | * The usage of this null location instead of the NULL pointer eliminates |
---|
82 | * a lot of branches. |
---|
83 | * |
---|
84 | * The user environment root and current directory are statically initialized |
---|
85 | * with the null location. Due to that all file system services are in a |
---|
86 | * defined state even if no root file system was mounted. |
---|
87 | */ |
---|
88 | extern rtems_filesystem_global_location_t rtems_filesystem_global_location_null; |
---|
89 | |
---|
90 | /* |
---|
91 | * rtems_libio_iop |
---|
92 | * |
---|
93 | * Macro to return the file descriptor pointer. |
---|
94 | */ |
---|
95 | |
---|
96 | #define rtems_libio_iop(_fd) \ |
---|
97 | ((((uint32_t)(_fd)) < rtems_libio_number_iops) ? \ |
---|
98 | &rtems_libio_iops[_fd] : 0) |
---|
99 | |
---|
100 | /* |
---|
101 | * rtems_libio_iop_to_descriptor |
---|
102 | * |
---|
103 | * Macro to convert an internal file descriptor pointer (iop) into |
---|
104 | * the integer file descriptor used by the "section 2" system calls. |
---|
105 | */ |
---|
106 | |
---|
107 | #define rtems_libio_iop_to_descriptor(_iop) \ |
---|
108 | ((!(_iop)) ? -1 : (_iop - rtems_libio_iops)) |
---|
109 | |
---|
110 | /* |
---|
111 | * rtems_libio_check_is_open |
---|
112 | * |
---|
113 | * Macro to check if a file descriptor is actually open. |
---|
114 | */ |
---|
115 | |
---|
116 | #define rtems_libio_check_is_open(_iop) \ |
---|
117 | do { \ |
---|
118 | if (((_iop)->flags & LIBIO_FLAGS_OPEN) == 0) { \ |
---|
119 | errno = EBADF; \ |
---|
120 | return -1; \ |
---|
121 | } \ |
---|
122 | } while (0) |
---|
123 | |
---|
124 | /* |
---|
125 | * rtems_libio_check_fd |
---|
126 | * |
---|
127 | * Macro to check if a file descriptor number is valid. |
---|
128 | */ |
---|
129 | |
---|
130 | #define rtems_libio_check_fd(_fd) \ |
---|
131 | do { \ |
---|
132 | if ((uint32_t) (_fd) >= rtems_libio_number_iops) { \ |
---|
133 | errno = EBADF; \ |
---|
134 | return -1; \ |
---|
135 | } \ |
---|
136 | } while (0) |
---|
137 | |
---|
138 | /* |
---|
139 | * rtems_libio_check_buffer |
---|
140 | * |
---|
141 | * Macro to check if a buffer pointer is valid. |
---|
142 | */ |
---|
143 | |
---|
144 | #define rtems_libio_check_buffer(_buffer) \ |
---|
145 | do { \ |
---|
146 | if ((_buffer) == 0) { \ |
---|
147 | errno = EINVAL; \ |
---|
148 | return -1; \ |
---|
149 | } \ |
---|
150 | } while (0) |
---|
151 | |
---|
152 | /* |
---|
153 | * rtems_libio_check_count |
---|
154 | * |
---|
155 | * Macro to check if a count or length is valid. |
---|
156 | */ |
---|
157 | |
---|
158 | #define rtems_libio_check_count(_count) \ |
---|
159 | do { \ |
---|
160 | if ((_count) == 0) { \ |
---|
161 | return 0; \ |
---|
162 | } \ |
---|
163 | } while (0) |
---|
164 | |
---|
165 | /* |
---|
166 | * rtems_libio_check_permissions_with_error |
---|
167 | * |
---|
168 | * Macro to check if a file descriptor is open for this operation. |
---|
169 | * On failure, return the user specified error. |
---|
170 | */ |
---|
171 | |
---|
172 | #define rtems_libio_check_permissions_with_error(_iop, _flag, _errno) \ |
---|
173 | do { \ |
---|
174 | if (((_iop)->flags & (_flag)) == 0) { \ |
---|
175 | rtems_set_errno_and_return_minus_one( _errno ); \ |
---|
176 | return -1; \ |
---|
177 | } \ |
---|
178 | } while (0) |
---|
179 | |
---|
180 | /* |
---|
181 | * rtems_libio_check_permissions |
---|
182 | * |
---|
183 | * Macro to check if a file descriptor is open for this operation. |
---|
184 | * On failure, return EINVAL |
---|
185 | */ |
---|
186 | |
---|
187 | #define rtems_libio_check_permissions(_iop, _flag) \ |
---|
188 | rtems_libio_check_permissions_with_error(_iop, _flag, EINVAL ) |
---|
189 | |
---|
190 | /** |
---|
191 | * @brief Clones a node. |
---|
192 | * |
---|
193 | * The caller must hold the file system instance lock. |
---|
194 | * |
---|
195 | * @param[out] clone The cloned location. |
---|
196 | * @param[in] master The master location. |
---|
197 | * |
---|
198 | * @see rtems_filesystem_instance_lock(). |
---|
199 | */ |
---|
200 | void rtems_filesystem_location_clone( |
---|
201 | rtems_filesystem_location_info_t *clone, |
---|
202 | const rtems_filesystem_location_info_t *master |
---|
203 | ); |
---|
204 | |
---|
205 | /** |
---|
206 | * @brief Returns the type of a node. |
---|
207 | * |
---|
208 | * This function obtains and releases the file system instance lock. |
---|
209 | * |
---|
210 | * @param[in] loc The location of the node. |
---|
211 | * |
---|
212 | * @retval type The node type. |
---|
213 | * |
---|
214 | * @see rtems_filesystem_instance_lock(). |
---|
215 | */ |
---|
216 | rtems_filesystem_node_types_t rtems_filesystem_node_type( |
---|
217 | const rtems_filesystem_location_info_t *loc |
---|
218 | ); |
---|
219 | |
---|
220 | /** |
---|
221 | * @brief Releases all resources of a location. |
---|
222 | * |
---|
223 | * This function may block on a mutex and may complete an unmount process. |
---|
224 | * |
---|
225 | * @param[in] loc The location to free. |
---|
226 | * |
---|
227 | * @note The file system root location is released by the file system |
---|
228 | * instance destruction handler (see @ref rtems_filesystem_fsunmount_me_t). |
---|
229 | * |
---|
230 | * @see rtems_filesystem_freenode_t. |
---|
231 | */ |
---|
232 | void rtems_filesystem_location_free( rtems_filesystem_location_info_t *loc ); |
---|
233 | |
---|
234 | /* |
---|
235 | * External structures |
---|
236 | */ |
---|
237 | #include <rtems/userenv.h> |
---|
238 | |
---|
239 | static inline void rtems_libio_lock( void ) |
---|
240 | { |
---|
241 | rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT ); |
---|
242 | } |
---|
243 | |
---|
244 | static inline void rtems_libio_unlock( void ) |
---|
245 | { |
---|
246 | rtems_semaphore_release( rtems_libio_semaphore ); |
---|
247 | } |
---|
248 | |
---|
249 | static inline void rtems_filesystem_mt_lock( void ) |
---|
250 | { |
---|
251 | rtems_libio_lock(); |
---|
252 | } |
---|
253 | |
---|
254 | static inline void rtems_filesystem_mt_unlock( void ) |
---|
255 | { |
---|
256 | rtems_libio_unlock(); |
---|
257 | } |
---|
258 | |
---|
259 | extern rtems_interrupt_lock rtems_filesystem_mt_entry_lock_control; |
---|
260 | |
---|
261 | #define rtems_filesystem_mt_entry_declare_lock_context( ctx ) \ |
---|
262 | rtems_interrupt_level ctx |
---|
263 | |
---|
264 | #define rtems_filesystem_mt_entry_lock( ctx ) \ |
---|
265 | rtems_interrupt_lock_acquire( &rtems_filesystem_mt_entry_lock_control, ctx ) |
---|
266 | |
---|
267 | #define rtems_filesystem_mt_entry_unlock( ctx ) \ |
---|
268 | rtems_interrupt_lock_release( &rtems_filesystem_mt_entry_lock_control, ctx ) |
---|
269 | |
---|
270 | static inline void rtems_filesystem_instance_lock( |
---|
271 | const rtems_filesystem_location_info_t *loc |
---|
272 | ) |
---|
273 | { |
---|
274 | const rtems_filesystem_mount_table_entry_t *mt_entry = loc->mt_entry; |
---|
275 | |
---|
276 | (*mt_entry->ops->lock_h)( mt_entry ); |
---|
277 | } |
---|
278 | |
---|
279 | static inline void rtems_filesystem_instance_unlock( |
---|
280 | const rtems_filesystem_location_info_t *loc |
---|
281 | ) |
---|
282 | { |
---|
283 | const rtems_filesystem_mount_table_entry_t *mt_entry = loc->mt_entry; |
---|
284 | |
---|
285 | (*mt_entry->ops->unlock_h)( mt_entry ); |
---|
286 | } |
---|
287 | |
---|
288 | /* |
---|
289 | * File Descriptor Routine Prototypes |
---|
290 | */ |
---|
291 | |
---|
292 | /** |
---|
293 | * This routine searches the IOP Table for an unused entry. If it |
---|
294 | * finds one, it returns it. Otherwise, it returns NULL. |
---|
295 | */ |
---|
296 | rtems_libio_t *rtems_libio_allocate(void); |
---|
297 | |
---|
298 | /** |
---|
299 | * Convert UNIX fnctl(2) flags to ones that RTEMS drivers understand |
---|
300 | */ |
---|
301 | uint32_t rtems_libio_fcntl_flags( int fcntl_flags ); |
---|
302 | |
---|
303 | /** |
---|
304 | * Convert RTEMS internal flags to UNIX fnctl(2) flags |
---|
305 | */ |
---|
306 | int rtems_libio_to_fcntl_flags( uint32_t flags ); |
---|
307 | |
---|
308 | /** |
---|
309 | * This routine frees the resources associated with an IOP (file descriptor) |
---|
310 | * and clears the slot in the IOP Table. |
---|
311 | */ |
---|
312 | void rtems_libio_free( |
---|
313 | rtems_libio_t *iop |
---|
314 | ); |
---|
315 | |
---|
316 | /* |
---|
317 | * File System Routine Prototypes |
---|
318 | */ |
---|
319 | |
---|
320 | rtems_filesystem_location_info_t * |
---|
321 | rtems_filesystem_eval_path_start( |
---|
322 | rtems_filesystem_eval_path_context_t *ctx, |
---|
323 | const char *path, |
---|
324 | int eval_flags |
---|
325 | ); |
---|
326 | |
---|
327 | rtems_filesystem_location_info_t * |
---|
328 | rtems_filesystem_eval_path_start_with_parent( |
---|
329 | rtems_filesystem_eval_path_context_t *ctx, |
---|
330 | const char *path, |
---|
331 | int eval_flags, |
---|
332 | rtems_filesystem_location_info_t *parentloc, |
---|
333 | int parent_eval_flags |
---|
334 | ); |
---|
335 | |
---|
336 | rtems_filesystem_location_info_t * |
---|
337 | rtems_filesystem_eval_path_start_with_root_and_current( |
---|
338 | rtems_filesystem_eval_path_context_t *ctx, |
---|
339 | const char *path, |
---|
340 | int eval_flags, |
---|
341 | rtems_filesystem_global_location_t *const *global_root_ptr, |
---|
342 | rtems_filesystem_global_location_t *const *global_current_ptr |
---|
343 | ); |
---|
344 | |
---|
345 | void rtems_filesystem_eval_path_continue( |
---|
346 | rtems_filesystem_eval_path_context_t *ctx |
---|
347 | ); |
---|
348 | |
---|
349 | void rtems_filesystem_eval_path_cleanup( |
---|
350 | rtems_filesystem_eval_path_context_t *ctx |
---|
351 | ); |
---|
352 | |
---|
353 | void rtems_filesystem_eval_path_recursive( |
---|
354 | rtems_filesystem_eval_path_context_t *ctx, |
---|
355 | const char *path, |
---|
356 | size_t pathlen |
---|
357 | ); |
---|
358 | |
---|
359 | void rtems_filesystem_eval_path_cleanup_with_parent( |
---|
360 | rtems_filesystem_eval_path_context_t *ctx, |
---|
361 | rtems_filesystem_location_info_t *parentloc |
---|
362 | ); |
---|
363 | |
---|
364 | /** |
---|
365 | * @brief Requests a path evaluation restart. |
---|
366 | * |
---|
367 | * Sets the start and current location to the new start location. The caller |
---|
368 | * must terminate its current evaluation process. The path evaluation |
---|
369 | * continues in the next loop iteration within |
---|
370 | * rtems_filesystem_eval_path_continue(). This avoids recursive invokations. |
---|
371 | * The function obtains the new start location and clones it to set the new |
---|
372 | * current location. The previous start and current locations are released. |
---|
373 | * |
---|
374 | * @param[in, out] ctx The path evaluation context. |
---|
375 | * @param[in, out] newstartloc_ptr Pointer to the new start location. |
---|
376 | */ |
---|
377 | void rtems_filesystem_eval_path_restart( |
---|
378 | rtems_filesystem_eval_path_context_t *ctx, |
---|
379 | rtems_filesystem_global_location_t **newstartloc_ptr |
---|
380 | ); |
---|
381 | |
---|
382 | typedef enum { |
---|
383 | RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_CONTINUE, |
---|
384 | RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_DONE, |
---|
385 | RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_NO_ENTRY |
---|
386 | } rtems_filesystem_eval_path_generic_status; |
---|
387 | |
---|
388 | /** |
---|
389 | * @brief Tests if the current location is a directory. |
---|
390 | * |
---|
391 | * @param[in, out] ctx The path evaluation context. |
---|
392 | * @param[in, out] arg The handler argument. |
---|
393 | * |
---|
394 | * @retval true The current location is a directory. |
---|
395 | * @retval false Otherwise. |
---|
396 | * |
---|
397 | * @see rtems_filesystem_eval_path_generic(). |
---|
398 | */ |
---|
399 | typedef bool (*rtems_filesystem_eval_path_is_directory)( |
---|
400 | rtems_filesystem_eval_path_context_t *ctx, |
---|
401 | void *arg |
---|
402 | ); |
---|
403 | |
---|
404 | /** |
---|
405 | * @brief Evaluates a token. |
---|
406 | * |
---|
407 | * @param[in, out] ctx The path evaluation context. |
---|
408 | * @param[in, out] arg The handler argument. |
---|
409 | * @param[in] token The token contents. |
---|
410 | * @param[in] tokenlen The token length in characters. |
---|
411 | * |
---|
412 | * @retval status The generic path evaluation status. |
---|
413 | * |
---|
414 | * @see rtems_filesystem_eval_path_generic(). |
---|
415 | */ |
---|
416 | typedef rtems_filesystem_eval_path_generic_status |
---|
417 | (*rtems_filesystem_eval_path_eval_token)( |
---|
418 | rtems_filesystem_eval_path_context_t *ctx, |
---|
419 | void *arg, |
---|
420 | const char *token, |
---|
421 | size_t tokenlen |
---|
422 | ); |
---|
423 | |
---|
424 | typedef struct { |
---|
425 | rtems_filesystem_eval_path_is_directory is_directory; |
---|
426 | rtems_filesystem_eval_path_eval_token eval_token; |
---|
427 | } rtems_filesystem_eval_path_generic_config; |
---|
428 | |
---|
429 | void rtems_filesystem_eval_path_generic( |
---|
430 | rtems_filesystem_eval_path_context_t *ctx, |
---|
431 | void *arg, |
---|
432 | const rtems_filesystem_eval_path_generic_config *config |
---|
433 | ); |
---|
434 | |
---|
435 | void rtems_filesystem_initialize(void); |
---|
436 | |
---|
437 | /** |
---|
438 | * @brief Copies a location. |
---|
439 | * |
---|
440 | * A bitwise copy is performed. The destination location will be added to the |
---|
441 | * corresponding mount entry. |
---|
442 | * |
---|
443 | * @param[out] dst The destination location. |
---|
444 | * @param[in] src The source location. |
---|
445 | * |
---|
446 | * @retval dst The destination location. |
---|
447 | * |
---|
448 | * @see rtems_filesystem_location_clone(). |
---|
449 | */ |
---|
450 | rtems_filesystem_location_info_t *rtems_filesystem_location_copy( |
---|
451 | rtems_filesystem_location_info_t *dst, |
---|
452 | const rtems_filesystem_location_info_t *src |
---|
453 | ); |
---|
454 | |
---|
455 | static inline rtems_filesystem_location_info_t * |
---|
456 | rtems_filesystem_location_initialize_to_null( |
---|
457 | rtems_filesystem_location_info_t *loc |
---|
458 | ) |
---|
459 | { |
---|
460 | return rtems_filesystem_location_copy( |
---|
461 | loc, |
---|
462 | &rtems_filesystem_global_location_null.location |
---|
463 | ); |
---|
464 | } |
---|
465 | |
---|
466 | rtems_filesystem_global_location_t * |
---|
467 | rtems_filesystem_location_transform_to_global( |
---|
468 | rtems_filesystem_location_info_t *loc |
---|
469 | ); |
---|
470 | |
---|
471 | /** |
---|
472 | * @brief Assigns a global file system location. |
---|
473 | * |
---|
474 | * @param[in, out] lhs_global_loc_ptr Pointer to the global left hand side file |
---|
475 | * system location. The current left hand side location will be released. |
---|
476 | * @param[in] rhs_global_loc The global right hand side file system location. |
---|
477 | */ |
---|
478 | void rtems_filesystem_global_location_assign( |
---|
479 | rtems_filesystem_global_location_t **lhs_global_loc_ptr, |
---|
480 | rtems_filesystem_global_location_t *rhs_global_loc |
---|
481 | ); |
---|
482 | |
---|
483 | /** |
---|
484 | * @brief Obtains a global file system location. |
---|
485 | * |
---|
486 | * Deferred releases will be processed in this function. |
---|
487 | * |
---|
488 | * This function must be called from normal thread context and may block on a |
---|
489 | * mutex. Thread dispatching is disabled to protect some critical sections. |
---|
490 | * |
---|
491 | * @param[in] global_loc_ptr Pointer to the global file system location. |
---|
492 | * |
---|
493 | * @return A global file system location. It returns always a valid object. |
---|
494 | * In case of an error, the global null location will be returned. Each |
---|
495 | * operation or handler of the null location returns an error status. The |
---|
496 | * errno indicates the error. The NULL pointer is never returned. |
---|
497 | * |
---|
498 | * @see rtems_filesystem_location_transform_to_global(), |
---|
499 | * rtems_filesystem_global_location_obtain_null(), and |
---|
500 | * rtems_filesystem_global_location_release(). |
---|
501 | */ |
---|
502 | rtems_filesystem_global_location_t *rtems_filesystem_global_location_obtain( |
---|
503 | rtems_filesystem_global_location_t *const *global_loc_ptr |
---|
504 | ); |
---|
505 | |
---|
506 | /** |
---|
507 | * @brief Releases a global file system location. |
---|
508 | * |
---|
509 | * In case the reference count reaches zero, all associated resources will be |
---|
510 | * released. This may include the complete unmount of the corresponding file |
---|
511 | * system instance. |
---|
512 | * |
---|
513 | * This function may block on a mutex. It may be called within critical |
---|
514 | * sections of the operating system. In this case the release will be |
---|
515 | * deferred. The next obtain call will do the actual release. |
---|
516 | * |
---|
517 | * @param[in] global_loc The global file system location. It must not be NULL. |
---|
518 | * |
---|
519 | * @see rtems_filesystem_global_location_obtain(). |
---|
520 | */ |
---|
521 | void rtems_filesystem_global_location_release( |
---|
522 | rtems_filesystem_global_location_t *global_loc |
---|
523 | ); |
---|
524 | |
---|
525 | void rtems_filesystem_location_detach( |
---|
526 | rtems_filesystem_location_info_t *detach |
---|
527 | ); |
---|
528 | |
---|
529 | void rtems_filesystem_location_copy_and_detach( |
---|
530 | rtems_filesystem_location_info_t *copy, |
---|
531 | rtems_filesystem_location_info_t *detach |
---|
532 | ); |
---|
533 | |
---|
534 | static inline rtems_filesystem_global_location_t * |
---|
535 | rtems_filesystem_global_location_obtain_null(void) |
---|
536 | { |
---|
537 | rtems_filesystem_global_location_t *global_loc = NULL; |
---|
538 | |
---|
539 | return rtems_filesystem_global_location_obtain( &global_loc ); |
---|
540 | } |
---|
541 | |
---|
542 | static inline bool rtems_filesystem_location_is_null( |
---|
543 | const rtems_filesystem_location_info_t *loc |
---|
544 | ) |
---|
545 | { |
---|
546 | return loc->handlers == &rtems_filesystem_null_handlers; |
---|
547 | } |
---|
548 | |
---|
549 | static inline bool rtems_filesystem_global_location_is_null( |
---|
550 | const rtems_filesystem_global_location_t *global_loc |
---|
551 | ) |
---|
552 | { |
---|
553 | return rtems_filesystem_location_is_null( &global_loc->location ); |
---|
554 | } |
---|
555 | |
---|
556 | static inline void rtems_filesystem_location_error( |
---|
557 | const rtems_filesystem_location_info_t *loc, |
---|
558 | int eno |
---|
559 | ) |
---|
560 | { |
---|
561 | if ( !rtems_filesystem_location_is_null( loc ) ) { |
---|
562 | errno = eno; |
---|
563 | } |
---|
564 | } |
---|
565 | |
---|
566 | int rtems_filesystem_mknod( |
---|
567 | const rtems_filesystem_location_info_t *parentloc, |
---|
568 | const char *name, |
---|
569 | size_t namelen, |
---|
570 | mode_t mode, |
---|
571 | dev_t dev |
---|
572 | ); |
---|
573 | |
---|
574 | int rtems_filesystem_chdir( rtems_filesystem_location_info_t *loc ); |
---|
575 | |
---|
576 | int rtems_filesystem_chmod( |
---|
577 | const rtems_filesystem_location_info_t *loc, |
---|
578 | mode_t mode |
---|
579 | ); |
---|
580 | |
---|
581 | int rtems_filesystem_chown( |
---|
582 | const rtems_filesystem_location_info_t *loc, |
---|
583 | uid_t owner, |
---|
584 | gid_t group |
---|
585 | ); |
---|
586 | |
---|
587 | static inline bool rtems_filesystem_is_ready_for_unmount( |
---|
588 | rtems_filesystem_mount_table_entry_t *mt_entry |
---|
589 | ) |
---|
590 | { |
---|
591 | bool ready = !mt_entry->mounted |
---|
592 | && rtems_chain_has_only_one_node( &mt_entry->location_chain ) |
---|
593 | && mt_entry->mt_fs_root->reference_count == 1; |
---|
594 | |
---|
595 | if ( ready ) { |
---|
596 | rtems_chain_initialize_empty( &mt_entry->location_chain ); |
---|
597 | } |
---|
598 | |
---|
599 | return ready; |
---|
600 | } |
---|
601 | |
---|
602 | static inline void rtems_filesystem_location_add_to_mt_entry( |
---|
603 | rtems_filesystem_location_info_t *loc |
---|
604 | ) |
---|
605 | { |
---|
606 | rtems_filesystem_mt_entry_declare_lock_context( lock_context ); |
---|
607 | |
---|
608 | rtems_filesystem_mt_entry_lock( lock_context ); |
---|
609 | rtems_chain_append_unprotected( |
---|
610 | &loc->mt_entry->location_chain, |
---|
611 | &loc->mt_entry_node |
---|
612 | ); |
---|
613 | rtems_filesystem_mt_entry_unlock( lock_context ); |
---|
614 | } |
---|
615 | |
---|
616 | void rtems_filesystem_location_remove_from_mt_entry( |
---|
617 | rtems_filesystem_location_info_t *loc |
---|
618 | ); |
---|
619 | |
---|
620 | void rtems_filesystem_do_unmount( |
---|
621 | rtems_filesystem_mount_table_entry_t *mt_entry |
---|
622 | ); |
---|
623 | |
---|
624 | static inline bool rtems_filesystem_location_is_instance_root( |
---|
625 | const rtems_filesystem_location_info_t *loc |
---|
626 | ) |
---|
627 | { |
---|
628 | const rtems_filesystem_mount_table_entry_t *mt_entry = loc->mt_entry; |
---|
629 | |
---|
630 | return (*mt_entry->ops->are_nodes_equal_h)( |
---|
631 | loc, |
---|
632 | &mt_entry->mt_fs_root->location |
---|
633 | ); |
---|
634 | } |
---|
635 | |
---|
636 | static inline const char *rtems_filesystem_eval_path_get_path( |
---|
637 | rtems_filesystem_eval_path_context_t *ctx |
---|
638 | ) |
---|
639 | { |
---|
640 | return ctx->path; |
---|
641 | } |
---|
642 | |
---|
643 | static inline size_t rtems_filesystem_eval_path_get_pathlen( |
---|
644 | rtems_filesystem_eval_path_context_t *ctx |
---|
645 | ) |
---|
646 | { |
---|
647 | return ctx->pathlen; |
---|
648 | } |
---|
649 | |
---|
650 | static inline void rtems_filesystem_eval_path_set_path( |
---|
651 | rtems_filesystem_eval_path_context_t *ctx, |
---|
652 | const char *path, |
---|
653 | size_t pathlen |
---|
654 | ) |
---|
655 | { |
---|
656 | ctx->path = path; |
---|
657 | ctx->pathlen = pathlen; |
---|
658 | } |
---|
659 | |
---|
660 | static inline void rtems_filesystem_eval_path_clear_path( |
---|
661 | rtems_filesystem_eval_path_context_t *ctx |
---|
662 | ) |
---|
663 | { |
---|
664 | ctx->pathlen = 0; |
---|
665 | } |
---|
666 | |
---|
667 | static inline const char *rtems_filesystem_eval_path_get_token( |
---|
668 | rtems_filesystem_eval_path_context_t *ctx |
---|
669 | ) |
---|
670 | { |
---|
671 | return ctx->token; |
---|
672 | } |
---|
673 | |
---|
674 | static inline size_t rtems_filesystem_eval_path_get_tokenlen( |
---|
675 | rtems_filesystem_eval_path_context_t *ctx |
---|
676 | ) |
---|
677 | { |
---|
678 | return ctx->tokenlen; |
---|
679 | } |
---|
680 | |
---|
681 | static inline void rtems_filesystem_eval_path_set_token( |
---|
682 | rtems_filesystem_eval_path_context_t *ctx, |
---|
683 | const char *token, |
---|
684 | size_t tokenlen |
---|
685 | ) |
---|
686 | { |
---|
687 | ctx->token = token; |
---|
688 | ctx->tokenlen = tokenlen; |
---|
689 | } |
---|
690 | |
---|
691 | static inline void rtems_filesystem_eval_path_clear_token( |
---|
692 | rtems_filesystem_eval_path_context_t *ctx |
---|
693 | ) |
---|
694 | { |
---|
695 | ctx->tokenlen = 0; |
---|
696 | } |
---|
697 | |
---|
698 | static inline void rtems_filesystem_eval_path_put_back_token( |
---|
699 | rtems_filesystem_eval_path_context_t *ctx |
---|
700 | ) |
---|
701 | { |
---|
702 | size_t tokenlen = ctx->tokenlen; |
---|
703 | |
---|
704 | ctx->path -= tokenlen; |
---|
705 | ctx->pathlen += tokenlen; |
---|
706 | ctx->tokenlen = 0; |
---|
707 | } |
---|
708 | |
---|
709 | void rtems_filesystem_eval_path_eat_delimiter( |
---|
710 | rtems_filesystem_eval_path_context_t *ctx |
---|
711 | ); |
---|
712 | |
---|
713 | void rtems_filesystem_eval_path_next_token( |
---|
714 | rtems_filesystem_eval_path_context_t *ctx |
---|
715 | ); |
---|
716 | |
---|
717 | static inline void rtems_filesystem_eval_path_get_next_token( |
---|
718 | rtems_filesystem_eval_path_context_t *ctx, |
---|
719 | const char **token, |
---|
720 | size_t *tokenlen |
---|
721 | ) |
---|
722 | { |
---|
723 | rtems_filesystem_eval_path_next_token(ctx); |
---|
724 | *token = ctx->token; |
---|
725 | *tokenlen = ctx->tokenlen; |
---|
726 | } |
---|
727 | |
---|
728 | static inline rtems_filesystem_location_info_t * |
---|
729 | rtems_filesystem_eval_path_get_currentloc( |
---|
730 | rtems_filesystem_eval_path_context_t *ctx |
---|
731 | ) |
---|
732 | { |
---|
733 | return &ctx->currentloc; |
---|
734 | } |
---|
735 | |
---|
736 | static inline bool rtems_filesystem_eval_path_has_path( |
---|
737 | const rtems_filesystem_eval_path_context_t *ctx |
---|
738 | ) |
---|
739 | { |
---|
740 | return ctx->pathlen > 0; |
---|
741 | } |
---|
742 | |
---|
743 | static inline bool rtems_filesystem_eval_path_has_token( |
---|
744 | const rtems_filesystem_eval_path_context_t *ctx |
---|
745 | ) |
---|
746 | { |
---|
747 | return ctx->tokenlen > 0; |
---|
748 | } |
---|
749 | |
---|
750 | static inline int rtems_filesystem_eval_path_get_flags( |
---|
751 | const rtems_filesystem_eval_path_context_t *ctx |
---|
752 | ) |
---|
753 | { |
---|
754 | return ctx->flags; |
---|
755 | } |
---|
756 | |
---|
757 | static inline void rtems_filesystem_eval_path_set_flags( |
---|
758 | rtems_filesystem_eval_path_context_t *ctx, |
---|
759 | int flags |
---|
760 | ) |
---|
761 | { |
---|
762 | ctx->flags = flags; |
---|
763 | } |
---|
764 | |
---|
765 | static inline void rtems_filesystem_eval_path_clear_and_set_flags( |
---|
766 | rtems_filesystem_eval_path_context_t *ctx, |
---|
767 | int clear, |
---|
768 | int set |
---|
769 | ) |
---|
770 | { |
---|
771 | int flags = ctx->flags; |
---|
772 | |
---|
773 | flags &= ~clear; |
---|
774 | flags |= set; |
---|
775 | |
---|
776 | ctx->flags = flags; |
---|
777 | } |
---|
778 | |
---|
779 | static inline void rtems_filesystem_eval_path_extract_currentloc( |
---|
780 | rtems_filesystem_eval_path_context_t *ctx, |
---|
781 | rtems_filesystem_location_info_t *get |
---|
782 | ) |
---|
783 | { |
---|
784 | rtems_filesystem_location_copy_and_detach( |
---|
785 | get, |
---|
786 | &ctx->currentloc |
---|
787 | ); |
---|
788 | } |
---|
789 | |
---|
790 | void rtems_filesystem_eval_path_error( |
---|
791 | rtems_filesystem_eval_path_context_t *ctx, |
---|
792 | int eno |
---|
793 | ); |
---|
794 | |
---|
795 | /** |
---|
796 | * @brief Checks that the locations exist in the same file system instance. |
---|
797 | * |
---|
798 | * @retval 0 The locations exist and are in the same file system instance. |
---|
799 | * @retval -1 An error occurred. The @c errno indicates the error. |
---|
800 | */ |
---|
801 | int rtems_filesystem_location_exists_in_same_instance_as( |
---|
802 | const rtems_filesystem_location_info_t *a, |
---|
803 | const rtems_filesystem_location_info_t *b |
---|
804 | ); |
---|
805 | |
---|
806 | bool rtems_filesystem_check_access( |
---|
807 | int eval_flags, |
---|
808 | mode_t node_mode, |
---|
809 | uid_t node_uid, |
---|
810 | gid_t node_gid |
---|
811 | ); |
---|
812 | |
---|
813 | bool rtems_filesystem_eval_path_check_access( |
---|
814 | rtems_filesystem_eval_path_context_t *ctx, |
---|
815 | int eval_flags, |
---|
816 | mode_t node_mode, |
---|
817 | uid_t node_uid, |
---|
818 | gid_t node_gid |
---|
819 | ); |
---|
820 | |
---|
821 | static inline bool rtems_filesystem_is_delimiter(char c) |
---|
822 | { |
---|
823 | return c == '/' || c == '\\'; |
---|
824 | } |
---|
825 | |
---|
826 | static inline bool rtems_filesystem_is_current_directory( |
---|
827 | const char *token, |
---|
828 | size_t tokenlen |
---|
829 | ) |
---|
830 | { |
---|
831 | return tokenlen == 1 && token [0] == '.'; |
---|
832 | } |
---|
833 | |
---|
834 | static inline bool rtems_filesystem_is_parent_directory( |
---|
835 | const char *token, |
---|
836 | size_t tokenlen |
---|
837 | ) |
---|
838 | { |
---|
839 | return tokenlen == 2 && token [0] == '.' && token [1] == '.'; |
---|
840 | } |
---|
841 | |
---|
842 | /** @} */ |
---|
843 | |
---|
844 | #ifdef __cplusplus |
---|
845 | } |
---|
846 | #endif |
---|
847 | |
---|
848 | #endif |
---|
849 | /* end of include file */ |
---|