Skip to content

Commit

Permalink
Bypass temp dir for incoming files
Browse files Browse the repository at this point in the history
There was an issue when renaming a temp file that was in a different
partition/filesystem. This change writes directly to the target.
  • Loading branch information
MiguelMoll committed Dec 14, 2016
1 parent 2aa8b58 commit ff616a7
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions sync/sync.go
Expand Up @@ -7,7 +7,6 @@ import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -175,7 +174,6 @@ func (s *Sync) syncIncomingAdds(adds []changes.Change, st Stream) {
switch header.Typeflag {
case tar.TypeReg:
rel, err := filepath.Rel(s.Remote, filepath.Join("/", header.Name))

if err != nil {
st <- fmt.Sprintf("error: %s", err)
return
Expand All @@ -187,41 +185,42 @@ func (s *Sync) syncIncomingAdds(adds []changes.Change, st Stream) {
s.outgoingBlocks[rel] += 1
s.lock.Unlock()

tmpfile, err := ioutil.TempFile("", filepath.Base(rel))
err = os.MkdirAll(filepath.Dir(local), 0755)
if err != nil {
st <- fmt.Sprintf("error: %s", err)
return
}

_, err = io.Copy(tmpfile, tr)
lf, err := os.Create(local)
if err != nil {
st <- fmt.Sprintf("error: %s", err)
return
}

err = tmpfile.Close()
_, err = io.Copy(lf, tr)
if err != nil {
st <- fmt.Sprintf("error: %s", err)
return
}

err = os.Chmod(tmpfile.Name(), os.FileMode(header.Mode))
err = lf.Sync()
if err != nil {
st <- fmt.Sprintf("error: %s", err)
return
}

err = os.MkdirAll(filepath.Dir(local), 0755)
err = lf.Close()
if err != nil {
st <- fmt.Sprintf("error: %s", err)
return
}

err = os.Rename(tmpfile.Name(), local)
err = os.Chmod(local, os.FileMode(header.Mode))
if err != nil {
st <- fmt.Sprintf("error: %s", err)
return
}

}
}
}()
Expand Down

0 comments on commit ff616a7

Please sign in to comment.