forked from rogpeppe/rog-go
/
write.go
68 lines (56 loc) · 1.6 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package g9pc
import "github.com/zenoss/rog-go/go9p/g9p"
// Write up to len(data) bytes starting from offset. Returns the
// number of bytes written, or an Error.
func (fid *Fid) WriteAt(data []byte, offset uint64) (int, error) {
clnt := fid.Client
tc := clnt.newFcall()
err := g9p.PackTwrite(tc, fid.Fid, offset, uint32(len(data)), data)
if err != nil {
return 0, err
}
rc, err := clnt.rpc(tc)
if err != nil {
return 0, err
}
if rc.Type == g9p.Rerror {
return 0, &g9p.Error{rc.Error, int(rc.Errornum)}
}
return int(rc.Count), nil
}
// Writes up to len(buf) bytes to a file. Returns the number of
// bytes written, or an Error.
func (file *File) Write(buf []byte) (int, error) {
n, err := file.WriteAt(buf, file.offset)
if err == nil {
file.offset += uint64(n)
}
return n, err
}
// Writes up to len(buf) bytes starting from offset. Returns the number
// of bytes written, or an Error.
func (file *File) WriteAt(buf []byte, offset uint64) (int, error) {
return file.fid.WriteAt(buf, offset)
}
// Writes exactly len(buf) bytes starting from offset. Returns the number of
// bytes written. If Error is returned the number of bytes can be less
// than len(buf).
func (file *File) Writen(buf []byte, offset uint64) (int, error) {
ret := 0
for len(buf) > 0 {
n, err := file.WriteAt(buf, offset)
if err != nil {
return ret, err
}
if n == 0 {
break
}
buf = buf[n:len(buf)]
offset += uint64(n)
ret += n
}
return ret, nil
}