Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup IMFS
0007  */
0008 
0009 /*
0010  *  COPYRIGHT (c) 1989-1999.
0011  *  On-Line Applications Research Corporation (OAR).
0012  *
0013  * Redistribution and use in source and binary forms, with or without
0014  * modification, are permitted provided that the following conditions
0015  * are met:
0016  * 1. Redistributions of source code must retain the above copyright
0017  *    notice, this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright
0019  *    notice, this list of conditions and the following disclaimer in the
0020  *    documentation and/or other materials provided with the distribution.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032  * POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 
0035 #ifdef HAVE_CONFIG_H
0036 #include "config.h"
0037 #endif
0038 
0039 #include <string.h>
0040 
0041 #include <rtems/imfsimpl.h>
0042 
0043 static ssize_t IMFS_linfile_read(
0044   rtems_libio_t *iop,
0045   void          *buffer,
0046   size_t         count
0047 )
0048 {
0049   IMFS_file_t *file = IMFS_iop_to_file( iop );
0050   off_t start = iop->offset;
0051   size_t size = file->File.size;
0052   const unsigned char *data = file->Linearfile.direct;
0053 
0054   if (count > size - start)
0055     count = size - start;
0056 
0057   IMFS_update_atime( &file->Node );
0058   iop->offset = start + count;
0059   memcpy(buffer, &data[start], count);
0060 
0061   return (ssize_t) count;
0062 }
0063 
0064 static int IMFS_linfile_open(
0065   rtems_libio_t *iop,
0066   const char    *pathname,
0067   int            oflag,
0068   mode_t         mode
0069 )
0070 {
0071   IMFS_file_t *file;
0072 
0073   file = iop->pathinfo.node_access;
0074 
0075   /*
0076    * Perform 'copy on write' for linear files
0077    */
0078   if (rtems_libio_iop_is_writeable(iop)) {
0079     uint32_t count = file->File.size;
0080     const unsigned char *buffer = file->Linearfile.direct;
0081 
0082     file->Node.control            = &IMFS_mknod_control_memfile.node_control;
0083     file->File.size               = 0;
0084     file->Memfile.indirect        = 0;
0085     file->Memfile.doubly_indirect = 0;
0086     file->Memfile.triply_indirect = 0;
0087 
0088     IMFS_Set_handlers( &iop->pathinfo );
0089 
0090     if ((count != 0)
0091      && (IMFS_memfile_write(&file->Memfile, 0, buffer, count) == -1))
0092         return -1;
0093   }
0094 
0095   return 0;
0096 }
0097 
0098 static const rtems_filesystem_file_handlers_r IMFS_linfile_handlers = {
0099   .open_h = IMFS_linfile_open,
0100   .close_h = rtems_filesystem_default_close,
0101   .read_h = IMFS_linfile_read,
0102   .write_h = rtems_filesystem_default_write,
0103   .ioctl_h = rtems_filesystem_default_ioctl,
0104   .lseek_h = rtems_filesystem_default_lseek_file,
0105   .fstat_h = IMFS_stat_file,
0106   .ftruncate_h = rtems_filesystem_default_ftruncate,
0107   .fsync_h = rtems_filesystem_default_fsync_or_fdatasync_success,
0108   .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync_success,
0109   .fcntl_h = rtems_filesystem_default_fcntl,
0110   .kqfilter_h = rtems_filesystem_default_kqfilter,
0111   .mmap_h = rtems_filesystem_default_mmap,
0112   .poll_h = rtems_filesystem_default_poll,
0113   .readv_h = rtems_filesystem_default_readv,
0114   .writev_h = rtems_filesystem_default_writev
0115 };
0116 
0117 static IMFS_jnode_t *IMFS_node_initialize_linfile(
0118   IMFS_jnode_t *node,
0119   void         *arg
0120 )
0121 {
0122   IMFS_linearfile_t *linfile;
0123   IMFS_linearfile_context *ctx;
0124 
0125   linfile = (IMFS_linearfile_t *) node;
0126   ctx = arg;
0127   linfile->File.size = ctx->size;
0128   linfile->direct = RTEMS_DECONST( void *, ctx->data );
0129 
0130   return node;
0131 }
0132 
0133 const IMFS_node_control IMFS_node_control_linfile = {
0134   .handlers = &IMFS_linfile_handlers,
0135   .node_initialize = IMFS_node_initialize_linfile,
0136   .node_remove = IMFS_node_remove_default,
0137   .node_destroy = IMFS_node_destroy_default
0138 };