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

feat: Allow to call BuildAndWriteFilesList on non-copied embedded dirs #41

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions embed_util/embedded_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io/fs"
"os"
"path/filepath"
"time"
)

type EmbeddedFiles struct {
Expand Down Expand Up @@ -129,7 +128,7 @@ func (e *EmbeddedFiles) copyEmbeddedFilesToTmp(embedFs fs.FS, fl *fileList) erro
if resolvedFle.Mode.Type() == existingSt.Mode().Type() {
if resolvedFle.Mode.IsDir() {
continue
} else if existingSt.Size() == resolvedFle.Size && existingSt.ModTime().Unix() == resolvedFle.ModTime {
} else if existingSt.Size() == resolvedFle.Size {
// unchanged
continue
}
Expand Down Expand Up @@ -177,12 +176,6 @@ func (e *EmbeddedFiles) copyEmbeddedFilesToTmp(embedFs fs.FS, fl *fileList) erro
if err != nil {
return err
}
if !resolvedFle.Mode.IsDir() {
err = os.Chtimes(path, time.Time{}, time.Unix(resolvedFle.ModTime, 0))
if err != nil {
return err
}
}
}

return nil
Expand Down
20 changes: 8 additions & 12 deletions embed_util/file_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type fileList struct {
type fileListEntry struct {
Name string `json:"name"`
Size int64 `json:"size"`
ModTime int64 `json:"modTime"`
Mode fs.FileMode `json:"perm"`
Symlink string `json:"symlink,omitempty"`
Compressed bool `json:"compressed,omitempty"`
Expand All @@ -44,15 +43,14 @@ func buildFileListFromDir(dir string) (*fileList, error) {
return err
}

if relPath == "." {
if relPath == "." || relPath == "files.json" {
return nil
}

fle := fileListEntry{
Name: relPath,
Size: info.Size(),
ModTime: info.ModTime().Unix(),
Mode: info.Mode(),
Name: relPath,
Size: info.Size(),
Mode: info.Mode(),
}

if info.Mode().Type() == fs.ModeSymlink {
Expand Down Expand Up @@ -84,7 +82,7 @@ func buildFileListFromFs(embedFs fs.FS) (*fileList, error) {
var fl fileList

err := fs.WalkDir(embedFs, ".", func(path string, d fs.DirEntry, err error) error {
if path == "." {
if path == "." || path == "files.json" {
return nil
}

Expand All @@ -94,17 +92,15 @@ func buildFileListFromFs(embedFs fs.FS) (*fileList, error) {
}

fle := fileListEntry{
Name: path,
Size: info.Size(),
ModTime: info.ModTime().Unix(),
Mode: info.Mode() | 0o600,
Name: path,
Size: info.Size(),
Mode: info.Mode() | 0o600,
}

if info.Mode().Type() == fs.ModeSymlink {
return fmt.Errorf("symlink not supported in buildFileListFromFs")
} else if info.Mode().IsDir() {
fle.Size = 0
fle.ModTime = 0
}

fl.Files = append(fl.Files, fle)
Expand Down
41 changes: 32 additions & 9 deletions embed_util/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,33 @@ func CopyForEmbed(out string, dir string) error {
return err
}

return doWriteFilesList(out, fl)
}

func BuildAndWriteFilesList(dir string) error {
fl, err := buildFileListFromDir(dir)
if err != nil {
return err
}
return doWriteFilesList(dir, fl)
}

func doWriteFilesList(dir string, fl *fileList) error {
var err error
fl.ContentHash, err = calcContentHash(dir, fl)
if err != nil {
return err
}
b, err := json.MarshalIndent(fl, "", " ")
if err != nil {
return err
}

err = os.WriteFile(filepath.Join(out, "files.json"), b, 0o644)
return err
err = os.WriteFile(filepath.Join(dir, "files.json"), b, 0o644)
if err != nil {
return err
}
return nil
}

func WriteEmbedGoFile(targetDir string, goOs string, goArch string) error {
Expand Down Expand Up @@ -128,43 +148,46 @@ func copyFiles(out string, dir string, fl *fileList) error {
return err
}

return nil
}

func calcContentHash(dir string, fl *fileList) (string, error) {
hash := sha256.New()
for _, fle := range fl.Files {
path := filepath.Join(dir, fle.Name)
st, err := os.Lstat(path)
if err != nil {
return err
return "", err
}
if st.Mode().Type() == os.ModeSymlink {
sl, err := os.Readlink(path)
if err != nil {
return err
return "", err
}
_ = binary.Write(hash, binary.LittleEndian, "symlink")
_ = binary.Write(hash, binary.LittleEndian, sl)
} else if st.Mode().IsDir() {
err = os.MkdirAll(path, fle.Mode.Perm())
if err != nil {
return err
return "", err
}
_ = binary.Write(hash, binary.LittleEndian, "dir")
_ = binary.Write(hash, binary.LittleEndian, fle.Name)
} else if st.Mode().IsRegular() {
outPath := filepath.Join(out, fle.Name)
outPath := filepath.Join(dir, fle.Name)
if fle.Compressed {
outPath += ".gz"
}

data, err := os.ReadFile(outPath)
if err != nil {
return err
return "", err
}

_ = binary.Write(hash, binary.LittleEndian, "regular")
_ = binary.Write(hash, binary.LittleEndian, fle.Name)
_ = binary.Write(hash, binary.LittleEndian, data)
}
}
fl.ContentHash = hex.EncodeToString(hash.Sum(nil))
return nil
return hex.EncodeToString(hash.Sum(nil)), nil
}
Loading