commit 4459080

chld  ·  2026-02-07 08:17:54 +0000 UTC
parent ac25659
pjush
3 files changed,  +27, -11
+12, -8
 1@@ -40,20 +40,24 @@ func help() {
 2 	fmt.Println()
 3 }
 4 
 5+func cr(a string, b string) {
 6+	if x := os.Getenv(a); x == "" {
 7+		os.Setenv(a, b)
 8+	}
 9+}
10+
11 func main() {
12 	if len(os.Args) < 2 {
13 		help()
14 		return
15 	}
16 
17-	if x := os.Getenv("DTR_DIR"); x == "" {
18-		os.Setenv("DTR_DIR", "/ports")
19-	}
20-	if x := os.Getenv("DTR_ROOT"); x == "" {
21-		os.Setenv("DTR_ROOT", "/")
22-	}
23-	if x := os.Getenv("DTR_DB"); x == "" {
24-		os.Setenv("DTR_DB", "/var/db/dtr")
25+	// set unset vars
26+	cr("DTR_DIR", "/ports")
27+	cr("DTR_ROOT", "/")
28+	cr("DTR_DB", "/var/db/dtr")
29+	if _, err := os.Stat(filepath.Join(os.Getenv("DTR_DB"), "installed")); err != nil {
30+		os.Create(filepath.Join(os.Getenv("DTR_DB"), "installed"))
31 	}
32 
33 	act := os.Args[1]
+8, -2
 1@@ -31,8 +31,14 @@ func DepRes(pkg string) []string {
 2 
 3 	nf := append(bt, append(lt, rt...)...)
 4 
 5-	installed, _ := os.ReadFile(filepath.Join(os.Getenv("DTR_DB"), "installed"))
 6-	inst := string(installed)
 7+	installedPath := filepath.Join(os.Getenv("DTR_DB"), "installed")
 8+	installed, _ := os.ReadFile(installedPath)
 9+
10+	inst := string(installed) + "new content" // whatever you want to append
11+
12+	in, _ := os.OpenFile(installedPath, os.O_WRONLY|os.O_TRUNC, 0644) // truncate if needed
13+	defer in.Close()
14+	in.WriteString(inst)
15 
16 	var out []string
17 	for _, dep := range nf {
+7, -1
 1@@ -23,7 +23,13 @@ func Inst(pkg string) {
 2 		dgr.Die("failed to copy package files to root", os.Getenv("DTR_ROOT"), 1)
 3 	} else {
 4 		// write the installed package to a installed file for tracking
 5-		f, _ := os.Create(filepath.Join(os.Getenv("DTR_DB"), "installed"))
 6+
 7+		f, err := os.OpenFile(
 8+			filepath.Join(os.Getenv("DTR_DB"), "installed"),
 9+			os.O_WRONLY|os.O_APPEND, 0644) // make sure to append
10+		if err != nil {
11+			dgr.Die("could not open file", filepath.Join(os.Getenv("DTR_DB"), "installed"), 1)
12+		}
13 		defer f.Close()
14 		f.Write([]byte(pkg))
15 		f.Write([]byte{10}) // write a EOL too!