source: mainline/uspace/app/bithenge/file.c@ 0d1a8fd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0d1a8fd was da0fef6, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: port to Linux and allow choosing a data source

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2012 Sean Bartell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @addtogroup bithenge
30 * @{
31 */
32/**
33 * @file
34 * Access files as blobs.
35 * @todo Provide more information about the file.
36 */
37
38#include <assert.h>
39#include <errno.h>
40#include <fcntl.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <sys/stat.h>
44#include <sys/types.h>
45#include <unistd.h>
46#include "blob.h"
47#include "file.h"
48#include "os.h"
49
50typedef struct {
51 bithenge_blob_t base;
52 int fd;
53 aoff64_t size; // needed by file_read()
54 bool needs_close;
55} file_blob_t;
56
57static inline file_blob_t *file_from_blob(bithenge_blob_t *base)
58{
59 return (file_blob_t *)base;
60}
61
62static inline bithenge_blob_t *blob_from_file(file_blob_t *blob)
63{
64 return &blob->base;
65}
66
67static int file_size(bithenge_blob_t *base, aoff64_t *size)
68{
69 file_blob_t *blob = file_from_blob(base);
70 *size = blob->size;
71 return EOK;
72}
73
74static int file_read(bithenge_blob_t *base, aoff64_t offset, char *buffer,
75 aoff64_t *size)
76{
77 file_blob_t *blob = file_from_blob(base);
78 if (offset > blob->size)
79 return ELIMIT;
80 if (lseek(blob->fd, offset, SEEK_SET) < 0)
81 return errno;
82
83 ssize_t amount_read;
84 aoff64_t remaining_size = *size;
85 *size = 0;
86 do {
87 amount_read = read(blob->fd, buffer, remaining_size);
88 if (amount_read < 0)
89 return errno;
90 buffer += amount_read;
91 *size += amount_read;
92 remaining_size -= amount_read;
93 } while (remaining_size && amount_read);
94 return EOK;
95}
96
97static int file_destroy(bithenge_blob_t *base)
98{
99 file_blob_t *blob = file_from_blob(base);
100 close(blob->fd);
101 free(blob);
102 return EOK;
103}
104
105static const bithenge_random_access_blob_ops_t file_ops = {
106 .size = file_size,
107 .read = file_read,
108 .destroy = file_destroy,
109};
110
111static int new_file_blob(bithenge_node_t **out, int fd, bool needs_close)
112{
113 assert(out);
114
115 struct stat stat;
116 int rc = fstat(fd, &stat);
117 if (rc != EOK) {
118 if (needs_close)
119 close(fd);
120 return rc;
121 }
122
123 // Create blob
124 file_blob_t *blob = malloc(sizeof(*blob));
125 if (!blob) {
126 if (needs_close)
127 close(fd);
128 return ENOMEM;
129 }
130 rc = bithenge_new_random_access_blob(blob_from_file(blob),
131 &file_ops);
132 if (rc != EOK) {
133 free(blob);
134 if (needs_close)
135 close(fd);
136 return rc;
137 }
138 blob->fd = fd;
139#ifdef __HELENOS__
140 blob->size = stat.size;
141#else
142 blob->size = stat.st_size;
143#endif
144 blob->needs_close = needs_close;
145 *out = bithenge_blob_as_node(blob_from_file(blob));
146
147 return EOK;
148}
149
150/** Create a blob for a file. The blob must be freed with @a
151 * bithenge_node_t::bithenge_node_destroy after it is used.
152 * @param[out] out Stores the created blob.
153 * @param filename The name of the file.
154 * @return EOK on success or an error code from errno.h. */
155int bithenge_new_file_blob(bithenge_node_t **out, const char *filename)
156{
157 assert(filename);
158
159 int fd = open(filename, O_RDONLY);
160 if (fd < 0)
161 return fd;
162
163 return new_file_blob(out, fd, true);
164}
165
166/** Create a blob for a file descriptor. The blob must be freed with @a
167 * bithenge_node_t::bithenge_node_destroy after it is used.
168 * @param[out] out Stores the created blob.
169 * @param fd The file descriptor.
170 * @return EOK on success or an error code from errno.h. */
171int bithenge_new_file_blob_from_fd(bithenge_node_t **out, int fd)
172{
173 return new_file_blob(out, fd, false);
174}
175
176/** Create a blob for a file pointer. The blob must be freed with @a
177 * bithenge_node_t::bithenge_node_destroy after it is used.
178 * @param[out] out Stores the created blob.
179 * @param file The file pointer.
180 * @return EOK on success or an error code from errno.h. */
181int bithenge_new_file_blob_from_file(bithenge_node_t **out, FILE *file)
182{
183 int fd = fileno(file);
184 if (fd < 0)
185 return errno;
186 return new_file_blob(out, fd, false);
187}
188
189/** @}
190 */
Note: See TracBrowser for help on using the repository browser.