Skip to content

Commit 5840e72

Browse files
author
Hongzhen Luo
committed
[EROFS] implement pread() for ErofsFile
Currently, ErofsFile did not support pread(), this patch addresses this deficiency. Subsequent unit tests will be added accordingly. Signed-off-by: Hongzhen Luo <hongzhen@linux.alibaba.com>
1 parent 8ae3331 commit 5840e72

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/overlaybd/tar/erofs/erofs_fs.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,50 @@ int ErofsFile::fiemap(struct photon::fs::fiemap *map)
241241
return 0;
242242
}
243243

244+
ssize_t ErofsFile::pread(void *buf, size_t count, off_t offset)
245+
{
246+
struct erofs_inode *inode = &file_private->inode;
247+
struct erofs_map_blocks map;
248+
erofs_off_t ptr = offset;
249+
ssize_t read = 0;
250+
int ret;
251+
252+
map.index = UINT_MAX;
253+
while (ptr < offset + count) {
254+
char *estart = (char*)buf + ptr - offset;
255+
erofs_off_t eend, moff = 0;
256+
map.m_la = ptr;
257+
ret = erofs_map_blocks(inode, &map, 0);
258+
if (ret || map.m_plen != map.m_llen)
259+
LOG_ERROR_RETURN(0, -1, "[erofs_fs] fail to map blocks");
260+
eend = std::min(offset + count, map.m_la + map.m_llen);
261+
if (ptr < map.m_la)
262+
LOG_ERROR_RETURN(0, -1, "[erofs_fs] invalid read offset");
263+
if (!(map.m_flags & EROFS_MAP_MAPPED)) {
264+
if (!map.m_llen) {
265+
/* reached EOF */
266+
memset((void*)estart, 0, offset + count - ptr);
267+
ptr = offset + count;
268+
continue;
269+
}
270+
memset((void*)estart, 0, eend - ptr);
271+
ptr = eend;
272+
continue;;
273+
}
274+
if (ptr > map.m_la) {
275+
moff = ptr - map.m_la;
276+
map.m_la = ptr;
277+
}
278+
ret = erofs_read_one_data(inode, &map, estart, moff,
279+
eend - map.m_la);
280+
if (ret)
281+
return ret;
282+
read += eend - map.m_la;
283+
ptr = eend;
284+
}
285+
return read;
286+
}
287+
244288
// ErofsFileSystem
245289
EROFS_UNIMPLEMENTED_FUNC(photon::fs::IFile*, ErofsFileSystem, open(const char *pathname, int flags, mode_t mode), NULL)
246290
EROFS_UNIMPLEMENTED_FUNC(photon::fs::IFile*, ErofsFileSystem, creat(const char *pathname, mode_t mode), NULL)

src/overlaybd/tar/erofs/erofs_fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class ErofsFile: public photon::fs::VirtualReadOnlyFile {
6565
photon::fs::IFileSystem *filesystem();
6666
int fstat(struct stat *buf);
6767
int fiemap(struct photon::fs::fiemap *map);
68+
ssize_t pread(void *buf, size_t count, off_t offset);
6869
private:
6970
ErofsFileSystem *fs;
7071
struct ErofsFileInt;

0 commit comments

Comments
 (0)