forked from jjhuff/go-owfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.go
59 lines (51 loc) · 1.43 KB
/
dir.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
package owfs
import (
"encoding/binary"
"fmt"
"net"
"strings"
)
func (oc *OwfsClient) Dir(path string) ([]string, error) {
conn, err := net.Dial("tcp", oc.connString)
if err != nil {
return nil, fmt.Errorf("Failed to connect to owserver: %s", err)
}
defer conn.Close()
payload := make([]byte, len(path)+1) // To get null terminated
copy(payload, []byte(path))
data := RequestHeader{
Type: msg_dir,
PayloadLength: int32(len(payload)),
}
ret := []string{}
//log.Println("Attempting to send data")
err = binary.Write(conn, binary.BigEndian, data)
if err != nil {
return nil, fmt.Errorf("Failed to write header to owserver: %s", err)
}
err = binary.Write(conn, binary.BigEndian, payload)
if err != nil {
return nil, fmt.Errorf("Failed to write payload to owserver: %s", err)
}
//log.Println("Attempting to read response")
var response ResponseHeader
for {
err = binary.Read(conn, binary.BigEndian, &response)
if err != nil {
return nil, fmt.Errorf("Failed to read header from owserver: %s", err)
}
//response.dump()
if response.PayloadLength > 0 {
buf := make([]byte, response.PayloadLength)
err = binary.Read(conn, binary.BigEndian, &buf)
if err != nil {
return nil, fmt.Errorf("Failed to read payload from owserver: %s", err)
}
ret = append(ret, strings.TrimSpace(string(buf[:len(buf)-1])))
//log.Println("Response Payload: ", string(buf))
} else {
break
}
}
return ret, nil
}