Skip to contentSkip to conversation

erofs-utils: fix the erofs_io_pread and erofs_io_pwrite

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

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: fix the erofs_io_pread and erofs_io_pwrite

When vf->ops is not null, vf->ops->pread returns the number of bytes successfully read, which is inconsistent with the semantics of erofs_io_pread. Similar situation occurs in erofs_io_pwrite. This fixes this issue.

---
 lib/io.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)
lib/io.c +16 -10
index c523f00..52a74dc 100644
@@ -34,15 +34,18 @@ ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf,
 	if (unlikely(cfg.c_dry_run))
 		return 0;
 
-	if (vf->ops)
-		return vf->ops->pwrite(vf, buf, pos, len);
-
 	pos += vf->offset;
 	do {
 #ifdef HAVE_PWRITE64
-		ret = pwrite64(vf->fd, buf, len, (off64_t)pos);
+		if (vf->ops)
+			ret = vf->ops->pwrite(vf, buf, pos, len);
+		else
+			ret = pwrite64(vf->fd, buf, len, (off64_t)pos);
 #else
-		ret = pwrite(vf->fd, buf, len, (off_t)pos);
+		if (vf->ops)
+			ret = vf->ops->pwrite(vf, buf, pos, len);
+		else
+			ret = pwrite(vf->fd, buf, len, (off_t)pos);
 #endif
 		if (ret <= 0) {
 			erofs_err("failed to write: %s", strerror(errno));
@@ -130,15 +133,18 @@ ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 pos, size_t len)
 	if (unlikely(cfg.c_dry_run))
 		return 0;
 
-	if (vf->ops)
-		return vf->ops->pread(vf, buf, pos, len);
-
 	pos += vf->offset;
 	do {
 #ifdef HAVE_PREAD64
-		ret = pread64(vf->fd, buf, len, (off64_t)pos);
+		if (vf->ops)
+			ret = vf->ops->pread(vf, buf, pos, len);
+		else
+			ret = pread64(vf->fd, buf, len, (off64_t)pos);
 #else
-		ret = pread(vf->fd, buf, len, (off_t)pos);
+		if (vf->ops)
+			ret = vf->ops->pread(vf, buf, pos, len);
+		else
+			ret = pread(vf->fd, buf, len, (off_t)pos);
 #endif
 		if (ret <= 0) {
 			if (!ret) {
Signature · 1 line
2.39.3
#1 lore raw
Quoted 6 lines · Hongzhen Luo

When vf->ops is not null, vf->ops->pread returns the number of bytes successfully read, which is inconsistent with the semantics of erofs_io_pread. Similar situation occurs in erofs_io_pwrite. This fixes this issue.

I think virtual files don't need to handle short read/write (or they need to handle those themselves.)

lib/io.c ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf, · line 35

Quoted 11 lines
---
  lib/io.c | 26 ++++++++++++++++----------
  1 file changed, 16 insertions(+), 10 deletions(-)
lib/io.c +0 -0
index c523f00..52a74dc 100644
@@ -34,15 +34,18 @@ ssize_t erofs_io_pwrite(struct erofs_vfile *vf, const void *buf,
  	if (unlikely(cfg.c_dry_run))
  		return 0;

But I think we might need to fix those return values for non-virtual files (return the written bytes.)

Quoted 22 lines
lib/io.c +8 -5
-	if (vf->ops)
-		return vf->ops->pwrite(vf, buf, pos, len);
-
  	pos += vf->offset;
  	do {
  #ifdef HAVE_PWRITE64
-		ret = pwrite64(vf->fd, buf, len, (off64_t)pos);
+		if (vf->ops)
+			ret = vf->ops->pwrite(vf, buf, pos, len);
+		else
+			ret = pwrite64(vf->fd, buf, len, (off64_t)pos);
  #else
-		ret = pwrite(vf->fd, buf, len, (off_t)pos);
+		if (vf->ops)
+			ret = vf->ops->pwrite(vf, buf, pos, len);
+		else
+			ret = pwrite(vf->fd, buf, len, (off_t)pos);
  #endif
  		if (ret <= 0) {
  			erofs_err("failed to write: %s", strerror(errno));

lib/io.c ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 pos, size_t len) · line 134

Quoted 2 lines
lib/io.c +0 -0
@@ -130,15 +133,18 @@ ssize_t erofs_io_pread(struct erofs_vfile *vf, void *buf, u64 pos, size_t len)
  	if (unlikely(cfg.c_dry_run))
  		return 0;

Same here.

Thanks,
Gao Xiang

Quoted 21 lines
lib/io.c +8 -5
-	if (vf->ops)
-		return vf->ops->pread(vf, buf, pos, len);
-
  	pos += vf->offset;
  	do {
  #ifdef HAVE_PREAD64
-		ret = pread64(vf->fd, buf, len, (off64_t)pos);
+		if (vf->ops)
+			ret = vf->ops->pread(vf, buf, pos, len);
+		else
+			ret = pread64(vf->fd, buf, len, (off64_t)pos);
  #else
-		ret = pread(vf->fd, buf, len, (off_t)pos);
+		if (vf->ops)
+			ret = vf->ops->pread(vf, buf, pos, len);
+		else
+			ret = pread(vf->fd, buf, len, (off_t)pos);
  #endif
  		if (ret <= 0) {
  			if (!ret) {