Files
kopia/internal/stat/stat_bsd.go
Ali Dowair eddde91f2d chore(snapshots): unify sparse and normal FS output paths (#1981)
* Unify sparse and normal IO output

This commit refactors the code paths that excercise normal and sparse
writing of restored content. The goal is to expose sparsefile.Copy()
and iocopy.Copy() to be interchangeable, thereby allowing us to wrap
or transform their behavior more easily in the future.

* Introduce getStreamCopier()

* Pull ioCopy() into getStreamCopier()

* Fix small nit in E2E test

We should be getting the block size of the destination file, not
the source file.

* Call stat.GetBlockSize() once per FilesystemOutput

A tiny refactor to pull this call out of the generated stream copier,
as the block size should not change from one file to the next within
a restore entry.

NOTE: as a side effect, if block size could not be found (an error
is returned), we will return the default stream copier instead of
letting the sparse copier fail. A warning will be logged, but this
error will not cause the restore to fail; it will proceed silently.
2022-06-14 18:09:45 +00:00

48 lines
951 B
Go

//go:build openbsd
// +build openbsd
// Package stat provides a cross-platform abstraction for
// common stat commands.
package stat
import (
"syscall"
"github.com/pkg/errors"
)
const (
diskBlockSize uint64 = 512
)
var errInvalidBlockSize = errors.New("invalid disk block size")
// GetFileAllocSize gets the space allocated on disk for the file.
// 'fname' in bytes.
func GetFileAllocSize(fname string) (uint64, error) {
var st syscall.Stat_t
err := syscall.Stat(fname, &st)
if err != nil {
return 0, err // nolint:wrapcheck
}
return uint64(st.Blocks) * diskBlockSize, nil
}
// GetBlockSize gets the disk block size of the underlying system.
func GetBlockSize(path string) (uint64, error) {
var st syscall.Statfs_t
err := syscall.Statfs(path, &st)
if err != nil {
return 0, err // nolint:wrapcheck
}
if st.F_bsize <= 0 {
return 0, errors.Wrapf(errInvalidBlockSize, "%d", st.F_bsize)
}
return uint64(st.F_bsize), nil
}