Skip to contentSkip to conversation

erofs-utils: add I/O control for tarerofs stream via `erofs_vfile`

PATCH Superseded Hongzhen Luo 2 participants 2 messages Jun 11, 2024 Library code

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 erofs-utils: add I/O control for tarerofs stream via `erofs_vfile`

This adds I/O control for tarerofs stream.

---
v2: Add I/O control for tarerofs stream and move the function erofs_read_from_fd to io.c
v1: https://lore.kernel.org/all/[email protected]/

---
 include/erofs/io.h  |  4 ++++
 include/erofs/tar.h |  2 +-
 lib/io.c            | 40 ++++++++++++++++++++++++++++++++++++++++
 lib/tar.c           | 34 +++++-----------------------------
 4 files changed, 50 insertions(+), 30 deletions(-)
include/erofs/io.h +4 -0
index c82dfdf..01147d7 100644
@@ -30,6 +30,8 @@ struct erofs_vfops {
 	int (*fsync)(struct erofs_vfile *vf);
 	int (*fallocate)(struct erofs_vfile *vf, u64 offset, size_t len, bool pad);
 	int (*ftruncate)(struct erofs_vfile *vf, u64 length);
+	int (*read)(struct erofs_vfile *vf, void *buf, size_t len);
+	u64 (*lseek)(struct erofs_vfile *vf, u64 offset, int whence);
 };
 
 struct erofs_vfile {
@@ -43,6 +45,8 @@ int erofs_io_fsync(struct erofs_vfile *vf);
 int erofs_io_fallocate(struct erofs_vfile *vf, u64 offset, size_t len, bool pad);
 int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length);
 int erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 offset, size_t len);
+int erofs_io_read(struct erofs_vfile *vf, void *buf, size_t len);
+u64 erofs_io_lseek(struct erofs_vfile *vf, u64 offset, int whence);
 
 ssize_t erofs_copy_file_range(int fd_in, u64 *off_in, int fd_out, u64 *off_out,
 			      size_t length);
