-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from nirs/convert-package
Add parallel convert
- Loading branch information
Showing
7 changed files
with
589 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/lima-vm/go-qcow2reader" | ||
"github.com/lima-vm/go-qcow2reader/convert" | ||
"github.com/lima-vm/go-qcow2reader/log" | ||
) | ||
|
||
func cmdConvert(args []string) error { | ||
var ( | ||
// Required | ||
source, target string | ||
|
||
// Options | ||
debug bool | ||
options convert.Options | ||
) | ||
|
||
fs := flag.NewFlagSet("convert", flag.ExitOnError) | ||
fs.Usage = func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s convert [OPTIONS...] SOURCE TARGET\n", os.Args[0]) | ||
flag.PrintDefaults() | ||
} | ||
fs.BoolVar(&debug, "debug", false, "enable printing debug messages") | ||
fs.Int64Var(&options.SegmentSize, "segment-size", convert.SegmentSize, "worker segment size in bytes") | ||
fs.IntVar(&options.BufferSize, "buffer-size", convert.BufferSize, "buffer size in bytes") | ||
fs.IntVar(&options.Workers, "workers", convert.Workers, "number of workers") | ||
if err := fs.Parse(args); err != nil { | ||
return err | ||
} | ||
|
||
if debug { | ||
log.SetDebugFunc(logDebug) | ||
} | ||
|
||
switch len(fs.Args()) { | ||
case 0: | ||
return errors.New("no file was specified") | ||
case 1: | ||
return errors.New("target file is required") | ||
case 2: | ||
source = fs.Arg(0) | ||
target = fs.Arg(1) | ||
default: | ||
return errors.New("too many files were specified") | ||
} | ||
|
||
f, err := os.Open(source) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
img, err := qcow2reader.Open(f) | ||
if err != nil { | ||
return err | ||
} | ||
defer img.Close() | ||
|
||
t, err := os.Create(target) | ||
if err != nil { | ||
return err | ||
} | ||
defer t.Close() | ||
|
||
if err := t.Truncate(img.Size()); err != nil { | ||
return err | ||
} | ||
|
||
c, err := convert.New(options) | ||
if err != nil { | ||
return err | ||
} | ||
if err := c.Convert(t, img, img.Size()); err != nil { | ||
return err | ||
} | ||
|
||
if err := t.Sync(); err != nil { | ||
return err | ||
} | ||
|
||
return t.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/lima-vm/go-qcow2reader" | ||
"github.com/lima-vm/go-qcow2reader/image" | ||
) | ||
|
||
func cmdInfo(args []string) error { | ||
var ( | ||
// Required | ||
filename string | ||
|
||
// Options | ||
debug bool | ||
) | ||
|
||
fs := flag.NewFlagSet("info", flag.ExitOnError) | ||
fs.Usage = func() { | ||
fmt.Fprintf(fs.Output(), "Usage: %s info [OPTIONS...] FILE\n", os.Args[0]) | ||
fs.PrintDefaults() | ||
} | ||
fs.BoolVar(&debug, "debug", false, "enable printing debug messages") | ||
if err := fs.Parse(args); err != nil { | ||
return err | ||
} | ||
|
||
switch len(fs.Args()) { | ||
case 0: | ||
return errors.New("no file was specified") | ||
case 1: | ||
filename = fs.Arg(0) | ||
default: | ||
return errors.New("too many files specified") | ||
} | ||
|
||
f, err := os.Open(filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
img, err := qcow2reader.Open(f) | ||
if err != nil { | ||
return err | ||
} | ||
defer img.Close() | ||
|
||
imgInfo := image.NewImageInfo(img) | ||
j, err := json.MarshalIndent(imgInfo, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
if _, err = fmt.Println(string(j)); err != nil { | ||
return err | ||
} | ||
if err = img.Readable(); err != nil { | ||
logWarn(err.Error()) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/lima-vm/go-qcow2reader" | ||
"github.com/lima-vm/go-qcow2reader/log" | ||
) | ||
|
||
func cmdRead(args []string) error { | ||
var ( | ||
// Required | ||
filename string | ||
|
||
// Options | ||
debug bool | ||
bufferSize int | ||
offset int64 | ||
length int64 | ||
) | ||
|
||
fs := flag.NewFlagSet("read", flag.ExitOnError) | ||
fs.Usage = func() { | ||
fmt.Fprintf(fs.Output(), "Usage: %s read [OPTIONS...] FILE\n", os.Args[0]) | ||
flag.PrintDefaults() | ||
} | ||
fs.BoolVar(&debug, "debug", false, "enable printing debug messages") | ||
fs.IntVar(&bufferSize, "buffer-size", 65536, "buffer size") | ||
fs.Int64Var(&offset, "offset", 0, "offset to read") | ||
fs.Int64Var(&length, "length", -1, "length to read") | ||
if err := fs.Parse(args); err != nil { | ||
return err | ||
} | ||
|
||
if debug { | ||
log.SetDebugFunc(logDebug) | ||
} | ||
|
||
switch len(fs.Args()) { | ||
case 0: | ||
return errors.New("no file was specified") | ||
case 1: | ||
filename = fs.Arg(0) | ||
default: | ||
return errors.New("too many files were specified") | ||
} | ||
|
||
f, err := os.Open(filename) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
img, err := qcow2reader.Open(f) | ||
if err != nil { | ||
return err | ||
} | ||
defer img.Close() | ||
|
||
if length < 0 { | ||
length = img.Size() | ||
} | ||
|
||
buf := make([]byte, bufferSize) | ||
sr := io.NewSectionReader(img, offset, length) | ||
_, err = io.CopyBuffer(os.Stdout, sr, buf) | ||
|
||
return err | ||
} |
Oops, something went wrong.