Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ const (
Renamed
)

// SendFlag is the options flag passed to SendSnapshot and SendSnapshotIncremental
type SendFlag int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably be int64


// Valid send options
const (
SendDefault SendFlag = 1 << iota
ReplicationStream = 1 << iota
IncrementalStream = 1 << iota
IncrementalPackage = 1 << iota
)

// DestroyFlag is the options flag passed to Destroy
type DestroyFlag int

Expand Down Expand Up @@ -224,24 +235,60 @@ func (d *Dataset) Mount(overlay bool, options []string) (*Dataset, error) {
// ReceiveSnapshot receives a ZFS stream from the input io.Reader, creates a
// new snapshot with the specified name, and streams the input data into the
// newly-created snapshot.
func ReceiveSnapshot(input io.Reader, name string) (*Dataset, error) {
// It includes the option "-F" like boolean value.
func ReceiveSnapshot(input io.Reader, name string, overwrite bool) (*Dataset, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing API is a no go, with the current scheme of things you're going to have to create a new function probably something like ReceiveSnapshotOptioned (bettername++) that takes a map[string]interface{} which we can check for overwrite existence and do the right thing.

c := command{Command: "zfs", Stdin: input}
_, err := c.Run("receive", name)
if err != nil {
return nil, err
if overwrite == false {
_, err := c.Run("receive", name)
if err != nil {
return nil, err
}
return GetDataset(name)
} else {
_, err := c.Run("receive", "-F",name)
if err != nil {
return nil, err
}
return GetDataset(name)
}
return GetDataset(name)
}

// SendSnapshot sends a ZFS stream of a snapshot to the input io.Writer.
// An error will be returned if the input dataset is not of snapshot type.
func (d *Dataset) SendSnapshot(output io.Writer) error {
// It includes the option "-R".
func (d *Dataset) SendSnapshot(output io.Writer, flags SendFlag) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto as ^. side note: man I really wish we had a more expandable api.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it's not a super improvement. Really, I have done this changes to adapt it to my necessities. I can do more progress but more later. If you want to add more functionalities, go! Because I'm rookie in golang and I take a lot of time^^

if d.Type != DatasetSnapshot {
return errors.New("can only send snapshots")
}
c := command{Command: "zfs", Stdout: output}

// Flags for SendSnapshot
if flags&IncrementalStream !=0 {
_, err := c.Run("send", "-R", d.Name)
return err
} else {
_, err := c.Run("send", d.Name)
return err
}
}

// SendSnapshotIncremental sends a ZFS stream of snapshots to the input io.Writer.
// It includes the flags "-i" and "-I".
func SendSnapshotIncremental(output io.Writer, d1 *Dataset, d2 *Dataset, flags SendFlag) error {
if d1.Type != DatasetSnapshot || d2.Type != DatasetSnapshot {
return errors.New("can only send snapshots")
}

// Flags for SendSnapshot
option := ""
if flags&IncrementalStream !=0 {
option = "-i"
}
if flags&IncrementalPackage !=0 {
option = "-I"
}
c := command{Command: "zfs", Stdout: output}
_, err := c.Run("send", d.Name)
_, err := c.Run("send", option, d1.Name, d2.Name)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion zfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func TestSendSnapshot(t *testing.T) {
ok(t, err)
defer os.Remove(file.Name())

err = s.SendSnapshot(file)
err = s.SendSnapshot(file, zfs.SendDefault)
ok(t, err)

ok(t, s.Destroy(zfs.DestroyDefault))
Expand Down