-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.go
50 lines (43 loc) · 1.28 KB
/
write.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
package ldcache
import (
"bufio"
"bytes"
"io"
"os"
)
// SaveAs creates a file with the given filename and writes the ld.so.cache data to it
func (f *File) SaveAs(filename string) error {
fp, err := os.Create(filename)
if err != nil {
return err
}
defer fp.Close()
return f.WriteTo(fp)
}
// WriteTo updates information found in Header and writes the file to the given io.Writer.
func (f *File) WriteTo(w io.Writer) error {
// generate string table
f.Header.NLibs = uint32(len(f.Entries))
offset := uint32(headerLength) + uint32(entryLength)*f.Header.NLibs
stringTable := &bytes.Buffer{}
for _, e := range f.Entries {
e.keyPos = uint32(stringTable.Len()) + offset
stringTable.WriteString(e.Key)
stringTable.WriteByte(0)
e.valuePos = uint32(stringTable.Len()) + offset
stringTable.WriteString(e.Value)
stringTable.WriteByte(0)
}
f.Header.TableSize = uint32(stringTable.Len())
// we're ready to write
// use a bufio writer so we don't really have to care about handling the errors.
// bufio Writer says flush will return the latest write error if any occurs and it won't
// process any further writes.
wr := bufio.NewWriter(w)
wr.Write(f.Header.Bytes(f.Order))
for _, e := range f.Entries {
wr.Write(e.Bytes(f.Order))
}
wr.Write(stringTable.Bytes())
return wr.Flush()
}