~vojtech-horky/helenos/helenos-qemu

« back to all changes in this revision

Viewing changes to uspace/lib/bithenge/src/file.c

  • Committer: Vojtech Horky
  • Date: 2017-06-14 06:22:31 UTC
  • mfrom: (2103.1.569 HelenOS.mainline)
  • Revision ID: vojtechhorky@users.sourceforge.net-20170614062231-q4ui9pyxp0gs2hrl
MergeĀ mainlineĀ changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
#include <assert.h>
39
39
#include <errno.h>
40
 
#include <fcntl.h>
41
40
#include <stdio.h>
42
41
#include <stdlib.h>
43
 
#include <sys/stat.h>
44
 
#include <sys/types.h>
45
 
#include <unistd.h>
 
42
#include <vfs/vfs.h>
 
43
#include <stddef.h>
46
44
#include "common.h"
47
45
#include <bithenge/blob.h>
48
46
#include <bithenge/file.h>
77
75
        file_blob_t *blob = blob_as_file(base);
78
76
        if (offset > blob->size)
79
77
                return ELIMIT;
80
 
        if (lseek(blob->fd, offset, SEEK_SET) < 0)
81
 
                return errno == EINVAL ? EIO : errno;
82
78
 
83
 
        ssize_t amount_read;
84
 
        aoff64_t remaining_size = *size;
85
 
        *size = 0;
86
 
        do {
87
 
                amount_read = read(blob->fd, buffer, remaining_size);
88
 
                if (amount_read < 0)
89
 
                        return errno;
90
 
                buffer += amount_read;
91
 
                *size += amount_read;
92
 
                remaining_size -= amount_read;
93
 
        } while (remaining_size && amount_read);
 
79
        ssize_t amount_read = vfs_read(blob->fd, &offset, buffer, *size);
 
80
        if (amount_read < 0)
 
81
                return errno;
 
82
        *size += amount_read;
94
83
        return EOK;
95
84
}
96
85
 
97
86
static void file_destroy(bithenge_blob_t *base)
98
87
{
99
88
        file_blob_t *blob = blob_as_file(base);
100
 
        close(blob->fd);
 
89
        vfs_put(blob->fd);
101
90
        free(blob);
102
91
}
103
92
 
112
101
        assert(out);
113
102
 
114
103
        struct stat stat;
115
 
        int rc = fstat(fd, &stat);
116
 
        if (rc != 0) {
 
104
        int rc = vfs_stat(fd, &stat);
 
105
        if (rc != EOK) {
117
106
                if (needs_close)
118
 
                        close(fd);
 
107
                        vfs_put(fd);
119
108
                return rc;
120
109
        }
121
110
 
123
112
        file_blob_t *blob = malloc(sizeof(*blob));
124
113
        if (!blob) {
125
114
                if (needs_close)
126
 
                        close(fd);
 
115
                        vfs_put(fd);
127
116
                return ENOMEM;
128
117
        }
129
118
        rc = bithenge_init_random_access_blob(file_as_blob(blob), &file_ops);
130
119
        if (rc != EOK) {
131
120
                free(blob);
132
121
                if (needs_close)
133
 
                        close(fd);
 
122
                        vfs_put(fd);
134
123
                return rc;
135
124
        }
136
125
        blob->fd = fd;
154
143
{
155
144
        assert(filename);
156
145
 
157
 
        int fd = open(filename, O_RDONLY);
 
146
        int fd = vfs_lookup_open(filename, WALK_REGULAR, MODE_READ);
158
147
        if (fd < 0)
159
148
                return errno;
160
149