1 | /* |
---|
2 | * IMFS Linear File Handlers |
---|
3 | * |
---|
4 | * This file contains the set of handlers used to process operations on |
---|
5 | * IMFS linear memory file nodes. Linear memory files are contiguous |
---|
6 | * blocks of memory created from a TAR or other filesystem image. |
---|
7 | * The blocks are nonwriteable and nonresizeable. |
---|
8 | * |
---|
9 | * COPYRIGHT (c) 1989-1999. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.OARcorp.com/rtems/license.html. |
---|
15 | * |
---|
16 | * $Id$ |
---|
17 | */ |
---|
18 | |
---|
19 | #if HAVE_CONFIG_H |
---|
20 | #include "config.h" |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | #include <assert.h> |
---|
25 | #include <errno.h> |
---|
26 | |
---|
27 | #include <rtems.h> |
---|
28 | #include <rtems/libio.h> |
---|
29 | #include "imfs.h" |
---|
30 | #include <rtems/libio_.h> |
---|
31 | |
---|
32 | /* |
---|
33 | * linearfile_read |
---|
34 | * |
---|
35 | * This routine processes the read() system call. |
---|
36 | */ |
---|
37 | |
---|
38 | int linearfile_read( |
---|
39 | rtems_libio_t *iop, |
---|
40 | void *buffer, |
---|
41 | unsigned32 count |
---|
42 | ) |
---|
43 | { |
---|
44 | IMFS_jnode_t *the_jnode; |
---|
45 | unsigned char *dest; |
---|
46 | unsigned char *file_ptr; |
---|
47 | int file_offset; |
---|
48 | |
---|
49 | |
---|
50 | the_jnode = iop->file_info; |
---|
51 | |
---|
52 | /* |
---|
53 | * Perform internal consistency checks |
---|
54 | */ |
---|
55 | |
---|
56 | assert( the_jnode ); |
---|
57 | if ( !the_jnode ) |
---|
58 | set_errno_and_return_minus_one( EIO ); |
---|
59 | |
---|
60 | assert( the_jnode->type == IMFS_LINEAR_FILE ); |
---|
61 | if ( the_jnode->type != IMFS_LINEAR_FILE ) |
---|
62 | set_errno_and_return_minus_one( EIO ); |
---|
63 | |
---|
64 | /* |
---|
65 | * Error checks on arguments |
---|
66 | */ |
---|
67 | |
---|
68 | dest = (unsigned char *)buffer; |
---|
69 | assert( dest ); |
---|
70 | if ( !dest ) |
---|
71 | set_errno_and_return_minus_one( EINVAL ); |
---|
72 | |
---|
73 | /* |
---|
74 | * Perform a simple memory copy. |
---|
75 | */ |
---|
76 | |
---|
77 | if (count == 0) |
---|
78 | return(0); |
---|
79 | |
---|
80 | the_jnode = iop->file_info; |
---|
81 | file_ptr = (unsigned char *)the_jnode->info.linearfile.direct; |
---|
82 | file_offset = (unsigned long)iop->offset; |
---|
83 | |
---|
84 | if (count > (the_jnode->info.linearfile.size - file_offset)) |
---|
85 | count = the_jnode->info.linearfile.size - file_offset; |
---|
86 | |
---|
87 | memcpy(dest, &file_ptr[file_offset], count); |
---|
88 | |
---|
89 | return(count); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | /* |
---|
94 | * linearfile_lseek |
---|
95 | * |
---|
96 | * This routine processes the lseek() system call. |
---|
97 | */ |
---|
98 | |
---|
99 | int linearfile_lseek( |
---|
100 | rtems_libio_t *iop, |
---|
101 | off_t offset, |
---|
102 | int whence |
---|
103 | ) |
---|
104 | { |
---|
105 | IMFS_jnode_t *the_jnode; |
---|
106 | |
---|
107 | the_jnode = iop->file_info; |
---|
108 | |
---|
109 | if (iop->offset > the_jnode->info.linearfile.size) |
---|
110 | iop->offset = the_jnode->info.linearfile.size; |
---|
111 | |
---|
112 | return iop->offset; |
---|
113 | } |
---|
114 | |
---|