Debian Codegraph @

fat/add_file_test.go

raw symbols SCIP
1 package fat
2
3 import (
4 "io"
5 "io/ioutil"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "github.com/mitchellh/go-fs"
12 )
13
14 func TestAddFile(t *testing.T) {
15 // Create a temporary file to be our floppy drive
16 floppyF, err := ioutil.TempFile("", "go-fs")
17 if err != nil {
18 t.Fatalf("Error creating temporary file for floppy: %s", err)
19 }
20 defer floppyF.Close()
21 // defer os.Remove(floppyF.Name())
22
23 // Set the size of the file to be a floppy sized
24 if err := floppyF.Truncate(1440 * 1024); err != nil {
25 t.Fatalf("Error creating floppy: %s", err)
26 }
27
28 // BlockDevice backed by the file for our filesystem
29 device, err := fs.NewFileDisk(floppyF)
30 if err != nil {
31 t.Fatalf("Error creating floppy: %s", err)
32 }
33
34 // Format the block device so it contains a valid FAT filesystem
35 formatConfig := &SuperFloppyConfig{
36 FATType: FAT12,
37 Label: "go-fs",
38 OEMName: "go-fs",
39 }
40 if FormatSuperFloppy(device, formatConfig); err != nil {
41 t.Fatalf("Error creating floppy: %s", err)
42 }
43
44 // The actual FAT filesystem
45 fatFs, err := New(device)
46 if err != nil {
47 t.Fatalf("Error creating floppy: %s", err)
48 }
49
50 // Get the root directory to the filesystem
51 rootDir, err := fatFs.RootDir()
52 if err != nil {
53 t.Fatalf("Error creating floppy: %s", err)
54 }
55
56 var filenames [128]string
57
58 // Go over each file and copy it.
59 for i := 0; i < 128; i++ {
60 filenames[i] = strings.Repeat("A", i+1) + ".EXT"
61 }
62
63 for _, filename := range filenames {
64 if addSingleFile(rootDir, filename); err != nil {
65 t.Fatalf("Error adding file to floppy: %s", err)
66 }
67 }
68
69 entries := rootDir.Entries()
70
71 for i, entry := range entries {
72 expecting := filenames[i]
73 if entry.Name() != expecting {
74 // Name() returns the short name, how do we get the long name?
75 // t.Fatalf("Excepting %s, found %s", expecting, entry.Name())
76 }
77 }
78 }
79
80 func addSingleFile(dir fs.Directory, src string) error {
81 inputF, err := os.Create(src)
82 if err != nil {
83 return err
84 }
85 defer inputF.Close()
86 defer os.Remove(src)
87
88 entry, err := dir.AddFile(filepath.Base(src))
89 if err != nil {
90 return err
91 }
92
93 fatFile, err := entry.File()
94 if err != nil {
95 return err
96 }
97
98 if _, err := io.Copy(fatFile, inputF); err != nil {
99 return err
100 }
101
102 return nil
103 }