[3b7c123] | 1 | /* |
---|
| 2 | * Copyright (c) 2012 embedded brains GmbH. All rights reserved. |
---|
| 3 | * |
---|
| 4 | * embedded brains GmbH |
---|
| 5 | * Obere Lagerstr. 30 |
---|
| 6 | * 82178 Puchheim |
---|
| 7 | * Germany |
---|
| 8 | * <rtems@embedded-brains.de> |
---|
| 9 | * |
---|
| 10 | * The license and distribution terms for this file may be |
---|
| 11 | * found in the file LICENSE in this distribution or at |
---|
| 12 | * http://www.rtems.com/license/LICENSE. |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #if HAVE_CONFIG_H |
---|
| 16 | #include "config.h" |
---|
| 17 | #endif |
---|
| 18 | |
---|
| 19 | #include <rtems/libio_.h> |
---|
| 20 | |
---|
| 21 | static bool is_fs_root( const rtems_filesystem_location_info_t *loc ) |
---|
| 22 | { |
---|
| 23 | const rtems_filesystem_location_info_t *mt_fs_root = |
---|
| 24 | &loc->mt_entry->mt_fs_root->location; |
---|
| 25 | |
---|
| 26 | return (*loc->ops->are_nodes_equal_h)( loc, mt_fs_root ); |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | static bool is_eval_path_root( |
---|
| 30 | const rtems_filesystem_eval_path_context_t *ctx, |
---|
| 31 | const rtems_filesystem_location_info_t *loc |
---|
| 32 | ) |
---|
| 33 | { |
---|
| 34 | const rtems_filesystem_location_info_t *rootloc = &ctx->rootloc->location; |
---|
| 35 | |
---|
| 36 | return loc->mt_entry == rootloc->mt_entry |
---|
| 37 | && (*loc->ops->are_nodes_equal_h)( loc, rootloc ); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | void rtems_filesystem_eval_path_generic( |
---|
| 41 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 42 | void *arg, |
---|
| 43 | const rtems_filesystem_eval_path_generic_config *config |
---|
| 44 | ) |
---|
| 45 | { |
---|
| 46 | rtems_filesystem_eval_path_generic_status status = |
---|
| 47 | RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_CONTINUE; |
---|
| 48 | |
---|
| 49 | while (status == RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_CONTINUE) { |
---|
| 50 | const char *token; |
---|
| 51 | size_t tokenlen; |
---|
| 52 | |
---|
| 53 | rtems_filesystem_eval_path_get_next_token(ctx, &token, &tokenlen); |
---|
| 54 | |
---|
| 55 | if (tokenlen > 0) { |
---|
| 56 | if ((*config->is_directory)(ctx, arg)) { |
---|
| 57 | if (rtems_filesystem_is_current_directory(token, tokenlen)) { |
---|
| 58 | if (rtems_filesystem_eval_path_has_path(ctx)) { |
---|
| 59 | status = (*config->eval_token)(ctx, arg, ".", 1); |
---|
| 60 | } else { |
---|
| 61 | int eval_flags = rtems_filesystem_eval_path_get_flags(ctx); |
---|
| 62 | |
---|
[2563410] | 63 | if ((eval_flags & RTEMS_FS_REJECT_TERMINAL_DOT) == 0) { |
---|
[3b7c123] | 64 | status = (*config->eval_token)(ctx, arg, ".", 1); |
---|
| 65 | } else { |
---|
| 66 | rtems_filesystem_eval_path_error(ctx, EINVAL); |
---|
| 67 | status = RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_DONE; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | } else if (rtems_filesystem_is_parent_directory(token, tokenlen)) { |
---|
| 71 | rtems_filesystem_location_info_t *currentloc = |
---|
| 72 | rtems_filesystem_eval_path_get_currentloc( ctx ); |
---|
| 73 | |
---|
| 74 | if (is_eval_path_root(ctx, currentloc)) { |
---|
| 75 | /* This prevents the escape from a chroot() environment */ |
---|
| 76 | status = (*config->eval_token)(ctx, arg, ".", 1); |
---|
| 77 | } else if (is_fs_root(currentloc)) { |
---|
| 78 | if (currentloc->mt_entry->mt_point_node != NULL) { |
---|
| 79 | rtems_filesystem_eval_path_put_back_token(ctx); |
---|
| 80 | rtems_filesystem_eval_path_restart( |
---|
| 81 | ctx, |
---|
| 82 | ¤tloc->mt_entry->mt_point_node |
---|
| 83 | ); |
---|
| 84 | status = RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_DONE; |
---|
| 85 | } else { |
---|
| 86 | /* This is the root file system */ |
---|
| 87 | status = (*config->eval_token)(ctx, arg, ".", 1); |
---|
| 88 | } |
---|
| 89 | } else { |
---|
| 90 | status = (*config->eval_token)(ctx, arg, "..", 2); |
---|
| 91 | } |
---|
| 92 | } else { |
---|
| 93 | status = (*config->eval_token)(ctx, arg, token, tokenlen); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | if (status == RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_NO_ENTRY) { |
---|
| 97 | if (rtems_filesystem_eval_path_has_path(ctx)) { |
---|
| 98 | int eval_flags; |
---|
| 99 | |
---|
| 100 | rtems_filesystem_eval_path_eat_delimiter(ctx); |
---|
| 101 | eval_flags = rtems_filesystem_eval_path_get_flags(ctx); |
---|
| 102 | if ( |
---|
[2563410] | 103 | (eval_flags & RTEMS_FS_ACCEPT_RESIDUAL_DELIMITERS) == 0 |
---|
[3b7c123] | 104 | || rtems_filesystem_eval_path_has_path(ctx) |
---|
| 105 | ) { |
---|
| 106 | rtems_filesystem_eval_path_error(ctx, ENOENT); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | } else { |
---|
| 111 | rtems_filesystem_eval_path_error(ctx, ENOTDIR); |
---|
| 112 | status = RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_DONE; |
---|
| 113 | } |
---|
| 114 | } else { |
---|
| 115 | status = RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_DONE; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | } |
---|