Skip to content

Commit

Permalink
Do not open a file after cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasbakken committed Jun 6, 2024
1 parent 4b2f43f commit dfeaa96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bin/prod/flash-mkfifo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ info "Overwriting block device $OUTFILE"

rm -f "$FIFO"
mkfifo "$FIFO"
xz -d < "$FIFO" > "$OUTFILE" &
xz -d < "$FIFO" > "$OUTFILE"

info "Mkfifo script done!"
30 changes: 19 additions & 11 deletions reflash/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,22 +376,23 @@ func goUploadMagic() {
}

func uploadMagicChunk(w http.ResponseWriter, r *http.Request) {
if state.State == CANCELLED {
response := map[string]bool{"success": false}
json.NewEncoder(w).Encode(response)
return
}

var chunk *Chunk = &Chunk{}
var err error
reqBody, _ := io.ReadAll(r.Body)
json.Unmarshal(reqBody, &chunk)

path := "/tmp/mypipe"
if state.File == nil {
state.File, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
if state.State == CANCELLED {
response := map[string]bool{"success": false}
json.NewEncoder(w).Encode(response)
return
} else {
path := "/tmp/mypipe"
if state.File == nil {
logInfo("Open file " + path)
state.File, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
}
}

Expand Down Expand Up @@ -477,6 +478,13 @@ func uploadFinish(w http.ResponseWriter, r *http.Request) {

func uploadCancel(w http.ResponseWriter, r *http.Request) {
state.State = CANCELLED
if state.File != nil {
logInfo("Closing file")
if err := state.File.Close(); err != nil {
log.Fatal(err)
}
state.File = nil
}
duration := time.Since(timeStart)
logInfo(fmt.Sprintf("Upload cancelled after %d minutes and %d seconds", int(duration.Minutes()), int(duration.Seconds())%60))
}
Expand Down

0 comments on commit dfeaa96

Please sign in to comment.