Skip to content

Commit 15ebb7b

Browse files
authored
Create godoc-friendly examples (#268)
Move tests from the diskfs.go global comment section and update them as they are slightly out of date.
1 parent 0aefa87 commit 15ebb7b

File tree

2 files changed

+145
-92
lines changed

2 files changed

+145
-92
lines changed

diskfs.go

-92
Original file line numberDiff line numberDiff line change
@@ -8,98 +8,6 @@
88
// This is not intended as a replacement for operating system filesystem and disk drivers. Instead,
99
// it is intended to make it easy to work with partitions, partition tables and filesystems directly
1010
// without requiring operating system mounts.
11-
//
12-
// Some examples:
13-
//
14-
// 1. Create a disk image of size 10MB with a FAT32 filesystem spanning the entire disk.
15-
//
16-
// import diskfs "github.com/diskfs/go-diskfs"
17-
// size := 10*1024*1024 // 10 MB
18-
//
19-
// diskImg := "/tmp/disk.img"
20-
// disk := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault)
21-
//
22-
// fs, err := disk.CreateFilesystem(0, diskfs.TypeFat32)
23-
//
24-
// 2. Create a disk of size 20MB with an MBR partition table, a single partition beginning at block 2048 (1MB),
25-
// of size 10MB filled with a FAT32 filesystem.
26-
//
27-
// import diskfs "github.com/diskfs/go-diskfs"
28-
//
29-
// diskSize := 10*1024*1024 // 10 MB
30-
//
31-
// diskImg := "/tmp/disk.img"
32-
// disk := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault)
33-
//
34-
// table := &mbr.Table{
35-
// LogicalSectorSize: 512,
36-
// PhysicalSectorSize: 512,
37-
// Partitions: []*mbr.Partition{
38-
// {
39-
// Bootable: false,
40-
// Type: Linux,
41-
// Start: 2048,
42-
// Size: 20480,
43-
// },
44-
// },
45-
// }
46-
//
47-
// fs, err := disk.CreateFilesystem(1, diskfs.TypeFat32)
48-
//
49-
// 3. Create a disk of size 20MB with a GPT partition table, a single partition beginning at block 2048 (1MB),
50-
// of size 10MB, and fill with the contents from the 10MB file "/root/contents.dat"
51-
//
52-
// import diskfs "github.com/diskfs/go-diskfs"
53-
//
54-
// diskSize := 10*1024*1024 // 10 MB
55-
//
56-
// diskImg := "/tmp/disk.img"
57-
// disk := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault)
58-
//
59-
// table := &gpt.Table{
60-
// LogicalSectorSize: 512,
61-
// PhysicalSectorSize: 512,
62-
// Partitions: []*gpt.Partition{
63-
// {
64-
// LogicalSectorSize: 512,
65-
// PhysicalSectorSize: 512,
66-
// ProtectiveMBR: true,
67-
// },
68-
// },
69-
// }
70-
//
71-
// f, err := os.Open("/root/contents.dat")
72-
// written, err := disk.WritePartitionContents(1, f)
73-
//
74-
// 4. Create a disk of size 20MB with an MBR partition table, a single partition beginning at block 2048 (1MB),
75-
// of size 10MB filled with a FAT32 filesystem, and create some directories and files in that filesystem.
76-
//
77-
// import diskfs "github.com/diskfs/go-diskfs"
78-
//
79-
// diskSize := 10*1024*1024 // 10 MB
80-
//
81-
// diskImg := "/tmp/disk.img"
82-
// disk := diskfs.Create(diskImg, size, diskfs.Raw, diskfs.SectorSizeDefault)
83-
//
84-
// table := &mbr.Table{
85-
// LogicalSectorSize: 512,
86-
// PhysicalSectorSize: 512,
87-
// Partitions: []*mbr.Partition{
88-
// {
89-
// Bootable: false,
90-
// Type: Linux,
91-
// Start: 2048,
92-
// Size: 20480,
93-
// },
94-
// },
95-
// }
96-
//
97-
// fs, err := disk.CreateFilesystem(1, diskfs.TypeFat32)
98-
// err := fs.Mkdir("/FOO/BAR")
99-
// rw, err := fs.OpenFile("/FOO/BAR/AFILE.EXE", os.O_CREATE|os.O_RDRWR)
100-
// b := make([]byte, 1024, 1024)
101-
// rand.Read(b)
102-
// err := rw.Write(b)
10311
package diskfs
10412

10513
import (

example_test.go

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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

Comments
 (0)