From 4c96cdb0062ff40eda96cd6c2fa9fa0b0399e58d Mon Sep 17 00:00:00 2001 From: Logan Blyth Date: Tue, 24 Sep 2024 21:41:29 -0400 Subject: [PATCH] added test for file tree creation with large files --- filesystem/fat32/fat32_test.go | 104 +++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/filesystem/fat32/fat32_test.go b/filesystem/fat32/fat32_test.go index 83818e4b..67cd543e 100644 --- a/filesystem/fat32/fat32_test.go +++ b/filesystem/fat32/fat32_test.go @@ -10,10 +10,14 @@ import ( "crypto/rand" "fmt" "io" + mRand "math/rand" "os" + "strconv" "strings" "testing" + "github.com/diskfs/go-diskfs" + "github.com/diskfs/go-diskfs/disk" "github.com/diskfs/go-diskfs/filesystem" "github.com/diskfs/go-diskfs/filesystem/fat32" "github.com/diskfs/go-diskfs/testhelper" @@ -1058,3 +1062,103 @@ func TestOpenFileCaseInsensitive(t *testing.T) { } } } + +// small helper function to make a large fat32 filesystem +func mkfs(name string) filesystem.FileSystem { + size := int64(6 * 1024 * 1024 * 1024) + d, err := diskfs.Create(name, size, diskfs.Raw, diskfs.SectorSizeDefault) + if err != nil { + fmt.Printf("error creating disk: %v", err) + os.Exit(1) + } + + spec := disk.FilesystemSpec{ + Partition: 0, + FSType: filesystem.TypeFat32, + } + + fs, err := d.CreateFilesystem(spec) + if err != nil { + panic(err) + } + return fs +} + +func mkdir(fs filesystem.FileSystem, name string) { + err := fs.Mkdir(name) + if err != nil { + panic(err) + } +} + +func mkMicroFile(fs filesystem.FileSystem, name string) { + rw, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR) + if err != nil { + panic(err) + } + + _, err = rw.Write([]byte("hello World")) + if err != nil { + panic(err) + } +} +func mkSmallFile(fs filesystem.FileSystem, name string) { + rw, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR) + if err != nil { + panic(err) + } + + size := 5 * 1024 * 1024 + smallFile := make([]byte, size, size) + _, err = rw.Write(smallFile) + if err != nil { + panic(err) + } +} +func mkRandFile(fs filesystem.FileSystem, name string, rSize int) { + rw, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR) + if err != nil { + panic(err) + } + + size := rSize * 1024 * 1024 + randFile := make([]byte, size, size) + _, err = rw.Write(randFile) + if err != nil { + panic(err) + } +} +func mkGigFile(fs filesystem.FileSystem, name string) { + rw, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR) + if err != nil { + panic(err) + } + + size := 1024 * 1024 * 1024 + smallFile := make([]byte, size, size) + _, err = rw.Write(smallFile) + if err != nil { + panic(err) + } +} +func TestCreateFileTree(t *testing.T) { + fileName := "file-tree.img" + os.Remove(fileName) + f := mkfs(fileName) + mkdir(f, "/A") + mkdir(f, "/b") + mkMicroFile(f, "/rootfile") + r := mRand.New(mRand.NewSource(37)) + for i := 0; i < 100; i++ { + inc := strconv.Itoa(i) + mkdir(f, "/b/sub"+inc) + mkdir(f, "/b/sub"+inc+"/blob/") + mkMicroFile(f, "/b/sub"+inc+"/blob/microfile") + mkRandFile(f, "/b/sub"+inc+"/blob/randfile", r.Intn(73)) + mkSmallFile(f, "/b/sub"+inc+"/blob/smallfile") + + } + mkGigFile(f, "/b/sub49/blob/gigfile1") + mkGigFile(f, "/b/sub50/blob/gigfile1") + mkGigFile(f, "/b/sub55/blob/gigfile1") +}