Skip to content

Commit

Permalink
Support Windows paths for local stores and explicitly close temp file…
Browse files Browse the repository at this point in the history
…d before rename (#61)
  • Loading branch information
folbricht authored Sep 25, 2018
1 parent ffa49c3 commit 7efc19a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
14 changes: 6 additions & 8 deletions cmd/desync/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"

"path"
"path/filepath"

"github.com/folbricht/desync"
"github.com/pkg/errors"
Expand Down Expand Up @@ -116,13 +117,11 @@ func storeFromLocation(location string, opts storeOptions) (desync.Store, error)
if err != nil {
return nil, err
}
case "":
s, err = desync.NewLocalStore(loc.Path)
default:
s, err = desync.NewLocalStore(location)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("Unsupported store access scheme %s", loc.Scheme)
}
return s, nil
}
Expand Down Expand Up @@ -197,17 +196,16 @@ func indexStoreFromLocation(location string, opts storeOptions) (desync.IndexSto
if err != nil {
return nil, "", err
}
case "":
default:
if location == "-" {
s, _ = desync.NewConsoleIndexStore()
} else {
s, err = desync.NewLocalIndexStore(p.String())
s, err = desync.NewLocalIndexStore(filepath.Dir(location))
if err != nil {
return nil, "", err
}
indexName = filepath.Base(location)
}
default:
return nil, "", fmt.Errorf("Unsupported store access scheme %s", loc.Scheme)
}
return s, indexName, nil
}
5 changes: 3 additions & 2 deletions local.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ func (s LocalStore) StoreChunk(id ChunkID, b []byte) error {
if err != nil {
return err
}
defer tmp.Close()
defer os.Remove(tmp.Name()) // in case we don't get to the rename, clean up
if _, err = tmp.Write(b); err != nil {
tmp.Close()
os.Remove(tmp.Name()) // clean up
return err
}
tmp.Close() // Windows can't rename open files, close explicitly
p := filepath.Join(d, sID) + chunkFileExt
return os.Rename(tmp.Name(), p)
}
Expand Down

0 comments on commit 7efc19a

Please sign in to comment.