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

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

Rename read() to vfs_read() and write() to vfs_write()

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