Skip to content

Commit

Permalink
use it in main
Browse files Browse the repository at this point in the history
  • Loading branch information
akalin committed Mar 7, 2021
1 parent a8b407f commit 8f9a7d6
Showing 1 changed file with 11 additions and 99 deletions.
110 changes: 11 additions & 99 deletions cmd/par/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,6 @@ import (
"github.com/akalin/gopar/rsec16"
)

type par1LogEncoderDelegate struct{}

func (par1LogEncoderDelegate) OnDataFileLoad(i, n int, path string, byteCount int, err error) {
if err != nil {
fmt.Printf("[%d/%d] Loading data file %q failed: %+v\n", i, n, path, err)
} else {
fmt.Printf("[%d/%d] Loaded data file %q (%d bytes)\n", i, n, path, byteCount)
}
}

func (par1LogEncoderDelegate) OnVolumeFileWrite(i, n int, path string, dataByteCount, byteCount int, err error) {
if err != nil {
fmt.Printf("[%d/%d] Writing volume file %q failed: %+v\n", i, n, path, err)
} else {
fmt.Printf("[%d/%d] Wrote volume file %q (%d data bytes, %d bytes)\n", i, n, path, dataByteCount, byteCount)
}
}

type par1LogDecoderDelegate struct{}

func (par1LogDecoderDelegate) OnHeaderLoad(headerInfo string) {
Expand Down Expand Up @@ -82,32 +64,6 @@ func (par1LogDecoderDelegate) OnVolumeFileLoad(i uint64, path string, storedSetH
}
}

type par2LogEncoderDelegate struct{}

func (par2LogEncoderDelegate) OnDataFileLoad(i, n int, path string, byteCount int, err error) {
if err != nil {
fmt.Printf("[%d/%d] Loading data file %q failed: %+v\n", i, n, path, err)
} else {
fmt.Printf("[%d/%d] Loaded data file %q (%d bytes)\n", i, n, path, byteCount)
}
}

func (par2LogEncoderDelegate) OnIndexFileWrite(path string, byteCount int, err error) {
if err != nil {
fmt.Printf("Writing index file %q failed: %+v\n", path, err)
} else {
fmt.Printf("Wrote index file %q (%d bytes)\n", path, byteCount)
}
}

func (par2LogEncoderDelegate) OnRecoveryFileWrite(start, count, total int, path string, dataByteCount, byteCount int, err error) {
if err != nil {
fmt.Printf("[%d+%d/%d] Writing recovery file %q failed: %+v\n", start, count, total, path, err)
} else {
fmt.Printf("[%d+%d/%d] Wrote recovery file %q (%d data bytes, %d bytes)\n", start, count, total, path, dataByteCount, byteCount)
}
}

type par2LogDecoderDelegate struct{}

func (par2LogDecoderDelegate) OnCreatorPacketLoad(clientID string) {
Expand Down Expand Up @@ -297,54 +253,13 @@ func printUsageAndExit(name string, mask commandMask, err error) {
os.Exit(eSuccess)
}

type encoder interface {
LoadFileData() error
ComputeParityData() error
Write(string) error
}

type decoder interface {
LoadFileData() error
LoadParityData() error
Verify() (needsRepair bool, err error)
Repair(checkParity bool) ([]string, error)
}

func newEncoder(parFile string, filePaths []string, sliceByteCount, numParityShards, numGoroutines int) (encoder, error) {
// TODO: Detect file type more robustly.
ext := path.Ext(parFile)
if ext == ".par2" {
parPath, err := filepath.Abs(parFile)
if err != nil {
return nil, err
}
basePath := filepath.Dir(parPath)
absFilePaths := make([]string, len(filePaths))
for i, path := range filePaths {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
absFilePaths[i] = absPath
}
return par2.NewEncoder(par2LogEncoderDelegate{}, basePath, absFilePaths, sliceByteCount, numParityShards, numGoroutines)
}

parDir := filepath.Dir(parFile)
allFilesInSameDir := true
for _, p := range filePaths {
if filepath.Dir(p) != parDir {
allFilesInSameDir = false
break
}
}
if !allFilesInSameDir {
fmt.Printf("Warning: PAR and data files not all in the same directory, which a decoder will expect\n")
}

return par1.NewEncoder(par1LogEncoderDelegate{}, filePaths, numParityShards)
}

func newDecoder(parFile string, numGoroutines int) (decoder, error) {
// TODO: Detect file type more robustly.
ext := path.Ext(parFile)
Expand Down Expand Up @@ -448,22 +363,19 @@ func main() {

allFiles := createFlagSet.Args()
parFile, filePaths := allFiles[0], allFiles[1:]
encoder, err := newEncoder(parFile, filePaths, createFlags.sliceByteCount, createFlags.numParityShards, globalFlags.numGoroutines)
if err != nil {
panic(err)
}

err = encoder.LoadFileData()
if err != nil {
panic(err)
}

err = encoder.ComputeParityData()
if err != nil {
panic(err)
ext := path.Ext(parFile)
if ext == ".par2" {
err = par1.Create(parFile, filePaths, par1.CreateOptions{NumParityFiles: createFlags.numParityShards})
} else if ext == ".par" {
err = par2.Create(parFile, filePaths, par2.CreateOptions{
SliceByteCount: createFlags.sliceByteCount,
NumParityShards: createFlags.numParityShards,
NumGoroutines: globalFlags.numGoroutines,
})
} else {
err = fmt.Errorf("unknown extension %s", ext)
}

err = encoder.Write(parFile)
if err != nil {
fmt.Printf("Write parity error: %s\n", err)
os.Exit(eFileIOError)
Expand Down

0 comments on commit 8f9a7d6

Please sign in to comment.