Skip to content

Commit

Permalink
cmd/p9: cleanup address parsing (#60)
Browse files Browse the repository at this point in the history
* cmd/p9: Remove redundant error wrapping when dialing.

* cmd/p9: Clean up address handling.

* all: Use custom Errorf that doesn't wrap io.EOF.
  • Loading branch information
DeedleFake authored Oct 11, 2019
1 parent bf16927 commit 589bc79
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 90 deletions.
7 changes: 4 additions & 3 deletions cmd/p9/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/signal"

"github.com/DeedleFake/p9"
"github.com/DeedleFake/p9/internal/util"
"github.com/DeedleFake/p9/proto"
)

Expand All @@ -34,7 +35,7 @@ func (cmd *exportCmd) Run(options GlobalOptions, args []string) error {
rw := fset.Bool("rw", false, "Make exported FS writable.")
err := fset.Parse(args[1:])
if err != nil {
return fmt.Errorf("parse flags: %w", err)
return util.Errorf("parse flags: %w", err)
}

args = fset.Args()
Expand All @@ -51,7 +52,7 @@ func (cmd *exportCmd) Run(options GlobalOptions, args []string) error {

lis, err := net.Listen(options.Network, options.Address)
if err != nil {
return fmt.Errorf("listen: %w", err)
return util.Errorf("listen: %w", err)
}
defer lis.Close()

Expand All @@ -63,7 +64,7 @@ func (cmd *exportCmd) Run(options GlobalOptions, args []string) error {
p9.FSConnHandler(fs, uint32(options.MSize)),
)
if err != nil {
errC <- fmt.Errorf("serve: %w", err)
errC <- util.Errorf("serve: %w", err)
}
}()

Expand Down
9 changes: 5 additions & 4 deletions cmd/p9/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/DeedleFake/p9"
"github.com/DeedleFake/p9/internal/util"
)

