forked from fclairamb/ftpserverlib
/
handle_dirs.go
104 lines (90 loc) · 2.37 KB
/
handle_dirs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package server
import (
"fmt"
"io"
"os"
"path"
"strings"
)
func (c *clientHandler) absPath(p string) string {
p2 := c.Path()
if strings.HasPrefix(p, "/") {
p2 = p
} else {
if p2 != "/" {
p2 += "/"
}
p2 += p
}
if ( p2 != "/" && strings.HasSuffix(p2, "/")) {
p2 = p2[0:len(p2) - 1]
}
return p2
}
func (c *clientHandler) handleCWD() {
if c.param == ".." {
c.handleCDUP()
return
}
p := c.absPath(c.param)
if err := c.driver.ChangeDirectory(c, p); err == nil {
c.SetPath(p)
c.writeMessage(250, fmt.Sprintf("CD worked on %s", p))
} else {
c.writeMessage(550, fmt.Sprintf("CD issue: %v", err))
}
}
func (c *clientHandler) handleMKD() {
p := c.absPath(c.param)
if err := c.driver.MakeDirectory(c, p); err == nil {
c.writeMessage(257, fmt.Sprintf("Created dir %s", p))
} else {
c.writeMessage(550, fmt.Sprintf("Could not create %s : %v", p, err))
}
}
func (c *clientHandler) handleRMD() {
p := c.absPath(c.param)
if err := c.driver.DeleteFile(c, p); err == nil {
c.writeMessage(250, fmt.Sprintf("Deleted dir %s", p))
} else {
c.writeMessage(550, fmt.Sprintf("Could not delete dir %s: %v", p, err))
}
}
func (c *clientHandler) handleCDUP() {
parent, _ := path.Split(c.Path())
if parent != "/" && strings.HasSuffix(parent, "/") {
parent = parent[0 : len(parent) - 1]
}
if err := c.driver.ChangeDirectory(c, parent); err == nil {
c.SetPath(parent)
c.writeMessage(250, fmt.Sprintf("CDUP worked on %s", parent))
} else {
c.writeMessage(550, fmt.Sprintf("CDUP issue: %v", err))
}
}
func (c *clientHandler) handlePWD() {
c.writeMessage(257, "\"" + c.Path() + "\" is the current directory")
}
func (c *clientHandler) handleLIST() {
if files, err := c.driver.ListFiles(c); err == nil {
if tr, err := c.TransferOpen(); err == nil {
defer c.TransferClose()
c.dirList(tr, files)
}
} else {
c.writeMessage(500, fmt.Sprintf("Could not list: %v", err))
}
}
func (c *clientHandler) dirList(w io.Writer, files []os.FileInfo) error {
for _, file := range files {
fmt.Fprint(w, file.Mode().String())
fmt.Fprintf(w, " 1 %s %s ", "ftp", "ftp")
fmt.Fprintf(w, "%12d", file.Size())
// There's no real reason to keep this strftime dependency
// fmt.Fprintf(w, strftime.Format(" %b %d %H:%M ", file.ModTime()))
fmt.Fprintf(w, file.ModTime().Format(" Jan _2 15:04 "))
fmt.Fprintf(w, "%s\r\n", file.Name())
}
fmt.Fprint(w, "\r\n")
return nil
}