Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checksum of binary file #419

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions air_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ runner = "green"
[misc]
# Delete tmp directory on exit
clean_on_exit = true

[screen]
# Clear screen while rebuild
clear_on_rebuild = false
# Keep screen scroll while clear
keep_scroll = true
52 changes: 52 additions & 0 deletions runner/engine.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package runner

import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -359,6 +361,15 @@ func (e *Engine) start() {
close(e.binStopCh)
e.binStopCh = make(chan bool)
})

// checksum with existed bin file and run it
if err := e.sumExistAndRun(); err != nil {
e.mainDebug("file checksum failed: %s", err.Error())
} else {
e.checkerLog("find existed bin and running it")
continue
}

go e.buildRun()
}
}
Expand Down Expand Up @@ -394,6 +405,11 @@ func (e *Engine) buildRun() {
return
default:
}

if err = e.sumBin(); err != nil {
e.mainDebug("failed to get sum, error: %s", err.Error())
}

if err = e.runBin(); err != nil {
e.runnerLog("failed to run, error: %s", err.Error())
}
Expand Down Expand Up @@ -431,6 +447,42 @@ func (e *Engine) building() error {
return nil
}

func (e *Engine) sumExistAndRun() error {
binName := strings.Split(e.config.Build.Bin, " ")
checksum, err := fileChecksum(binName[len(binName)-1])
if err != nil {
return err
}
// compare file checksum
shaFile := binName[len(binName)-1] + ".sha"
shaText, err := os.ReadFile(shaFile)
if err != nil {
return err
}
if checksum != string(shaText) {
return errors.New("file checksum mismatching")
}
// run existed bin file
if err = e.runBin(); err != nil {
return err
}
return nil
}

func (e *Engine) sumBin() error {
binName := strings.Split(e.config.Build.Bin, " ")
checksum, err := fileChecksum(binName[len(binName)-1])
if err != nil {
return err
}
// save file checksum
shaFile := binName[len(binName)-1] + ".sha"
if err = os.WriteFile(shaFile, []byte(checksum), fs.ModePerm); err != nil {
return err
}
return nil
}

func (e *Engine) runBin() error {
// control killFunc should be kill or not
killCh := make(chan struct{})
Expand Down
4 changes: 4 additions & 0 deletions runner/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func (l *logger) build() logFunc {
return l.getLogger("build")
}

func (l *logger) checker() logFunc {
return l.getLogger("checker")
}

func (l *logger) runner() logFunc {
return l.getLogger("runner")
}
Expand Down
8 changes: 8 additions & 0 deletions runner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ func (e *Engine) buildLog(format string, v ...interface{}) {
}
}

func (e *Engine) checkerLog(format string, v ...interface{}) {
if e.debugMode || !e.config.Log.MainOnly {
e.logWithLock(func() {
e.logger.checker()(format, v...)
})
}
}

func (e *Engine) runnerLog(format string, v ...interface{}) {
if e.debugMode || !e.config.Log.MainOnly {
e.logWithLock(func() {
Expand Down