-
Notifications
You must be signed in to change notification settings - Fork 43
/
sftpindex.go
63 lines (53 loc) · 1.45 KB
/
sftpindex.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
package desync
import (
"net/url"
"os"
"path"
"io"
"github.com/pkg/errors"
)
// SFTPIndexStore is an index store backed by SFTP over SSH
type SFTPIndexStore struct {
*SFTPStoreBase
}
// NewSFTPIndexStore initializes and index store backed by SFTP over SSH.
func NewSFTPIndexStore(location *url.URL, opt StoreOptions) (*SFTPIndexStore, error) {
b, err := newSFTPStoreBase(location, opt)
if err != nil {
return nil, err
}
return &SFTPIndexStore{b}, nil
}
// GetIndexReader returns a reader of an index from an SFTP store. Fails if the specified
// index file does not exist.
func (s *SFTPIndexStore) GetIndexReader(name string) (r io.ReadCloser, e error) {
f, err := s.client.Open(s.pathFromName(name))
if err != nil {
if os.IsNotExist(err) {
err = errors.Errorf("Index file does not exist: %v", err)
}
return r, err
}
return f, nil
}
// GetIndex reads an index from an SFTP store, returns an error if the specified index file does not exist.
func (s *SFTPIndexStore) GetIndex(name string) (i Index, e error) {
f, err := s.GetIndexReader(name)
if err != nil {
return i, err
}
defer f.Close()
return IndexFromReader(f)
}
// StoreIndex adds a new index to the store
func (s *SFTPIndexStore) StoreIndex(name string, idx Index) error {
r, w := io.Pipe()
go func() {
defer w.Close()
idx.WriteTo(w)
}()
return s.StoreObject(s.pathFromName(name), r)
}
func (s *SFTPIndexStore) pathFromName(name string) string {
return path.Join(s.path, name)
}