ебать крупнейшее обновление 1.0

This commit is contained in:
huker667 committed 2026-08-11 18:58:45 +03:00
1 parent 22359129af
commit d6ebd95cbd
6 files changed
+1809 -351

No files matched your search

+114 -57
View File
@@ -1,70 +1,112 @@
package database
import (
"fmt"
"os"
// "archive/tar"
"bufio"
"context"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"sync"
"time"
// "net/url"
"io"
"slices"
"strings"
"qulay/config"
"codeberg.org/UzbekLinux/uzbekdb-go"
"sync"
"qulay/config"
"qulay/helpers"
"codeberg.org/UzbekLinux/uzbekdb-go"
"github.com/codeclysm/extract/v3"
// "github.com/klauspost/compress/zstd"
)
var _name string = filepath.Base(os.Args[0])
func GetReposUrls(destDir string) []string {
f, err := os.Open(filepath.Join(destDir, paths.ReposUrlsFile))
if err != nil {
return []string{}
}
defer f.Close()
var urls []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
urls = append(urls, scanner.Text())
}
return urls
type Repo struct {
Name string
URL string
}
func DownloadRepos(urls []string, destDir string) {
var wg sync.WaitGroup
var links []string
func GetRepos(destDir string) []Repo {
dir := filepath.Join(destDir, paths.ReposConfigDir)
for _, link := range urls {
link = strings.TrimSpace(link)
if link == "" {
entries, err := os.ReadDir(dir)
if err != nil {
return nil
}
var repos []Repo
for _, entry := range entries {
if entry.IsDir() {
continue
}
links = append(links, link)
data, err := os.ReadFile(
filepath.Join(dir, entry.Name()),
)
if err != nil {
continue
}
m := uzbekdb.Loads(string(data), "map")
if m == nil {
continue
}
mp, ok := m.(map[interface{}][]interface{})
if !ok {
continue
}
repo := Repo{Name: entry.Name()}
if vals, ok := mp["url"]; ok && len(vals) > 0 {
repo.URL = fmt.Sprintf("%v", vals[0])
}
repos = append(repos, repo)
}
wg.Add(len(links))
for _, link := range links {
go func(link string) {
slices.SortFunc(repos, func(a, b Repo) int {
return strings.Compare(a.Name, b.Name)
})
return repos
}
func RepoURL(destDir, name string) string {
for _, repo := range GetRepos(destDir) {
if repo.Name == name {
return repo.URL
}
}
return ""
}
func DownloadRepos(destDir string) {
var wg sync.WaitGroup
for _, repo := range GetRepos(destDir) {
wg.Add(1)
go func(base, name string) {
defer wg.Done()
archiveName := path.Base(link)
repoName := strings.TrimSuffix(archiveName, ".tar.zst")
destArchivePath := filepath.Join(destDir, "tmp", archiveName)
destNArchivePath := filepath.Join(destDir, "tmp")
destPath := filepath.Join(destDir, paths.ReposDir)
fullDestPath := filepath.Join(destDir, paths.ReposDir, repoName)
os.MkdirAll(destNArchivePath, 0755)
client := &http.Client{
Timeout: 10 * time.Second,
link := base
if !strings.HasSuffix(link, "/") {
link += "/"
}
link += name + ".tar.zst"
archiveName := path.Base(link)
destArchivePath := filepath.Join(destDir, "tmp", archiveName)
destPath := filepath.Join(destDir, paths.ReposDir)
fullDestPath := filepath.Join(destPath, name)
os.MkdirAll(filepath.Dir(destArchivePath), 0755)
defer os.Remove(destArchivePath)
client := helpers.HTTPClient()
resp, err := client.Get(link)
if err != nil {
@@ -72,6 +114,10 @@ func DownloadRepos(urls []string, destDir string) {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Println("!! http error", resp.StatusCode, link)
return
}
f, err := os.Create(destArchivePath)
if err != nil {
@@ -88,8 +134,8 @@ func DownloadRepos(urls []string, destDir string) {
fmt.Println(":: downloaded", archiveName, "to", destArchivePath)
err = os.RemoveAll(fullDestPath)
fmt.Println(":: removing old repo", fullDestPath)
err = os.RemoveAll(fullDestPath)
if err != nil {
fmt.Println("w! failed to delete old repo", fullDestPath, err)
}
@@ -99,13 +145,16 @@ func DownloadRepos(urls []string, destDir string) {
fmt.Println("!! failed to open archive file", destArchivePath, err)
return
}
err = extract.Zstd(context.TODO(), file, destPath, nil)
defer file.Close()
err = extract.Zstd(context.TODO(), file, fullDestPath, nil)
if err != nil {
fmt.Println("!! failed to extract", err)
return
}
}(link)
}(repo.URL, repo.Name)
}
wg.Wait()
fmt.Println(":: repo updating ended")
}
@@ -123,7 +172,7 @@ func ReadInstalled(destDir string) map[interface{}][]interface{} {
}
data := uzbekdb.Loads(string(text), "map")
if data == nil {
return map[interface{}][]interface{}{}
return map[interface{}][]interface{}{}
}
return data.(map[interface{}][]interface{})
}
@@ -150,18 +199,27 @@ func IsInstalled(name string, destDir string) bool {
func GetPackageVersion(name string, destDir string) string {
data := ReadInstalled(destDir)
if !strings.Contains(name, "/") {
for k := range data {
if kStr, ok := k.(string); ok {
if strings.HasSuffix(kStr, "/"+name) {
name = kStr
break
}
}
}
}
alo, _ := data[name]
if len(alo) > 0 {
return alo[0].(string)
} else {
return ""
return fmt.Sprintf("%v", alo[0])
}
return ""
}
func RegPackage(name string, version string, destDir string) error {
data := ReadInstalled(destDir)
val, installed := data[name]
if installed && len(val) > 0 {
currentVersion := fmt.Sprintf("%v", val[0])
if currentVersion != version {
@@ -177,7 +235,7 @@ func UnRegPackage(name string, destDir string) error {
data := ReadInstalled(destDir)
_, ok := data[name]
if !ok {
return fmt.Errorf("cant unregister '%s': package is not installed", name)
return nil
}
delete(data, name)
return WriteInstalled(data, destDir)
@@ -185,13 +243,12 @@ func UnRegPackage(name string, destDir string) error {
func PurgeRepository(name string, destDir string) error {
dirPath := filepath.Join(destDir, paths.ReposDir, name)
if _, err := os.Stat(dirPath); err != nil {
return fmt.Errorf("repository '%s' not found", name)
}
err := os.RemoveAll(dirPath)
if err != nil {
return err
}
return nil
}
// func Ensure(args, destDir) error {
//
// }