changing to packets user and creating internal.db, improved bat-bin

Packet.lua
This commit is contained in:
2025-11-02 21:29:52 -03:00
parent 22fb19a550
commit 1c3e448b8d
7 changed files with 57 additions and 7 deletions

View File

@@ -89,3 +89,13 @@ func MarkAsInstalled(pkg packet.PacketLua, db *sql.DB, image *[]byte) error {
}
return nil
}
func MarkAsUninstalled(id string, db *sql.DB) error {
_, err := db.Exec("DELETE FROM installed_packages WHERE id = ?", id)
if err != nil {
return err
}
return nil
}
func PrepareDataBase(db *sql.DB) { _, _ = db.Exec(CreateInstructions) }

View File

@@ -1,12 +1,15 @@
package main
import (
"database/sql"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
_ "github.com/mattn/go-sqlite3"
"github.com/roboogg133/packets/cmd/packets/database"
"github.com/roboogg133/packets/pkg/install"
"github.com/roboogg133/packets/pkg/packet.lua.d"
"github.com/spf13/cobra"
@@ -29,6 +32,10 @@ var executeCmd = &cobra.Command{
return GetConfiguration()
},
Run: func(cmd *cobra.Command, args []string) {
if os.Geteuid() != 0 {
fmt.Println("error: this operation must be run as root")
os.Exit(1)
}
for _, v := range args {
if !strings.HasSuffix(v, ".lua") {
@@ -93,8 +100,12 @@ var executeCmd = &cobra.Command{
fmt.Printf("error: %s", err.Error())
os.Exit(1)
}
_ = ChangeToNoPermission()
pkg.ExecuteBuild(configs)
pkg.ExecuteInstall(configs)
_ = ElevatePermission()
os.Chdir(backupDir)
files, err := install.GetPackageFiles(configs.PacketDir)
@@ -108,6 +119,18 @@ var executeCmd = &cobra.Command{
os.Exit(1)
}
db, err := sql.Open("sqlite3", InternalDB)
if err != nil {
fmt.Printf("error: %s", err.Error())
os.Exit(1)
}
defer db.Close()
database.PrepareDataBase(db)
if err := database.MarkAsInstalled(pkg, db, nil); err != nil {
fmt.Printf("error: %s", err.Error())
os.Exit(1)
}
}
},
}

View File

@@ -2,6 +2,7 @@ package main
const (
ConfigurationDir = "/etc/packets"
InternalDB = ConfigurationDir + "/internal.db"
HomeDir = "/var/lib/packets"
PackageRootDir = "_pkgtest"
NumberOfTryAttempts = 4

View File

@@ -1,7 +1,6 @@
package install
import (
"fmt"
"io"
"os"
"path/filepath"
@@ -45,10 +44,8 @@ func walkAll(dirToWalk string) ([]BasicFileStatus, error) {
}
func InstallFiles(files []BasicFileStatus, packetDir string) error {
for i, v := range files {
for _, v := range files {
sysPath, _ := strings.CutPrefix(v.Filepath, packetDir)
fmt.Printf("[%d] Installing file %s\n", i, v.Filepath)
fmt.Printf("[%d] NEED to track file %s\n", i, sysPath)
if v.IsDir {
if err := os.MkdirAll(sysPath, v.PermMode.Perm()); err != nil {
return err

View File

@@ -27,7 +27,7 @@ type OperationalSystem string
type PacketLua struct {
Name string
Version string
Maintaner string
Maintainer string
Description string
Serial int
@@ -133,7 +133,7 @@ func ReadPacket(f []byte, cfg *Config) (PacketLua, error) {
packetLua := &PacketLua{
Name: getStringFromTable(pkgTable, "name"),
Version: getStringFromTable(pkgTable, "version"),
Maintaner: getStringFromTable(pkgTable, "maintainer"),
Maintainer: getStringFromTable(pkgTable, "maintainer"),
Description: getStringFromTable(pkgTable, "description"),
Serial: getIntFromTable(pkgTable, "serial"),

View File

@@ -18,7 +18,7 @@ func (pkg PacketLua) IsValid() bool {
switch {
case pkg.Serial == -133:
return false
case pkg.Description == "" || pkg.Maintaner == "" || pkg.Name == "" || pkg.Version == "":
case pkg.Description == "" || pkg.Maintainer == "" || pkg.Name == "" || pkg.Version == "":
return false
}
return true

View File

@@ -2,6 +2,7 @@ package packet
import (
"math/rand"
"strings"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$%!@%&*()-=+[]{}:;.,1234567890"
@@ -13,3 +14,21 @@ func randStringBytes(n int) string {
}
return string(b)
}
type PackageID struct {
ID string
}
func (id PackageID) Name() string {
return strings.SplitAfter(id.ID, "@")[0]
}
func (id PackageID) Version() string {
return strings.SplitAfter(id.ID, "@")[1]
}
func NewId(id string) PackageID {
var ID PackageID
ID.ID = id
return ID
}