Merge pull request #374 from schollz/v91

V91
This commit is contained in:
Zack 2021-04-20 15:55:19 -07:00 committed by GitHub
commit 35106d4dbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 12 deletions

View File

@ -198,6 +198,7 @@ func send(c *cli.Context) (err error) {
NoCompress: c.Bool("no-compress"),
Overwrite: c.Bool("overwrite"),
Curve: c.String("curve"),
HashAlgorithm: "xxhash",
}
if crocOptions.RelayAddress != models.DEFAULT_RELAY {
crocOptions.RelayAddress6 = ""
@ -395,6 +396,7 @@ func receive(c *cli.Context) (err error) {
IP: c.String("ip"),
Overwrite: c.Bool("overwrite"),
Curve: c.String("curve"),
HashAlgorithm: "xxhash",
}
if crocOptions.RelayAddress != models.DEFAULT_RELAY {
crocOptions.RelayAddress6 = ""

View File

@ -67,6 +67,7 @@ type Options struct {
IP string
Overwrite bool
Curve string
HashAlgorithm string
}
// Client holds the state of the croc transfer
@ -149,6 +150,7 @@ type SenderInfo struct {
Ask bool
SendingText bool
NoCompress bool
HashAlgorithm string
}
// New establishes a new connection for transferring files between two instances.
@ -222,8 +224,11 @@ func (c *Client) sendCollectFiles(options TransferOptions) (err error) {
}
log.Debugf("%+v", c.FilesToTransfer[i])
}
c.FilesToTransfer[i].Hash, err = utils.HashFile(fullPath)
if c.Options.HashAlgorithm == "" {
c.Options.HashAlgorithm = "xxhash"
}
c.FilesToTransfer[i].Hash, err = utils.HashFile(fullPath, c.Options.HashAlgorithm)
log.Debugf("hashed %s to %x using %s", fullPath, c.FilesToTransfer[i].Hash, c.Options.HashAlgorithm)
totalFilesSize += fstats.Size()
if err != nil {
return
@ -769,6 +774,11 @@ func (c *Client) processMessageFileInfo(m message.Message) (done bool, err error
}
c.Options.SendingText = senderInfo.SendingText
c.Options.NoCompress = senderInfo.NoCompress
c.Options.HashAlgorithm = senderInfo.HashAlgorithm
if c.Options.HashAlgorithm == "" {
c.Options.HashAlgorithm = "imohash"
}
log.Debugf("using hash algorithm: %s", c.Options.HashAlgorithm)
if c.Options.NoCompress {
log.Debug("disabling compression")
}
@ -1047,6 +1057,7 @@ func (c *Client) updateIfSenderChannelSecured() (err error) {
Ask: c.Options.Ask,
SendingText: c.Options.SendingText,
NoCompress: c.Options.NoCompress,
HashAlgorithm: c.Options.HashAlgorithm,
})
if err != nil {
log.Error(err)
@ -1232,11 +1243,17 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
if _, ok := c.FilesHasFinished[i]; ok {
continue
}
log.Debugf("checking %+v", fileInfo)
if i < c.FilesToTransferCurrentNum {
continue
}
fileHash, errHash := utils.HashFile(path.Join(fileInfo.FolderRemote, fileInfo.Name))
log.Debugf("checking %+v", fileInfo)
recipientFileInfo, errRecipientFile := os.Lstat(path.Join(fileInfo.FolderRemote, fileInfo.Name))
var errHash error
var fileHash []byte
if errRecipientFile == nil && recipientFileInfo.Size() == fileInfo.Size {
// the file exists, but is same size, so hash it
fileHash, errHash = utils.HashFile(path.Join(fileInfo.FolderRemote, fileInfo.Name), c.Options.HashAlgorithm)
}
if fileInfo.Size == 0 || fileInfo.Symlink != "" {
err = c.createEmptyFileAndFinish(fileInfo, i)
if err != nil {
@ -1248,8 +1265,9 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
}
log.Debugf("%s %+x %+x %+v", fileInfo.Name, fileHash, fileInfo.Hash, errHash)
if !bytes.Equal(fileHash, fileInfo.Hash) {
log.Debugf("hashed %s to %x using %s", fileInfo.Name, fileHash, c.Options.HashAlgorithm)
log.Debugf("hashes are not equal %x != %x", fileHash, fileInfo.Hash)
if errHash == nil && !c.Options.Overwrite {
if errHash == nil && !c.Options.Overwrite && errRecipientFile == nil {
log.Debug("asking to overwrite")
ans := utils.GetInput(fmt.Sprintf("\nOverwrite '%s'? (y/n) ", path.Join(fileInfo.FolderRemote, fileInfo.Name)))
if strings.TrimSpace(strings.ToLower(ans)) != "y" {
@ -1275,7 +1293,6 @@ func (c *Client) updateIfRecipientHasFileInfo() (err error) {
c.LastFolder = newFolder
break
}
// TODO: print out something about this file already existing
}
err = c.recipientGetFileReady(finished)
return

View File

@ -43,9 +43,8 @@ func GetInput(prompt string) string {
}
// HashFile returns the hash of a file or, in case of a symlink, the
// SHA256 hash of its target
// HashFile returns the hash of a file
func HashFile(fname string) (hash256 []byte, err error) {
// SHA256 hash of its target. Takes an argument to specify the algorithm to use.
func HashFile(fname string, algorithm string) (hash256 []byte, err error) {
var fstats os.FileInfo
fstats, err = os.Lstat(fname)
if err != nil {
@ -59,7 +58,16 @@ func HashFile(fname string) (hash256 []byte, err error) {
if err != nil {
return nil, err
}
return IMOHashFile(fname)
switch algorithm {
case "imohash":
return IMOHashFile(fname)
case "md5":
return MD5HashFile(fname)
case "xxhash":
return XXHashFile(fname)
}
err = fmt.Errorf("unspecified algorithm")
return
}
// MD5HashFile returns MD5 hash

View File

@ -175,9 +175,9 @@ func TestHashFile(t *testing.T) {
if err := tmpfile.Close(); err != nil {
panic(err)
}
hashed, err := HashFile(tmpfile.Name())
hashed, err := HashFile(tmpfile.Name(), "xxhash")
assert.Nil(t, err)
assert.Equal(t, "18c9673a4bb8325d323e7f24fda9ae1e", fmt.Sprintf("%x", hashed))
assert.Equal(t, "e66c561610ad51e2", fmt.Sprintf("%x", hashed))
}
func TestPublicIP(t *testing.T) {