Skip to content

Commit

Permalink
preallocate: use custom preallocate directives where possible
Browse files Browse the repository at this point in the history
Updates #4
  • Loading branch information
igungor committed Dec 9, 2016
1 parent b083ddf commit 3ec57c1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
10 changes: 7 additions & 3 deletions sync/preallocate.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// +build !linux

package sync

import "os"

func fallocate(f *os.File, size int64) error {
func Preallocate(f *os.File, size int64) error {
err := preallocate(f, size)
if err == nil {
return nil
}

// use default truncation if platform specific allocation fails
return f.Truncate(size)
}
8 changes: 2 additions & 6 deletions sync/preallocate_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
"syscall"
)

func fallocate(f *os.File, size int64) error {
err := syscall.Fallocate(int(f.Fd()), 0, 0, size)
if err == syscall.ENOTSUP {
return f.Truncate(size)
}
return err
func preallocate(f *os.File, size int64) error {
return syscall.Fallocate(int(f.Fd()), 0, 0, size)
}
12 changes: 12 additions & 0 deletions sync/preallocate_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build !linux,!darwin

package sync

import (
"fmt"
"os"
)

func preallocate(f *os.File, size int64) error {
return fmt.Errorf("Operation not supported on this platform")
}
2 changes: 1 addition & 1 deletion sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func (c *Client) download(ctx context.Context, t *task) error {
defer f.Close()

// pre-allocate file space
_ = fallocate(f, t.state.FileLength)
_ = Preallocate(f, t.state.FileLength)

t.state.DownloadStartedAt = time.Now()
t.state.DownloadStatus = DownloadInProgress
Expand Down

0 comments on commit 3ec57c1

Please sign in to comment.