Make PromptKit profile handling safer and more consistent
This commit is contained in:
@@ -27,18 +27,21 @@ func promptKitProfileFingerprint(profileDir, profileFile, fallbackProfileDigest
|
||||
|
||||
switch {
|
||||
case strings.TrimSpace(profileFile) != "":
|
||||
data, err := os.ReadFile(strings.TrimSpace(profileFile))
|
||||
cleanProfileFile := strings.TrimSpace(profileFile)
|
||||
data, err := os.ReadFile(cleanProfileFile)
|
||||
if err != nil {
|
||||
return CheckpointFingerprint{}, fmt.Errorf("read PromptKit profile file for checkpoint identity")
|
||||
}
|
||||
writeFingerprintPart(hasher, []byte(filepath.ToSlash(filepath.Base(cleanProfileFile))))
|
||||
writeFingerprintPart(hasher, data)
|
||||
case strings.TrimSpace(profileDir) != "":
|
||||
digests, err := promptKitProfileFileDigests(strings.TrimSpace(profileDir))
|
||||
files, err := promptKitProfileFiles(strings.TrimSpace(profileDir))
|
||||
if err != nil {
|
||||
return CheckpointFingerprint{}, err
|
||||
}
|
||||
for _, digest := range digests {
|
||||
writeFingerprintPart(hasher, digest)
|
||||
for _, file := range files {
|
||||
writeFingerprintPart(hasher, []byte(file.path))
|
||||
writeFingerprintPart(hasher, file.digest)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,8 +61,13 @@ func promptKitLocalBackendFingerprint(endpoint string) CheckpointFingerprint {
|
||||
}
|
||||
}
|
||||
|
||||
func promptKitProfileFileDigests(root string) ([][]byte, error) {
|
||||
var digests [][]byte
|
||||
type promptKitProfileFile struct {
|
||||
path string
|
||||
digest []byte
|
||||
}
|
||||
|
||||
func promptKitProfileFiles(root string) ([]promptKitProfileFile, error) {
|
||||
var files []promptKitProfileFile
|
||||
err := filepath.WalkDir(root, func(name string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
@@ -75,17 +83,24 @@ func promptKitProfileFileDigests(root string) ([][]byte, error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
relativePath, err := filepath.Rel(root, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sum := sha256.Sum256(data)
|
||||
digests = append(digests, append([]byte(nil), sum[:]...))
|
||||
files = append(files, promptKitProfileFile{
|
||||
path: filepath.ToSlash(relativePath),
|
||||
digest: append([]byte(nil), sum[:]...),
|
||||
})
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read PromptKit profile directory for checkpoint identity")
|
||||
}
|
||||
sort.Slice(digests, func(i, j int) bool {
|
||||
return string(digests[i]) < string(digests[j])
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
return files[i].path < files[j].path
|
||||
})
|
||||
return digests, nil
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func writeFingerprintPart(hasher interface{ Write([]byte) (int, error) }, value []byte) {
|
||||
|
||||
Reference in New Issue
Block a user