try to cleanup files

This commit is contained in:
Zack Scholl 2018-07-06 07:31:01 -07:00
parent 085550c94b
commit d906630f3a
3 changed files with 13 additions and 3 deletions

View File

@ -3,9 +3,13 @@ package croc
import ( import (
"os" "os"
"strconv" "strconv"
"time"
) )
func (c *Croc) cleanup() { func (c *Croc) cleanup() {
c.cleanupTime = true
time.Sleep(250 * time.Millisecond) // race condition, wait for
// sending/receiving to finish
// erase all the croc files and their possible numbers // erase all the croc files and their possible numbers
for i := 0; i < 16; i++ { for i := 0; i < 16; i++ {
fname := c.crocFile + "." + strconv.Itoa(i) fname := c.crocFile + "." + strconv.Itoa(i)

View File

@ -531,6 +531,9 @@ func (c *Croc) receiveFile(filename string, id int, connection net.Conn) error {
var receivedBytes int64 var receivedBytes int64
receivedFirstBytes := false receivedFirstBytes := false
for { for {
if c.cleanupTime {
break
}
if (chunkSize - receivedBytes) < bufferSize { if (chunkSize - receivedBytes) < bufferSize {
log.Debugf("%d at the end: %d < %d", id, (chunkSize - receivedBytes), bufferSize) log.Debugf("%d at the end: %d < %d", id, (chunkSize - receivedBytes), bufferSize)
io.CopyN(newFile, connection, (chunkSize - receivedBytes)) io.CopyN(newFile, connection, (chunkSize - receivedBytes))
@ -558,13 +561,13 @@ func (c *Croc) sendFile(filename string, id int, connection net.Conn) error {
// open encrypted file chunk, if it exists // open encrypted file chunk, if it exists
log.Debug("opening encrypted file chunk: " + filename) log.Debug("opening encrypted file chunk: " + filename)
defer os.Remove(filename)
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return nil return nil
} }
defer file.Close() defer file.Close()
defer os.Remove(filename)
// determine and send the file size to client // determine and send the file size to client
fi, err := file.Stat() fi, err := file.Stat()
@ -590,6 +593,9 @@ func (c *Croc) sendFile(filename string, id int, connection net.Conn) error {
sendBuffer := make([]byte, bufferSize) sendBuffer := make([]byte, bufferSize)
totalBytesSent := 0 totalBytesSent := 0
for range throttle.C { for range throttle.C {
if c.cleanupTime {
break
}
_, err := file.Read(sendBuffer) _, err := file.Read(sendBuffer)
written, _ := connection.Write(sendBuffer) written, _ := connection.Write(sendBuffer)
totalBytesSent += written totalBytesSent += written
@ -607,7 +613,5 @@ func (c *Croc) sendFile(filename string, id int, connection net.Conn) error {
} }
log.Debug("file is sent") log.Debug("file is sent")
log.Debug("removing piece") log.Debug("removing piece")
os.Remove(filename)
return err return err
} }

View File

@ -59,6 +59,8 @@ type Croc struct {
crocFileEncrypted string crocFileEncrypted string
// bothConnected // bothConnected
bothConnected bool bothConnected bool
// cleanupTime tells processes to close up
cleanupTime bool
} }
// Init will initialize the croc relay // Init will initialize the croc relay