source: mainline/uspace/lib/bithenge/src/file.c@ 23a0368

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 23a0368 was 23a0368, checked in by Jakub Jermar <jakub@…>, 8 years ago

Rename stat() to vfs_stat_path() and fstat() to vfs_stat()

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