oopsie
This commit is contained in:
parent
b50133a06a
commit
a7cae14449
1 changed files with 128 additions and 128 deletions
256
spm/download.go
256
spm/download.go
|
@ -1,128 +1,128 @@
|
||||||
package spm
|
package spm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DownloadPackageFromAppIndex selects and downloads the correct package from the APPINDEX.
|
// DownloadPackageFromAppIndex selects and downloads the correct package from the APPINDEX.
|
||||||
func DownloadPackageFromAppIndex(appIndexPath string, packageName string, release string, pkgType string, destDir string) error {
|
func DownloadPackageFromAppIndex(appIndexPath string, packageName string, release string, pkgType string, destDir string) error {
|
||||||
// Parse the APPINDEX
|
// Parse the APPINDEX
|
||||||
entries, err := ParseAppIndex(appIndexPath)
|
entries, err := ParseAppIndex(appIndexPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to parse APPINDEX: %w", err)
|
return fmt.Errorf("failed to parse APPINDEX: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the right entry
|
// Find the right entry
|
||||||
var selected *AppIndexEntry
|
var selected *AppIndexEntry
|
||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
if e.Name == packageName &&
|
if e.Name == packageName &&
|
||||||
e.Release == release &&
|
e.Release == release &&
|
||||||
e.Type == pkgType &&
|
e.Type == pkgType &&
|
||||||
e.OS == runtime.GOOS &&
|
e.OS == runtime.GOOS &&
|
||||||
e.Arch == runtime.GOARCH {
|
e.Arch == runtime.GOARCH {
|
||||||
selected = &e
|
selected = &e
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle no matching entry
|
// Handle no matching entry
|
||||||
if selected == nil {
|
if selected == nil {
|
||||||
return fmt.Errorf("package not found in APPINDEX: %s (release: %s, type: %s, os: %s, arch: %s)", packageName, release, pkgType, runtime.GOOS, runtime.GOARCH)
|
return fmt.Errorf("package not found in APPINDEX: %s (release: %s, type: %s, os: %s, arch: %s)", packageName, release, pkgType, runtime.GOOS, runtime.GOARCH)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the package is already installed and up-to-date
|
// Check if the package is already installed and up-to-date
|
||||||
installDir, err := GetInstallDir()
|
installDir, err := GetInstallDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get install directory: %w", err)
|
return fmt.Errorf("failed to get install directory: %w", err)
|
||||||
}
|
}
|
||||||
needsUpdate, err := IsUpdateNeeded(installDir, packageName, release, selected.Version, selected.Arch, selected.OS)
|
needsUpdate, err := IsUpdateNeeded(installDir, packageName, release, selected.Version, selected.Arch, selected.OS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to check update status: %w", err)
|
return fmt.Errorf("failed to check update status: %w", err)
|
||||||
}
|
}
|
||||||
if !needsUpdate {
|
if !needsUpdate {
|
||||||
UpdateProgress(0, "Already up-to-date, skipping download.")
|
UpdateProgress(0, "Already up-to-date, skipping download.")
|
||||||
return nil // Skip download
|
return fmt.Errorf("Already up-to-date")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download the package
|
// Download the package
|
||||||
UpdateProgress(0, fmt.Sprintf("Downloading %s %s (%s)...", packageName, selected.Version, selected.Type))
|
UpdateProgress(0, fmt.Sprintf("Downloading %s %s (%s)...", packageName, selected.Version, selected.Type))
|
||||||
resp, err := http.Get(selected.DownloadURL)
|
resp, err := http.Get(selected.DownloadURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to download package: %w", err)
|
return fmt.Errorf("failed to download package: %w", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Save the downloaded file
|
// Save the downloaded file
|
||||||
downloadedFileName := filepath.Base(selected.DownloadURL)
|
downloadedFileName := filepath.Base(selected.DownloadURL)
|
||||||
downloadedFilePath := filepath.Join(destDir, downloadedFileName)
|
downloadedFilePath := filepath.Join(destDir, downloadedFileName)
|
||||||
|
|
||||||
out, err := os.OpenFile(downloadedFilePath, os.O_CREATE|os.O_WRONLY, 0644)
|
out, err := os.OpenFile(downloadedFilePath, os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create output file: %w", err)
|
return fmt.Errorf("failed to create output file: %w", err)
|
||||||
}
|
}
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
|
|
||||||
totalSize := resp.ContentLength
|
totalSize := resp.ContentLength
|
||||||
var downloaded int64
|
var downloaded int64
|
||||||
buf := make([]byte, 32*1024) // Use a larger buffer for efficiency
|
buf := make([]byte, 32*1024) // Use a larger buffer for efficiency
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, errRead := resp.Body.Read(buf)
|
n, errRead := resp.Body.Read(buf)
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
downloaded += int64(n)
|
downloaded += int64(n)
|
||||||
percentage := int(float64(downloaded) / float64(totalSize) * 100)
|
percentage := int(float64(downloaded) / float64(totalSize) * 100)
|
||||||
UpdateProgress(percentage, fmt.Sprintf("Downloading %s %s (%s)...", packageName, selected.Version, selected.Type))
|
UpdateProgress(percentage, fmt.Sprintf("Downloading %s %s (%s)...", packageName, selected.Version, selected.Type))
|
||||||
if _, errWrite := out.Write(buf[:n]); errWrite != nil {
|
if _, errWrite := out.Write(buf[:n]); errWrite != nil {
|
||||||
return fmt.Errorf("failed to write to output file: %w", errWrite)
|
return fmt.Errorf("failed to write to output file: %w", errWrite)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if errRead == io.EOF {
|
if errRead == io.EOF {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if errRead != nil {
|
if errRead != nil {
|
||||||
return fmt.Errorf("error while reading response: %w", errRead)
|
return fmt.Errorf("error while reading response: %w", errRead)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the file handle is closed before renaming
|
// Ensure the file handle is closed before renaming
|
||||||
out.Close()
|
out.Close()
|
||||||
|
|
||||||
// Construct the expected filename
|
// Construct the expected filename
|
||||||
expectedFileName := fmt.Sprintf("%s@%s@%s@%s@%s@%s.tar.gz",
|
expectedFileName := fmt.Sprintf("%s@%s@%s@%s@%s@%s.tar.gz",
|
||||||
packageName, selected.Arch, selected.OS, selected.Type, selected.Release, selected.Version)
|
packageName, selected.Arch, selected.OS, selected.Type, selected.Release, selected.Version)
|
||||||
|
|
||||||
expectedFilePath := filepath.Join(destDir, expectedFileName)
|
expectedFilePath := filepath.Join(destDir, expectedFileName)
|
||||||
|
|
||||||
// I dont know why is this happening, I dont want to know but sometimes some process is helding up the donwloaded files so thats why it retries here
|
// I dont know why is this happening, I dont want to know but sometimes some process is helding up the donwloaded files so thats why it retries here
|
||||||
maxRetries := 5
|
maxRetries := 5
|
||||||
for i := 0; i < maxRetries; i++ {
|
for i := 0; i < maxRetries; i++ {
|
||||||
err = os.Rename(downloadedFilePath, expectedFilePath)
|
err = os.Rename(downloadedFilePath, expectedFilePath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if file is in use
|
// Check if file is in use
|
||||||
f, checkErr := os.Open(downloadedFilePath)
|
f, checkErr := os.Open(downloadedFilePath)
|
||||||
if checkErr != nil {
|
if checkErr != nil {
|
||||||
return fmt.Errorf("file is locked by another process: %w", checkErr)
|
return fmt.Errorf("file is locked by another process: %w", checkErr)
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
|
|
||||||
if i < maxRetries-1 {
|
if i < maxRetries-1 {
|
||||||
time.Sleep(500 * time.Millisecond) // Wait before retrying
|
time.Sleep(500 * time.Millisecond) // Wait before retrying
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to rename downloaded file after retries: %w", err)
|
return fmt.Errorf("failed to rename downloaded file after retries: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateProgress(100, fmt.Sprintf("Downloaded %s %s (%s).", packageName, selected.Version, selected.Type))
|
UpdateProgress(100, fmt.Sprintf("Downloaded %s %s (%s).", packageName, selected.Version, selected.Type))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue