reflink

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2023 License: MIT Imports: 4 Imported by: 0

README

Package reflink provides a simple library allowing to create reflinks (shallow copies) of files enabling copy-on-write behavior.

This library currently only supports creating reflinks on Linux and requires a compatible filesystem (e.g. btrfs)

A small command line utility is also provided.

Installation

Install using go get:

go get github.com/morelj/reflink

Usage

Use the Clone function to create a reflink:

import "reflink"

func main() {
	// Clone /source to /target
	err := reflink.Clone("/source", "/target", 0, 0644)
	if err != nil {
		panic(err)
	}

	// Clone /source to /target2, preserving source mode & owner if possible
	err = Clone("/source", "/target2", reflink.PreserveMode|reflink.PreserveOwner|reflink.FailSafe, 0644)
	if err != nil {
		panic(err)
	}
}

You may also only clone a subset of a file using CloneRange:

import "reflink"

func main() {
	// Clone the first 512 bytes of /source to /target
	err := reflink.CloneRange("/source", "/target", 0, 0644, reflink.Offsets{
		SrcOffset:  0,
		SrcLength:  512,
		DestOffset: 0,
	})
	if err != nil {
		panic(err)
	}
}

On unsupported operating systems, Clone and CloneRange always return reflink.ErrUnsupported.

Documentation

Overview

Package reflink provides a simple library allowing to create reflinks (shallow copies) of files enabling copy-on-write behavior.

This library currently only supports creating reflinks on Linux and requires a compatible filesystem (e.g. btrfs)

A small command line utility is also provided.

Index

Examples

Constants

View Source
const ErrUnsupported = internalError("reflinks are not supported on this platform")

ErrUnsupported indicated that the operation is not supported

Variables

This section is empty.

Functions

func Clone

func Clone(src, dst string, flags CloneFlags, mode os.FileMode) error

Clone creates a reflink (clone) of the src file to the dst file. flags can be used to control how the clone will be created. Unless the PreserveMode flag is set, the target file mode is set to mode.

Example
// Clone /source to /target
err := Clone("/source", "/target", 0, 0644)
if err != nil {
	panic(err)
}

// Clone /source to /target2, preserving source mode & owner if possible
err = Clone("/source", "/target2", PreserveMode|PreserveOwner|FailSafe, 0644)
if err != nil {
	panic(err)
}
Output:

func CloneRange

func CloneRange(src, dst string, flags CloneFlags, mode os.FileMode, ofs Offsets) error

CloneRange creates a reflink (clone) of a sub-range of the src file to the dst file. ofs define offsets of the source and target file. flags can be used to control how the clone will be created. Unless the PreserveMode flag is set, the target file mode is set to mode.

Example
// Clone the first 512 bytes of /source to /target
err := CloneRange("/source", "/target", 0, 0644, Offsets{
	SrcOffset:  0,
	SrcLength:  512,
	DestOffset: 0,
})
if err != nil {
	panic(err)
}
Output:

Types

type CloneFlags

type CloneFlags int

CloneFlags are the flags

const (
	// Preserve file permissions
	PreserveMode CloneFlags = 1

	// Preserve file owner
	PreserveOwner CloneFlags = 1 << 1

	// Preserve file times
	PreserveTimes CloneFlags = 1 << 2

	// Don't return an error if mode / owner / times cannot be set on target file
	FailSafe CloneFlags = 1 << 6

	// Delete the target file if the operation fails
	UnlinkOnFailure CloneFlags = 1 << 7
)

Available clone flags

func (CloneFlags) IsSet

func (cf CloneFlags) IsSet(f CloneFlags) bool

IsSet returns whether f is set on cf

func (CloneFlags) Set

func (cf CloneFlags) Set(f CloneFlags) CloneFlags

Set returns a copy of cf with f set

func (CloneFlags) UnSet

func (cf CloneFlags) UnSet(f CloneFlags) CloneFlags

UnSet returns a copy of cf with f unset

type Offsets

type Offsets struct {
	SrcOffset  uint64
	SrcLength  uint64
	DestOffset uint64
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL