Skip to content

Commit

Permalink
Finish removing afero in favor of hackpadfs
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnStarich committed Jul 28, 2021
1 parent 46c34ea commit 96a77b2
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 246 deletions.
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ require (
github.com/machinebox/progress v0.2.0
github.com/mattn/go-tty v0.0.3
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.3.0
github.com/stretchr/testify v1.5.1
go.uber.org/atomic v1.6.0
mvdan.cc/sh/v3 v3.1.2
)

replace github.com/spf13/afero v1.3.0 => github.com/johnstarich/afero v1.3.2-0.20210214021553-81c4e4e83b19
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ github.com/hack-pad/hackpadfs v0.0.0-20210721061701-8173de332f89 h1:N78BVvRpFvF1
github.com/hack-pad/hackpadfs v0.0.0-20210721061701-8173de332f89/go.mod h1:zXCMPoXvOxqv/hQh5drtWQmJZ1MtKKejvF+qaI+aPmo=
github.com/hack-pad/hackpadfs v0.0.0-20210728023418-c0d176de33ad h1:X98BtOtlhzejdiKec+i0xmTl2UFuYcuv/Vl243vSZro=
github.com/hack-pad/hackpadfs v0.0.0-20210728023418-c0d176de33ad/go.mod h1:Ecy1tNXrYbhYs5Bdd9MnEBOvDTCjLMfI99hTQ9oV5uA=
github.com/johnstarich/afero v1.3.2-0.20200824034706-e0c81fb79d7b h1:8XJ1QRVoSx+rcCoch3GI3lOCEIWLu7bsoU5xd+GO3sk=
github.com/johnstarich/afero v1.3.2-0.20200824034706-e0c81fb79d7b/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/johnstarich/afero v1.3.2-0.20210214021553-81c4e4e83b19 h1:dF789piTv5rw9ztiRTsTEWyVeVo90AC+5W1XIPawBEo=
github.com/johnstarich/afero v1.3.2-0.20210214021553-81c4e4e83b19/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/johnstarich/go v0.1.2 h1:F9dlaaVfeouu87o0phoCutz41FlzrFYKzwC4gV/DRlg=
github.com/johnstarich/go/datasize v0.0.1 h1:Hjswen8gwmO7trXtQ8Xl8NUOQAKKm1wvus3/xK5eHGY=
github.com/johnstarich/go/datasize v0.0.1/go.mod h1:4eHLMGz7Q5uCmZeS9rZdahvAih1QmBg1EW3bBXTJpi4=
Expand Down
4 changes: 1 addition & 3 deletions internal/fs/fs_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

package fs

import "github.com/spf13/afero"

type persistFs struct {
afero.Fs
hackpadfs.FS
}

func newPersistDB(name string, relaxedDurability bool, shouldCache ShouldCacher) (*persistFs, error) {
Expand Down
9 changes: 2 additions & 7 deletions internal/fs/null_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"os"
"time"

"github.com/spf13/afero"
"github.com/hack-pad/hackpadfs"
)

type nullFile struct {
name string
}

