Skip to content

Commit

Permalink
cmd/upspinfs: use the new errors package
Browse files Browse the repository at this point in the history
I also took out a lot of redundant debugging.  Now that I can
turn on the fuse libaries debugging a lot of what I had was
redundant.

Change-Id: I3aa6867da8acd272816736bcb36b347c300da01d
Reviewed-on: https://upspin-review.git.corp.google.com/2314
Reviewed-by: Eduardo Pinheiro <edpin@google.com>
Reviewed-by: Rob Pike <r@google.com>
  • Loading branch information
presotto committed Jun 20, 2016
1 parent db82b18 commit 93d04ea
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 192 deletions.
35 changes: 18 additions & 17 deletions cmd/upspinfs/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"upspin.io/access"
"upspin.io/bind"
"upspin.io/client"
"upspin.io/errors"
"upspin.io/log"
"upspin.io/pack"
"upspin.io/upspin"
Expand Down Expand Up @@ -70,13 +71,13 @@ func (c *cache) mkTemp() string {
// The corresponding node should be locked.
func (c *cache) create(h *handle) error {
if h.n.cf != nil {
return eio("unexpected create of an open file")
return errors.E(errors.IO, errors.Str("create of an open file"))
}
cf := &cachedFile{c: c, dirty: true}
cf.fname = c.mkTemp()
var err error
if cf.file, err = os.OpenFile(cf.fname, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700); err != nil {
return eio("creating %q file %q: %s", h.n.uname, cf.fname, err)
return err
}
h.n.cf = cf
return nil
Expand All @@ -97,11 +98,11 @@ func (c *cache) open(h *handle, flags fuse.OpenFlags) error {
cf := &cachedFile{c: c, inStore: true}
dir, err := n.f.dc.lookup(n.user)
if err != nil {
return enoent("%q", n.user)
return err
}
de, err := dir.Lookup(n.uname)
if err != nil {
return enoent("%q", n.uname)
return err
}

// Loop following redirects from the store.
Expand All @@ -111,7 +112,7 @@ func (c *cache) open(h *handle, flags fuse.OpenFlags) error {
loc := locations[i]
store, err := bind.Store(n.f.context, loc.Endpoint)
if err != nil {
finalErr = eio("%s bind.Store %v", err, loc)
finalErr = err
continue
}
var cdir string
Expand All @@ -135,7 +136,7 @@ func (c *cache) open(h *handle, flags fuse.OpenFlags) error {
var data []byte
var locs []upspin.Location
if data, locs, err = store.Get(loc.Reference); err != nil {
finalErr = eio("%s Get %q ref %q file %q", err, n.uname, loc.Reference, cf.fname)
finalErr = err
continue
}
if len(locs) > 0 {
Expand All @@ -153,18 +154,18 @@ func (c *cache) open(h *handle, flags fuse.OpenFlags) error {
}
packer := pack.Lookup(de.Metadata.Packing())
if packer == nil {
finalErr = eio("couldn't lookup %q ref %q file %q", n.uname, loc.Reference, cf.fname)
finalErr = errors.E(errors.IO, errors.Str("no packer found"))
continue
}
clearLen := packer.UnpackLen(n.f.context, data, de)
if clearLen < 0 {
finalErr = eio("couldn't unpack %q ref %q file %q", h.n.uname, loc.Reference, cf.fname)
finalErr = errors.E(errors.IO, errors.Str("unpack len < 0"))
continue
}
cleartext := make([]byte, clearLen)
rlen, err := packer.Unpack(n.f.context, cleartext, data, de)
if err != nil {
finalErr = eio("%s unpacking %q ref %q file %q", err, h.n.uname, loc.Reference, cf.fname)
finalErr = err
continue
}
cleartext = cleartext[:rlen]
Expand All @@ -173,12 +174,12 @@ func (c *cache) open(h *handle, flags fuse.OpenFlags) error {
if file, err = os.OpenFile(cf.fname, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700); err != nil {
os.Mkdir(cdir, 0777)
if file, err = os.OpenFile(cf.fname, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700); err != nil {
return eio("%s creating %q ref %q file %q", err, h.n.uname, loc.Reference, cf.fname)
return err
}
}
if wlen, err := file.Write(cleartext); err != nil || rlen != wlen {
file.Close()
return eio("%s writing %q ref %q file %q", err, h.n.uname, loc.Reference, cf.fname)
return err
}
cf.file = file
h.flags = flags
Expand Down Expand Up @@ -214,7 +215,7 @@ func (cf *cachedFile) markDirty() error {
fname := cf.c.mkTemp()
err := os.Rename(cf.fname, fname)
if err != nil {
return eio("renaming %q to %q: %s", cf.fname, fname, err)
return err
}
cf.fname = fname
return nil
Expand Down Expand Up @@ -244,14 +245,14 @@ func (cf *cachedFile) writeBack(h *handle) error {
log.Debug.Printf("writeBack %q, %s opened", n, cf.fname)
info, err := cf.file.Stat()
if err != nil {
return eio("stating %q (%q): %s", cf.fname, n.uname, err)
return err
}
cleartext := make([]byte, info.Size())
var sofar int64
for sofar != info.Size() {
len, err := cf.file.ReadAt(cleartext[sofar:], sofar)
if err != nil {
return eio("reading %q (%q): %s", cf.fname, n.uname, err)
return err
}
sofar += int64(len)
}
Expand All @@ -272,7 +273,7 @@ func (cf *cachedFile) writeBack(h *handle) error {
break
}
if tries > 5 || !strings.Contains(err.Error(), "unreachable") {
return eio("writing back %s (%q): %s", cf.fname, n.uname, err)
return err
}
time.Sleep(100 * time.Millisecond)
}
Expand All @@ -285,7 +286,7 @@ func (cf *cachedFile) writeBack(h *handle) error {
if err := os.Rename(cf.fname, fname); err != nil {
os.Mkdir(cdir, 0700)
if err := os.Rename(cf.fname, fname); err != nil {
return eio("renaming %s to %s: %s", cf.fname, fname, err)
return err
}
}
cf.fname = fname
Expand All @@ -303,7 +304,7 @@ func (c *cache) putRedirect(n *node, target string) error {
// Use the client library to write it.
loc, err := c.client.Put(n.uname, []byte(target))
if err != nil {
return eio("writing symlink %s: %s", n.uname, err)
return err
}

// Save it in the cache. If we can't, that's fine.
Expand Down
61 changes: 0 additions & 61 deletions cmd/upspinfs/errors.go

This file was deleted.

96 changes: 96 additions & 0 deletions cmd/upspinfs/errors_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2016 The Upspin Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin linux freebsd netbsd openbsd

package main

import (
"strings"
"syscall"

"bazil.org/fuse"

"upspin.io/errors"
"upspin.io/log"
)

// errnoError is a go string with a POSIX syscall error number.
type errnoError struct {
errno syscall.Errno
err error
}

func (u *errnoError) Error() string {
return u.err.Error()
}

func (u *errnoError) Errno() fuse.Errno {
return fuse.Errno(u.errno)
}

var errs = []struct {
str string
errno syscall.Errno
}{
{"not found", syscall.ENOENT},
{"not a directory", syscall.ENOTDIR},
{"no such", syscall.ENOENT},
{"permission", syscall.EPERM},
{"not empty", syscall.ENOTEMPTY},
}

var errnoToKind = map[syscall.Errno]errors.Kind{
syscall.EPERM: errors.Permission,
syscall.EEXIST: errors.Exist,
syscall.ENOENT: errors.NotExist,
syscall.EISDIR: errors.NotFile,
syscall.ENOTDIR: errors.NotDir,
syscall.ENOTEMPTY: errors.NotEmpty,
}

var kindToErrno = map[errors.Kind]syscall.Errno{
errors.Permission: syscall.EPERM,
errors.Exist: syscall.EEXIST,
errors.NotExist: syscall.ENOENT,
errors.NotFile: syscall.EISDIR,
errors.NotDir: syscall.ENOTDIR,
errors.NotEmpty: syscall.ENOTEMPTY,
errors.Syntax: syscall.ENOENT,
}

// e2e converts an upspin error into a fuse one.
func e2e(err error) *errnoError {
errno := syscall.EIO
if ue, ok := err.(*errors.Error); ok {
if e, ok := kindToErrno[ue.Kind]; ok {
errno = e
}
} else {
for _, e := range errs {
if strings.Contains(err.Error(), e.str) {
errno = e.errno
break
}
}
}
log.Debug.Println(err.Error())
return &errnoError{errno, err}
}

// classify returns the Kind of error whether or not this is from the upspin errors pkg.
func classify(err error) errors.Kind {
if ue, ok := err.(*errors.Error); ok {
return ue.Kind
}
for _, e := range errs {
if strings.Contains(err.Error(), e.str) {
if k, ok := errnoToKind[e.errno]; ok {
return k
}
break
}
}
return errors.IO
}
Loading

0 comments on commit 93d04ea

Please sign in to comment.