Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:18

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup rtems_rfs
0007  *
0008  * @brief Set of Utility Functions to Support RTEMS RFS on RTEMS
0009  */
0010 
0011 /*
0012  *  COPYRIGHT (c) 2010 Chris Johns <chrisj@rtems.org>
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifdef HAVE_CONFIG_H
0037 #include "config.h"
0038 #endif
0039 
0040 #include <sys/types.h>
0041 #include <sys/stat.h>
0042 #include <stdlib.h>
0043 
0044 #include "rtems-rfs-rtems.h"
0045 
0046 /*
0047  * The following sets the handlers based on the type of inode.
0048  */
0049 
0050 bool
0051 rtems_rfs_rtems_set_handlers (rtems_filesystem_location_info_t* loc,
0052                               rtems_rfs_inode_handle*           inode)
0053 {
0054   uint16_t mode = rtems_rfs_inode_get_mode (inode);
0055   loc->handlers = NULL;
0056   if (RTEMS_RFS_S_ISDIR (mode))
0057     loc->handlers = rtems_rfs_rtems_handlers (dir);
0058   else if (RTEMS_RFS_S_ISCHR (mode) || RTEMS_RFS_S_ISBLK(mode))
0059     loc->handlers = rtems_rfs_rtems_handlers (device);
0060   else if (RTEMS_RFS_S_ISLNK (mode))
0061     loc->handlers = rtems_rfs_rtems_handlers (link);
0062   else if (RTEMS_RFS_S_ISREG (mode))
0063     loc->handlers = rtems_rfs_rtems_handlers (file);
0064   else
0065   {
0066     printf ("rtems-rfs: mode type unknown: %04x\n", mode);
0067     return false;
0068   }
0069   return true;
0070 }
0071 
0072 uint16_t
0073 rtems_rfs_rtems_imode (mode_t mode)
0074 {
0075   /*
0076    * Mapping matches RTEMS so no need to change.
0077    */
0078   return mode;
0079 }
0080 
0081 mode_t
0082 rtems_rfs_rtems_mode (int imode)
0083 {
0084   /*
0085    * Mapping matches RTEMS so no need to change.
0086    */
0087   return imode;
0088 }
0089 
0090 /*
0091  * Only provide if there is no macro version.
0092  */
0093 #if !defined (rtems_rfs_rtems_error)
0094 int
0095 rtems_rfs_rtems_error (const char* mesg, int error)
0096 {
0097   if (error)
0098     printf ("rtems-rfs: %s: %d: %s\n", mesg, error, strerror (error));
0099   errno = error;
0100   return error == 0 ? 0 : -1;
0101 }
0102 #endif
0103 
0104 #if RTEMS_RFS_RTEMS_TRACE
0105 static uint32_t rtems_rfs_rtems_trace_mask;
0106 
0107 bool
0108 rtems_rfs_rtems_trace (uint32_t mask)
0109 {
0110   bool result = false;
0111   if (mask & rtems_rfs_rtems_trace_mask)
0112     result = true;
0113   return result;
0114 }
0115 
0116 void
0117 rtems_rfs_trace_rtems_set_mask (uint32_t mask)
0118 {
0119   rtems_rfs_rtems_trace_mask |= mask;
0120 }
0121 
0122 void
0123 rtems_rfs_trace_rtems_clear_mask (uint32_t mask)
0124 {
0125   rtems_rfs_rtems_trace_mask &= ~mask;
0126 }
0127 
0128 int
0129 rtems_rfs_rtems_trace_shell_command (int argc, char *argv[])
0130 {
0131   const char* table[] =
0132   {
0133     "error-msgs",
0134     "eval-path"
0135     "eval-for-make",
0136     "eval-perms",
0137     "mknod",
0138     "rmnod",
0139     "link",
0140     "unlink",
0141     "chown",
0142     "readlink",
0143     "fchmod",
0144     "stat",
0145     "dir-rmnod",
0146     "file-open",
0147     "file-close",
0148     "file-read",
0149     "file-write",
0150     "file-lseek",
0151     "file-ftrunc"
0152   };
0153 
0154   bool set = true;
0155   int  arg;
0156   int  t;
0157 
0158   for (arg = 1; arg < argc; arg++)
0159   {
0160     if (argv[arg][0] == '-')
0161     {
0162       switch (argv[arg][1])
0163       {
0164         case 'h':
0165           printf ("usage: %s [-hl] [set/clear] [flags]\n", argv[0]);
0166           return 0;
0167         case 'l':
0168           printf ("%s: valid flags to set or clear are:\n", argv[0]);
0169           for (t = 0; t < (sizeof (table) / sizeof (const char*)); t++)
0170             printf ("  %s\n", table[t]);
0171           return 0;
0172         default:
0173           printf ("error: unknown option\n");
0174           return 1;
0175       }
0176     }
0177     else
0178     {
0179       uint32_t value = 0;
0180       if (strcmp (argv[arg], "set") == 0)
0181         set = true;
0182       if (strcmp (argv[arg], "clear") == 0)
0183         set = false;
0184       else if (strcmp (argv[arg], "all") == 0)
0185         value = RTEMS_RFS_RTEMS_DEBUG_ALL;
0186       else
0187       {
0188         for (t = 0; t < (sizeof (table) / sizeof (const char*)); t++)
0189         {
0190           if (strcmp (argv[arg], table[t]) == 0)
0191           {
0192             value = 1 << t;
0193             break;
0194           }
0195         }
0196       }
0197 
0198       if (set)
0199         rtems_rfs_rtems_trace_mask |= value;
0200       else
0201         rtems_rfs_rtems_trace_mask &= ~value;
0202     }
0203   }
0204 
0205   return 0;
0206 }
0207 
0208 #endif