diff --git a/ext4/internal/compactext4/compact.go b/ext4/internal/compactext4/compact.go index f2274fd4cf..57b265530d 100644 --- a/ext4/internal/compactext4/compact.go +++ b/ext4/internal/compactext4/compact.go @@ -945,7 +945,18 @@ func (w *Writer) writeDirectoryRecursive(dir, parent *inode) error { if err := w.writeDirectory(dir, parent); err != nil { return err } - for _, child := range dir.Children { + + // Follow e2fsck's convention and sort the children by inode number. + var children []string + for name := range dir.Children { + children = append(children, name) + } + sort.Slice(children, func(i, j int) bool { + return dir.Children[children[i]].Number < dir.Children[children[j]].Number + }) + + for _, name := range children { + child := dir.Children[name] if child.IsDir() { if err := w.writeDirectoryRecursive(child, dir); err != nil { return err