Skip to content

Commit

Permalink
Merge pull request #36 from mvt-project/fix/kill-running-adb-server
Browse files Browse the repository at this point in the history
Fix reliability issues when an existing ADB process is running
  • Loading branch information
DonnchaC authored Dec 20, 2023
2 parents de4c9ec + ef64791 commit ec585b6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions acquisition/acquisition.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func (a *Acquisition) Complete() {
a.Collector.Clean()
}

// Stop ADB server before trying to remove extracted assets
adb.Client.KillServer()
assets.CleanAssets()
}

Expand Down
14 changes: 14 additions & 0 deletions adb/adb.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func New() (*ADB, error) {
}
log.Debugf("ADB found at path: %s", adb.ExePath)

log.Debug("Killing existing ADB server if running")
adb.KillServer()
return &adb, nil
}

Expand Down Expand Up @@ -129,3 +131,15 @@ func (a *ADB) ListFiles(remotePath string, recursive bool) ([]string, error) {

return remoteFiles, nil
}

func (a *ADB) KillServer() (string, error) {
log.Debug("Killing adb server")
out, err := exec.Command(a.ExePath, "kill-server").Output()
if err != nil {
log.Debug("kill-server failed")
return "", err
}

log.Debug("kill-server ok")
return strings.TrimSpace(string(out)), nil
}
15 changes: 14 additions & 1 deletion assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package assets
import (
"embed"
"os"
"errors"
"path/filepath"

saveRuntime "github.com/botherder/go-savetime/runtime"
Expand All @@ -27,7 +28,19 @@ func DeployAssets() error {

for _, asset := range getAssets() {
assetPath := filepath.Join(cwd, asset.Name)
err := os.WriteFile(assetPath, asset.Data, 0o755)

assetFile, err := os.OpenFile(assetPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o755)
if err != nil {
// Asset file already exists. Do not try overwriting
if (errors.Is(err, os.ErrExist)) {
continue
} else {
return err
}
}
defer assetFile.Close()

_, err = assetFile.Write(asset.Data)
if err != nil {
return err
}
Expand Down

0 comments on commit ec585b6

Please sign in to comment.