libopenraw
lib
io
posix_io.c
1
/*
2
* libopenraw - posix_io.c
3
*
4
* Copyright (C) 2005 Hubert Figuiere
5
*
6
* This library is free software: you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public License
8
* as published by the Free Software Foundation, either version 3 of
9
* the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library. If not, see
18
* <http://www.gnu.org/licenses/>.
19
*/
20
21
22
#include <stdlib.h>
23
#include <string.h>
24
#include <unistd.h>
25
#include <sys/types.h>
26
#include <sys/stat.h>
27
#include <sys/mman.h>
28
#include <fcntl.h>
29
#include <errno.h>
30
31
#include "io_private.h"
32
#include "posix_io.h"
33
34
36
struct
io_data_posix
{
38
int
fd
;
39
};
40
41
static
IOFileRef
raw_posix_open(
const
char
*path,
int
mode);
42
static
int
raw_posix_close(
IOFileRef
f);
43
static
int
raw_posix_seek(
IOFileRef
f, off_t offset,
int
whence);
44
static
int
raw_posix_read(
IOFileRef
f,
void
*buf,
size_t
count);
45
static
off_t raw_posix_filesize(
IOFileRef
f);
46
static
void
*raw_posix_mmap(
IOFileRef
f,
size_t
length, off_t offset);
47
static
int
raw_posix_munmap(
IOFileRef
f,
void
*addr,
size_t
length);
48
50
struct
io_methods
posix_io_methods = {
51
&raw_posix_open,
52
&raw_posix_close,
53
&raw_posix_seek,
54
&raw_posix_read,
55
&raw_posix_filesize,
56
&raw_posix_mmap,
57
&raw_posix_munmap
58
};
59
60
62
static
IOFileRef
raw_posix_open(
const
char
*path,
int
mode)
63
{
64
struct
io_data_posix
*data =
65
(
struct
io_data_posix
*)malloc(
sizeof
(
struct
io_data_posix
));
66
IOFileRef
f = (
IOFileRef
)malloc(
sizeof
(
struct
_IOFile
));
67
68
memset(f, 0,
sizeof
(
struct
_IOFile
));
69
memset(data, 0,
sizeof
(
struct
io_data_posix
));
70
71
f->
methods
= &posix_io_methods;
72
f->
_private
= data;
73
f->
path
= strdup(path);
74
data->
fd
= open(path, mode);
75
if
(data->
fd
== -1) {
76
free(data);
77
free(f);
78
f = NULL;
79
}
80
81
82
return
f;
83
}
84
85
86
88
static
int
raw_posix_close(
IOFileRef
f)
89
{
90
int
retval = 0;
91
struct
io_data_posix
*data = (
struct
io_data_posix
*)f->
_private
;
92
93
retval = close(data->
fd
);
94
free(data);
95
free(f->
path
);
96
return
retval;
97
}
98
99
101
static
int
raw_posix_seek(
IOFileRef
f, off_t offset,
int
whence)
102
{
103
int
retval = 0;
104
struct
io_data_posix
*data = (
struct
io_data_posix
*)f->
_private
;
105
106
retval = lseek(data->
fd
, offset, whence);
107
if
(retval == -1) {
108
f->
error
= errno;
109
}
110
else
{
111
f->
error
= 0;
112
}
113
return
retval;
114
}
115
116
118
static
int
raw_posix_read(
IOFileRef
f,
void
*buf,
size_t
count)
119
{
120
int
retval = 0;
121
struct
io_data_posix
*data = (
struct
io_data_posix
*)f->
_private
;
122
123
retval = read(data->
fd
, buf, count);
124
if
(retval == -1) {
125
f->
error
= errno;
126
}
127
else
{
128
f->
error
= 0;
129
}
130
return
retval;
131
}
132
133
134
static
off_t raw_posix_filesize(
IOFileRef
f)
135
{
136
off_t size = -1;
137
struct
io_data_posix
*data = (
struct
io_data_posix
*)f->
_private
;
138
struct
stat sb;
139
140
if
(fstat(data->
fd
, &sb) >= 0) {
141
size = sb.st_size;
142
}
143
144
return
size;
145
}
146
147
148
149
static
void
*raw_posix_mmap(
IOFileRef
f,
size_t
length, off_t offset)
150
{
151
struct
io_data_posix
*data = (
struct
io_data_posix
*)f->
_private
;
152
153
return
mmap(NULL, length, PROT_READ, MAP_SHARED, data->
fd
, offset);
154
}
155
156
157
static
int
raw_posix_munmap(
IOFileRef
f,
void
*addr,
size_t
length)
158
{
159
(void)f;
160
return
munmap(addr, length);
161
}
io_data_posix
Definition:
posix_io.c:36
_IOFile::error
int error
Definition:
io_private.h:34
_IOFile::_private
void * _private
Definition:
io_private.h:30
_IOFile::methods
struct io_methods * methods
Definition:
io_private.h:28
_IOFile
Definition:
io_private.h:26
io_data_posix::fd
int fd
Definition:
posix_io.c:38
io_methods
Definition:
io.h:38
_IOFile::path
char * path
Definition:
io_private.h:32
Generated by
1.8.13