source: rtems/cpukit/libfs/src/imfs/imfs_linfile.c @ 3cffd66d

5
Last change on this file since 3cffd66d was 3cffd66d, checked in by Sebastian Huber <sebastian.huber@…>, on 09/13/17 at 08:36:11

libio: Add rtems_libio_iop_is_writeable()

Update #3132.

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup IMFS
5 */
6
7/*
8 *  COPYRIGHT (c) 1989-1999.
9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  The license and distribution terms for this file may be
12 *  found in the file LICENSE in this distribution or at
13 *  http://www.rtems.org/license/LICENSE.
14 */
15
16#if HAVE_CONFIG_H
17  #include "config.h"
18#endif
19
20#include <string.h>
21
22#include "imfs.h"
23
24static ssize_t IMFS_linfile_read(
25  rtems_libio_t *iop,
26  void          *buffer,
27  size_t         count
28)
29{
30  IMFS_file_t *file = IMFS_iop_to_file( iop );
31  off_t start = iop->offset;
32  size_t size = file->File.size;
33  const unsigned char *data = file->Linearfile.direct;
34
35  if (count > size - start)
36    count = size - start;
37
38  IMFS_update_atime( &file->Node );
39  iop->offset = start + count;
40  memcpy(buffer, &data[start], count);
41
42  return (ssize_t) count;
43}
44
45static int IMFS_linfile_open(
46  rtems_libio_t *iop,
47  const char    *pathname,
48  int            oflag,
49  mode_t         mode
50)
51{
52  IMFS_file_t *file;
53
54  file = iop->pathinfo.node_access;
55
56  /*
57   * Perform 'copy on write' for linear files
58   */
59  if (rtems_libio_iop_is_writeable(iop)) {
60    uint32_t count = file->File.size;
61    const unsigned char *buffer = file->Linearfile.direct;
62
63    file->Node.control            = &IMFS_mknod_control_memfile.node_control;
64    file->File.size               = 0;
65    file->Memfile.indirect        = 0;
66    file->Memfile.doubly_indirect = 0;
67    file->Memfile.triply_indirect = 0;
68
69    IMFS_Set_handlers( &iop->pathinfo );
70
71    if ((count != 0)
72     && (IMFS_memfile_write(&file->Memfile, 0, buffer, count) == -1))
73        return -1;
74  }
75
76  return 0;
77}
78
79static const rtems_filesystem_file_handlers_r IMFS_linfile_handlers = {
80  .open_h = IMFS_linfile_open,
81  .close_h = rtems_filesystem_default_close,
82  .read_h = IMFS_linfile_read,
83  .write_h = rtems_filesystem_default_write,
84  .ioctl_h = rtems_filesystem_default_ioctl,
85  .lseek_h = rtems_filesystem_default_lseek_file,
86  .fstat_h = IMFS_stat_file,
87  .ftruncate_h = rtems_filesystem_default_ftruncate,
88  .fsync_h = rtems_filesystem_default_fsync_or_fdatasync_success,
89  .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync_success,
90  .fcntl_h = rtems_filesystem_default_fcntl,
91  .kqfilter_h = rtems_filesystem_default_kqfilter,
92  .mmap_h = rtems_filesystem_default_mmap,
93  .poll_h = rtems_filesystem_default_poll,
94  .readv_h = rtems_filesystem_default_readv,
95  .writev_h = rtems_filesystem_default_writev
96};
97
98const IMFS_node_control IMFS_node_control_linfile = {
99  .handlers = &IMFS_linfile_handlers,
100  .node_initialize = IMFS_node_initialize_default,
101  .node_remove = IMFS_node_remove_default,
102  .node_destroy = IMFS_node_destroy_default
103};
Note: See TracBrowser for help on using the repository browser.