Skip to content

Commit

Permalink
Merge pull request #155 from moov-io/sync-and-check-close
Browse files Browse the repository at this point in the history
fix: sync files after writing to ensure storage, return .Close() error
  • Loading branch information
adamdecaf authored Aug 17, 2023
2 parents ce7cb7e + 027d6e2 commit fad29d0
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,29 @@ func (c *client) UploadFile(path string, contents io.ReadCloser) error {
if err != nil {
return fmt.Errorf("sftp: problem creating remote file %s: %w", path, err)
}
defer fd.Close()

n, err := io.Copy(fd, contents)
if err != nil {
return fmt.Errorf("sftp: problem copying (n=%d) %s: %w", n, path, err)
}

if err := fd.Sync(); err != nil {
// Skip sync if the remote server doesn't support it
if !strings.Contains(err.Error(), "SSH_FX_OP_UNSUPPORTED") {
return fmt.Errorf("sftp: problem with sync on %s: %v", path, err)
}
}

if !c.cfg.SkipChmodAfterUpload {
if err = fd.Chmod(0600); err != nil {
return fmt.Errorf("sftp: problem chmod %s: %w", path, err)
}
}

if err := fd.Close(); err != nil {
return fmt.Errorf("sftp: closing %s after writing failed: %w", path, err)
}

return nil
}

Expand Down

0 comments on commit fad29d0

Please sign in to comment.