1
0
mirror of https://github.com/juanfont/headscale.git synced 2025-08-10 13:46:46 +02:00

integration/hsic: improve database extraction with better error handling

- Remove overly specific file name matching that may have been failing
- Extract the first regular file found in the database tar archive
- Add file size checking to detect empty database files
- Provide better error messages for debugging extraction issues
This commit is contained in:
Kristoffer Dalby 2025-06-19 16:07:43 +02:00
parent 3401c84c2c
commit 73a368b98c
No known key found for this signature in database

View File

@ -643,7 +643,7 @@ func (t *HeadscaleInContainer) SaveDatabase(savePath string) error {
return err
}
// For database, extract the single SQLite file directly
// For database, extract the first regular file (should be the SQLite file)
tarReader := tar.NewReader(bytes.NewReader(tarFile))
for {
header, err := tarReader.Next()
@ -654,24 +654,30 @@ func (t *HeadscaleInContainer) SaveDatabase(savePath string) error {
return fmt.Errorf("failed to read tar header: %w", err)
}
if header.Typeflag == tar.TypeReg && strings.Contains(header.Name, ".sqlite") {
// Extract the first regular file we find
if header.Typeflag == tar.TypeReg {
dbPath := path.Join(savePath, t.hostname+".db")
outFile, err := os.Create(dbPath)
if err != nil {
return fmt.Errorf("failed to create database file: %w", err)
}
if _, err := io.Copy(outFile, tarReader); err != nil {
outFile.Close()
written, err := io.Copy(outFile, tarReader)
outFile.Close()
if err != nil {
return fmt.Errorf("failed to copy database file: %w", err)
}
outFile.Close()
// Check if we actually wrote something
if written == 0 {
return fmt.Errorf("database file is empty (size: %d, header size: %d)", written, header.Size)
}
return nil
}
}
return fmt.Errorf("database file not found in tar archive")
return fmt.Errorf("no regular file found in database tar archive")
}
// Execute runs a command inside the Headscale container and returns the