Skip to content

Commit

Permalink
Closing the temp file before opening it in sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
ivoras committed Feb 1, 2019
1 parent 8a4278b commit bb6fcae
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,37 +669,41 @@ func (p2pc *p2pConnection) handleBlock(msg StrIfMap) {
return
}
} else if encoding == "http" {
blockFile, err = ioutil.TempFile("", "daisy")
if err != nil {
log.Println(err)
return
}
defer func() {
err = blockFile.Close()
if err != nil {
log.Printf("handleBlock blockFile.Close: %v", err)
}
err = os.Remove(blockFile.Name())
if err != nil {
log.Printf("remove: %v", err)
}
}()
log.Println("Getting block", hash, "from", dataString)
resp, err := http.Get(dataString)
if err != nil {
log.Println("Error receiving block at", dataString, err)
return
}
defer resp.Body.Close()
blockFile, err = ioutil.TempFile("", "daisy")
if err != nil {
log.Println(err)
return
}
written, err := io.Copy(blockFile, resp.Body)
if err != nil {
log.Println("Error saving block:", err)
blockFile.Close()
os.Remove(blockFile.Name())
return
}
if written != fileSize {
log.Println("Error decoding block: sizes don't match:", written, "vs", fileSize)
blockFile.Close()
os.Remove(blockFile.Name())
return
}
err = blockFile.Close()
if err != nil {
log.Printf("handleBlock blockFile.Close: %v", err)
}
defer func() {
err = os.Remove(blockFile.Name())
if err != nil {
log.Printf("remove: %v", err)
}
}()
} else {
log.Println("Unknown block encoding:", encoding)
return
Expand Down

0 comments on commit bb6fcae

Please sign in to comment.