-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.go
More file actions
88 lines (80 loc) · 2.52 KB
/
Copy pathcreate.go
File metadata and controls
88 lines (80 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package hfsgo
import (
"errors"
"fmt"
"strings"
"time"
"github.com/ObsoleteMadness/HFS-Go/disk"
"github.com/ObsoleteMadness/HFS-Go/hfs"
"github.com/ObsoleteMadness/HFS-Go/hfsplus"
"github.com/ObsoleteMadness/HFS-Go/part/apm"
)
// CreateVolume writes a volume-only classic HFS image (Basilisk II / Mini vMac .hfv).
func CreateVolume(path string, size int64, name string) error {
return CreateImage(path, size, name, "hfs", time.Time{})
}
// CreateHFSPlusVolume writes a volume-only HFS+ image.
func CreateHFSPlusVolume(path string, size int64, name string) error {
return CreateImage(path, size, name, "hfsplus", time.Time{})
}
// CreateImage writes a volume-only image formatted with filesystem
// ("hfs", "hfsplus", or "hfsx").
func CreateImage(path string, size int64, name, filesystem string, created time.Time) error {
d, err := disk.Create(path, "RAW", size)
if err != nil {
return err
}
defer d.Close()
return FormatContent(d.Content(), filesystem, name, created)
}
// CreateDisk writes a full disk: DDM + APM + one HFS+ partition.
func CreateDisk(path string, size int64, name string) error {
d, err := disk.Create(path, "RAW", size)
if err != nil {
return err
}
defer d.Close()
tbl, err := apm.Initialize(d, apm.Options{Name: name})
if err != nil {
return err
}
for _, p := range tbl.Partitions() {
t := p.TypeAsString()
if t == "Apple_HFS" || t == "Apple_HFSX" {
return hfsplus.Format(p.Content(), hfsplus.Options{Name: name})
}
}
return errors.New("hfsgo: no HFS partition in map")
}
// NormalizeFilesystem maps CLI names to hfs, hfsplus, or hfsx.
func NormalizeFilesystem(s string) (string, error) {
s = strings.ToLower(strings.TrimSpace(s))
s = strings.ReplaceAll(s, "+", "plus")
switch s {
case "hfs":
return "hfs", nil
case "hfsplus":
return "hfsplus", nil
case "hfsx":
return "hfsx", nil
default:
return "", fmt.Errorf("hfsgo: unknown filesystem %q (want hfs, hfsplus, or hfsx)", s)
}
}
// FormatContent formats volume content as classic HFS, HFS+, or HFSX.
func FormatContent(c disk.Content, filesystem, name string, created time.Time) error {
kind, err := NormalizeFilesystem(filesystem)
if err != nil {
return err
}
switch kind {
case "hfs":
return hfs.Format(c, hfs.Options{Name: name, Created: created})
case "hfsplus":
return hfsplus.Format(c, hfsplus.Options{Name: name, Created: created})
case "hfsx":
return hfsplus.Format(c, hfsplus.Options{Name: name, CaseSensitive: true, Created: created})
default:
return fmt.Errorf("hfsgo: unknown filesystem %q", filesystem)
}
}