Changeset 50985c34 in mainline for uspace/app/bithenge/file.c


Ignore:
Timestamp:
2012-06-01T00:04:30Z (12 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
11b9ad7
Parents:
5c5c346a
Message:

Bithenge: create file blobs from fds and FILE*s

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/file.c

    r5c5c346a r50985c34  
    4040#include <fcntl.h>
    4141#include <macros.h>
     42#include <stdio.h>
    4243#include <stdlib.h>
    4344#include <sys/stat.h>
     
    4950        int fd;
    5051        aoff64_t size; // needed by file_read()
     52        bool needs_close;
    5153} file_blob_t;
    5254
     
    9597};
    9698
     99static int new_file_blob(bithenge_blob_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 = blob_from_file(blob);
     130
     131        return EOK;
     132}
     133
    97134/** Create a blob for a file. The blob must be freed with @a
    98135 * bithenge_blob_t::bithenge_blob_destroy after it is used.
     
    102139int bithenge_new_file_blob(bithenge_blob_t **out, const char *filename)
    103140{
    104         assert(out);
    105141        assert(filename);
    106142
    107         int fd;
    108         fd = open(filename, O_RDONLY);
     143        int fd = open(filename, O_RDONLY);
    109144        if (fd < 0)
    110145                return fd;
    111146
    112         struct stat stat;
    113         int rc = fstat(fd, &stat);
    114         if (rc != EOK) {
    115                 close(fd);
    116                 return rc;
    117         }
     147        return new_file_blob(out, fd, true);
     148}
    118149
    119         // Create blob
    120         file_blob_t *blob = malloc(sizeof(*blob));
    121         if (!blob) {
    122                 close(fd);
    123                 return ENOMEM;
    124         }
    125         rc = bithenge_new_random_access_blob(blob_from_file(blob),
    126             &file_ops);
    127         if (rc != EOK) {
    128                 free(blob);
    129                 close(fd);
    130                 return rc;
    131         }
    132         blob->fd = fd;
    133         blob->size = stat.size;
    134         *out = blob_from_file(blob);
     150/** Create a blob for a file descriptor. The blob must be freed with @a
     151 * bithenge_blob_t::bithenge_blob_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_blob_t **out, int fd)
     156{
     157        return new_file_blob(out, fd, false);
     158}
    135159
    136         return EOK;
     160/** Create a blob for a file pointer. The blob must be freed with @a
     161 * bithenge_blob_t::bithenge_blob_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_blob_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);
    137171}
    138172
Note: See TracChangeset for help on using the changeset viewer.