2
* Copyright (c) 2012 Sean Bartell
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
/** @addtogroup bithenge
34
* Access files as blobs.
35
* @todo Provide more information about the file.
44
#include <sys/types.h>
47
#include <bithenge/blob.h>
48
#include <bithenge/file.h>
53
aoff64_t size; // needed by file_read()
57
static inline file_blob_t *blob_as_file(bithenge_blob_t *base)
59
return (file_blob_t *)base;
62
static inline bithenge_blob_t *file_as_blob(file_blob_t *blob)
67
static int file_size(bithenge_blob_t *base, aoff64_t *size)
69
file_blob_t *blob = blob_as_file(base);
74
static int file_read(bithenge_blob_t *base, aoff64_t offset, char *buffer,
77
file_blob_t *blob = blob_as_file(base);
78
if (offset > blob->size)
80
if (lseek(blob->fd, offset, SEEK_SET) < 0)
81
return errno == EINVAL ? EIO : errno;
84
aoff64_t remaining_size = *size;
87
amount_read = read(blob->fd, buffer, remaining_size);
90
buffer += amount_read;
92
remaining_size -= amount_read;
93
} while (remaining_size && amount_read);
97
static void file_destroy(bithenge_blob_t *base)
99
file_blob_t *blob = blob_as_file(base);
104
static const bithenge_random_access_blob_ops_t file_ops = {
107
.destroy = file_destroy,
110
static int new_file_blob(bithenge_node_t **out, int fd, bool needs_close)
115
int rc = fstat(fd, &stat);
123
file_blob_t *blob = malloc(sizeof(*blob));
129
rc = bithenge_init_random_access_blob(file_as_blob(blob), &file_ops);
138
blob->size = stat.size;
140
blob->size = stat.st_size;
142
blob->needs_close = needs_close;
143
*out = bithenge_blob_as_node(file_as_blob(blob));
148
/** Create a blob for a file. The blob must be freed with @a
149
* bithenge_node_t::bithenge_node_destroy after it is used.
150
* @param[out] out Stores the created blob.
151
* @param filename The name of the file.
152
* @return EOK on success or an error code from errno.h. */
153
int bithenge_new_file_blob(bithenge_node_t **out, const char *filename)
157
int fd = open(filename, O_RDONLY);
161
return new_file_blob(out, fd, true);
164
/** Create a blob for a file descriptor. The blob must be freed with @a
165
* bithenge_node_t::bithenge_node_destroy after it is used.
166
* @param[out] out Stores the created blob.
167
* @param fd The file descriptor.
168
* @return EOK on success or an error code from errno.h. */
169
int bithenge_new_file_blob_from_fd(bithenge_node_t **out, int fd)
171
return new_file_blob(out, fd, false);
174
/** Create a blob for a file pointer. The blob must be freed with @a
175
* bithenge_node_t::bithenge_node_destroy after it is used.
176
* @param[out] out Stores the created blob.
177
* @param file The file pointer.
178
* @return EOK on success or an error code from errno.h. */
179
int bithenge_new_file_blob_from_file(bithenge_node_t **out, FILE *file)
181
int fd = fileno(file);
184
return new_file_blob(out, fd, false);