Luncher/spm/install.go

205 lines
4.6 KiB
Go
Raw Normal View History

2025-02-03 15:52:19 +01:00
package spm
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
)
func DecompressToTemp(filePath string) (string, error) {
UpdateProgress(0, "Decompressing package")
// 1) Base temp dir
baseTempDir := GetTempDir()
// 2) Create a unique subfolder inside the base temp dir
subfolderName := fmt.Sprintf("spm_decompress_%d", rand.Intn(1000000))
decompressDir := filepath.Join(baseTempDir, subfolderName)
if err := os.MkdirAll(decompressDir, 0755); err != nil {
return "", fmt.Errorf("failed to create decompress dir: %w", err)
}
// 3) Open the tar.gz file
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
gzr, err := gzip.NewReader(file)
if err != nil {
return "", err
}
defer gzr.Close()
tarReader := tar.NewReader(gzr)
// 4) Count total files
var totalFiles, processedFiles int
for {
_, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return "", err
}
totalFiles++
}
// 5) Reset file position and tar reader
if _, err := file.Seek(0, io.SeekStart); err != nil {
return "", err
}
if err := gzr.Reset(file); err != nil {
return "", err
}
tarReader = tar.NewReader(gzr)
// 6) Extract into `decompressDir`
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return "", err
}
targetPath := filepath.Join(decompressDir, header.Name)
switch header.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(targetPath, os.FileMode(header.Mode)); err != nil {
return "", err
}
case tar.TypeReg:
outFile, err := os.Create(targetPath)
if err != nil {
return "", err
}
if _, err := io.Copy(outFile, tarReader); err != nil {
outFile.Close()
return "", err
}
outFile.Close()
}
processedFiles++
UpdateProgress(int(float64(processedFiles)/float64(totalFiles)*100), "Decompressing package")
}
UpdateProgress(100, "Package decompressed")
return decompressDir, nil
}
func MoveFilesToInstallDir(tempDir, installDir, pkgType string) error {
// Ensure tempDir exists before processing
if _, err := os.Stat(tempDir); os.IsNotExist(err) {
return fmt.Errorf("tempDir does not exist: %s", tempDir)
}
// If the package type is "browser", set the subdirectory to "browser"
if pkgType == "browser" {
installDir = filepath.Join(installDir, "browser")
}
// Count total files to copy
var totalFiles, copiedFiles int
err := filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
totalFiles++
}
return nil
})
if err != nil {
return err
}
// Copy files and track progress
err = filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(tempDir, path)
if err != nil {
return err
}
targetPath := filepath.Join(installDir, relPath)
if info.IsDir() {
// Create directories in the install directory
if err := os.MkdirAll(targetPath, os.ModePerm); err != nil {
return err
}
} else {
// Copy files to the install directory
if err := copyFile(path, targetPath); err != nil {
return err
}
copiedFiles++
UpdateProgress(int(float64(copiedFiles)/float64(totalFiles)*100), "Copying files to install directory")
}
return nil
})
if err != nil {
return err
}
// Clean up temporary directory
UpdateProgress(100, "Cleaning up temporary files")
return os.RemoveAll(tempDir)
}
// copyFile copies the contents of the source file to the destination file.
func copyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
// Create the destination file
destinationFile, err := os.Create(dst)
if err != nil {
return err
}
defer destinationFile.Close()
// Copy the file content
if _, err := io.Copy(destinationFile, sourceFile); err != nil {
return err
}
// Preserve file permissions
info, err := sourceFile.Stat()
if err != nil {
return err
}
return os.Chmod(dst, info.Mode())
}
// FinalizeInstall finalizes the installation by updating installed.ini.
func FinalizeInstall(packageName, release, version, arch, osName string) error {
installDir, err := GetDefaultInstallDir()
if err != nil {
return err
}
pkgInfo := AppIndexEntry{
Name: packageName,
Version: version,
Release: release,
Arch: arch,
OS: osName,
}
return UpdateInstalledPackage(installDir, pkgInfo)
}