summaryrefslogtreecommitdiffstats
path: root/c/src/libfs/src/imfs/linearfile.c
blob: 7a47c7c5efaee0cd5c9aea08636ec5316e617dd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 *  IMFS Linear File Handlers
 *
 *  This file contains the set of handlers used to process operations on
 *  IMFS linear memory file nodes.  Linear memory files are contiguous
 *  blocks of memory created from a TAR or other filesystem image.
 *  The blocks are nonwriteable and nonresizeable.
 *
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $Id$
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <assert.h>
#include <errno.h>

#include <rtems.h>
#include <rtems/libio.h>
#include "imfs.h"
#include <rtems/libio_.h>

/*
 * linearfile_read
 *
 *  This routine processes the read() system call.
 */

int linearfile_read(
  rtems_libio_t *iop,
  void          *buffer,
  unsigned32     count
)
{
  IMFS_jnode_t   *the_jnode;
  unsigned char  *dest;
  unsigned char  *file_ptr;
  int            file_offset;


  the_jnode = iop->file_info;

  /*
   *  Perform internal consistency checks
   */

  assert( the_jnode );
  if ( !the_jnode )
    set_errno_and_return_minus_one( EIO );

  assert( the_jnode->type == IMFS_LINEAR_FILE );
  if ( the_jnode->type != IMFS_LINEAR_FILE )
    set_errno_and_return_minus_one( EIO );

  /*
   *  Error checks on arguments
   */

  dest = (unsigned char *)buffer;
  assert( dest );
  if ( !dest )
    set_errno_and_return_minus_one( EINVAL );

  /*
   *  Perform a simple memory copy.
   */

  if (count == 0)
     return(0);

  the_jnode = iop->file_info;
  file_ptr    = (unsigned char *)the_jnode->info.linearfile.direct;
  file_offset = (unsigned long)iop->offset;

  if (count > (the_jnode->info.linearfile.size - file_offset))
     count = the_jnode->info.linearfile.size - file_offset;

  memcpy(dest, &file_ptr[file_offset], count);

  return(count);
}


/*
 *  linearfile_lseek
 *
 *  This routine processes the lseek() system call.
 */

int linearfile_lseek(
  rtems_libio_t   *iop,
  off_t            offset,
  int              whence
)
{
  IMFS_jnode_t   *the_jnode;

  the_jnode = iop->file_info;

  if (iop->offset > the_jnode->info.linearfile.size)
    iop->offset = the_jnode->info.linearfile.size;

  return iop->offset;
}