-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathclnt_open.go
97 lines (81 loc) · 1.96 KB
/
clnt_open.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2009 The Go9p 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 go9p
import (
"strings"
)
// Opens the file associated with the fid. Returns nil if
// the operation is successful.
func (clnt *Clnt) Open(fid *Fid, mode uint8) error {
tc := clnt.NewFcall()
err := PackTopen(tc, fid.Fid, mode)
if err != nil {
return err
}
rc, err := clnt.Rpc(tc)
if err != nil {
return err
}
fid.Qid = rc.Qid
fid.Iounit = rc.Iounit
if fid.Iounit == 0 || fid.Iounit > clnt.Msize-IOHDRSZ {
fid.Iounit = clnt.Msize - IOHDRSZ
}
fid.Mode = mode
return nil
}
// Creates a file in the directory associated with the fid. Returns nil
// if the operation is successful.
func (clnt *Clnt) Create(fid *Fid, name string, perm uint32, mode uint8, ext string) error {
tc := clnt.NewFcall()
err := PackTcreate(tc, fid.Fid, name, perm, mode, ext, clnt.Dotu)
if err != nil {
return err
}
rc, err := clnt.Rpc(tc)
if err != nil {
return err
}
fid.Qid = rc.Qid
fid.Iounit = rc.Iounit
if fid.Iounit == 0 || fid.Iounit > clnt.Msize-IOHDRSZ {
fid.Iounit = clnt.Msize - IOHDRSZ
}
fid.Mode = mode
return nil
}
// Creates and opens a named file.
// Returns the file if the operation is successful, or an Error.
func (clnt *Clnt) FCreate(path string, perm uint32, mode uint8) (*File, error) {
n := strings.LastIndex(path, "/")
if n < 0 {
n = 0
}
fid, err := clnt.FWalk(path[0:n])
if err != nil {
return nil, err
}
if path[n] == '/' {
n++
}
err = clnt.Create(fid, path[n:], perm, mode, "")
if err != nil {
clnt.Clunk(fid)
return nil, err
}
return &File{fid, 0}, nil
}
// Opens a named file. Returns the opened file, or an Error.
func (clnt *Clnt) FOpen(path string, mode uint8) (*File, error) {
fid, err := clnt.FWalk(path)
if err != nil {
return nil, err
}
err = clnt.Open(fid, mode)
if err != nil {
clnt.Clunk(fid)
return nil, err
}
return &File{fid, 0}, nil
}