-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdirectories.go
66 lines (60 loc) · 2.11 KB
/
directories.go
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
package main
import (
"time"
)
func WriteDirectoryRecord(w *SectorWriter, identifier string, firstSectorNum uint32) uint32 {
if len(identifier) > 30 {
Panicf("directory identifier length %d is out of bounds", len(identifier))
}
recordLength := 33 + len(identifier)
w.WriteByte(byte(recordLength))
w.WriteByte(0) // number of sectors in extended attribute record
w.WriteBothEndianDWord(firstSectorNum)
w.WriteBothEndianDWord(SectorSize) // directory length
writeDirectoryRecordtimestamp(w, time.Now())
w.WriteByte(byte(3)) // bitfield; directory
w.WriteByte(byte(0)) // file unit size for an interleaved file
w.WriteByte(byte(0)) // interleave gap size for an interleaved file
w.WriteBothEndianWord(1) // volume sequence number
w.WriteByte(byte(len(identifier)))
w.WriteString(identifier)
// optional padding to even length
if recordLength % 2 == 1 {
recordLength++
w.WriteByte(0)
}
return uint32(recordLength)
}
func WriteFileRecordHeader(w *SectorWriter, identifier string, firstSectorNum uint32, fileSize uint32) uint32 {
if len(identifier) > 30 {
Panicf("directory identifier length %d is out of bounds", len(identifier))
}
recordLength := 33 + len(identifier)
w.WriteByte(byte(recordLength))
w.WriteByte(0) // number of sectors in extended attribute record
w.WriteBothEndianDWord(firstSectorNum) // first sector
w.WriteBothEndianDWord(fileSize)
writeDirectoryRecordtimestamp(w, time.Now())
w.WriteByte(byte(0)) // bitfield; normal file
w.WriteByte(byte(0)) // file unit size for an interleaved file
w.WriteByte(byte(0)) // interleave gap size for an interleaved file
w.WriteBothEndianWord(1) // volume sequence number
w.WriteByte(byte(len(identifier)))
w.WriteString(identifier)
// optional padding to even length
if recordLength % 2 == 1 {
recordLength++
w.WriteByte(0)
}
return uint32(recordLength)
}
func writeDirectoryRecordtimestamp(w *SectorWriter, t time.Time) {
t = t.UTC()
w.WriteByte(byte(t.Year() - 1900))
w.WriteByte(byte(t.Month()))
w.WriteByte(byte(t.Day()))
w.WriteByte(byte(t.Hour()))
w.WriteByte(byte(t.Minute()))
w.WriteByte(byte(t.Second()))
w.WriteByte(0) // UTC offset
}