Skip to content
Snippets Groups Projects
Commit a802e2b8 authored by Jono Wenger's avatar Jono Wenger
Browse files

Fix file transfer partStore and minor integration test fixes

parent 45bf3665
No related branches found
No related tags found
1 merge request!117Release
...@@ -18,10 +18,8 @@ import ( ...@@ -18,10 +18,8 @@ import (
"gitlab.com/elixxir/crypto/contact" "gitlab.com/elixxir/crypto/contact"
ftCrypto "gitlab.com/elixxir/crypto/fileTransfer" ftCrypto "gitlab.com/elixxir/crypto/fileTransfer"
"gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/netTime"
"gitlab.com/xx_network/primitives/utils" "gitlab.com/xx_network/primitives/utils"
"io/ioutil" "io/ioutil"
"strconv"
"time" "time"
) )
...@@ -186,23 +184,29 @@ func sendFile(filePath, fileType, filePreviewPath, filePreviewString, ...@@ -186,23 +184,29 @@ func sendFile(filePath, fileType, filePreviewPath, filePreviewString,
} }
} }
// Truncate file path if it is too long to be a file name
fileName := filePath
if len(fileName) > ft.FileNameMaxLen {
fileName = fileName[:ft.FileNameMaxLen]
}
// Get recipient contact from file // Get recipient contact from file
recipient := getContactFromFile(recipientContactPath) recipient := getContactFromFile(recipientContactPath)
jww.DEBUG.Printf("Sending file %q of size %d to recipient %s.", jww.DEBUG.Printf("Sending file %q of size %d to recipient %s.",
filePath, len(fileData), recipient.ID) fileName, len(fileData), recipient.ID)
// Create sent progress callback that prints the results // Create sent progress callback that prints the results
progressCB := func(completed bool, sent, arrived, total uint16, progressCB := func(completed bool, sent, arrived, total uint16,
t interfaces.FilePartTracker, err error) { t interfaces.FilePartTracker, err error) {
jww.DEBUG.Printf("Sent progress callback for %q "+ jww.DEBUG.Printf("Sent progress callback for %q "+
"{completed: %t, sent: %d, arrived: %d, total: %d, err: %v}\n", "{completed: %t, sent: %d, arrived: %d, total: %d, err: %v}\n",
filePath, completed, sent, arrived, total, err) fileName, completed, sent, arrived, total, err)
if (sent == 0 && arrived == 0) || (arrived == total) || completed || if (sent == 0 && arrived == 0) || (arrived == total) || completed ||
err != nil { err != nil {
fmt.Printf("Sent progress callback for %q "+ fmt.Printf("Sent progress callback for %q "+
"{completed: %t, sent: %d, arrived: %d, total: %d, err: %v}\n", "{completed: %t, sent: %d, arrived: %d, total: %d, err: %v}\n",
filePath, completed, sent, arrived, total, err) fileName, completed, sent, arrived, total, err)
} }
if completed { if completed {
...@@ -215,11 +219,11 @@ func sendFile(filePath, fileType, filePreviewPath, filePreviewString, ...@@ -215,11 +219,11 @@ func sendFile(filePath, fileType, filePreviewPath, filePreviewString,
} }
// Send the file // Send the file
_, err = m.Send(filePath, fileType, fileData, recipient.ID, retry, _, err = m.Send(fileName, fileType, fileData, recipient.ID, retry,
filePreviewData, progressCB, callbackPeriod) filePreviewData, progressCB, callbackPeriod)
if err != nil { if err != nil {
jww.FATAL.Panicf("Failed to send file %q to %s: %+v", jww.FATAL.Panicf("Failed to send file %q to %s: %+v",
filePath, recipient.ID, err) fileName, recipient.ID, err)
} }
} }
...@@ -236,9 +240,9 @@ func receiveNewFileTransfers(receive chan receivedFtResults, done, ...@@ -236,9 +240,9 @@ func receiveNewFileTransfers(receive chan receivedFtResults, done,
"message.") "message.")
return return
case r := <-receive: case r := <-receive:
jww.DEBUG.Printf("Received new file %q transfer %s from %s of size %d "+ jww.DEBUG.Printf("Received new file %q transfer %s of type %q "+
"bytes with preview: %q", "from %s of size %d bytes with preview: %q",
r.fileName, r.tid, r.sender, r.size, r.preview) r.fileName, r.tid, r.fileType, r.sender, r.size, r.preview)
fmt.Printf("Received new file transfer %q of size %d "+ fmt.Printf("Received new file transfer %q of size %d "+
"bytes with preview: %q\n", r.fileName, r.size, r.preview) "bytes with preview: %q\n", r.fileName, r.size, r.preview)
...@@ -305,16 +309,16 @@ func getContactFromFile(path string) contact.Contact { ...@@ -305,16 +309,16 @@ func getContactFromFile(path string) contact.Contact {
// init initializes commands and flags for Cobra. // init initializes commands and flags for Cobra.
func init() { func init() {
ftCmd.Flags().String("sendFile", "", ftCmd.Flags().String("sendFile", "",
"Sends a file to a recipient with with the contact at this path.") "Sends a file to a recipient with with the contact file at this path.")
bindPFlagCheckErr("sendFile") bindPFlagCheckErr("sendFile")
ftCmd.Flags().String("filePath", "testFile-"+timeNanoString()+".txt", ftCmd.Flags().String("filePath", "",
"The path to the file to send. Also used as the file name.") "The path to the file to send. Also used as the file name.")
bindPFlagCheckErr("filePath") bindPFlagCheckErr("filePath")
ftCmd.Flags().String("fileType", "txt", ftCmd.Flags().String("fileType", "txt",
"8-byte file type.") "8-byte file type.")
bindPFlagCheckErr("filePath") bindPFlagCheckErr("fileType")
ftCmd.Flags().String("filePreviewPath", "", ftCmd.Flags().String("filePreviewPath", "",
"The path to the file preview to send. Set either this flag or "+ "The path to the file preview to send. Set either this flag or "+
...@@ -336,11 +340,6 @@ func init() { ...@@ -336,11 +340,6 @@ func init() {
rootCmd.AddCommand(ftCmd) rootCmd.AddCommand(ftCmd)
} }
// timeNanoString returns the current UNIX time in nanoseconds as a string.
func timeNanoString() string {
return strconv.Itoa(int(netTime.Now().UnixNano()))
}
// bindPFlagCheckErr binds the key to a pflag.Flag used by Cobra and prints an // bindPFlagCheckErr binds the key to a pflag.Flag used by Cobra and prints an
// error if one occurs. // error if one occurs.
func bindPFlagCheckErr(key string) { func bindPFlagCheckErr(key string) {
......
...@@ -74,7 +74,8 @@ func (ps *partStore) addPart(part []byte, partNum uint16) error { ...@@ -74,7 +74,8 @@ func (ps *partStore) addPart(part []byte, partNum uint16) error {
ps.mux.Lock() ps.mux.Lock()
defer ps.mux.Unlock() defer ps.mux.Unlock()
ps.parts[partNum] = part ps.parts[partNum] = make([]byte, len(part))
copy(ps.parts[partNum], part)
err := ps.savePart(partNum) err := ps.savePart(partNum)
if err != nil { if err != nil {
...@@ -90,7 +91,10 @@ func (ps *partStore) getPart(partNum uint16) ([]byte, bool) { ...@@ -90,7 +91,10 @@ func (ps *partStore) getPart(partNum uint16) ([]byte, bool) {
defer ps.mux.Unlock() defer ps.mux.Unlock()
part, exists := ps.parts[partNum] part, exists := ps.parts[partNum]
return part, exists newPart := make([]byte, len(part))
copy(newPart, part)
return newPart, exists
} }
// getFile returns all file parts concatenated into a single file. Returns the // getFile returns all file parts concatenated into a single file. Returns the
...@@ -268,7 +272,8 @@ func partSliceToMap(parts ...[]byte) map[uint16][]byte { ...@@ -268,7 +272,8 @@ func partSliceToMap(parts ...[]byte) map[uint16][]byte {
// Add each file part to the map // Add each file part to the map
for partNum, part := range parts { for partNum, part := range parts {
partMap[uint16(partNum)] = part partMap[uint16(partNum)] = make([]byte, len(part))
copy(partMap[uint16(partNum)], part)
} }
return partMap return partMap
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment