-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
113 additions
and
69 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
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,12 @@ | ||
package checksum | ||
|
||
type CheckSum interface { | ||
GetCheckSum(filePath string) (string, error) | ||
} | ||
|
||
type CheckSumType uint8 | ||
|
||
const ( | ||
MD5 CheckSumType = iota | ||
CRC32 | ||
) |
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,10 @@ | ||
package checksum | ||
|
||
import "errors" | ||
|
||
type CRC32CheckSum struct { | ||
} | ||
|
||
func (c CRC32CheckSum) GetCheckSum(filePath string) (string, error) { | ||
return "", errors.New("not implemented") | ||
} |
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,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 | ||
} |
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,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 | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.