2024-05-22 17:06:49 +00:00
|
|
|
package fastmirror
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-05-22 20:31:22 +00:00
|
|
|
"regexp"
|
2024-05-22 17:06:49 +00:00
|
|
|
"strings"
|
2024-05-22 20:58:36 +00:00
|
|
|
|
|
|
|
cp "github.com/otiai10/copy"
|
2024-05-22 17:06:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func findSourcesFilePath() (string, error) {
|
|
|
|
const sourcesListPath = "/etc/apt/sources.list"
|
|
|
|
const sourcesListDPath = "/etc/apt/sources.list.d/"
|
|
|
|
if isUbuntuSourcesFile(sourcesListPath) {
|
|
|
|
log.Printf("Found Ubuntu sources file: %s", sourcesListPath)
|
|
|
|
return sourcesListPath, nil
|
|
|
|
}
|
2024-05-22 20:31:22 +00:00
|
|
|
|
2024-05-22 17:06:49 +00:00
|
|
|
files, err := os.ReadDir(sourcesListDPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to read directory %s: %v", sourcesListDPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath := filepath.Join(sourcesListDPath, file.Name())
|
|
|
|
if isUbuntuSourcesFile(filePath) {
|
|
|
|
log.Printf("Found Ubuntu sources file: %s", filePath)
|
|
|
|
return filePath, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("no Ubuntu sources file found")
|
|
|
|
}
|
|
|
|
|
2024-05-22 20:31:22 +00:00
|
|
|
var ubuntuMirrorURLRegex = regexp.MustCompile(`http://((?P<countrycode>[a-z]{2})\.)?(archive|ports)\.ubuntu\.com/(?P<path>.*)`)
|
|
|
|
|
|
|
|
func findMirrorURLInFile(filePath string) (string, error) {
|
|
|
|
fileContent, err := os.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to read file %s: %v", filePath, err)
|
|
|
|
}
|
|
|
|
match := ubuntuMirrorURLRegex.FindString(string(fileContent))
|
|
|
|
if match != "" {
|
|
|
|
return match, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("no URL found in file %s", filePath)
|
|
|
|
}
|
|
|
|
|
2024-05-22 17:06:49 +00:00
|
|
|
func isUbuntuSourcesFile(filePath string) bool {
|
2024-05-22 20:31:22 +00:00
|
|
|
match, err := findMirrorURLInFile(filePath)
|
2024-05-22 17:06:49 +00:00
|
|
|
if err != nil {
|
2024-05-22 20:31:22 +00:00
|
|
|
log.Printf("Failed to find OEM mirror URL in file %s: %v", filePath, err)
|
2024-05-22 17:06:49 +00:00
|
|
|
return false
|
|
|
|
}
|
2024-05-22 20:31:22 +00:00
|
|
|
log.Printf("Found OEM mirror URL in file %s: %s", filePath, match)
|
|
|
|
return true
|
2024-05-22 17:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createBackup(filePath string) error {
|
|
|
|
backupPath := filePath + ".bak"
|
|
|
|
if _, err := os.Stat(backupPath); err == nil {
|
|
|
|
return fmt.Errorf("backup file %s already exists", backupPath)
|
|
|
|
}
|
2024-05-22 20:58:36 +00:00
|
|
|
// copy the file to the backup file
|
|
|
|
if err := cp.Copy(filePath, backupPath); err != nil {
|
|
|
|
return fmt.Errorf("failed to create backup file %s: %v", backupPath, err)
|
2024-05-22 17:06:49 +00:00
|
|
|
}
|
|
|
|
log.Printf("Created backup: %s", backupPath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractSuites(filePath string) ([]string, error) {
|
|
|
|
content, err := os.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read file %s: %v", filePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var suites []string
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(string(content)))
|
|
|
|
|
|
|
|
isModernFormat := false
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.HasPrefix(line, "Types:") {
|
|
|
|
isModernFormat = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner = bufio.NewScanner(strings.NewReader(string(content)))
|
|
|
|
if isModernFormat {
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.HasPrefix(line, "Suites:") {
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
suites = append(suites, parts[1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.HasPrefix(line, "deb ") || strings.HasPrefix(line, "deb-src ") {
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
if len(parts) >= 4 {
|
|
|
|
suites = append(suites, parts[3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to scan file %s: %v", filePath, err)
|
|
|
|
}
|
|
|
|
return suites, nil
|
|
|
|
}
|
|
|
|
|
2024-05-22 20:31:22 +00:00
|
|
|
func updateSourcesFile(filePath, oldMirrorURL, newMirrorURL string) error {
|
|
|
|
file, err := os.Open(filePath)
|
2024-05-22 17:06:49 +00:00
|
|
|
if err != nil {
|
2024-05-22 20:31:22 +00:00
|
|
|
return fmt.Errorf("failed to open sources file %s: %v", filePath, err)
|
2024-05-22 17:06:49 +00:00
|
|
|
}
|
2024-05-22 20:31:22 +00:00
|
|
|
file.Close()
|
|
|
|
contentBytes, err := os.ReadFile(filePath)
|
2024-05-22 17:06:49 +00:00
|
|
|
if err != nil {
|
2024-05-22 20:31:22 +00:00
|
|
|
return fmt.Errorf("failed to read sources file %s: %v", filePath, err)
|
2024-05-22 17:06:49 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 20:31:22 +00:00
|
|
|
log.Printf("update: replacing %s with %s in %s", oldMirrorURL, newMirrorURL, filePath)
|
|
|
|
// replace old mirror URL with new mirror URL
|
|
|
|
content := string(contentBytes)
|
|
|
|
content = strings.ReplaceAll(content, oldMirrorURL, newMirrorURL)
|
2024-05-22 17:06:49 +00:00
|
|
|
|
2024-05-22 20:31:22 +00:00
|
|
|
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
|
|
|
|
return fmt.Errorf("failed to write sources file %s: %v", filePath, err)
|
2024-05-22 17:06:49 +00:00
|
|
|
}
|
|
|
|
log.Printf("Updated sources file: %s", filePath)
|
|
|
|
return nil
|
|
|
|
}
|