type lsCmd struct {
Expand Down Expand Up @@ -36,7 +37,7 @@ func (cmd *lsCmd) Run(options GlobalOptions, args []string) error {
fset.BoolVar(&cmd.showDetails, "l", false, "Show details.")
err := fset.Parse(args[1:])
if err != nil {
return fmt.Errorf("parse flags: %w", err)
return util.Errorf("parse flags: %w", err)
}

args = fset.Args()
Expand All @@ -52,13 +53,13 @@ func (cmd *lsCmd) Run(options GlobalOptions, args []string) error {

d, err := a.Open(arg, p9.OREAD)
if err != nil {
return fmt.Errorf("open %q: %w", arg, err)
return util.Errorf("open %q: %w", arg, err)
}
defer d.Close()

fi, err := d.Stat("")
if err != nil {
return fmt.Errorf("stat %q: %w", arg, err)
return util.Errorf("stat %q: %w", arg, err)
}

if !fi.IsDir() {
Expand All @@ -68,7 +69,7 @@ func (cmd *lsCmd) Run(options GlobalOptions, args []string) error {

entries, err := d.Readdir()
if err != nil {
return fmt.Errorf("read dir %q: %w", arg, err)
return util.Errorf("read dir %q: %w", arg, err)
}
sort.Slice(entries, func(i1, i2 int) bool {
return entries[i1].EntryName < entries[i2].EntryName
Expand Down
67 changes: 26 additions & 41 deletions cmd/p9/p9.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/DeedleFake/p9"
"github.com/DeedleFake/p9/internal/util"
)

const StandardPort = "564"
Expand Down Expand Up @@ -106,62 +107,54 @@ func (versionCmd) Run(options GlobalOptions, args []string) error {
func attach(options GlobalOptions, f func(*p9.Remote) error) error {
c, err := p9.Dial(options.Network, options.Address)
if err != nil {
return fmt.Errorf("dial %q, %q: %w", options.Network, options.Address, err)
return err
}
defer c.Close()

_, err = c.Handshake(uint32(options.MSize))
if err != nil {
return fmt.Errorf("handshake: %w", err)
return util.Errorf("handshake: %w", err)
}

a, err := c.Attach(nil, options.UName, options.AName)
if err != nil {
return fmt.Errorf("attach %q: %w", options.AName, err)
return util.Errorf("attach %q: %w", options.AName, err)
}
defer a.Close()

return f(a)
}

func parseAddr(addr string) (network, host, port string) {
defer func() {
if (len(host) > 0) && (host[0] == '$') {
network, host, port = getNamespaceHost(network, host, port)
}
func parseAddr(addr string) (network, address string) {
switch {
case strings.HasPrefix(addr, "$"):
return getNamespace(addr[1:])

case strings.HasPrefix(addr, "./"), strings.HasPrefix(addr, "/"):
return "unix", addr
}

switch port {
case "9fs", "9p":
port = StandardPort
parts := strings.SplitN(addr, ":", 2)
if len(parts) == 2 {
if (parts[1] == "9p") || (parts[1] == "9fs") {
parts[1] = StandardPort
}
}()

if (len(addr) > 0) && (addr[0] == '/') {
return "unix", addr, ""
return "tcp", strings.Join(parts, ":")
}

parts := strings.SplitN(addr, "!", 3)
parts = strings.SplitN(addr, "!", 3)
switch len(parts) {
case 0:
return "tcp", "localhost", StandardPort

case 1:
port = StandardPort
sub := strings.SplitN(parts[0], ":", 2)
host = sub[0]
if len(sub) == 2 {
port = sub[1]
}
return "tcp", host, port

case 2:
return parts[0], parts[1], ""

return parts[0], parts[1] + ":" + StandardPort
case 3:
return parts[0], parts[1], parts[2]
if (parts[2] == "9p") || (parts[2] == "9fs") {
parts[2] = StandardPort
}
return parts[0], strings.Join(parts[1:], ":")
}

panic("This should never be reached.")
return "tcp", addr + ":" + StandardPort
}

type GlobalOptions struct {
Expand All @@ -187,7 +180,7 @@ func main() {
flag.StringVar(
&options.Address,
"addr",
"localhost:"+StandardPort,
"",
"When acting as a server, the address to bind to. When acting as a client, the address to connect to.",
)
flag.UintVar(
Expand All @@ -201,15 +194,7 @@ func main() {
help := flag.Bool("help", false, "Show this help.")
flag.Parse()

n, a, p := parseAddr(options.Address)
if (p == "") && strings.HasPrefix(a, "tcp") {
p = StandardPort
}
options.Network = n
options.Address = a
if p != "" {
options.Address += ":" + p
}
options.Network, options.Address = parseAddr(options.Address)

runCommand := func(c Command) {
err := c.Run(options, flag.Args())
Expand Down
18 changes: 16 additions & 2 deletions cmd/p9/p9_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

package main

func getNamespaceHost(network, host, port string) (string, string, string) {
return network, host, port
import (
"os"
"os/user"
"path/filepath"
)

func getNamespace(name string) (network, addr string) {
u, err := user.Current()
if err != nil {
panic(err)
}

nsdir := filepath.Join(os.TempDir(), "ns."+u.Username+".:0")
os.MkdirAll(nsdir, 0700)

return "unix", filepath.Join(nsdir, name)
}
6 changes: 3 additions & 3 deletions cmd/p9/p9_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"path/filepath"
)

func getNamespaceHost(network, host, port string) (string, string, string) {
func getNamespace(name string) (network, addr string) {
u, err := user.Current()
if err != nil {
panic(err)
}

nsdir := filepath.Join("/", "tmp", "ns."+u.Username+".:0")
_ = os.MkdirAll(nsdir, 0700)
os.MkdirAll(nsdir, 0700)

return "unix", filepath.Join(nsdir, host[1:]), ""
return "unix", filepath.Join(nsdir, name)
}
19 changes: 10 additions & 9 deletions cmd/p9/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"

"github.com/DeedleFake/p9"
"github.com/DeedleFake/p9/internal/util"
)

type readCmd struct {
Expand Down Expand Up @@ -36,7 +37,7 @@ func (cmd *readCmd) Run(options GlobalOptions, args []string) error {
fset.BoolVar(&cmd.tar, "tar", false, "Output files as tar.")
err := fset.Parse(args[1:])
if err != nil {
return fmt.Errorf("parse flags: %v", err)
return util.Errorf("parse flags: %v", err)
}

args = fset.Args()
Expand All @@ -51,7 +52,7 @@ func (cmd *readCmd) Run(options GlobalOptions, args []string) error {

_, err = io.Copy(os.Stdout, f)
if err != nil {
return fmt.Errorf("read %q: %w", arg, err)
return util.Errorf("read %q: %w", arg, err)
}

return nil
Expand All @@ -66,7 +67,7 @@ func (cmd *readCmd) Run(options GlobalOptions, args []string) error {

fi, err := f.Stat("")
if err != nil {
return fmt.Errorf("stat %q: %w", arg, err)
return util.Errorf("stat %q: %w", arg, err)
}

switch {
Expand All @@ -75,13 +76,13 @@ func (cmd *readCmd) Run(options GlobalOptions, args []string) error {
case fi.IsDir():
children, err := f.Readdir()
if err != nil {
return fmt.Errorf("read dir %q: %w", arg, err)
return util.Errorf("read dir %q: %w", arg, err)
}

for _, c := range children {
cf, err := f.Open(c.EntryName, p9.OREAD)
if err != nil {
return fmt.Errorf("open %q: %w", path.Join(arg, c.EntryName), err)
return util.Errorf("open %q: %w", path.Join(arg, c.EntryName), err)
}

err = writeFile(path.Join(arg, c.EntryName), cf)
Expand All @@ -93,20 +94,20 @@ func (cmd *readCmd) Run(options GlobalOptions, args []string) error {
default:
hdr, err := tar.FileInfoHeader(fi, "")
if err != nil {
return fmt.Errorf("file info header for %q: %w", arg, err)
return util.Errorf("file info header for %q: %w", arg, err)
}
hdr.Name = arg
hdr.Uname = fi.UID
hdr.Gname = fi.GID

err = out.WriteHeader(hdr)
if err != nil {
return fmt.Errorf("write header for %q: %w", arg, err)
return util.Errorf("write header for %q: %w", arg, err)
}

_, err = io.Copy(out, f)
if err != nil {
return fmt.Errorf("read %q: %w", arg, err)
return util.Errorf("read %q: %w", arg, err)
}
}

Expand All @@ -118,7 +119,7 @@ func (cmd *readCmd) Run(options GlobalOptions, args []string) error {
for _, arg := range args {
f, err := a.Open(arg, p9.OREAD)
if err != nil {
return fmt.Errorf("open %q: %w", arg, err)
return util.Errorf("open %q: %w", arg, err)
}

err = writeFile(arg, f)
Expand Down
5 changes: 3 additions & 2 deletions cmd/p9/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"text/tabwriter"

"github.com/DeedleFake/p9"
"github.com/DeedleFake/p9/internal/util"
)

type statCmd struct{}
Expand All @@ -33,7 +34,7 @@ func (cmd *statCmd) Run(options GlobalOptions, args []string) error {
format := fset.String("f", "text", "Output format. Supported formats are text and json.")
err := fset.Parse(args[1:])
if err != nil {
return fmt.Errorf("parse flags: %w", err)
return util.Errorf("parse flags: %w", err)
}

p := map[string]func(p9.DirEntry){
Expand All @@ -60,7 +61,7 @@ func (cmd *statCmd) Run(options GlobalOptions, args []string) error {
for i, arg := range args {
fi, err := a.Stat(arg)
if err != nil {
return fmt.Errorf("stat %q: %w", arg, err)
return util.Errorf("stat %q: %w", arg, err)
}

p(fi)
Expand Down
9 changes: 5 additions & 4 deletions cmd/p9/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"

"github.com/DeedleFake/p9"
"github.com/DeedleFake/p9/internal/util"
)

type writeCmd struct{}
Expand Down Expand Up @@ -37,7 +38,7 @@ func (cmd *writeCmd) Run(options GlobalOptions, args []string) error {
)
err := fset.Parse(args[1:])
if err != nil {
return fmt.Errorf("parse flags: %v", err)
return util.Errorf("parse flags: %v", err)
}

args = fset.Args()
Expand Down Expand Up @@ -65,20 +66,20 @@ func (cmd *writeCmd) Run(options GlobalOptions, args []string) error {

f, err := open()
if err != nil {
return fmt.Errorf("open %q: %v", args[0], err)
return util.Errorf("open %q: %v", args[0], err)
}
defer f.Close()

if *app {
_, err := f.Seek(0, io.SeekEnd)
if err != nil {
return fmt.Errorf("seek %q: %v", args[0], err)
return util.Errorf("seek %q: %v", args[0], err)
}
}

_, err = io.Copy(f, os.Stdin)
if err != nil {
return fmt.Errorf("write %q: %v", args[0], err)
return util.Errorf("write %q: %v", args[0], err)
}

return nil
Expand Down
Loading

0 comments on commit 589bc79

Please sign in to comment.