Merge pull request #99 from moorereason/unused
Remove unused code
diff --git a/README.md b/README.md
index db20817..d9e3327 100644
--- a/README.md
+++ b/README.md
@@ -198,17 +198,17 @@
Then in my tests I would initialize a new MemMapFs for each test:
```go
func TestExist(t *testing.T) {
- appFS = afero.NewMemMapFs()
+ appFS := afero.NewMemMapFs()
// create test files and directories
- appFS.MkdirAll("src/a", 0755))
+ appFS.MkdirAll("src/a", 0755)
afero.WriteFile(appFS, "src/a/b", []byte("file b"), 0644)
afero.WriteFile(appFS, "src/c", []byte("file c"), 0644)
- _, err := appFS.Stat("src/c")
+ name := "src/c"
+ _, err := appFS.Stat(name)
if os.IsNotExist(err) {
- t.Errorf("file \"%s\" does not exist.\n", name)
+ t.Errorf("file \"%s\" does not exist.\n", name)
}
}
-
```
# Available Backends
diff --git a/afero.go b/afero.go
index e3df3f4..f5b5e12 100644
--- a/afero.go
+++ b/afero.go
@@ -77,7 +77,7 @@
// happens.
Remove(name string) error
- // RemoveAll removes a directory path and all any children it contains. It
+ // RemoveAll removes a directory path and any children it contains. It
// does not fail if the path does not exist (return nil).
RemoveAll(path string) error
diff --git a/composite_test.go b/composite_test.go
index 2663574..a8af48f 100644
--- a/composite_test.go
+++ b/composite_test.go
@@ -23,9 +23,9 @@
func CleanupTempDirs(t *testing.T) {
osfs := NewOsFs()
- type ev struct{
+ type ev struct {
path string
- e error
+ e error
}
errs := []ev{}
@@ -33,7 +33,7 @@
for _, x := range tempDirs {
err := osfs.RemoveAll(x)
if err != nil {
- errs = append(errs, ev{path:x,e: err})
+ errs = append(errs, ev{path: x, e: err})
}
}
diff --git a/mem/file.go b/mem/file.go
index 9096ff0..3c1e09a 100644
--- a/mem/file.go
+++ b/mem/file.go
@@ -59,7 +59,9 @@
modtime time.Time
}
-func (d FileData) Name() string {
+func (d *FileData) Name() string {
+ d.Lock()
+ defer d.Unlock()
return d.name
}
@@ -107,9 +109,7 @@
}
func (f *File) Name() string {
- f.fileData.Lock()
- defer f.fileData.Unlock()
- return f.fileData.name
+ return f.fileData.Name()
}
func (f *File) Stat() (os.FileInfo, error) {
diff --git a/memmap_test.go b/memmap_test.go
index 44d525b..9efd1f2 100644
--- a/memmap_test.go
+++ b/memmap_test.go
@@ -259,3 +259,26 @@
}
}
}
+
+// This test should be run with the race detector on:
+// go test -race -v -timeout 10s -run TestRacingDeleteAndClose
+func TestRacingDeleteAndClose(t *testing.T) {
+ fs := NewMemMapFs()
+ pathname := "testfile"
+ f, err := fs.Create(pathname)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ in := make(chan bool)
+
+ go func() {
+ <-in
+ f.Close()
+ }()
+ go func() {
+ <-in
+ fs.Remove(pathname)
+ }()
+ close(in)
+}
diff --git a/sftp.go b/sftp.go
deleted file mode 100644
index a095a54..0000000
--- a/sftp.go
+++ /dev/null
@@ -1,128 +0,0 @@
-// Copyright © 2015 Jerry Jacobs <jerry.jacobs@xor-gate.org>.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package afero
-
-import (
- "os"
- "time"
-
- "github.com/spf13/afero/sftp"
-
- "github.com/pkg/sftp"
-)
-
-// SftpFs is a Fs implementation that uses functions provided by the sftp package.
-//
-// For details in any method, check the documentation of the sftp package
-// (github.com/pkg/sftp).
-type SftpFs struct{
- SftpClient *sftp.Client
-}
-
-func (s SftpFs) Name() string { return "SftpFs" }
-
-func (s SftpFs) Create(name string) (File, error) {
- f, err := sftpfs.FileCreate(s.SftpClient, name)
- return f, err
-}
-
-func (s SftpFs) Mkdir(name string, perm os.FileMode) error {
- err := s.SftpClient.Mkdir(name)
- if err != nil {
- return err
- }
- return s.SftpClient.Chmod(name, perm)
-}
-
-func (s SftpFs) MkdirAll(path string, perm os.FileMode) error {
- // Fast path: if we can tell whether path is a directory or file, stop with success or error.
- dir, err := s.Stat(path)
- if err == nil {
- if dir.IsDir() {
- return nil
- }
- return err
- }
-
- // Slow path: make sure parent exists and then call Mkdir for path.
- i := len(path)
- for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
- i--
- }
-
- j := i
- for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
- j--
- }
-
- if j > 1 {
- // Create parent
- err = s.MkdirAll(path[0:j-1], perm)
- if err != nil {
- return err
- }
- }
-
- // Parent now exists; invoke Mkdir and use its result.
- err = s.Mkdir(path, perm)
- if err != nil {
- // Handle arguments like "foo/." by
- // double-checking that directory doesn't exist.
- dir, err1 := s.Lstat(path)
- if err1 == nil && dir.IsDir() {
- return nil
- }
- return err
- }
- return nil
-}
-
-func (s SftpFs) Open(name string) (File, error) {
- f, err := sftpfs.FileOpen(s.SftpClient, name)
- return f, err
-}
-
-func (s SftpFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
- return nil,nil
-}
-
-func (s SftpFs) Remove(name string) error {
- return s.SftpClient.Remove(name)
-}
-
-func (s SftpFs) RemoveAll(path string) error {
- // TODO have a look at os.RemoveAll
- // https://github.com/golang/go/blob/master/src/os/path.go#L66
- return nil
-}
-
-func (s SftpFs) Rename(oldname, newname string) error {
- return s.SftpClient.Rename(oldname, newname)
-}
-
-func (s SftpFs) Stat(name string) (os.FileInfo, error) {
- return s.SftpClient.Stat(name)
-}
-
-func (s SftpFs) Lstat(p string) (os.FileInfo, error) {
- return s.SftpClient.Lstat(p)
-}
-
-func (s SftpFs) Chmod(name string, mode os.FileMode) error {
- return s.SftpClient.Chmod(name, mode)
-}
-
-func (s SftpFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
- return s.SftpClient.Chtimes(name, atime, mtime)
-}
diff --git a/sftp/file.go b/sftpfs/file.go
similarity index 96%
rename from sftp/file.go
rename to sftpfs/file.go
index bab4abc..e4ccb55 100644
--- a/sftp/file.go
+++ b/sftpfs/file.go
@@ -14,8 +14,8 @@
package sftpfs
import (
- "os"
"github.com/pkg/sftp"
+ "os"
)
type File struct {
@@ -64,17 +64,17 @@
// TODO
func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
- return 0,nil
+ return 0, nil
}
// TODO
func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
- return nil,nil
+ return nil, nil
}
// TODO
func (f *File) Readdirnames(n int) (names []string, err error) {
- return nil,nil
+ return nil, nil
}
func (f *File) Seek(offset int64, whence int) (int64, error) {
@@ -87,7 +87,7 @@
// TODO
func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
- return 0,nil
+ return 0, nil
}
func (f *File) WriteString(s string) (ret int, err error) {
diff --git a/sftpfs/sftp.go b/sftpfs/sftp.go
new file mode 100644
index 0000000..28721da
--- /dev/null
+++ b/sftpfs/sftp.go
@@ -0,0 +1,129 @@
+// Copyright © 2015 Jerry Jacobs <jerry.jacobs@xor-gate.org>.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package sftpfs
+
+import (
+ "os"
+ "time"
+
+ "github.com/pkg/sftp"
+ "github.com/spf13/afero"
+)
+
+// Fs is a afero.Fs implementation that uses functions provided by the sftp package.
+//
+// For details in any method, check the documentation of the sftp package
+// (github.com/pkg/sftp).
+type Fs struct {
+ client *sftp.Client
+}
+
+func New(client *sftp.Client) afero.Fs {
+ return &Fs{client: client}
+}
+
+func (s Fs) Name() string { return "sftpfs" }
+
+func (s Fs) Create(name string) (afero.File, error) {
+ return FileCreate(s.client, name)
+}
+
+func (s Fs) Mkdir(name string, perm os.FileMode) error {
+ err := s.client.Mkdir(name)
+ if err != nil {
+ return err
+ }
+ return s.client.Chmod(name, perm)
+}
+
+func (s Fs) MkdirAll(path string, perm os.FileMode) error {
+ // Fast path: if we can tell whether path is a directory or file, stop with success or error.
+ dir, err := s.Stat(path)
+ if err == nil {
+ if dir.IsDir() {
+ return nil
+ }
+ return err
+ }
+
+ // Slow path: make sure parent exists and then call Mkdir for path.
+ i := len(path)
+ for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
+ i--
+ }
+
+ j := i
+ for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
+ j--
+ }
+
+ if j > 1 {
+ // Create parent
+ err = s.MkdirAll(path[0:j-1], perm)
+ if err != nil {
+ return err
+ }
+ }
+
+ // Parent now exists; invoke Mkdir and use its result.
+ err = s.Mkdir(path, perm)
+ if err != nil {
+ // Handle arguments like "foo/." by
+ // double-checking that directory doesn't exist.
+ dir, err1 := s.Lstat(path)
+ if err1 == nil && dir.IsDir() {
+ return nil
+ }
+ return err
+ }
+ return nil
+}
+
+func (s Fs) Open(name string) (afero.File, error) {
+ return FileOpen(s.client, name)
+}
+
+func (s Fs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
+ return nil, nil
+}
+
+func (s Fs) Remove(name string) error {
+ return s.client.Remove(name)
+}
+
+func (s Fs) RemoveAll(path string) error {
+ // TODO have a look at os.RemoveAll
+ // https://github.com/golang/go/blob/master/src/os/path.go#L66
+ return nil
+}
+
+func (s Fs) Rename(oldname, newname string) error {
+ return s.client.Rename(oldname, newname)
+}
+
+func (s Fs) Stat(name string) (os.FileInfo, error) {
+ return s.client.Stat(name)
+}
+
+func (s Fs) Lstat(p string) (os.FileInfo, error) {
+ return s.client.Lstat(p)
+}
+
+func (s Fs) Chmod(name string, mode os.FileMode) error {
+ return s.client.Chmod(name, mode)
+}
+
+func (s Fs) Chtimes(name string, atime time.Time, mtime time.Time) error {
+ return s.client.Chtimes(name, atime, mtime)
+}
diff --git a/sftp_test_go b/sftpfs/sftp_test_go
similarity index 100%
rename from sftp_test_go
rename to sftpfs/sftp_test_go