-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.go
92 lines (75 loc) · 1.6 KB
/
file.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
// Copyright 2022 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package go_ftp
import (
"io"
"io/fs"
"time"
"github.com/jlaffaye/ftp"
)
// File represents a fs.File object of a location on a SFTP server.
type File struct {
Filename string
Contents io.ReadCloser
// ModTime is a timestamp of when the last modification occurred
// to this file. The default will be the current UTC time.
ModTime time.Time
fileinfo fs.FileInfo
cleanup func() error
}
var _ fs.File = (&File{})
func (f *File) Close() error {
if f == nil {
return nil
}
if f.Contents != nil {
if err := f.Contents.Close(); err != nil {
return err
}
}
if f.cleanup != nil {
if err := f.cleanup(); err != nil {
return err
}
}
return nil
}
func (f *File) Stat() (fs.FileInfo, error) {
if f == nil {
return nil, io.EOF
}
return f.fileinfo, nil
}
func (f *File) Read(buf []byte) (int, error) {
if f == nil || f.Contents == nil {
return 0, io.EOF
}
return f.Contents.Read(buf)
}
// Entry implements fs.DirEntry
type Entry struct {
fd *ftp.Entry
}
var _ fs.DirEntry = (&Entry{})
func (e Entry) Name() string {
return e.fd.Name
}
func (e Entry) IsDir() bool {
return e.fd.Type == ftp.EntryTypeFolder
}
// Type only returns fs.ModeDir or fs.ModeSymlink
func (e Entry) Type() fs.FileMode {
switch e.fd.Type {
case ftp.EntryTypeFile:
// TODO(adam):
case ftp.EntryTypeFolder:
return fs.ModeDir
case ftp.EntryTypeLink:
return fs.ModeSymlink
}
return fs.ModeIrregular
}
func (e Entry) Info() (fs.FileInfo, error) {
return nil, nil
}