From 51f9efe40979758608c03f3ddcb97c7d5509b730 Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Tue, 27 Aug 2024 10:32:51 +0300 Subject: [PATCH] fix fat32 case-insensitivity when creating files, add test for it Signed-off-by: Avi Deitcher --- filesystem/fat32/fat32.go | 6 +++++- filesystem/fat32/fat32_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/filesystem/fat32/fat32.go b/filesystem/fat32/fat32.go index ae653173..1f968f07 100644 --- a/filesystem/fat32/fat32.go +++ b/filesystem/fat32/fat32.go @@ -830,7 +830,11 @@ func (fs *FileSystem) readDirWithMkdir(p string, doMake bool) (*Directory, []*di // do we have an entry whose name is the same as this name? found := false for _, e := range entries { - if e.filenameLong != subp && e.filenameShort != subp && (!e.lowercaseShortname || (e.lowercaseShortname && !strings.EqualFold(e.filenameShort, subp))) { + // if the filename does not match, continue + // match is determined by any one of: + // - long filename == provided name + // - uppercase(short filename) == uppercase(provided name) + if e.filenameLong != subp && !strings.EqualFold(e.filenameShort, subp) { continue } if !e.isSubdirectory { diff --git a/filesystem/fat32/fat32_test.go b/filesystem/fat32/fat32_test.go index fab083ce..3217f431 100644 --- a/filesystem/fat32/fat32_test.go +++ b/filesystem/fat32/fat32_test.go @@ -946,3 +946,35 @@ func TestFat32Label(t *testing.T) { } }) } + +func TestFat32MkdirCases(t *testing.T) { + f, err := tmpFat32(false, 0, 0) + if err != nil { + t.Fatal(err) + } + defer os.Remove(f.Name()) + fs, err := fat32.Create(f, 1048576, 0, 512, "") + if err != nil { + t.Error(err.Error()) + } + err = fs.Mkdir("/EFI/BOOT") + if err != nil { + t.Error(err.Error()) + } + // Make the same folders but now lowercase ... I expect it not to create anything new, + // these folders exist but are named /EFI/BOOT + err = fs.Mkdir("/efi/boot") + if err != nil { + t.Error(err.Error()) + } + files, err := fs.ReadDir("/") + if err != nil { + t.Error(err.Error()) + } + if len(files) != 1 { + for _, file := range files { + fmt.Printf("file: %s\n", file.Name()) + } + t.Fatalf("expected 1 file, found %d", len(files)) + } +}