0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

close file after finished

This commit is contained in:
Zack Scholl 2019-09-07 06:35:10 -07:00
parent ee75d2c8ef
commit 5b980fea35

View file

@ -91,9 +91,10 @@ type Client struct {
spinner *spinner.Spinner spinner *spinner.Spinner
firstSend bool firstSend bool
mutex *sync.Mutex mutex *sync.Mutex
fread *mmap.ReaderAt fread *mmap.ReaderAt
quit chan bool numfinished int
quit chan bool
} }
type Chunk struct { type Chunk struct {
@ -851,6 +852,7 @@ func (c *Client) updateState() (err error) {
) )
c.fread, err = mmap.Open(pathToFile) c.fread, err = mmap.Open(pathToFile)
c.numfinished = 0
if err != nil { if err != nil {
return return
} }
@ -950,6 +952,11 @@ func (c *Client) receiveData(i int) {
func (c *Client) sendData(i int) { func (c *Client) sendData(i int) {
defer func() { defer func() {
log.Debugf("finished with %d", i) log.Debugf("finished with %d", i)
c.numfinished++
if c.numfinished == len(c.Options.RelayPorts) {
log.Debug("closing file")
c.fread.Close()
}
}() }()
// pathToFile := path.Join( // pathToFile := path.Join(
// c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderSource, // c.FilesToTransfer[c.FilesToTransferCurrentNum].FolderSource,
@ -968,19 +975,13 @@ func (c *Client) sendData(i int) {
for { for {
// Read file // Read file
data := make([]byte, models.TCP_BUFFER_SIZE/2) data := make([]byte, models.TCP_BUFFER_SIZE/2)
if readingPos >= int64(c.fread.Len()) { // if readingPos >= int64(c.fread.Len()) {
break // break
} // }
log.Debug("trying to read") log.Debugf("%d trying to read", i)
n, err := c.fread.ReadAt(data, readingPos) n, errRead := c.fread.ReadAt(data, readingPos)
log.Debugf("read %d bytes", n) log.Debugf("%d read %d bytes", i, n)
readingPos += int64(n) readingPos += int64(n)
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
if math.Mod(curi, float64(len(c.Options.RelayPorts))) == float64(i) { if math.Mod(curi, float64(len(c.Options.RelayPorts))) == float64(i) {
// check to see if this is a chunk that the recipient wants // check to see if this is a chunk that the recipient wants
@ -1024,8 +1025,15 @@ func (c *Client) sendData(i int) {
curi++ curi++
pos += uint64(n) pos += uint64(n)
if errRead != nil {
if errRead == io.EOF {
break
}
panic(errRead)
}
} }
time.Sleep(100 * time.Millisecond) // time.Sleep(10 * time.Second)
return return
} }