diff options
| author | Jeff Carr <[email protected]> | 2025-10-14 09:15:17 -0500 | 
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-14 10:48:54 -0500 | 
| commit | f4858dd58ee07309692909c350e0675cc6226d7e (patch) | |
| tree | b250a2fe748e2c802691fd8eab8d38a15087b6a7 | |
| parent | 094f1b14e396d09d352f54e9cad50a88fa3e5d43 (diff) | |
more housecleaning
| -rw-r--r-- | config.text | 4 | ||||
| -rw-r--r-- | doIncoming.go | 93 | ||||
| -rw-r--r-- | doRelease.go | 27 | ||||
| -rw-r--r-- | main.go | 4 | ||||
| -rw-r--r-- | structs.go | 22 | ||||
| -rw-r--r-- | stuff.go | 224 | 
6 files changed, 63 insertions, 311 deletions
diff --git a/config.text b/config.text index dfbf1a9..0941913 100644 --- a/config.text +++ b/config.text @@ -13,6 +13,10 @@ configs:  {    value:  "/home/mirrors/wit/mirrors.wit.com.pb"  }  configs:  { +  key:  "BaseDir" +  value:  "/home/mirrors/wit" +} +configs:  {    key:  "distPath"    value:  "/home/mirrors/wit/dists/sid"  } diff --git a/doIncoming.go b/doIncoming.go index f1b8cb9..636aa14 100644 --- a/doIncoming.go +++ b/doIncoming.go @@ -29,57 +29,56 @@ func doIncoming(pb *zoopb.Packages) (string, error) {  				counter += 1  			}  			newcount += 1 -			if strings.HasPrefix(path, "pool/main") { -				_, filename := filepath.Split(path) -				parts := strings.Split(filename, "_") -				if len(parts) != 3 { -					// todo: add more filename checking here (check for ".deb") -					log.Println("bad filenae", filename) -					return nil -				} -				packageName := parts[0] -				letteredDir := log.Sprintf("%1.1s", filename) -				newfilename := filepath.Join("pool/main", letteredDir, packageName, filename) -				if newfilename == path { -					// the filename is correct -					return nil -				} -				destDir := filepath.Dir(newfilename) -				if err := os.MkdirAll(destDir, 0755); err != nil { -					log.Printf("%s  move incoming oldname %s newname: %s\n", packageName, path, newfilename) -					log.Fatal("Failed to create destination directory: %v", err) -				} -				if !argv.Force { -					if config.Exists(newfilename) { -						olddata, _ := os.ReadFile(path) -						newdata, _ := os.ReadFile(newfilename) -						oldmd5 := md5.Sum(olddata) -						newmd5 := md5.Sum(newdata) -						if oldmd5 == newmd5 { -							log.Info("OLD FILE", path) -							log.Info("NEW FILE", newfilename) -							log.Printf("files are the same %x %x\n", md5.Sum(olddata), md5.Sum(newdata)) -						} else { -							shell.RunVerbose([]string{"dpkg", "-I", path}) -							shell.RunVerbose([]string{"dpkg", "-I", newfilename}) -							log.Printf("different checksums: %s %s\n", path, newfilename) -							log.Printf("md5sum old %x vs new %x\n", md5.Sum(olddata), md5.Sum(newdata)) -						} -						// return "file already exists. use --force to replace", errors.New("duplicate .deb in incoming/") -						me.sh.GoodExit("file already exists. use --force to replace") -					} -				} -				os.Rename(path, newfilename) -				log.Printf("%s  moved incoming oldname %s newname: %s\n", packageName, path, newfilename) -				incount += 1 -				if incount > 100 { -					me.sh.GoodExit("file moved") +			if !strings.HasPrefix(path, "pool/main") { +				if config.Verbose() { +					log.Info("already processed", path)  				} +				log.Info("TODO: fix this: ignoring files not in pool/main", path) +			} +			_, filename := filepath.Split(path) +			parts := strings.Split(filename, "_") +			if len(parts) != 3 { +				// todo: add more filename checking here (check for ".deb") +				log.Println("bad filenae", filename)  				return nil  			} -			if argv.Verbose { -				log.Info("already processed", path) +			packageName := parts[0] +			letteredDir := log.Sprintf("%1.1s", filename) +			newfilename := filepath.Join("pool/main", letteredDir, packageName, filename) +			if newfilename == path { +				// the filename is correct +				return nil +			} +			destDir := filepath.Dir(newfilename) +			if err := os.MkdirAll(destDir, 0755); err != nil { +				log.Printf("%s  move incoming oldname %s newname: %s\n", packageName, path, newfilename) +				log.Fatal("Failed to create destination directory: %v", err) +			} +			if config.Exists(newfilename) { +				olddata, _ := os.ReadFile(path) +				newdata, _ := os.ReadFile(newfilename) +				oldmd5 := md5.Sum(olddata) +				newmd5 := md5.Sum(newdata) +				if oldmd5 == newmd5 { +					log.Info("OLD FILE", path) +					log.Info("NEW FILE", newfilename) +					log.Printf("files are the same %x %x\n", md5.Sum(olddata), md5.Sum(newdata)) +				} else { +					shell.RunVerbose([]string{"dpkg", "-I", path}) +					shell.RunVerbose([]string{"dpkg", "-I", newfilename}) +					log.Printf("different checksums: %s %s\n", path, newfilename) +					log.Printf("md5sum old %x vs new %x\n", md5.Sum(olddata), md5.Sum(newdata)) +				} +				// return "file already exists. use --force to replace", errors.New("duplicate .deb in incoming/") +				me.sh.GoodExit("file already exists. TODO: fix this to use --force to replace") +			} +			os.Rename(path, newfilename) +			log.Printf("%s  moved incoming oldname %s newname: %s\n", packageName, path, newfilename) +			incount += 1 +			if incount > 100 { +				me.sh.GoodExit("file moved")  			} +			return nil  			// Get control info  			// cmd := exec.Command("dpkg-deb", "-I", path) diff --git a/doRelease.go b/doRelease.go index c436e70..91908d1 100644 --- a/doRelease.go +++ b/doRelease.go @@ -17,26 +17,19 @@ import (  )  func doRelease() (string, error) { -	// log.Info(r, err2) -	shell.RunVerbose([]string{"rm", "-rf", "/home/mirrors/wit/dists"}) -	// shell.RunVerbose([]string{"rm", "f", "/home/mirrors/wit/dists/sid/InRelease"}) -	// shell.RunVerbose([]string{"rm", "f", "/home/mirrors/wit/dists/sid/Release"}) -	// shell.RunVerbose([]string{"rm", "f", "/home/mirrors/wit/dists/sid/Release.gpg"}) +	// all paths should be relative to this BaseDir path +	if err := os.Chdir(config.GetPanic("BaseDir")); err != nil { +		log.Info("could not change to dir", config.GetPanic("BaseDir"), err) +		panic("could not change to dir") +	} +	shell.RunVerbose([]string{"rm", "-rf", "dists"}) -	// dists.working/sid/main/binary-amd64 -	if err := os.MkdirAll("/home/mirrors/wit/dists/sid/main/binary-amd64", 0755); err != nil { +	if err := os.MkdirAll("dists/sid/main/binary-amd64", 0755); err != nil {  		log.Info("did not work", err)  	} else {  		log.Info("did work")  	} -	if me.pb.BaseDir != "/home/mirrors/wit" { -		me.pb.Filename = "/home/mirrors/wit/mirrors.wit.com.pb" -		me.pb.BaseDir = "/home/mirrors/wit" -		me.pb.Save() -		panic("missing /home/mirrors/wit as BaseDir") -	} -  	if err := os.Chdir(me.pb.BaseDir); err != nil {  		me.sh.BadExit("no '"+me.pb.BaseDir+"' directory", err)  	} @@ -52,7 +45,7 @@ func doRelease() (string, error) {  	newest := doGetNewest("amd64")  	newest.SortPackage()  	thefile := doMakePackagesFile(newest) -	fullname := "/home/mirrors/wit/dists/sid/main/binary-amd64/Packages" +	fullname := "dists/sid/main/binary-amd64/Packages"  	if err := os.WriteFile(fullname, []byte(thefile), os.ModePerm); err != nil {  		return fullname, err  	} @@ -69,7 +62,7 @@ func doRelease() (string, error) {  		log.Info(i, filename)  	} -	releasePath := filepath.Join("/home/mirrors/wit/dists/sid", "Release") +	releasePath := filepath.Join("dists/sid", "Release")  	rfile, _ := os.OpenFile(releasePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)  	fmt.Fprintf(rfile, "Origin: WIT.COM Debian Sid\n") @@ -152,7 +145,7 @@ func doRelease() (string, error) {  	log.Info("Finished Everything")  	log.Info("") -	if argv.Verbose { +	if config.Verbose() {  		log.Info("")  		shell.RunVerbose(cmd)  		log.Info("") @@ -41,7 +41,9 @@ func main() {  		}  	} -	if me.pb.BaseDir == "" { +	// force check the PB variable +	// todo: redo all this now that there is a generalized lib/config/ +	if me.pb.BaseDir != config.GetPanic("BaseDir") {  		me.pb.BaseDir = config.GetPanic("BaseDir")  		me.pb.Save()  		me.sh.BadExit("pb.BaseDir is bank", nil) @@ -8,28 +8,6 @@ import (  	"go.wit.com/lib/protobuf/zoopb"  ) -// --- Configuration --- -// !!! IMPORTANT: Set your GPG Key ID here! -// Find it with: gpg --list-secret-keys --keyid-format=long -// const gpgKeyID = "5D7C9BE47836D2FA48F83C2B4A854AEAF7E0E16D" - -const dist = "sid" -const component = "main" -const poolDir = "pool" -const distsDir = "dists" - -var architectures = []string{"amd64", "riscv64", "arm64", "all"} - -// DebInfo holds the control information for a single .deb package. -type DebInfo struct { -	ControlData map[string]string -	Filename    string -	Size        int64 -	MD5Sum      string -	SHA1Sum     string -	SHA256Sum   string -} -  var me *mainType  // this app's variables diff --git a/stuff.go b/stuff.go deleted file mode 100644 index 0877e76..0000000 --- a/stuff.go +++ /dev/null @@ -1,224 +0,0 @@ -package main - -/* -func runCommand(cmd *exec.Cmd) error { -	var stderr bytes.Buffer -	cmd.Stderr = &stderr -	err := cmd.Run() -	if err != nil { -		return fmt.Errorf("command '%s' failed: %v\nStderr: %s", cmd.String(), err, stderr.String()) -	} -	return nil -} - -// parseControlData converts the text output of dpkg-deb into a map. -func parseControlData(data string) map[string]string { -	control := make(map[string]string) -	scanner := bufio.NewScanner(strings.NewReader(data)) -	for scanner.Scan() { -		line := strings.TrimSpace(scanner.Text()) -		parts := strings.SplitN(line, ":", 2) -		if len(parts) == 2 { -			control[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1]) -		} -	} -	return control -} - -// getChecksums calculates all required hashes for a file. -// FIX 1: Renamed return variables to avoid shadowing package names. -func getChecksums(filePath string) (md5sum, sha1sum, sha256sum string, err error) { -	file, err := os.Open(filePath) -	if err != nil { -		return "", "", "", err -	} -	defer file.Close() - -	hMD5 := md5.New() -	hSHA1 := sha1.New() -	hSHA256 := sha256.New() - -	// TeeReader allows writing to multiple hashers at once -	multiWriter := io.MultiWriter(hMD5, hSHA1, hSHA256) -	if _, err := io.Copy(multiWriter, file); err != nil { -		return "", "", "", err -	} - -	return fmt.Sprintf("%x", hMD5.Sum(nil)), -		fmt.Sprintf("%x", hSHA1.Sum(nil)), -		fmt.Sprintf("%x", hSHA256.Sum(nil)), -		nil -} - -// compressFile creates a compressed version of a file (gz or bz2). -func compressFile(sourcePath, format string) error { -	if format == "gz" { -		sourceFile, err := os.Open(sourcePath) -		if err != nil { -			return err -		} -		defer sourceFile.Close() - -		destPath := sourcePath + ".gz" -		destFile, err := os.Create(destPath) -		if err != nil { -			return err -		} -		defer destFile.Close() - -		writer := gzip.NewWriter(destFile) -		defer writer.Close() - -		_, err = io.Copy(writer, sourceFile) -		return err -	} else if format == "bz2" { -		// FIX 2: Shell out to the bzip2 command for compression. -		destPath := sourcePath + ".bz2" -		cmd := exec.Command("bzip2", "-c", sourcePath) - -		outfile, err := os.Create(destPath) -		if err != nil { -			return fmt.Errorf("failed to create destination file for bzip2: %v", err) -		} -		defer outfile.Close() -		cmd.Stdout = outfile - -		return runCommand(cmd) -	} - -	return fmt.Errorf("unsupported compression format: %s", format) -} - -// generateAndSignReleaseFile creates the main Release file and signs it. -func generateAndSignReleaseFile(distPath string) error { -	releasePath := filepath.Join(distPath, "Release") -	var content strings.Builder - -	// Add headers -	fmt.Fprintf(&content, "Origin: WIT.COM Debian Sid\n") -	fmt.Fprintf(&content, "Label: WIT.COM Debian Sid\n") -	fmt.Fprintf(&content, "Suite: %s\n", dist) -	fmt.Fprintf(&content, "Codename: %s\n", dist) -	fmt.Fprintf(&content, "Date: %s\n", time.Now().UTC().Format(time.RFC1123Z)) -	fmt.Fprintf(&content, "Architectures: %s\n", strings.Join(architectures, " ")) -	fmt.Fprintf(&content, "Components: %s\n", component) -	fmt.Fprintf(&content, "Description: Tooling for RiscV, Semiconductor Designs & Private Clouds\n") - -	// Add checksums -	addChecksums := func(name string) { -		fmt.Fprintf(&content, "%s:\n", name) -		err := filepath.Walk(distPath, func(path string, info os.FileInfo, err error) error { -			if err != nil { -				return err -			} -			if !info.IsDir() && info.Name() != "Release" && info.Name() != "InRelease" && info.Name() != "Release.gpg" { -				relativePath, _ := filepath.Rel(distPath, path) - -				fileBytes, err := os.ReadFile(path) -				if err != nil { -					return err -				} - -				var sum string -				switch name { -				case "MD5Sum": -					sum = fmt.Sprintf("%x", md5.Sum(fileBytes)) -				case "SHA1": -					sum = fmt.Sprintf("%x", sha1.Sum(fileBytes)) -				case "SHA256": -					// FIX 3: Use the correct sha256.Sum256 function. -					sum = fmt.Sprintf("%x", sha256.Sum256(fileBytes)) -				} -				fmt.Fprintf(&content, " %s %d %s\n", sum, info.Size(), relativePath) -			} -			return nil -		}) -		if err != nil { -			log.Printf("Warning: could not walk path for checksums: %v", err) -		} -	} - -	addChecksums("MD5Sum") -	addChecksums("SHA1") -	addChecksums("SHA256") - -	if err := os.WriteFile(releasePath, []byte(content.String()), 0644); err != nil { -		return fmt.Errorf("failed to write Release file: %v", err) -	} - -	// Sign the file -	log.Println("Signing with GPG key:", gpgKeyID) - -	// Create InRelease -	cmdClearSign := exec.Command("gpg", "--default-key", gpgKeyID, "--clearsign", "-o", filepath.Join(distPath, "InRelease"), releasePath) -	if err := runCommand(cmdClearSign); err != nil { -		return fmt.Errorf("failed to create InRelease: %v", err) -	} - -	// Create Release.gpg -	cmdDetachedSign := exec.Command("gpg", "--default-key", gpgKeyID, "-abs", "-o", filepath.Join(distPath, "Release.gpg"), releasePath) -	if err := runCommand(cmdDetachedSign); err != nil { -		return fmt.Errorf("failed to create Release.gpg: %v", err) -	} - -	return nil -} - -// scanDebs finds all .deb files and extracts their metadata. -func scanDebs(root string, count int) ([]DebInfo, error) { -	var debs []DebInfo -	var counter int -	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { -		if err != nil { -			return err -		} -		if !info.IsDir() && strings.HasSuffix(info.Name(), ".deb") { -			found := me.pb.FindByFilename(path) -			if found != nil { -				// log.Info("already processed", path) -				return nil -			} -			if count == -1 { -				return nil -			} -			counter += 1 -			if counter > count { -				return nil -			} -			log.Printf("  -> Processing %s\n", path) - -			// Get control info -			cmd := exec.Command("dpkg-deb", "-I", path) -			var out bytes.Buffer -			cmd.Stdout = &out -			if err := cmd.Run(); err != nil { -				return fmt.Errorf("failed to run dpkg-deb on %s: %v", path, err) -			} - -			controlData := parseControlData(out.String()) - -			// Get checksums -			md5sum, sha1sum, sha256sum, err := getChecksums(path) -			if err != nil { -				return err -			} - -			relativePath, err := filepath.Rel(".", path) -			if err != nil { -				return err -			} - -			debs = append(debs, DebInfo{ -				ControlData: controlData, -				Filename:    relativePath, -				Size:        info.Size(), -				MD5Sum:      md5sum, -				SHA1Sum:     sha1sum, -				SHA256Sum:   sha256sum, -			}) -		} -		return nil -	}) -	return debs, err -} -*/  | 
