Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions fsdrivers/disk/disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Package disk provides access to local files on the disk
package disk

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/fclairamb/ftpserver/server"
"github.com/fclairamb/ftpserver/server/log"
)

// Driver provides an implementation of driver for disk access
type Driver struct {
baseDir string // Local directory used as base directory
logger log.Logger // Logger
}

// ChangeDirectory changes the current working directory
func (driver *Driver) ChangeDirectory(cc server.ClientContext, directory string) error {
if strings.HasPrefix(directory, "/root") {
return errors.New("this doesn't look good")
} else if directory == "/virtual" {
return nil
}

_, err := os.Stat(driver.baseDir + directory)

return err
}

// MakeDirectory creates a directory
func (driver *Driver) MakeDirectory(cc server.ClientContext, directory string) error {
return os.Mkdir(driver.baseDir+directory, 0750)
}

// ListFiles lists the files of a directory
func (driver *Driver) ListFiles(cc server.ClientContext) ([]os.FileInfo, error) {
return ioutil.ReadDir(driver.baseDir + cc.Path())
}

// OpenFile opens a file in 3 possible modes: read, write, appending write (use appropriate flags)
func (driver *Driver) OpenFile(cc server.ClientContext, path string, flag int) (server.FileStream, error) {
path = filepath.Join(driver.baseDir, path)

// If we are writing and we are not in append mode, we should remove the file
if (flag & os.O_WRONLY) != 0 {
flag |= os.O_CREATE
if (flag & os.O_APPEND) == 0 {
if _, errStat := os.Stat(path); errStat == nil {
if errRemove := os.Remove(path); errRemove != nil {
driver.logger.Error(
"msg", "Could not remove file",
"path", path,
"err", errRemove,
)
}
} else if !os.IsNotExist(errStat) {
driver.logger.Error("We had an error checking for a file",
"path", path,
"err", errStat,
)
}
}
}

return os.OpenFile(path, flag, 0600)
}

// CanAllocate gives the approval to allocate some data
func (driver *Driver) CanAllocate(cc server.ClientContext, size int) (bool, error) {
return true, nil
}

// GetFileInfo gets some info around a file or a directory
func (driver *Driver) GetFileInfo(cc server.ClientContext, path string) (os.FileInfo, error) {
path = driver.baseDir + path

return os.Stat(path)
}

// DeleteFile deletes a file or a directory
func (driver *Driver) DeleteFile(cc server.ClientContext, path string) error {
return os.Remove(filepath.Join(driver.baseDir, path))
}

// RenameFile renames a file or a directory
func (driver *Driver) RenameFile(cc server.ClientContext, from, to string) error {
return os.Rename(
filepath.Join(driver.baseDir, from),
filepath.Join(driver.baseDir, to),
)
}

// ChmodFile changes the attributes of the file
func (driver *Driver) ChmodFile(cc server.ClientContext, path string, mode os.FileMode) error {
path = driver.baseDir + path

return os.Chmod(path, mode)
}

// NewDriver creates a new instance on a particular directory
func NewDriver(directory string, logger log.Logger) (*Driver, error) {
return &Driver{
baseDir: directory,
logger: logger,
}, nil
}

// NewDriverTemp creates a new instance of this on a temporary directory
func NewDriverTemp(logger log.Logger) (*Driver, error) {
dir := "/tmp/ftpisback"

if errStat := os.MkdirAll(dir, 0750); errStat != nil {
logger.Info("msg", "Couldn't get our preferred dir", "dir", dir, "err", errStat)
dir, errStat = ioutil.TempDir("", "ftpserver")

if errStat != nil {
logger.Error("msg", "Could not find a temporary dir", "err", errStat)
}
}

return NewDriver(dir, logger)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ require (
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.11 // indirect
github.com/naoina/go-stringutil v0.1.0 // indirect
github.com/naoina/toml v0.1.1
github.com/secsy/goftp v0.0.0-20190720192957-f31499d7c79a
gopkg.in/dutchcoders/goftp.v1 v1.0.0-20170301105846-ed59a591ce14
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec
)

go 1.13
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks=
github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0=
github.com/naoina/toml v0.1.1 h1:PT/lllxVVN0gzzSqSlHEmP8MJB4MY2U7STGxiouV4X8=
github.com/naoina/toml v0.1.1/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E=
github.com/secsy/goftp v0.0.0-20190720192957-f31499d7c79a h1:C6IhVTxNkhlb0tlCB6JfHOUv1f0xHPK7V8X4HlJZEJw=
github.com/secsy/goftp v0.0.0-20190720192957-f31499d7c79a/go.mod h1:MnkX001NG75g3p8bhFycnyIjeQoOjGL6CEIsdE/nKSY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/dutchcoders/goftp.v1 v1.0.0-20170301105846-ed59a591ce14 h1:tHqNpm9sPaE6BSuMLXBzgTwukQLdBEt4OYU2coQjEQQ=
gopkg.in/dutchcoders/goftp.v1 v1.0.0-20170301105846-ed59a591ce14/go.mod h1:nzmlZQ+UqB5+55CRTV/dOaiK8OrPl6Co96Ob8lH4Wxw=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec h1:RlWgLqCMMIYYEVcAR5MDsuHlVkaIPDAF+5Dehzg8L5A=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=