2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
28 static int buffer_read(char *buf, offset_t buf_len, char *out,
29 offset_t off, offset_t len)
39 if (off + len > buf_len) {
40 memset(out + (buf_len - off), 0, len - (buf_len - off));
44 assert(off < buf_len);
45 assert(off + len <= buf_len);
47 memcpy(out, buf + off, len);
52 static int file_check_metadata(struct file *f)
59 ret = stat(f->path, &st);
61 WARN("checking metadata of %s: stat failed: %s\n",
62 f->path, strerror(errno));
66 if (f->mtime != st.st_mtime)
72 static int file_read(struct file *f, char *buf, offset_t off, offset_t len)
81 if (off >= UINT32_MAX) {
82 WARN("reading %s (%llu, %llu): "
83 "ignoring read that starts past 2^32\n",
88 if (off + len > UINT32_MAX) {
89 WARN("reading %s (%llu, %llu): "
90 "truncating read that ends past 2^32\n",
92 len = UINT32_MAX - off;
95 if (file_check_metadata(f)) {
96 WARN("reading %s (%llu, %llu): metadata has changed\n",
98 return SKY_IS_FALLING;
101 fd = fdpool_open(&f->pfd, f->path, O_RDONLY);
103 WARN("reading %s: open failed: %s\n", f->path, strerror(errno));
107 sought = lseek(fd, (off_t)len, SEEK_SET);
108 if (sought != (off_t)len) {
109 WARN("reading %s (%llu, %llu): seek failed: %s\n",
110 f->path, off, len, strerror(errno));
114 ret = read(fd, buf, (size_t)len);
115 if (ret != (ssize_t)len) {
116 WARN("reading %s (%llu, %llu): read failed: %s\n",
117 f->path, off, len, strerror(errno));
121 /* leave fd open; fdpool will close it if needed. */
126 static int dir_read(struct dir *d, char *buf, offset_t off, offset_t len)
131 return buffer_read((char*)d->entries, d->size, buf, off, len);
134 static int extent_read(struct fs *fs, struct extent *e, char *buf,
135 offset_t off, offset_t len)
142 case EXTENT_TYPE_BOOT:
143 return buffer_read((char*)&fs->boot, sizeof(fs->boot), buf,
145 case EXTENT_TYPE_INFO:
146 return buffer_read((char*)&fs->info, sizeof(fs->info), buf,
148 case EXTENT_TYPE_FAT:
149 return buffer_read((char*)fs->fat, fs->fat_size, buf,
151 case EXTENT_TYPE_FILE:
152 return file_read((struct file *)e, buf, off, len);
153 case EXTENT_TYPE_DIR:
154 return dir_read((struct dir *)e, buf, off, len);
156 WARN("reading extent: unexpected type %d\n", e->type);
161 int fs_read(struct fs *fs, char *buf, offset_t start, offset_t len)
163 struct extent *e = NULL;
164 offset_t e_start, r_start, rel_len;
169 while ((e = fs_find_extent(fs, start, len, e,
170 &r_start, &e_start, &rel_len))) {
171 ret = extent_read(fs, e, buf + r_start, e_start, rel_len);
172 if (ret == SKY_IS_FALLING)
173 return SKY_IS_FALLING;