include/erofs/tar.h +1 -1
index b5c966b..e1de0df 100644
@@ -39,7 +39,7 @@ struct erofs_iostream_liblzma {
 
 struct erofs_iostream {
 	union {
-		int fd;			/* original fd */
+		struct erofs_vfile vf;
 		void *handler;
 #ifdef HAVE_LIBLZMA
 		struct erofs_iostream_liblzma *lzma;
lib/io.c +40 -0
index 2db384c..0919951 100644
@@ -420,3 +420,43 @@ out:
 #endif
 	return __erofs_copy_file_range(fd_in, off_in, fd_out, off_out, length);
 }
+
+s64 erofs_read_from_fd(int fd, void *buf, u64 bytes)
+{
+	s64 i = 0;
+
+	while (bytes) {
+		int len = bytes > INT_MAX ? INT_MAX : bytes;
+		int ret;
+
+		ret = read(fd, buf + i, len);
+		if (ret < 1) {
+			if (ret == 0) {
+				break;
+			} else if (errno != EINTR) {
+				erofs_err("failed to read : %s\n",
+					  strerror(errno));
+				return -errno;
+			}
+		}
+		bytes -= ret;
+		i += ret;
+        }
+        return i;
+}
+
+int erofs_io_read(struct erofs_vfile *vf, void *buf, size_t len)
+{
+	if (vf->ops)
+		return vf->ops->read(vf, buf, len);
+
+	return erofs_read_from_fd(vf->fd, buf, len);
+}
+
+u64 erofs_io_lseek(struct erofs_vfile *vf, u64 offset, int whence)
+{
+	if (vf->ops)
+		return vf->ops->lseek(vf, offset, whence);
+
+	return lseek(vf->fd, offset, whence);
+}
lib/tar.c +5 -29
index 3514381..6202d35 100644
@@ -39,30 +39,6 @@ struct tar_header {
 	char padding[12];	/* 500-512 (pad to exactly the 512 byte) */
 };
 
-s64 erofs_read_from_fd(int fd, void *buf, u64 bytes)
-{
-	s64 i = 0;
-
-	while (bytes) {
-		int len = bytes > INT_MAX ? INT_MAX : bytes;
-		int ret;
-
-		ret = read(fd, buf + i, len);
-		if (ret < 1) {
-			if (ret == 0) {
-				break;
-			} else if (errno != EINTR) {
-				erofs_err("failed to read : %s\n",
-					  strerror(errno));
-				return -errno;
-			}
-		}
-		bytes -= ret;
-		i += ret;
-        }
-        return i;
-}
-
 void erofs_iostream_close(struct erofs_iostream *ios)
 {
 	free(ios->buffer);
@@ -79,7 +55,7 @@ void erofs_iostream_close(struct erofs_iostream *ios)
 #endif
 		return;
 	}
-	close(ios->fd);
+	close(ios->vf.fd);
 }
 
 int erofs_iostream_open(struct erofs_iostream *ios, int fd, int decoder)
@@ -119,7 +95,7 @@ int erofs_iostream_open(struct erofs_iostream *ios, int fd, int decoder)
 		return -EOPNOTSUPP;
 #endif
 	} else {
-		ios->fd = fd;
+		ios->vf.fd = fd;
 		fsz = lseek(fd, 0, SEEK_END);
 		if (fsz <= 0) {
 			ios->feof = !fsz;
@@ -218,8 +194,8 @@ int erofs_iostream_read(struct erofs_iostream *ios, void **buf, u64 bytes)
 			return -EOPNOTSUPP;
 #endif
 		} else {
-			ret = erofs_read_from_fd(ios->fd, ios->buffer + rabytes,
-						 ios->bufsize - rabytes);
+			ret = erofs_io_read(&ios->vf, ios->buffer + rabytes,
+					    ios->bufsize - rabytes);
 			if (ret < 0)
 				return ret;
 			ios->tail += ret;
@@ -271,7 +247,7 @@ int erofs_iostream_lskip(struct erofs_iostream *ios, u64 sz)
 		return sz;
 
 	if (ios->sz && likely(ios->dumpfd < 0)) {
-		s64 cur = lseek(ios->fd, sz, SEEK_CUR);
+		s64 cur = erofs_io_lseek(&ios->vf, sz, SEEK_CUR);
 
 		if (cur > ios->sz)
 			return cur - ios->sz;
Signature · 1 line
2.39.3
#1 lore raw
Quoted 53 lines · Hongzhen Luo

This adds I/O control for tarerofs stream.

---
v2: Add I/O control for tarerofs stream and move the function erofs_read_from_fd to io.c
v1: https://lore.kernel.org/all/[email protected]/

---
  include/erofs/io.h  |  4 ++++
  include/erofs/tar.h |  2 +-
  lib/io.c            | 40 ++++++++++++++++++++++++++++++++++++++++
  lib/tar.c           | 34 +++++-----------------------------
  4 files changed, 50 insertions(+), 30 deletions(-)
include/erofs/io.h +4 -0
index c82dfdf..01147d7 100644
@@ -30,6 +30,8 @@ struct erofs_vfops {
  	int (*fsync)(struct erofs_vfile *vf);
  	int (*fallocate)(struct erofs_vfile *vf, u64 offset, size_t len, bool pad);
  	int (*ftruncate)(struct erofs_vfile *vf, u64 length);
+	int (*read)(struct erofs_vfile *vf, void *buf, size_t len);
+	u64 (*lseek)(struct erofs_vfile *vf, u64 offset, int whence);
  };
 
  struct erofs_vfile {
@@ -43,6 +45,8 @@ int erofs_io_fsync(struct erofs_vfile *vf);
  int erofs_io_fallocate(struct erofs_vfile *vf, u64 offset, size_t len, bool pad);
  int erofs_io_ftruncate(struct erofs_vfile *vf, u64 length);
  int erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 offset, size_t len);
+int erofs_io_read(struct erofs_vfile *vf, void *buf, size_t len);
+u64 erofs_io_lseek(struct erofs_vfile *vf, u64 offset, int whence);
 
  ssize_t erofs_copy_file_range(int fd_in, u64 *off_in, int fd_out, u64 *off_out,
  			      size_t length);
include/erofs/tar.h +1 -1
index b5c966b..e1de0df 100644
@@ -39,7 +39,7 @@ struct erofs_iostream_liblzma {
 
  struct erofs_iostream {
  	union {
-		int fd;			/* original fd */
+		struct erofs_vfile vf;
  		void *handler;
  #ifdef HAVE_LIBLZMA
  		struct erofs_iostream_liblzma *lzma;
lib/io.c +0 -0
index 2db384c..0919951 100644

lib/io.c out: · line 424

Quoted 5 lines
lib/io.c +2 -0
@@ -420,3 +420,43 @@ out:
  #endif
  	return __erofs_copy_file_range(fd_in, off_in, fd_out, off_out, length);
  }
+
+s64 erofs_read_from_fd(int fd, void *buf, u64 bytes)

I guess we could fold this function into erofs_io_read(). Otherwise it looks good to me.

Thanks,
Gao Xiang

Quoted 31 lines
lib/io.c +31 -0
+{
+	s64 i = 0;
+
+	while (bytes) {
+		int len = bytes > INT_MAX ? INT_MAX : bytes;
+		int ret;
+
+		ret = read(fd, buf + i, len);
+		if (ret < 1) {
+			if (ret == 0) {
+				break;
+			} else if (errno != EINTR) {
+				erofs_err("failed to read : %s\n",
+					  strerror(errno));
+				return -errno;
+			}
+		}
+		bytes -= ret;
+		i += ret;
+        }
+        return i;
+}
+
+int erofs_io_read(struct erofs_vfile *vf, void *buf, size_t len)
+{
+	if (vf->ops)
+		return vf->ops->read(vf, buf, len);
+
+	return erofs_read_from_fd(vf->fd, buf, len);
+}
+