Changeset fa44c39 in rtems


Ignore:
Timestamp:
Feb 29, 2020, 3:14:31 PM (4 months ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
5, master
Children:
7e3b5c0
Parents:
13b71f8
git-author:
Sebastian Huber <sebastian.huber@…> (02/29/20 15:14:31)
git-committer:
Sebastian Huber <sebastian.huber@…> (03/04/20 08:30:03)
Message:

imfs: Add IMFS_add_node()

Update #3894.

Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • cpukit/Makefile.am

    r13b71f8 rfa44c39  
    414414librtemscpu_a_SOURCES += libfs/src/dosfs/msdos_statvfs.c
    415415librtemscpu_a_SOURCES += libfs/src/imfs/deviceio.c
     416librtemscpu_a_SOURCES += libfs/src/imfs/imfs_add_node.c
    416417librtemscpu_a_SOURCES += libfs/src/imfs/imfs_chown.c
    417418librtemscpu_a_SOURCES += libfs/src/imfs/imfs_config.c
     
    436437librtemscpu_a_SOURCES += libfs/src/imfs/imfs_mount.c
    437438librtemscpu_a_SOURCES += libfs/src/imfs/imfs_node.c
     439librtemscpu_a_SOURCES += libfs/src/imfs/imfs_node_destroy_default.c
    438440librtemscpu_a_SOURCES += libfs/src/imfs/imfs_rename.c
    439441librtemscpu_a_SOURCES += libfs/src/imfs/imfs_rmnod.c
  • cpukit/include/rtems/imfs.h

    r13b71f8 rfa44c39  
    208208
    209209/**
     210 * @brief Does nothing.
     211 *
     212 * @param node The IMFS node.
     213 *
     214 * @see IMFS_node_control.
     215 */
     216void IMFS_do_nothing_destroy( IMFS_jnode_t *node );
     217
     218/**
    210219 * @brief IMFS node control.
    211220 */
     
    319328  size_t      size;
    320329} IMFS_linearfile_context;
     330
     331static inline IMFS_jnode_t *IMFS_iop_to_node( const rtems_libio_t *iop )
     332{
     333  return (IMFS_jnode_t *) iop->pathinfo.node_access;
     334}
    321335
    322336static inline IMFS_directory_t *IMFS_iop_to_directory(
     
    595609}
    596610
     611/**
     612 * @brief Initializer for an IMFS node control.
     613 *
     614 * @param handlers The file system node handlers.
     615 * @param init The node initialization method.
     616 * @param destroy The node destruction method.
     617 */
     618#define IMFS_NODE_CONTROL_INITIALIZER( handlers, init, destroy ) \
     619  { \
     620    ( handlers ), \
     621    ( init ), \
     622    IMFS_node_remove_default, \
     623    ( destroy ) \
     624  }
     625
     626/**
     627 * @brief Initializer for an IMFS node.
     628 *
     629 * Initialize the node control with IMFS_NODE_CONTROL_INITIALIZER().
     630 *
     631 * @param node_control The node control of the IMFS node.
     632 * @param name The name of the IMFS node.
     633 * @param namelen The length of the name of the IMFS node.
     634 * @param mode The mode of the IMFS node.
     635 *
     636 * @see IMFS_node_preinitialize().
     637 */
     638#define IMFS_NODE_INITIALIZER( node_control, name, namelen, mode ) \
     639  { \
     640    { NULL, NULL }, \
     641    NULL, \
     642    ( name ), \
     643    ( namelen ), \
     644    ( mode ), \
     645    0, \
     646    0, \
     647    0, \
     648    0, \
     649    0, \
     650    0, \
     651    0, \
     652    ( node_control ) \
     653  }
     654
     655/**
     656 * @brief Preinitializes an IMFS node.
     657 *
     658 * Initialize the node control with IMFS_NODE_CONTROL_INITIALIZER().
     659 *
     660 * @param node The IMFS node to preinitialize.
     661 * @param node_control The node control of the IMFS node.
     662 * @param name The name of the IMFS node.
     663 * @param namelen The length of the name of the IMFS node.
     664 * @param mode The mode of the IMFS node.
     665 *
     666 * @see IMFS_NODE_INITIALIZER().
     667 */
     668static inline void IMFS_node_preinitialize(
     669  IMFS_jnode_t            *node,
     670  const IMFS_node_control *node_control,
     671  const char              *name,
     672  size_t                   namelen,
     673  mode_t                   mode
     674)
     675{
     676  node->control = node_control;
     677  node->name = name;
     678  node->namelen = namelen;
     679  node->st_mode = mode;
     680}
     681
     682/**
     683 * @brief Adds an IMFS node.
     684 *
     685 * Initialize the node with IMFS_NODE_INITIALIZER(), IMFS_node_preinitialize(),
     686 * IMFS_GENERIC_NODE_INITIALIZER(), or IMFS_generic_node_preinitialize().
     687 *
     688 * @param path The path of parent directories for the IMFS node to add.
     689 * @param node The IMFS node to add.
     690 * @param arg The argument passed to the node initialization method.
     691 *
     692 * @retval 0 Successful operation.
     693 * @retval -1 An error occurred.  The @c errno indicates the error.
     694 */
     695int IMFS_add_node( const char *path, IMFS_jnode_t *node, void *arg );
     696
    597697extern int IMFS_make_node(
    598698  const char              *path,
     
    639739 */
    640740
     741/* Provided for backward compatibility */
     742#define IMFS_GENERIC_INITIALIZER( handlers, init, destroy ) \
     743  IMFS_NODE_CONTROL_INITIALIZER( handlers, init, destroy )
     744
    641745/**
    642746 * @brief Initializer for a generic node control.
    643747 *
    644  * @param[in] handlers The file system node handlers.
    645  * @param[in] init The node initialization method.
    646  * @param[in] destroy The node destruction method.
    647  */
    648 #define IMFS_GENERIC_INITIALIZER( handlers, init, destroy ) \
    649   { \
    650     ( handlers ), \
    651     ( init ), \
    652     IMFS_node_remove_default, \
    653     ( destroy ) \
    654   }
     748 * @param handlers The file system node handlers.
     749 * @param init The node initialization method.
     750 * @param destroy The node destruction method.
     751 */
     752#define IMFS_GENERIC_CONTROL_INITIALIZER( handlers, init, destroy ) \
     753  IMFS_NODE_CONTROL_INITIALIZER( handlers, init, destroy )
     754
     755/**
     756 * @brief Initializer for a generic node.
     757 *
     758 * Initialize the node control with IMFS_GENERIC_CONTROL_INITIALIZER().
     759 *
     760 * @param node_control The node control of the IMFS generic node.
     761 * @param name The name of the IMFS generic node.
     762 * @param namelen The length of the name of the IMFS generic node.
     763 * @param mode The mode of the IMFS generic node.
     764 */
     765#define IMFS_GENERIC_NODE_INITIALIZER( node_control, name, namelen, mode ) \
     766  { IMFS_NODE_INITIALIZER( node_control, name, namelen, mode ), NULL }
     767
     768/**
     769 * @brief Preinitializes a generic IMFS node.
     770 *
     771 * Initialize the node control with IMFS_GENERIC_CONTROL_INITIALIZER().
     772 *
     773 * @param node The generic IMFS node to preinitialize.
     774 * @param node_control The node control of the generic IMFS node.
     775 * @param name The name of the generic IMFS node.
     776 * @param namelen The length of the name of the generic IMFS node.
     777 * @param mode The mode of the generic IMFS node.
     778 *
     779 * @see IMFS_GENERIC_NODE_INITIALIZER().
     780 */
     781static inline void IMFS_generic_node_preinitialize(
     782  IMFS_generic_t          *node,
     783  const IMFS_node_control *node_control,
     784  const char              *name,
     785  size_t                   namelen,
     786  mode_t                   mode
     787)
     788{
     789  IMFS_node_preinitialize( &node->Node, node_control, name, namelen, mode );
     790}
    655791
    656792/**
     
    693829 * }
    694830 *
    695  * static const IMFS_node_control some_node_control = IMFS_GENERIC_INITIALIZER(
     831 * static const IMFS_node_control
     832 * some_node_control = IMFS_GENERIC_CONTROL_INITIALIZER(
    696833 *   &some_node_handlers,
    697834 *   some_node_init,
  • cpukit/libfs/src/imfs/imfs_node.c

    r13b71f8 rfa44c39  
    1919
    2020#if HAVE_CONFIG_H
    21   #include "config.h"
     21#include "config.h"
    2222#endif
    2323
    2424#include <rtems/imfs.h>
    25 
    26 #include <stdlib.h>
    2725
    2826IMFS_jnode_t *IMFS_initialize_node(
     
    106104}
    107105
    108 void IMFS_node_destroy_default( IMFS_jnode_t *node )
     106void IMFS_do_nothing_destroy( IMFS_jnode_t *node )
    109107{
    110   free( node );
     108  (void) node;
    111109}
  • testsuites/fstests/fsimfsgeneric01/init.c

    r13b71f8 rfa44c39  
    11/*
    2  * Copyright (c) 2012-2014 embedded brains GmbH.  All rights reserved.
    3  *
    4  *  embedded brains GmbH
    5  *  Dornierstr. 4
    6  *  82178 Puchheim
    7  *  Germany
    8  *  <rtems@embedded-brains.de>
     2 * Copyright (C) 2012, 2020 embedded brains GmbH (http://www.embedded-brains.de)
    93 *
    104 * The license and distribution terms for this file may be
     
    148
    159#ifdef HAVE_CONFIG_H
    16   #include "config.h"
     10#include "config.h"
    1711#endif
    18 
    19 #include "tmacros.h"
    2012
    2113#include <sys/stat.h>
     
    2921#include <rtems/malloc.h>
    3022#include <rtems/libcsupport.h>
     23
     24#include <tmacros.h>
    3125
    3226const char rtems_test_name[] = "FSIMFSGENERIC 1";
     
    303297};
    304298
    305 static void test_imfs_make_generic_node(void)
    306 {
    307   test_state state = TEST_NEW;
    308   int rv = 0;
    309   int fd = 0;
    310   const char *path = "generic";
    311   char buf [1];
    312   ssize_t n = 0;
    313   off_t off = 0;
     299static void test_node_operations(const char *path)
     300{
     301  int rv;
     302  int fd;
     303  char buf[1];
     304  ssize_t n;
     305  off_t off;
    314306  struct iovec iov = {
    315     .iov_base = &buf [0],
     307    .iov_base = &buf[0],
    316308    .iov_len = (int) sizeof(buf)
    317309  };
    318310
     311  fd = open(path, O_RDWR);
     312  rtems_test_assert(fd >= 0);
     313
     314  n = read(fd, buf, sizeof(buf));
     315  rtems_test_assert(n == 0);
     316
     317  n = write(fd, buf, sizeof(buf));
     318  rtems_test_assert(n == 0);
     319
     320  rv = ioctl(fd, 0);
     321  rtems_test_assert(rv == 0);
     322
     323  off = lseek(fd, 0, SEEK_SET);
     324  rtems_test_assert(off == 0);
     325
     326  rv = ftruncate(fd, 0);
     327  rtems_test_assert(rv == 0);
     328
     329  rv = fsync(fd);
     330  rtems_test_assert(rv == 0);
     331
     332  rv = fdatasync(fd);
     333  rtems_test_assert(rv == 0);
     334
     335  rv = fcntl(fd, F_GETFD);
     336  rtems_test_assert(rv >= 0);
     337
     338  rv = readv(fd, &iov, 1);
     339  rtems_test_assert(rv == 0);
     340
     341  rv = writev(fd, &iov, 1);
     342  rtems_test_assert(rv == 0);
     343
     344  rv = close(fd);
     345  rtems_test_assert(rv == 0);
     346
     347  rv = unlink(path);
     348  rtems_test_assert(rv == 0);
     349}
     350
     351static void test_imfs_make_generic_node(void)
     352{
     353  static const char path[] = "generic";
     354  test_state state;
     355  int rv;
     356
     357  state = TEST_NEW;
    319358  rv = IMFS_make_generic_node(
    320359    path,
     
    325364  rtems_test_assert(rv == 0);
    326365
    327   fd = open(path, O_RDWR);
    328   rtems_test_assert(fd >= 0);
    329 
    330   n = read(fd, buf, sizeof(buf));
    331   rtems_test_assert(n == 0);
    332 
    333   n = write(fd, buf, sizeof(buf));
    334   rtems_test_assert(n == 0);
    335 
    336   rv = ioctl(fd, 0);
    337   rtems_test_assert(rv == 0);
    338 
    339   off = lseek(fd, off, SEEK_SET);
    340   rtems_test_assert(off == 0);
    341 
    342   rv = ftruncate(fd, 0);
    343   rtems_test_assert(rv == 0);
    344 
    345   rv = fsync(fd);
    346   rtems_test_assert(rv == 0);
    347 
    348   rv = fdatasync(fd);
    349   rtems_test_assert(rv == 0);
    350 
    351   rv = fcntl(fd, F_GETFD);
    352   rtems_test_assert(rv >= 0);
    353 
    354   rv = readv(fd, &iov, 1);
    355   rtems_test_assert(rv == 0);
    356 
    357   rv = writev(fd, &iov, 1);
    358   rtems_test_assert(rv == 0);
    359 
    360   rv = close(fd);
    361   rtems_test_assert(rv == 0);
    362 
    363   rv = unlink(path);
    364   rtems_test_assert(rv == 0);
    365 
     366  test_node_operations(path);
    366367  rtems_test_assert(state == TEST_DESTROYED);
    367368}
     
    403404}
    404405
     406static rtems_filesystem_mount_table_entry_t *get_imfs_mt_entry(void)
     407{
     408  return (rtems_filesystem_mount_table_entry_t *)
     409    rtems_chain_first(&rtems_filesystem_mount_table);
     410}
     411
    405412static void test_imfs_make_generic_node_errors(void)
    406413{
    407   int rv = 0;
    408   const char *path = "generic";
    409   rtems_chain_control *chain = &rtems_filesystem_mount_table;
    410   rtems_filesystem_mount_table_entry_t *mt_entry =
    411     (rtems_filesystem_mount_table_entry_t *) rtems_chain_first(chain);
     414  static const char path[] = "generic";
     415  rtems_filesystem_mount_table_entry_t *mt_entry;
    412416  rtems_filesystem_operations_table other_ops;
    413   void *opaque = NULL;
    414417  rtems_resource_snapshot before;
     418  void *opaque;
     419  int rv;
    415420
    416421  rtems_resource_snapshot_take(&before);
     
    428433
    429434  errno = 0;
     435  mt_entry = get_imfs_mt_entry();
    430436  imfs_ops = mt_entry->ops;
    431437  other_ops = *imfs_ops;
     
    479485}
    480486
     487static void user_node_destroy(IMFS_jnode_t *node)
     488{
     489  test_state *state = IMFS_generic_get_context_by_node(node);
     490
     491  rtems_test_assert(*state == TEST_REMOVED);
     492  *state = TEST_DESTROYED;
     493}
     494
     495static const IMFS_node_control user_node_control = {
     496  .handlers = &node_handlers,
     497  .node_initialize = node_initialize,
     498  .node_remove = node_remove,
     499  .node_destroy = user_node_destroy
     500};
     501
     502static void test_imfs_add_node(void)
     503{
     504  static const char path[] = "/";
     505  static const char name[] = "node";
     506  size_t namelen = sizeof(name) - 1;
     507  void *opaque;
     508  rtems_resource_snapshot before;
     509  IMFS_generic_t node = IMFS_GENERIC_NODE_INITIALIZER(
     510    &user_node_control,
     511    name,
     512    namelen,
     513    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO
     514  );
     515  test_state state;
     516  int rv;
     517
     518  /* Ensure that sure no dynamic memory is used */
     519  opaque = rtems_heap_greedy_allocate(NULL, 0);
     520
     521  rtems_resource_snapshot_take(&before);
     522
     523  state = TEST_NEW;
     524  rv = IMFS_add_node(path, &node.Node, &state);
     525  rtems_test_assert(rv == 0);
     526
     527  test_node_operations(name);
     528  rtems_test_assert(state == TEST_DESTROYED);
     529
     530  rtems_test_assert(rtems_resource_snapshot_check(&before));
     531  rtems_heap_greedy_free(opaque);
     532}
     533
     534static void test_imfs_add_node_errors(void)
     535{
     536  static const char path[] = "/";
     537  static const char name[] = "node";
     538  size_t namelen = sizeof(name) - 1;
     539  const char invalid_name[] = "/node";
     540  size_t invalid_namelen = sizeof(invalid_name) - 1;
     541  IMFS_jnode_t node = IMFS_NODE_INITIALIZER(
     542    &user_node_control,
     543    name,
     544    namelen,
     545    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO
     546  );
     547  IMFS_jnode_t invalid_mode_node = IMFS_NODE_INITIALIZER(
     548    &user_node_control,
     549    name,
     550    namelen,
     551    S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO
     552  );
     553  IMFS_jnode_t init_error_node = IMFS_NODE_INITIALIZER(
     554    &node_initialization_error_control,
     555    name,
     556    namelen,
     557    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO
     558  );
     559  IMFS_jnode_t invalid_name_node = IMFS_NODE_INITIALIZER(
     560    &user_node_control,
     561    invalid_name,
     562    invalid_namelen,
     563    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO
     564  );
     565  rtems_filesystem_mount_table_entry_t *mt_entry;
     566  rtems_filesystem_operations_table other_ops;
     567  void *opaque;
     568  rtems_resource_snapshot before;
     569  int rv;
     570
     571  /* Ensure that sure no dynamic memory is used */
     572  opaque = rtems_heap_greedy_allocate(NULL, 0);
     573
     574  rtems_resource_snapshot_take(&before);
     575
     576  errno = 0;
     577  rv = IMFS_add_node(path, &invalid_mode_node, NULL);
     578  rtems_test_assert(rv == -1);
     579  rtems_test_assert(errno == EINVAL);
     580  rtems_test_assert(rtems_resource_snapshot_check(&before));
     581
     582  errno = 0;
     583  mt_entry = get_imfs_mt_entry();
     584  imfs_ops = mt_entry->ops;
     585  other_ops = *imfs_ops;
     586  other_ops.clonenod_h = other_clone;
     587  mt_entry->ops = &other_ops;
     588  rv = IMFS_add_node(path, &node, NULL);
     589  mt_entry->ops = imfs_ops;
     590  rtems_test_assert(rv == -1);
     591  rtems_test_assert(errno == ENOTSUP);
     592  rtems_test_assert(rtems_resource_snapshot_check(&before));
     593
     594  errno = 0;
     595  rv = IMFS_add_node(path, &init_error_node, NULL);
     596  rtems_test_assert(rv == -1);
     597  rtems_test_assert(errno == EIO);
     598  rtems_test_assert(rtems_resource_snapshot_check(&before));
     599
     600  errno = 0;
     601  rv = IMFS_add_node("/nil/nada", &node, NULL);
     602  rtems_test_assert(rv == -1);
     603  rtems_test_assert(errno == ENOENT);
     604  rtems_test_assert(rtems_resource_snapshot_check(&before));
     605
     606  errno = 0;
     607  rv = IMFS_add_node(path, &invalid_name_node, NULL);
     608  rtems_test_assert(rv == -1);
     609  rtems_test_assert(errno == EINVAL);
     610  rtems_test_assert(rtems_resource_snapshot_check(&before));
     611
     612  rtems_heap_greedy_free(opaque);
     613}
     614
    481615static void Init(rtems_task_argument arg)
    482616{
     
    485619  test_imfs_make_generic_node();
    486620  test_imfs_make_generic_node_errors();
     621  test_imfs_add_node();
     622  test_imfs_add_node_errors();
    487623
    488624  TEST_END();
     
    497633#define CONFIGURE_MAXIMUM_TASKS 1
    498634
     635#define CONFIGURE_UNIFIED_WORK_AREAS
     636
    499637#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
    500638
Note: See TracChangeset for help on using the changeset viewer.