From 6f108a42032f0ad76f93b7988f140077adcb19cb Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Sun, 21 Jan 2024 19:13:08 +0200 Subject: [PATCH] fix: do not extract embedded app on every execution (#488) * Do not extract embedded app on every execution * Add app_checksum.txt to .dockerignore * Rename embeddedAppHash to embeddedAppChecksum * Remove check for empty directory --- .dockerignore | 1 + app_checksum.txt | 0 build-static.sh | 2 ++ embed.go | 19 +++++++++---------- 4 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 app_checksum.txt diff --git a/.dockerignore b/.dockerignore index 55549ca..39f5c22 100644 --- a/.dockerignore +++ b/.dockerignore @@ -11,3 +11,4 @@ !testdata/*.txt !build-static.sh !app.tar +!app_checksum.txt diff --git a/app_checksum.txt b/app_checksum.txt new file mode 100644 index 0000000..e69de29 diff --git a/build-static.sh b/build-static.sh index b6b5dc7..d573485 100755 --- a/build-static.sh +++ b/build-static.sh @@ -122,6 +122,7 @@ cd ../.. # Embed PHP app, if any if [ -n "${EMBED}" ] && [ -d "${EMBED}" ]; then tar -cf app.tar -C "${EMBED}" . + md5 -q app.tar > app_checksum.txt fi if [ "${os}" = "linux" ]; then @@ -139,6 +140,7 @@ cd ../.. if [ -d "${EMBED}" ]; then truncate -s 0 app.tar + truncate -s 0 app_checksum.txt fi "dist/${bin}" version diff --git a/embed.go b/embed.go index 6cdd58f..8ab0cea 100644 --- a/embed.go +++ b/embed.go @@ -3,9 +3,7 @@ package frankenphp import ( "archive/tar" "bytes" - "crypto/md5" _ "embed" - "encoding/hex" "errors" "fmt" "io" @@ -25,21 +23,22 @@ var EmbeddedAppPath string //go:embed app.tar var embeddedApp []byte +//go:embed app_checksum.txt +var embeddedAppChecksum []byte + func init() { if len(embeddedApp) == 0 { // No embedded app return } - h := md5.Sum(embeddedApp) - appPath := filepath.Join(os.TempDir(), "frankenphp_"+hex.EncodeToString(h[:])) + appPath := filepath.Join(os.TempDir(), "frankenphp_"+strings.TrimSuffix(string(embeddedAppChecksum[:]), "\n")) - if err := os.RemoveAll(appPath); err != nil { - panic(err) - } - if err := untar(appPath); err != nil { - os.RemoveAll(appPath) - panic(err) + if _, err := os.Stat(appPath); os.IsNotExist(err) { + if err := untar(appPath); err != nil { + os.RemoveAll(appPath) + panic(err) + } } EmbeddedAppPath = appPath