Commit b9b6493 fixed uncompressed packed inodes by not always writing
compressed data, but it broke compressed packed inodes because now
uncompressed file data is always written after the compressed data.

The new error handling always rewinds with lseek and falls through to
write_uncompressed_file_from_fd, regardless of whether the compressed
data was written successfully (ret = 0) or not (ret = -ENOSPC). This
can result in corrupted files.

Fix it by simplifying the error handling to better match the old code.

Fixes: b9b6493 ("erofs-utils: lib: fix uncompressed packed inode")
Signed-off-by: Danny Lin <[email protected]>
---
 lib/inode.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/lib/inode.c b/lib/inode.c
index bc3cb76..797c622 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -1927,14 +1927,16 @@ struct erofs_inode 
*erofs_mkfs_build_special_from_fd(struct erofs_sb_info *sbi,
 
                DBG_BUGON(!ictx);
                ret = erofs_write_compressed_file(ictx);
-               if (ret && ret != -ENOSPC)
-                        return ERR_PTR(ret);
+               if (ret == -ENOSPC) {
+                       ret = lseek(fd, 0, SEEK_SET);
+                       if (ret < 0)
+                               return ERR_PTR(-errno);
 
-               ret = lseek(fd, 0, SEEK_SET);
-               if (ret < 0)
-                       return ERR_PTR(-errno);
+                       ret = write_uncompressed_file_from_fd(inode, fd);
+               }
+       } else {
+               ret = write_uncompressed_file_from_fd(inode, fd);
        }
-       ret = write_uncompressed_file_from_fd(inode, fd);
        if (ret)
                return ERR_PTR(ret);
        erofs_prepare_inode_buffer(inode);
-- 
2.46.0

Reply via email to