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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*
* libopenraw - posix_io.c
*
* Copyright (C) 2005 Hubert Figuiere
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include "io_private.h"
#include "posix_io.h"
/** private data to be store in the _RawFile */
struct io_data_posix {
/** POSIX fd returned by open() */
int fd;
};
static IOFileRef posix_open(const char *path, int mode);
static int posix_close(IOFileRef f);
static int posix_seek(IOFileRef f, off_t offset, int whence);
static int posix_read(IOFileRef f, void *buf, size_t count);
static off_t posix_filesize(IOFileRef f);
static void *posix_mmap(IOFileRef f, size_t length, off_t offset);
static int posix_munmap(IOFileRef f, void *addr, size_t length);
/** posix io methods instance. Constant. */
struct io_methods posix_io_methods = {
&posix_open,
&posix_close,
&posix_seek,
&posix_read,
&posix_filesize,
&posix_mmap,
&posix_munmap
};
/** posix implementation for open() */
static IOFileRef posix_open(const char *path, int mode)
{
struct io_data_posix *data =
(struct io_data_posix *)malloc(sizeof(struct io_data_posix));
IOFileRef f = (IOFileRef)malloc(sizeof(struct _IOFile));
memset(f, 0, sizeof(struct _IOFile));
memset(data, 0, sizeof(struct io_data_posix));
f->methods = &posix_io_methods;
f->_private = data;
f->path = strdup(path);
data->fd = open(path, mode);
if (data->fd == -1) {
free(data);
free(f);
f = NULL;
}
return f;
}
/** posix implementation for close() */
static int posix_close(IOFileRef f)
{
int retval = 0;
struct io_data_posix *data = (struct io_data_posix*)f->_private;
retval = close(data->fd);
free(data);
free(f->path);
return retval;
}
/** posix implementation for seek() */
static int posix_seek(IOFileRef f, off_t offset, int whence)
{
int retval = 0;
struct io_data_posix *data = (struct io_data_posix*)f->_private;
retval = lseek(data->fd, offset, whence);
if (retval == -1) {
f->error = errno;
}
else {
f->error = 0;
}
return retval;
}
/** posix implementation for read() */
static int posix_read(IOFileRef f, void *buf, size_t count)
{
int retval = 0;
struct io_data_posix *data = (struct io_data_posix*)f->_private;
retval = read(data->fd, buf, count);
if (retval == -1) {
f->error = errno;
}
else {
f->error = 0;
}
return retval;
}
static off_t posix_filesize(IOFileRef f)
{
off_t size = -1;
struct io_data_posix *data = (struct io_data_posix*)f->_private;
struct stat sb;
if(fstat(data->fd, &sb) >= 0) {
size = sb.st_size;
}
return size;
}
static void *posix_mmap(IOFileRef f, size_t length, off_t offset)
{
struct io_data_posix *data = (struct io_data_posix*)f->_private;
return mmap(NULL, length, PROT_READ, MAP_SHARED, data->fd, offset);
}
static int posix_munmap(IOFileRef f, void *addr, size_t length)
{
(void)f;
return munmap(addr, length);
}
|