Skip to content

Commit

Permalink
Add checksum and move verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanchyk committed Apr 17, 2024
1 parent 562668e commit f4948c2
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 69 deletions.
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"os"

"github.com/rpanchyk/fsync/internal/checksum"
"github.com/rpanchyk/fsync/internal/service"
"github.com/rpanchyk/fsync/internal/verify"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -38,7 +38,7 @@ Attention! Use this tool on your own risk! Author is not responsible of synced f
VerboseFlag: verboseFlag,
Source: args[0],
Destination: args[1],
Verifier: &verify.MD5Verifier{},
Verifier: checksum.NewVerifier(checksum.MD5),
}
if err := syncer.Sync(); err != nil {
fmt.Println("Sync failed")
Expand Down
12 changes: 12 additions & 0 deletions internal/checksum/checksum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package checksum

type CheckSum interface {
GetCheckSum(filePath string) (string, error)
}

type CheckSumType uint8

const (
MD5 CheckSumType = iota
CRC32
)
10 changes: 10 additions & 0 deletions internal/checksum/crc32_checksum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package checksum

import "errors"

type CRC32CheckSum struct {
}

func (c CRC32CheckSum) GetCheckSum(filePath string) (string, error) {
return "", errors.New("not implemented")
}
25 changes: 25 additions & 0 deletions internal/checksum/md5_checksum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package checksum

import (
"crypto/md5"
"fmt"
"io"
"os"
)

type MD5CheckSum struct {
}

func (c MD5CheckSum) GetCheckSum(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()

hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
62 changes: 62 additions & 0 deletions internal/checksum/verifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package checksum

import (
"sync"
)

type Verifier struct {
checkSum CheckSum
}

func NewVerifier(checkSumType CheckSumType) *Verifier {
verifier := &Verifier{}
switch checkSumType {
case CRC32:
verifier.checkSum = &CRC32CheckSum{}
case MD5:
fallthrough
default:
verifier.checkSum = &MD5CheckSum{}
}
return verifier
}

func (v Verifier) Same(file1, file2 string) (bool, error) {
var wg sync.WaitGroup
outChan := make(chan string, 2)
errChan := make(chan error, 2)

wg.Add(1)
go func() {
defer wg.Done()
v.getSum(file1, outChan, errChan)
}()

wg.Add(1)
go func() {
defer wg.Done()
v.getSum(file2, outChan, errChan)
}()

go func() {
wg.Wait()
close(outChan)
close(errChan)
}()

for err := range errChan {
return false, err
}

sumA := <-outChan
sumB := <-outChan
return sumA == sumB, nil
}

func (v Verifier) getSum(filePath string, outChan chan string, errChan chan error) {
if sum, err := v.checkSum.GetCheckSum(filePath); err != nil {
errChan <- err
} else {
outChan <- sum
}
}
4 changes: 2 additions & 2 deletions internal/service/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"path/filepath"
"strings"

"github.com/rpanchyk/fsync/internal/verify"
"github.com/rpanchyk/fsync/internal/checksum"
)

type Syncer struct {
VerboseFlag bool
Source string
Destination string
Verifier verify.Verifier
Verifier *checksum.Verifier

// runtime
absoluteSourcePath string
Expand Down
60 changes: 0 additions & 60 deletions internal/verify/md5_verifier.go

This file was deleted.

5 changes: 0 additions & 5 deletions internal/verify/verifier.go

This file was deleted.

0 comments on commit f4948c2

Please sign in to comment.