Skip to content

Commit c7640cb

Browse files
Merge pull request #108 from fclairamb/feature/fs-driver
Disk driver
2 parents ac1ca48 + 1332e64 commit c7640cb

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

fsdrivers/disk/disk.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Package disk provides access to local files on the disk
2+
package disk
3+
4+
import (
5+
"errors"
6+
"io/ioutil"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/fclairamb/ftpserver/server"
12+
"github.com/fclairamb/ftpserver/server/log"
13+
)
14+
15+
// Driver provides an implementation of driver for disk access
16+
type Driver struct {
17+
baseDir string // Local directory used as base directory
18+
logger log.Logger // Logger
19+
}
20+
21+
// ChangeDirectory changes the current working directory
22+
func (driver *Driver) ChangeDirectory(cc server.ClientContext, directory string) error {
23+
if strings.HasPrefix(directory, "/root") {
24+
return errors.New("this doesn't look good")
25+
} else if directory == "/virtual" {
26+
return nil
27+
}
28+
29+
_, err := os.Stat(driver.baseDir + directory)
30+
31+
return err
32+
}
33+
34+
// MakeDirectory creates a directory
35+
func (driver *Driver) MakeDirectory(cc server.ClientContext, directory string) error {
36+
return os.Mkdir(driver.baseDir+directory, 0750)
37+
}
38+
39+
// ListFiles lists the files of a directory
40+
func (driver *Driver) ListFiles(cc server.ClientContext) ([]os.FileInfo, error) {
41+
return ioutil.ReadDir(driver.baseDir + cc.Path())
42+
}
43+
44+
// OpenFile opens a file in 3 possible modes: read, write, appending write (use appropriate flags)
45+
func (driver *Driver) OpenFile(cc server.ClientContext, path string, flag int) (server.FileStream, error) {
46+
path = filepath.Join(driver.baseDir, path)
47+
48+
// If we are writing and we are not in append mode, we should remove the file
49+
if (flag & os.O_WRONLY) != 0 {
50+
flag |= os.O_CREATE
51+
if (flag & os.O_APPEND) == 0 {
52+
if _, errStat := os.Stat(path); errStat == nil {
53+
if errRemove := os.Remove(path); errRemove != nil {
54+
driver.logger.Error(
55+
"msg", "Could not remove file",
56+
"path", path,
57+
"err", errRemove,
58+
)
59+
}
60+
} else if !os.IsNotExist(errStat) {
61+
driver.logger.Error("We had an error checking for a file",
62+
"path", path,
63+
"err", errStat,
64+
)
65+
}
66+
}
67+
}
68+
69+
return os.OpenFile(path, flag, 0600)
70+
}
71+
72+
// CanAllocate gives the approval to allocate some data
73+
func (driver *Driver) CanAllocate(cc server.ClientContext, size int) (bool, error) {
74+
return true, nil
75+
}
76+
77+
// GetFileInfo gets some info around a file or a directory
78+
func (driver *Driver) GetFileInfo(cc server.ClientContext, path string) (os.FileInfo, error) {
79+
path = driver.baseDir + path
80+
81+
return os.Stat(path)
82+
}
83+
84+
// DeleteFile deletes a file or a directory
85+
func (driver *Driver) DeleteFile(cc server.ClientContext, path string) error {
86+
return os.Remove(filepath.Join(driver.baseDir, path))
87+
}
88+
89+
// RenameFile renames a file or a directory
90+
func (driver *Driver) RenameFile(cc server.ClientContext, from, to string) error {
91+
return os.Rename(
92+
filepath.Join(driver.baseDir, from),
93+
filepath.Join(driver.baseDir, to),
94+
)
95+
}
96+
97+
// ChmodFile changes the attributes of the file
98+
func (driver *Driver) ChmodFile(cc server.ClientContext, path string, mode os.FileMode) error {
99+
path = driver.baseDir + path
100+
101+
return os.Chmod(path, mode)
102+
}
103+
104+
// NewDriver creates a new instance on a particular directory
105+
func NewDriver(directory string, logger log.Logger) (*Driver, error) {
106+
return &Driver{
107+
baseDir: directory,
108+
logger: logger,
109+
}, nil
110+
}
111+
112+
// NewDriverTemp creates a new instance of this on a temporary directory
113+
func NewDriverTemp(logger log.Logger) (*Driver, error) {
114+
dir := "/tmp/ftpisback"
115+
116+
if errStat := os.MkdirAll(dir, 0750); errStat != nil {
117+
logger.Info("msg", "Couldn't get our preferred dir", "dir", dir, "err", errStat)
118+
dir, errStat = ioutil.TempDir("", "ftpserver")
119+
120+
if errStat != nil {
121+
logger.Error("msg", "Could not find a temporary dir", "err", errStat)
122+
}
123+
}
124+
125+
return NewDriver(dir, logger)
126+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ require (
55
github.com/go-logfmt/logfmt v0.4.0 // indirect
66
github.com/go-stack/stack v1.8.0 // indirect
77
github.com/kylelemons/godebug v1.1.0 // indirect
8+
github.com/mattn/go-colorable v0.1.4 // indirect
9+
github.com/mattn/go-isatty v0.0.11 // indirect
810
github.com/naoina/go-stringutil v0.1.0 // indirect
911
github.com/naoina/toml v0.1.1
1012
github.com/secsy/goftp v0.0.0-20190720192957-f31499d7c79a
1113
gopkg.in/dutchcoders/goftp.v1 v1.0.0-20170301105846-ed59a591ce14
14+
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec
1215
)
1316

1417
go 1.13

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj
1010
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
1111
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
1212
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
13+
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
14+
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
15+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
16+
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
17+
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
1318
github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks=
1419
github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0=
1520
github.com/naoina/toml v0.1.1 h1:PT/lllxVVN0gzzSqSlHEmP8MJB4MY2U7STGxiouV4X8=
1621
github.com/naoina/toml v0.1.1/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E=
1722
github.com/secsy/goftp v0.0.0-20190720192957-f31499d7c79a h1:C6IhVTxNkhlb0tlCB6JfHOUv1f0xHPK7V8X4HlJZEJw=
1823
github.com/secsy/goftp v0.0.0-20190720192957-f31499d7c79a/go.mod h1:MnkX001NG75g3p8bhFycnyIjeQoOjGL6CEIsdE/nKSY=
24+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
25+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1926
gopkg.in/dutchcoders/goftp.v1 v1.0.0-20170301105846-ed59a591ce14 h1:tHqNpm9sPaE6BSuMLXBzgTwukQLdBEt4OYU2coQjEQQ=
2027
gopkg.in/dutchcoders/goftp.v1 v1.0.0-20170301105846-ed59a591ce14/go.mod h1:nzmlZQ+UqB5+55CRTV/dOaiK8OrPl6Co96Ob8lH4Wxw=
28+
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec h1:RlWgLqCMMIYYEVcAR5MDsuHlVkaIPDAF+5Dehzg8L5A=
29+
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=

0 commit comments

Comments
 (0)