// Given the networks & addresses of the source and destination filesystems, // this will connect to them and return a *CowFS ready to use them. (or an // error) func Mount(netSrc, addrSrc, netDst, addrDst string, user ninep.User) (*CowFS, error) { // the empty string here (and below) is the aname - which subtree to // connect to. right now we're just punting on providing a nice interface // to this. you get the whole tree. src, err := clnt.Mount(netSrc, addrSrc, "", user) if err != nil { return nil, fmt.Errorf("Error connecting to source fs : %s\n", err) } dst, err := clnt.Mount(netDst, addrDst, "", user) if err != nil { src.Unmount() return nil, fmt.Errorf("Error connecting to destination fs : %s\n", err) } return &CowFS{src: src, dst: dst}, nil }
// Connect to a 9P server that is listening at `addr', // and wrap ctl and data files of a remote serial device // into a Port. // Basename is the a file name prefix or directory, // where the device is expected to be found. It can // be "", if ctl and data files live in the 9P servers // root directory func Connect9P(addr, basename string) (port Port, err error) { c, err := clnt.Mount("tcp", addr, "", user.Current()) if err != nil { return } d := new(dev9) d.clnt = c if d.data, err = d.clnt.FOpen(basename+"/data", p.ORDWR); err == nil { if d.ctl, err = d.clnt.FOpen(basename+"/ctl", p.OWRITE); err != nil { goto noctl } } else if d.data, err = d.clnt.FOpen(basename, p.ORDWR); err == nil { if d.ctl, err = d.clnt.FOpen(basename+"ctl", p.OWRITE); err != nil { goto noctl } } else { goto unmount } port = d return noctl: d.data.Close() unmount: d.clnt.Unmount() return }
func main() { var n int var user p.User var err error var c *clnt.Clnt var file *clnt.File var buf []byte flag.Parse() user = p.OsUsers.Uid2User(os.Geteuid()) clnt.DefaultDebuglevel = *debuglevel c, err = clnt.Mount("tcp", *addr, "", user) if err != nil { goto error } if flag.NArg() != 1 { log.Println("invalid arguments") return } file, err = c.FOpen(flag.Arg(0), p.OREAD) if err != nil { goto error } buf = make([]byte, 8192) for { n, err = file.Read(buf) if n == 0 { break } os.Stdout.Write(buf[0:n]) } file.Close() if err != nil && err != io.EOF { goto error } return error: log.Println("Error", err) }
func main() { var user p.User var err error var c *clnt.Clnt var file *clnt.File var d []*p.Dir flag.Parse() user = p.OsUsers.Uid2User(os.Geteuid()) clnt.DefaultDebuglevel = *debuglevel c, err = clnt.Mount("tcp", *addr, "", user) if err != nil { log.Fatal(err) } lsarg := "/" if flag.NArg() == 1 { lsarg = flag.Arg(0) } else if flag.NArg() > 1 { log.Fatal("error: only one argument expected") } file, err = c.FOpen(lsarg, p.OREAD) if err != nil { log.Fatal(err) } for { d, err = file.Readdir(0) if d == nil || len(d) == 0 || err != nil { break } for i := 0; i < len(d); i++ { os.Stdout.WriteString(d[i].Name + "\n") } } file.Close() if err != nil && err != io.EOF { log.Fatal(err) } return }
func main() { var user p.User var ba [][]byte var nreqs int var rchan chan *clnt.Req var tag *clnt.Tag var fid *clnt.Fid var wnames []string flag.Parse() user = p.OsUsers.Uid2User(os.Geteuid()) clnt.DefaultDebuglevel = *debuglevel c, err := clnt.Mount("tcp", *addr, "", user) if err != nil { goto error } if flag.NArg() != 1 { log.Println("invalid arguments") return } ba = make([][]byte, 100) for i := 0; i < len(ba); i++ { ba[i] = make([]byte, 8192) } nreqs = 0 rchan = make(chan *clnt.Req) tag = c.TagAlloc(rchan) // walk the file wnames = strings.Split(flag.Arg(0), "/") for wnames[0] == "" { wnames = wnames[1:] } fid = c.FidAlloc() for root := c.Root; len(wnames) > 0; root = fid { n := len(wnames) if n > 8 { n = 8 } err = tag.Walk(root, fid, wnames[0:n]) if err != nil { goto error } nreqs++ wnames = wnames[n:] } err = tag.Open(fid, p.OREAD) if err != nil { goto error } for i := 0; i < len(ba); i++ { err = tag.Read(fid, uint64(i*8192), 8192) if err != nil { goto error } nreqs++ } err = tag.Clunk(fid) // now start reading... for nreqs > 0 { r := <-rchan if r.Tc.Type == p.Tread { i := r.Tc.Offset / 8192 copy(ba[i], r.Rc.Data) ba[i] = ba[i][0:r.Rc.Count] } nreqs-- } for i := 0; i < len(ba); i++ { os.Stdout.Write(ba[i]) } return error: log.Println("error: ", err) }