From 1c3e448b8d6003a77350f54d0d2ddcf2f5d1f7d3 Mon Sep 17 00:00:00 2001 From: robogg133 Date: Sun, 2 Nov 2025 21:29:52 -0300 Subject: [PATCH] changing to packets user and creating internal.db, improved bat-bin Packet.lua --- cmd/packets/database/database.go | 10 ++++++++++ cmd/packets/main.go | 23 +++++++++++++++++++++++ cmd/packets/specifications.go | 1 + pkg/install/main.go | 5 +---- pkg/packet.lua.d/main.go | 4 ++-- pkg/packet.lua.d/properties.go | 2 +- pkg/packet.lua.d/utils.go | 19 +++++++++++++++++++ 7 files changed, 57 insertions(+), 7 deletions(-) diff --git a/cmd/packets/database/database.go b/cmd/packets/database/database.go index 3c571b6..a9d6127 100644 --- a/cmd/packets/database/database.go +++ b/cmd/packets/database/database.go @@ -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) } diff --git a/cmd/packets/main.go b/cmd/packets/main.go index a458991..31fa516 100644 --- a/cmd/packets/main.go +++ b/cmd/packets/main.go @@ -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) + } } }, } diff --git a/cmd/packets/specifications.go b/cmd/packets/specifications.go index 1876f94..98e9508 100644 --- a/cmd/packets/specifications.go +++ b/cmd/packets/specifications.go @@ -2,6 +2,7 @@ package main const ( ConfigurationDir = "/etc/packets" + InternalDB = ConfigurationDir + "/internal.db" HomeDir = "/var/lib/packets" PackageRootDir = "_pkgtest" NumberOfTryAttempts = 4 diff --git a/pkg/install/main.go b/pkg/install/main.go index 0933d9a..c045e79 100644 --- a/pkg/install/main.go +++ b/pkg/install/main.go @@ -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 diff --git a/pkg/packet.lua.d/main.go b/pkg/packet.lua.d/main.go index 2620984..14009c2 100644 --- a/pkg/packet.lua.d/main.go +++ b/pkg/packet.lua.d/main.go @@ -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"), diff --git a/pkg/packet.lua.d/properties.go b/pkg/packet.lua.d/properties.go index f5756f0..5150a92 100644 --- a/pkg/packet.lua.d/properties.go +++ b/pkg/packet.lua.d/properties.go @@ -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 diff --git a/pkg/packet.lua.d/utils.go b/pkg/packet.lua.d/utils.go index 03504bb..ac80b5b 100644 --- a/pkg/packet.lua.d/utils.go +++ b/pkg/packet.lua.d/utils.go @@ -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 +}