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

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

Remove unistd.h

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