Commit 6aad4f26 authored by Amos Wenger's avatar Amos Wenger

Add Preallocate, with tests

parent f143fb13
module github.com/itchio/ox
require (
github.com/detailyang/go-fallocate v0.0.0-20180908115635-432fa640bd2e
github.com/itchio/headway v0.0.0-20190702175331-a4c65c5306de
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b // indirect
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb
github.com/stretchr/testify v1.4.0
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe
)
go 1.13
......@@ -2,8 +2,12 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/detailyang/go-fallocate v0.0.0-20180908115635-432fa640bd2e h1:lj77EKYUpYXTd8CD/+QMIf8b6OIOTsfEBSXiAzuEHTU=
github.com/detailyang/go-fallocate v0.0.0-20180908115635-432fa640bd2e/go.mod h1:3ZQK6DMPSz/QZ73jlWxBtUhNA8xZx7LzUFSq/OfP8vk=
github.com/itchio/headway v0.0.0-20190702175331-a4c65c5306de h1:RQW9xPqYtvjdHHRZR95XsaEA9B4URCuNHK78IuJcc+Y=
github.com/itchio/headway v0.0.0-20190702175331-a4c65c5306de/go.mod h1:Iif+7HeesRB0PvTYf0gOIFX8lj0za0SUsWryENQYt1E=
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b h1:PMbSa9CgaiQR9NLlUTwKi+7aeLl3GG5JX5ERJxfQ3IE=
github.com/logrusorgru/aurora v0.0.0-20190803045625-94edacc10f9b/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
......@@ -12,5 +16,12 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709 h1:Ko2LQMrRU+Oy/+EDBwX7eZ2jp3C47eDBB8EIhKTun+I=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSFqbNiQZpcgJQAgJsK6k=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe h1:6fAMxZRR6sl1Uq8U61gxU+kPTs2tR8uOySCbBP7BN/M=
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
//+build !windows
package ox
import (
"io"
"os"
"github.com/detailyang/go-fallocate"
"github.com/pkg/errors"
)
// Reserve `size` bytes of space for f, in the
// quickest way possible. f must be opened with O_RDWR.
func Preallocate(f *os.File, size int64) error {
currentSize, err := f.Seek(0, io.SeekEnd)
if err != nil {
return errors.WithStack(err)
}
remaining := size - currentSize
if remaining <= 0 {
return nil
}
err = fallocate.Fallocate(f, currentSize, remaining)
if err != nil {
return errors.WithStack(err)
}
}
package ox_test
import (
"fmt"
"io"
"io/ioutil"
"testing"
"github.com/itchio/ox"
"github.com/stretchr/testify/assert"
)
func Test_Preallocate(t *testing.T) {
assert := assert.New(t)
f, err := ioutil.TempFile("", "")
assertSize := func(expected int64) {
s, err := f.Stat()
must(err)
assert.Equal(expected, s.Size())
}
assertSize(0)
must(ox.Preallocate(f, 2048))
assertSize(2048)
_, err = f.Seek(0, io.SeekStart)
must(err)
_, err = f.Write([]byte("hello"))
must(err)
assertSize(2048)
must(ox.Preallocate(f, 4096))
assertSize(4096)
buf := make([]byte, 5)
n, err := f.ReadAt(buf, 0)
must(err)
assert.Equal(5, n)
assert.Equal("hello", string(buf))
}
func must(err error) {
if err != nil {
panic(fmt.Sprintf("%+v", err))
}
}
//+build windows
package ox
import (
"io"
"os"
"github.com/pkg/errors"
"golang.org/x/sys/windows"
)
// Reserve `size` bytes of space for f, in the
// quickest way possible. f must be opened with O_RDWR.
func Preallocate(f *os.File, size int64) error {
_, err := f.Seek(size, io.SeekStart)
if err != nil {
return errors.WithStack(err)
}
err = windows.SetEndOfFile(windows.Handle(f.Fd()))
if err != nil {
return errors.WithStack(err)
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment