Skip to contentSkip to conversation

erofs-utils: fsck: fix hole handling during extraction

PATCH New Yifan Zhao 1 participant 4 messages Sep 17 Library code

lore
Share & tools
lore

Yifan Zhao

author
#0 lore raw

PATCH 1/4 erofs-utils: fsck: fix hole handling during extraction

Fix the following two issues in fsck.erofs --extract:

- Trailing holes leave extracted files too short.
- With --xattr-inode-digest, holes are hashed without advancing the output offset, so subsequent data is written at an earlier offset.

Assisted-by Codex:gpt-6-astra
---
 fsck/main.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fsck/main.c +7 -1
index b2d8f1a..55da9ea 100644
@@ -569,7 +569,8 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd,
 						(const u8 *)zeros, chunk);
 					remain -= chunk;
 				}
-			} else if (outfd >= 0) {
+			}
+			if (outfd >= 0) {
 				ret = lseek(outfd, map.m_llen, SEEK_CUR);
 				if (ret < 0) {
 					ret = -errno;
@@ -855,6 +856,11 @@ again:
 	}
 
 	ret = erofsfsck_calc_inode_data(inode, fd);
+	if (!ret && ftruncate(fd, inode->i_size)) {
+		ret = -errno;
+		erofs_err("failed to set file size: %s (%s)",
+			  fsckcfg.extract_path, strerror(-ret));
+	}
 	close(fd);
 	return ret;
 }
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH 2/4 erofs-utils: mkfs: validate chunksize before computing chunkbits

Assisted-by Codex:gpt-6-astra
---
 mkfs/main.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mkfs/main.c +2 -1
index d9b4e22..f0d3d8c 100644
@@ -1262,8 +1262,9 @@ static int mkfs_parse_options_cfg(struct erofs_importer_params *params,
 		}
 
 		case 11:
+			errno = 0;
 			i = strtol(optarg, &endptr, 0);
-			if (*endptr != '\0') {
+			if (errno || *endptr != '\0' || i <= 0 || i > INT_MAX) {
 				erofs_err("invalid chunksize %s", optarg);
 				return -EINVAL;
 			}
Signature · 1 line
2.55.0
author
#2 lore raw

PATCH 3/4 erofs-utils: lib: fix rebuilding empty chunk-based files

In reply to Yifan Zhao

Switch empty files to FLAT_PLAIN before setting u.i_blkaddr to EROFS_NULL_ADDR, which would otherwise overwrite u.chunkformat.

Fixes b0b1711d4611 ("erofs-utils: support building image with reserved space")
Assisted-by Codex:gpt-6-astra
---
 lib/rebuild.c | 1 +
 1 file changed, 1 insertion(+)
lib/rebuild.c +1 -0
index ebac1c5..c378885 100644
@@ -335,6 +335,7 @@ static int erofs_rebuild_update_inode(struct erofs_sb_info *dst_sb,
 	}
 	case S_IFREG:
 		if (!inode->i_size) {
+			inode->datalayout = EROFS_INODE_FLAT_PLAIN;
 			inode->u.i_blkaddr = EROFS_NULL_ADDR;
 			break;
 		}
Signature · 1 line
2.55.0
author
#3 lore raw

PATCH 4/4 erofs-utils: fsck: avoid truncating lseek() return values

In reply to Yifan Zhao

Check lseek() directly to avoid truncating its return value to int, which can turn successful seeks beyond INT_MAX into extraction failures.

Fixes b063ea316aa9 ("erofs-utils: fsck: extract chunk-based file with hole correctly")
Assisted-by Codex:gpt-6-astra
---
 fsck/main.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fsck/main.c +1 -2
index 55da9ea..e54c523 100644
@@ -571,8 +571,7 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd,
 				}
 			}
 			if (outfd >= 0) {
-				ret = lseek(outfd, map.m_llen, SEEK_CUR);
-				if (ret < 0) {
+				if (lseek(outfd, map.m_llen, SEEK_CUR) < 0) {
 					ret = -errno;
 					goto out;
 				}
Signature · 1 line
2.55.0