Debian Codegraph @

scan/ftp.go

raw
1 // Copyright (c) 2014-2019 Ludovic Fauvet
2 // Licensed under the MIT license
3
4 package scan
5
6 import (
7 "fmt"
8 "net/url"
9 "strings"
10 "time"
11
12 ftp "github.com/etix/goftp"
13 "github.com/etix/mirrorbits/core"
14 "github.com/etix/mirrorbits/utils"
15 "github.com/gomodule/redigo/redis"
16 )
17
18 const (
19 ftpConnTimeout = 5 * time.Second
20 ftpRWTimeout = 30 * time.Second
21 )
22
23 // FTPScanner is the implementation of an ftp scanner
24 type FTPScanner struct {
25 scan *scan
26
27 featMLST bool
28 featMDTM bool
29 precision core.Precision // Used for truncating time for comparison
30 }
31
32 // Scan starts an ftp scan of the given mirror
33 func (f *FTPScanner) Scan(scanurl, identifier string, conn redis.Conn, stop <-chan struct{}) (core.Precision, error) {
34 if !strings.HasPrefix(scanurl, "ftp://") {
35 return 0, fmt.Errorf("%s does not start with ftp://", scanurl)
36 }
37
38 ftpurl, err := url.Parse(scanurl)
39 if err != nil {
40 return 0, err
41 }
42
43 host := ftpurl.Host
44 if !strings.Contains(host, ":") {
45 host += ":21"
46 }
47
48 if utils.IsStopped(stop) {
49 return 0, ErrScanAborted
50 }
51
52 c, err := ftp.DialTimeout(host, ftpConnTimeout, ftpRWTimeout)
53 if err != nil {
54 return 0, err
55 }
56 defer c.Quit()
57
58 username, password := "anonymous", "anonymous"
59
60 if ftpurl.User != nil {
61 username = ftpurl.User.Username()
62 pass, hasPassword := ftpurl.User.Password()
63 if hasPassword {
64 password = pass
65 }
66 }
67
68 err = c.Login(username, password)
69 if err != nil {
70 return 0, err
71 }
72
73 _, f.featMLST = c.Feature("MLST")
74 _, f.featMDTM = c.Feature("MDTM")
75
76 if !f.featMLST || !f.featMDTM {
77 log.Warning("This server does not support some of the RFC 3659 extensions, consider using rsync instead.")
78 }
79
80 log.Infof("[%s] Requesting file list via ftp...", identifier)
81
82 files := make([]*filedata, 0, 1000)
83
84 err = c.ChangeDir(ftpurl.Path)
85 if err != nil {
86 return 0, fmt.Errorf("ftp error %s", err.Error())
87 }
88
89 _, err = c.CurrentDir()
90 if err != nil {
91 return 0, fmt.Errorf("ftp error %s", err.Error())
92 }
93
94 // Remove the trailing slash
95 prefix := strings.TrimRight(ftpurl.Path, "/")
96
97 files, err = f.walkFtp(c, files, prefix+"/", stop)
98 if err != nil {
99 return 0, fmt.Errorf("ftp error %s", err.Error())
100 }
101
102 count := 0
103 for _, fd := range files {
104 fd.path = strings.TrimPrefix(fd.path, prefix)
105 f.scan.ScannerAddFile(*fd)
106 count++
107 }
108
109 return f.precision, nil
110 }
111
112 // Walk inside an FTP repository
113 func (f *FTPScanner) walkFtp(c *ftp.ServerConn, files []*filedata, path string, stop <-chan struct{}) ([]*filedata, error) {
114 if utils.IsStopped(stop) {
115 return nil, ErrScanAborted
116 }
117
118 flist, err := c.List(path)
119 if err != nil {
120 return nil, err
121 }
122 for _, e := range flist {
123 if e.Type == ftp.EntryTypeFile {
124 newf := &filedata{}
125 newf.path = path + e.Name
126 newf.size = int64(e.Size)
127
128 if f.featMDTM {
129 t, _ := c.LastModificationDate(path + e.Name)
130 if !t.IsZero() {
131 newf.modTime = t
132
133 if f.precision != core.Precision(time.Millisecond) {
134 // We are not yet sure that we can have millisecond precision
135 if newf.modTime.Truncate(time.Second).Equal(newf.modTime) {
136 // The mod time is precise up to the second (for this file)
137 f.precision = core.Precision(time.Second)
138 } else {
139 // The mod time is precise up to the millisecond
140 f.precision = core.Precision(time.Millisecond)
141 }
142 }
143 }
144 }
145 if newf.modTime.IsZero() {
146 if f.featMLST {
147 newf.modTime = e.Time
148 if f.precision == 0 {
149 f.precision = core.Precision(time.Second)
150 }
151 } else {
152 newf.modTime = time.Time{}
153 }
154 }
155 files = append(files, newf)
156 } else if e.Type == ftp.EntryTypeFolder {
157 if e.Name == "." || e.Name == ".." {
158 continue
159 }
160 files, err = f.walkFtp(c, files, path+e.Name+"/", stop)
161 if err != nil {
162 return files, err
163 }
164 }
165 }
166 return files, err
167 }