Skip to content

Commit

Permalink
fixed RBDDiffV2 format and added a ceph-import-diff example
Browse files Browse the repository at this point in the history
  • Loading branch information
svenwiltink committed Oct 5, 2021
1 parent 4b83669 commit 614e33c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
15 changes: 12 additions & 3 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,16 @@ func (d *Decoder) isSeekableFile(writer io.Writer) (*os.File, bool) {
}

func NewEncoder(file *os.File) *Encoder {
return &Encoder{file: file, Format: format.RbdDiffv1}
return &Encoder{file: file, Format: format.RbdDiffv1, MaxSectionSize: 1 << 32}
}

// Encoder encodes a file to a stream of sparsecat data.
type Encoder struct {
file *os.File
Format format.Format
file *os.File

Format format.Format
MaxSectionSize int64

fileSize int64

currentOffset int64
Expand Down Expand Up @@ -262,6 +265,12 @@ func (e *Encoder) parseSection() error {
}

length := end - start

if length > e.MaxSectionSize {
end = start + e.MaxSectionSize
length = e.MaxSectionSize
}

e.currentSectionEnd = end

_, err = e.file.Seek(start, io.SeekStart)
Expand Down
68 changes: 68 additions & 0 deletions example/ceph-import-diff/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"encoding/binary"
"github.com/svenwiltink/sparsecat"
"github.com/svenwiltink/sparsecat/format"
"io"
"log"
"os"
)

const (
RBDImageHeader = "rbd image v2\n"
RBDImageEndTag = "E"

RBDImageDiffsV2Header = "rbd image diffs v2\n"
RBDImageDiffV2Header = "rbd diff v2\n"
)

func main() {
log.SetFlags(log.Llongfile)
if len(os.Args) != 2 {
log.Fatalln(os.Args[0], " <file to import>")
}

file, err := os.Open(os.Args[1])
if err != nil {
panic(err)
}
defer file.Close()

_, err = os.Stdout.WriteString(RBDImageHeader)
if err != nil {
panic(err)
}

_, err = os.Stdout.WriteString(RBDImageEndTag)
if err != nil {
panic(err)
}

_, err = os.Stdout.WriteString(RBDImageDiffsV2Header)
if err != nil {
panic(err)
}

var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], 1)

_, err = os.Stdout.Write(buf[:])
if err != nil {
panic(err)
}

_, err = os.Stdout.WriteString(RBDImageDiffV2Header)
if err != nil {
panic(err)
}

encoder := sparsecat.NewEncoder(file)
encoder.Format = format.RbdDiffv2
encoder.MaxSectionSize = 16_000_000

_, err = io.Copy(os.Stdout, encoder)
if err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion format/rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (r rbdDiffv2) GetSectionReader(source io.Reader, section Section) (reader i
buf := make([]byte, headerSize)
buf[0] = dataIndicator

binary.LittleEndian.PutUint64(buf[1:], 16)
binary.LittleEndian.PutUint64(buf[1:], 16+uint64(section.Length))
binary.LittleEndian.PutUint64(buf[1+8:], uint64(section.Offset))
binary.LittleEndian.PutUint64(buf[1+8+8:], uint64(section.Length))

Expand Down
10 changes: 1 addition & 9 deletions sparse_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,8 @@ func detectDataSection(file *os.File, offset int64) (start int64, end int64, err
}

func supportsSeekHole(file *os.File) bool {
var syserr syscall.Errno

_, err := unix.Seek(int(file.Fd()), 0, SEEK_DATA)
if errors.As(err, &syserr) {
if syserr == syscall.EINVAL {
return false
}
}

return true
return err == nil
}

func getBlockDeviceSize(file *os.File) (int, error) {
Expand Down

0 comments on commit 614e33c

Please sign in to comment.