func newNullFile(name string) afero.File {
func newNullFile(name string) hackpadfs.File {
return nullFile{name: name}
}

Expand All @@ -22,13 +22,8 @@ func (f nullFile) ReadAt(p []byte, off int64) (n int, err error) { return 0, io
func (f nullFile) Seek(offset int64, whence int) (int64, error) { return 0, nil }
func (f nullFile) Write(p []byte) (n int, err error) { return len(p), nil }
func (f nullFile) WriteAt(p []byte, off int64) (n int, err error) { return len(p), nil }
func (f nullFile) Name() string { return f.name }
func (f nullFile) Readdir(count int) ([]os.FileInfo, error) { return nil, nil }
func (f nullFile) Readdirnames(n int) ([]string, error) { return nil, nil }
func (f nullFile) Stat() (os.FileInfo, error) { return nullStat{f}, nil }
func (f nullFile) Sync() error { return nil }
func (f nullFile) Truncate(size int64) error { return nil }
func (f nullFile) WriteString(s string) (ret int, err error) { return len(s), nil }

type nullStat struct {
f nullFile
Expand Down
2 changes: 1 addition & 1 deletion internal/fs/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (p pipeStat) Sys() interface{} { return nil }

func (p *pipeChan) Stat() (os.FileInfo, error) {
return &pipeStat{
name: p.Name(),
name: "",
size: int64(len(p.buf)),
mode: os.ModeNamedPipe,
}, nil
Expand Down
6 changes: 3 additions & 3 deletions internal/fs/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"sync"
"time"

"github.com/hack-pad/hackpadfs"
"github.com/johnstarich/go-wasm/log"
"github.com/spf13/afero"
)

var (
stdout afero.File = &bufferedLogger{name: "dev/stdout", printFn: log.Print}
stderr afero.File = &bufferedLogger{name: "dev/stderr", printFn: log.Error}
stdout hackpadfs.File = &bufferedLogger{name: "dev/stdout", printFn: log.Print}
stderr hackpadfs.File = &bufferedLogger{name: "dev/stderr", printFn: log.Error}
)

type bufferedLogger struct {
Expand Down
30 changes: 5 additions & 25 deletions internal/fs/unimplemented.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,15 @@ package fs
import (
"os"

"github.com/hack-pad/hackpadfs"
"github.com/johnstarich/go-wasm/internal/interop"
)

// unimplementedFile can be embedded in special files like /dev/null to provide a default unimplemented afero.File interface
var _ hackpadfs.File = &unimplementedFile{}

// unimplementedFile can be embedded in special files like /dev/null to provide a default unimplemented hackpadfs.File interface
type unimplementedFile struct{}

func (f unimplementedFile) Close() error { return interop.ErrNotImplemented }
func (f unimplementedFile) Read(p []byte) (n int, err error) { return 0, interop.ErrNotImplemented }
func (f unimplementedFile) ReadAt(p []byte, off int64) (n int, err error) {
return 0, interop.ErrNotImplemented
}
func (f unimplementedFile) Seek(offset int64, whence int) (int64, error) {
return 0, interop.ErrNotImplemented
}
func (f unimplementedFile) Write(p []byte) (n int, err error) { return 0, interop.ErrNotImplemented }
func (f unimplementedFile) WriteAt(p []byte, off int64) (n int, err error) {
return 0, interop.ErrNotImplemented
}

func (f unimplementedFile) Name() string { return "" }
func (f unimplementedFile) Readdir(count int) ([]os.FileInfo, error) {
return nil, interop.ErrNotImplemented
}
func (f unimplementedFile) Readdirnames(n int) ([]string, error) {
return nil, interop.ErrNotImplemented
}
func (f unimplementedFile) Stat() (os.FileInfo, error) { return nil, interop.ErrNotImplemented }
func (f unimplementedFile) Sync() error { return interop.ErrNotImplemented }
func (f unimplementedFile) Truncate(size int64) error { return interop.ErrNotImplemented }
func (f unimplementedFile) WriteString(s string) (ret int, err error) {
return 0, interop.ErrNotImplemented
}
func (f unimplementedFile) Stat() (os.FileInfo, error) { return nil, interop.ErrNotImplemented }
23 changes: 9 additions & 14 deletions internal/interop/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package interop
import (
"fmt"
"io"
"os"
"os/exec"

"github.com/hack-pad/hackpadfs"
"github.com/johnstarich/go-wasm/internal/common"
"github.com/johnstarich/go-wasm/log"
"github.com/pkg/errors"
"github.com/spf13/afero"
)

var (
Expand Down Expand Up @@ -55,24 +54,20 @@ func mapToErrNo(err error, debugMessage string) string {
return mapToErrNo(err.Unwrap(), debugMessage)
}
switch err {
case io.EOF, os.ErrNotExist, exec.ErrNotFound:
case io.EOF, exec.ErrNotFound:
return "ENOENT"
case os.ErrExist:
return "EEXIST"
case os.ErrPermission:
return "EPERM"
}
switch err.Error() {
case os.ErrClosed.Error(), afero.ErrFileClosed.Error():
return "EBADF" // if it was already closed, then the file descriptor was invalid
}
switch {
case os.IsNotExist(err):
case errors.Is(err, hackpadfs.ErrClosed):
return "EBADF" // if it was already closed, then the file descriptor was invalid
case errors.Is(err, hackpadfs.ErrNotExist):
return "ENOENT"
case os.IsExist(err):
case errors.Is(err, hackpadfs.ErrExist):
return "EEXIST"
case afero.IsDirErr(err):
case errors.Is(err, hackpadfs.ErrIsDir):
return "EISDIR"
case errors.Is(err, hackpadfs.ErrPermission):
return "EPERM"
default:
log.Errorf("Unknown error type: (%T) %+v\n\n%s", err, err, debugMessage)
return "EPERM"
Expand Down
2 changes: 1 addition & 1 deletion internal/js/fs/chown.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ func chownSync(args []js.Value) (interface{}, error) {
}

func Chown(path string, uid, gid int) error {
// TODO no-op, consider adding user and group ID support to afero
// TODO no-op, consider adding user and group ID support to hackpadfs
return nil
}
1 change: 0 additions & 1 deletion internal/js/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func Init() {

global.Set("getMounts", js.FuncOf(getMounts))
global.Set("destroyMount", js.FuncOf(destroyMount))
global.Set("overlayZip", js.FuncOf(overlayZip))
global.Set("overlayTarGzip", js.FuncOf(overlayTarGzip))
global.Set("overlayIndexedDB", js.FuncOf(overlayIndexedDB))
global.Set("dumpZip", js.FuncOf(dumpZip))
Expand Down
62 changes: 0 additions & 62 deletions internal/js/fs/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
package fs

import (
"archive/zip"
"bytes"
"context"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
Expand All @@ -19,8 +16,6 @@ import (
"github.com/hack-pad/hackpadfs"
"github.com/hack-pad/hackpadfs/indexeddb"
"github.com/machinebox/progress"
"github.com/spf13/afero"
"github.com/spf13/afero/zipfs"

"github.com/johnstarich/go-wasm/internal/common"
"github.com/johnstarich/go-wasm/internal/fs"
Expand All @@ -31,63 +26,6 @@ import (
"github.com/johnstarich/go/datasize"
)

func overlayZip(this js.Value, args []js.Value) interface{} {
resolve, reject, prom := promise.New()
log.Debug("Backgrounding overlay request")
go func() {
err := OverlayZip(args)
if err != nil {
reject(interop.WrapAsJSError(err, "Failed overlaying zip FS"))
} else {
log.Debug("Successfully overlayed zip FS")
resolve(nil)
}
}()
return prom
}

func OverlayZip(args []js.Value) error {
if len(args) != 2 {
return errors.New("overlayZip: mount path and zip URL path is required")
}

mountPath := args[0].String()
zipPath := args[1].String()
log.Debug("Downloading overlay zip FS: ", zipPath)
u, err := url.Parse(zipPath)
if err != nil {
return err
}
resp, err := http.Get(u.Path) // only download from current server, not just any URL
if err != nil {
return err
}
log.Debug("Download response received. Reading body...")

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
log.Debug("Finished reading download data. Overlaying FS...")
if err := resp.Body.Close(); err != nil {
return err
}

z, err := zip.NewReader(bytes.NewReader(body), resp.ContentLength)
if err != nil {
return err
}
return fs.Overlay(mountPath, &zipFS{zipfs.New(z)})
}

type zipFS struct {
afero.Fs
}

func (z *zipFS) Open(name string) (hackpadfs.File, error) {
return z.Fs.Open(name)
}

func overlayIndexedDB(this js.Value, args []js.Value) interface{} {
resolve, reject, prom := promise.New()
go func() {
Expand Down
Loading

0 comments on commit 96a77b2

Please sign in to comment.