|
| 1 | +package diskfs_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + |
| 8 | + diskfs "github.com/diskfs/go-diskfs" |
| 9 | + "github.com/diskfs/go-diskfs/disk" |
| 10 | + "github.com/diskfs/go-diskfs/filesystem" |
| 11 | + "github.com/diskfs/go-diskfs/partition/gpt" |
| 12 | + "github.com/diskfs/go-diskfs/partition/mbr" |
| 13 | +) |
| 14 | + |
| 15 | +func check(err error) { |
| 16 | + if err != nil { |
| 17 | + log.Fatal(err) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func unused(_ ...any) { |
| 22 | +} |
| 23 | + |
| 24 | +// Create a disk image of size 10MB with a FAT32 filesystem spanning the entire disk. |
| 25 | +func ExampleCreate_fat32() { |
| 26 | + var size int64 = 10 * 1024 * 1024 // 10 MB |
| 27 | + |
| 28 | + diskImg := "/tmp/disk.img" |
| 29 | + defer os.Remove(diskImg) |
| 30 | + theDisk, _ := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault) |
| 31 | + |
| 32 | + fs, err := theDisk.CreateFilesystem(disk.FilesystemSpec{ |
| 33 | + Partition: 0, |
| 34 | + FSType: filesystem.TypeFat32, |
| 35 | + }) |
| 36 | + check(err) |
| 37 | + unused(fs) |
| 38 | +} |
| 39 | + |
| 40 | +// Create a disk of size 20MB with an MBR partition table, a single partition beginning at block 2048 (1MB), |
| 41 | +// of size 10MB filled with a FAT32 filesystem. |
| 42 | +func ExampleCreate_mbr() { |
| 43 | + var size int64 = 20 * 1024 * 1024 // 20 MB |
| 44 | + |
| 45 | + diskImg := "/tmp/disk.img" |
| 46 | + defer os.Remove(diskImg) |
| 47 | + theDisk, _ := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault) |
| 48 | + |
| 49 | + table := &mbr.Table{ |
| 50 | + LogicalSectorSize: 512, |
| 51 | + PhysicalSectorSize: 512, |
| 52 | + Partitions: []*mbr.Partition{ |
| 53 | + { |
| 54 | + Bootable: false, |
| 55 | + Type: mbr.Linux, |
| 56 | + Start: 2048, |
| 57 | + Size: 20480, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + check(theDisk.Partition(table)) |
| 63 | + |
| 64 | + fs, err := theDisk.CreateFilesystem(disk.FilesystemSpec{ |
| 65 | + Partition: 1, |
| 66 | + FSType: filesystem.TypeFat32, |
| 67 | + }) |
| 68 | + check(err) |
| 69 | + unused(fs) |
| 70 | +} |
| 71 | + |
| 72 | +// Create a disk of size 20MB with a GPT partition table, a single partition beginning at block 2048 (1MB), of size 10MB, and fill with the contents from the 10MB file "/root/contents.dat" |
| 73 | +func ExampleCreate_gpt() { |
| 74 | + var size int64 = 20 * 1024 * 1024 // 20 MB |
| 75 | + |
| 76 | + diskImg := "/tmp/disk.img" |
| 77 | + defer os.Remove(diskImg) |
| 78 | + theDisk, _ := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault) |
| 79 | + |
| 80 | + table := &gpt.Table{ |
| 81 | + LogicalSectorSize: 512, |
| 82 | + PhysicalSectorSize: 512, |
| 83 | + ProtectiveMBR: true, |
| 84 | + Partitions: []*gpt.Partition{ |
| 85 | + { |
| 86 | + Start: 1 * 1024 * 1024 / 512, |
| 87 | + Size: 10 * 1024 * 1024, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + check(theDisk.Partition(table)) |
| 93 | + |
| 94 | + f, err := os.Open("/root/contents.dat") |
| 95 | + check(err) |
| 96 | + |
| 97 | + written, err := theDisk.WritePartitionContents(1, f) |
| 98 | + check(err) |
| 99 | + unused(written) |
| 100 | +} |
| 101 | + |
| 102 | +// Create a disk of size 20MB with an MBR partition table, a single partition beginning at block 2048 (1MB), |
| 103 | +// of size 10MB filled with a FAT32 filesystem, and create some directories and files in that filesystem. |
| 104 | +func ExampleCreate_fat32WithDirsAndFiles() { |
| 105 | + var size int64 = 20 * 1024 * 1024 // 20 MB |
| 106 | + |
| 107 | + diskImg := "/tmp/disk.img" |
| 108 | + defer os.Remove(diskImg) |
| 109 | + theDisk, _ := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault) |
| 110 | + |
| 111 | + table := &mbr.Table{ |
| 112 | + LogicalSectorSize: 512, |
| 113 | + PhysicalSectorSize: 512, |
| 114 | + Partitions: []*mbr.Partition{ |
| 115 | + { |
| 116 | + Bootable: false, |
| 117 | + Type: mbr.Linux, |
| 118 | + Start: 2048, |
| 119 | + Size: 20480, |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + check(theDisk.Partition(table)) |
| 125 | + |
| 126 | + fs, err := theDisk.CreateFilesystem(disk.FilesystemSpec{ |
| 127 | + Partition: 1, |
| 128 | + FSType: filesystem.TypeFat32, |
| 129 | + }) |
| 130 | + check(err) |
| 131 | + |
| 132 | + err = fs.Mkdir("/FOO/BAR") |
| 133 | + check(err) |
| 134 | + |
| 135 | + rw, err := fs.OpenFile("/FOO/BAR/AFILE.EXE", os.O_CREATE|os.O_RDWR) |
| 136 | + check(err) |
| 137 | + b := make([]byte, 1024) |
| 138 | + |
| 139 | + _, err = rand.Read(b) |
| 140 | + check(err) |
| 141 | + |
| 142 | + written, err := rw.Write(b) |
| 143 | + check(err) |
| 144 | + unused(written) |
| 145 | +} |
0 commit comments