source: mainline/uspace/app/bithenge/file.c@ 8b36bf2

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

Bithenge: add Doxygen comments

  • Property mode set to 100644
File size: 4.7 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 <macros.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <sys/stat.h>
45#include "blob.h"
46#include "file.h"
47
48typedef struct {
49 bithenge_blob_t base;
50 int fd;
51 aoff64_t size; // needed by file_read()
52 bool needs_close;
53} file_blob_t;
54
55static inline file_blob_t *file_from_blob(bithenge_blob_t *base)
56{
57 return (file_blob_t *)base;
58}
59
60static inline bithenge_blob_t *blob_from_file(file_blob_t *blob)
61{
62 return &blob->base;
63}
64
65static int file_size(bithenge_blob_t *base, aoff64_t *size)
66{
67 file_blob_t *blob = file_from_blob(base);
68 *size = blob->size;
69 return EOK;
70}
71
72static int file_read(bithenge_blob_t *base, aoff64_t offset, char *buffer,
73 aoff64_t *size)
74{
75 file_blob_t *blob = file_from_blob(base);
76 if (offset > blob->size)
77 return ELIMIT;
78 *size = min(*size, blob->size - offset);
79 int rc = lseek(blob->fd, offset, SEEK_SET);
80 if (rc != EOK)
81 return rc;
82 return read(blob->fd, buffer, *size);
83}
84
85static int file_destroy(bithenge_blob_t *base)
86{
87 file_blob_t *blob = file_from_blob(base);
88 close(blob->fd);
89 free(blob);
90 return EOK;
91}
92
93static const bithenge_random_access_blob_ops_t file_ops = {
94 .size = file_size,
95 .read = file_read,
96 .destroy = file_destroy,
97};
98
99static int new_file_blob(bithenge_node_t **out, int fd, bool needs_close)
100{
101 assert(out);
102
103 struct stat stat;
104 int rc = fstat(fd, &stat);
105 if (rc != EOK) {
106 if (needs_close)
107 close(fd);
108 return rc;
109 }
110
111 // Create blob
112 file_blob_t *blob = malloc(sizeof(*blob));
113 if (!blob) {
114 if (needs_close)
115 close(fd);
116 return ENOMEM;
117 }
118 rc = bithenge_new_random_access_blob(blob_from_file(blob),
119 &file_ops);
120 if (rc != EOK) {
121 free(blob);
122 if (needs_close)
123 close(fd);
124 return rc;
125 }
126 blob->fd = fd;
127 blob->size = stat.size;
128 blob->needs_close = needs_close;
129 *out = bithenge_blob_as_node(blob_from_file(blob));
130
131 return EOK;
132}
133
134/** Create a blob for a file. The blob must be freed with @a
135 * bithenge_node_t::bithenge_node_destroy after it is used.
136 * @param[out] out Stores the created blob.
137 * @param filename The name of the file.
138 * @return EOK on success or an error code from errno.h. */
139int bithenge_new_file_blob(bithenge_node_t **out, const char *filename)
140{
141 assert(filename);
142
143 int fd = open(filename, O_RDONLY);
144 if (fd < 0)
145 return fd;
146
147 return new_file_blob(out, fd, true);
148}
149
150/** Create a blob for a file descriptor. 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 fd The file descriptor.
154 * @return EOK on success or an error code from errno.h. */
155int bithenge_new_file_blob_from_fd(bithenge_node_t **out, int fd)
156{
157 return new_file_blob(out, fd, false);
158}
159
160/** Create a blob for a file pointer. The blob must be freed with @a
161 * bithenge_node_t::bithenge_node_destroy after it is used.
162 * @param[out] out Stores the created blob.
163 * @param file The file pointer.
164 * @return EOK on success or an error code from errno.h. */
165int bithenge_new_file_blob_from_file(bithenge_node_t **out, FILE *file)
166{
167 int fd = fileno(file);
168 if (fd < 0)
169 return errno;
170 return new_file_blob(out, fd, false);
171}
172
173/** @}
174 */
Note: See TracBrowser for help on using the repository browser.