]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "bytes" | |
5 | "encoding/binary" | |
6 | "errors" | |
7 | "fmt" | |
8 | "io" | |
9 | ) | |
10 | ||
11 | type transfer struct { | |
12 | Protocol [4]byte // "HTXF" 0x48545846 | |
13 | ReferenceNumber [4]byte // Unique Type generated for the transfer | |
14 | DataSize [4]byte // File size | |
15 | RSVD [4]byte // Not implemented in Hotline Protocol | |
16 | } | |
17 | ||
18 | var HTXF = [4]byte{0x48, 0x54, 0x58, 0x46} // (HTXF) is the only supported transfer protocol | |
19 | ||
20 | func (tf *transfer) Write(b []byte) (int, error) { | |
21 | if err := binary.Read(bytes.NewReader(b), binary.BigEndian, tf); err != nil { | |
22 | return 0, err | |
23 | } | |
24 | ||
25 | if tf.Protocol != HTXF { | |
26 | return 0, errors.New("invalid protocol") | |
27 | } | |
28 | ||
29 | return len(b), nil | |
30 | } | |
31 | ||
32 | func receiveFile(r io.Reader, targetFile, resForkFile, infoFork, counterWriter io.Writer) error { | |
33 | var ffo flattenedFileObject | |
34 | if _, err := ffo.ReadFrom(r); err != nil { | |
35 | return fmt.Errorf("read flatted file object: %v", err) | |
36 | } | |
37 | ||
38 | // Write the information fork | |
39 | _, err := io.Copy(infoFork, &ffo.FlatFileInformationFork) | |
40 | if err != nil { | |
41 | return fmt.Errorf("write the information fork: %v", err) | |
42 | } | |
43 | ||
44 | if _, err = io.CopyN(targetFile, io.TeeReader(r, counterWriter), ffo.dataSize()); err != nil { | |
45 | return fmt.Errorf("copy file data to partial file: %v", err) | |
46 | } | |
47 | ||
48 | if ffo.FlatFileHeader.ForkCount == [2]byte{0, 3} { | |
49 | if err := binary.Read(r, binary.BigEndian, &ffo.FlatFileResForkHeader); err != nil { | |
50 | return fmt.Errorf("read resource fork header: %v", err) | |
51 | } | |
52 | ||
53 | if _, err = io.CopyN(resForkFile, io.TeeReader(r, counterWriter), ffo.rsrcSize()); err != nil { | |
54 | return fmt.Errorf("read resource fork: %v", err) | |
55 | } | |
56 | } | |
57 | return nil | |
58 | } |