Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.

Commit 8195b07

Browse files
committed
Don't create the version dir before downloading data to a tmp location
This was causing the same problem as #12, because the empty version dir was left around after a failure.
1 parent fb12dbc commit 8195b07

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func localSetup(localPath string, opts sequinsOptions) *sequins {
136136
}
137137

138138
backend := backend.NewLocalBackend(absPath)
139-
opts.LocalPath = absPath
139+
opts.localPath = absPath
140140

141141
return newSequins(backend, opts)
142142
}
@@ -162,13 +162,13 @@ func s3Setup(bucketName string, path string, opts sequinsOptions) *sequins {
162162

163163
bucket := s3.New(auth, region).Bucket(bucketName)
164164
backend := backend.NewS3Backend(bucket, path)
165-
if opts.LocalPath == "" {
165+
if opts.localPath == "" {
166166
tmpDir, err := ioutil.TempDir("", "sequins-")
167167
if err != nil {
168168
log.Fatal(err)
169169
}
170170

171-
opts.LocalPath = tmpDir
171+
opts.localPath = tmpDir
172172
}
173173

174174
return newSequins(backend, opts)
@@ -181,13 +181,13 @@ func hdfsSetup(namenode string, path string, opts sequinsOptions) *sequins {
181181
}
182182

183183
backend := backend.NewHdfsBackend(client, namenode, path)
184-
if opts.LocalPath == "" {
184+
if opts.localPath == "" {
185185
tmpDir, err := ioutil.TempDir("", "sequins-")
186186
if err != nil {
187187
log.Fatal(err)
188188
}
189189

190-
opts.LocalPath = tmpDir
190+
opts.localPath = tmpDir
191191
}
192192

193193
return newSequins(backend, opts)

sequins.go

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
)
2121

2222
type sequinsOptions struct {
23-
LocalPath string
24-
CheckForSuccessFile bool
23+
localPath string
24+
checkForSuccessFile bool
2525
}
2626

2727
type sequins struct {
@@ -96,7 +96,7 @@ func (s *sequins) refresh() error {
9696
s.reloadLock.Lock()
9797
defer s.reloadLock.Unlock()
9898

99-
version, err := s.backend.LatestVersion(s.options.CheckForSuccessFile)
99+
version, err := s.backend.LatestVersion(s.options.checkForSuccessFile)
100100
if err != nil {
101101
return err
102102
}
@@ -109,14 +109,9 @@ func (s *sequins) refresh() error {
109109
}
110110

111111
if version != currentVersion {
112-
path := filepath.Join(s.options.LocalPath, version)
112+
path := filepath.Join(s.options.localPath, version)
113113

114-
err := os.Mkdir(path, 0700|os.ModeDir)
115-
if err != nil && !os.IsExist(err) {
116-
return err
117-
}
118-
119-
if os.IsExist(err) {
114+
if _, err := os.Stat(path); err == nil {
120115
log.Printf("Version %s is already downloaded", version)
121116
} else {
122117
log.Printf("Downloading version %s from %s", version, s.backend.DisplayPath(version))
@@ -146,28 +141,26 @@ func (s *sequins) refresh() error {
146141
return nil
147142
}
148143

149-
func (s *sequins) download(version, destPath string) (rterr error) {
150-
// To avoid loading an incomplete download (#12), download into a temp dir
151-
// then rename the temp dir to destPath only if all downloads succeed.
152-
baseDir := path.Dir(destPath)
153-
workDir, err := ioutil.TempDir(baseDir, fmt.Sprintf("version-%v", version))
144+
func (s *sequins) download(version, destPath string) error {
145+
workDir, err := ioutil.TempDir(path.Dir(destPath), fmt.Sprintf("version-%s-tmp-", version))
154146
if err != nil {
155147
return err
156148
}
157-
defer func() {
158-
// Clean up the temp download dir in the event of a download error
159-
if err := os.RemoveAll(workDir); err != nil && !os.IsNotExist(err) {
160-
rterr = err
161-
}
162-
}()
163149

164-
if err := s.backend.Download(version, workDir); err != nil {
150+
// Clean up the temp download dir in the event of a download error
151+
defer os.RemoveAll(workDir)
152+
153+
err = s.backend.Download(version, workDir)
154+
if err != nil {
165155
return err
166156
}
167157

168-
if err := os.Rename(workDir, destPath); err != nil {
158+
err = os.Rename(workDir, destPath)
159+
if err != nil {
160+
os.RemoveAll(destPath)
169161
return err
170162
}
163+
171164
return nil
172165
}
173166

0 commit comments

Comments
 (0)