summaryrefslogtreecommitdiff
path: root/doIncomingOld.go
blob: 35873f0950beff0fbd0de30c0bd3952df2f806af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main

import (
	"crypto/md5"
	"errors"
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/lib/config"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

func moveOutOfIncomingOld(fullname string) error {
	if strings.HasPrefix(fullname, "pool/main/incoming") {
		log.Info("file is in incoming", fullname)
	} else {
		log.Info("file isn't in incoming", fullname)
		return nil
	}

	_, filename := filepath.Split(fullname)
	parts := strings.Split(filename, "_")
	if len(parts) != 3 {
		// todo: add more filename checking here (check for ".deb")
		return errors.New(log.Sprintf("bad filenae %s", filename))
	}
	packageName := parts[0]
	letteredDir := log.Sprintf("%1.1s", filename)
	newfilename := filepath.Join("pool/main", letteredDir, packageName, filename)
	if newfilename == fullname {
		// the filename is correct
		return errors.New(log.Sprintf("should be impossible %s", filename))
	}
	destDir := filepath.Dir(newfilename)
	if err := os.MkdirAll(destDir, 0755); err != nil {
		log.Printf("%s  move incoming oldname %s newname: %s\n", packageName, fullname, newfilename)
		log.Fatal("Failed to create destination directory: %v", err)
	}
	if config.Exists(newfilename) {
		olddata, _ := os.ReadFile(fullname)
		newdata, _ := os.ReadFile(newfilename)
		oldmd5 := md5.Sum(olddata)
		newmd5 := md5.Sum(newdata)
		if oldmd5 == newmd5 {
			log.Info("OLD FILE", fullname)
			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", fullname})
			shell.RunVerbose([]string{"dpkg", "-I", newfilename})
			log.Printf("different checksums: %s %s\n", fullname, newfilename)
			log.Printf("md5sum old %x vs new %x\n", md5.Sum(olddata), md5.Sum(newdata))
		}
		if argv.Force {
			moveFileToBroken(newfilename)
			// os.Rename(path, newfilename)
			// log.Printf("%s  moved incoming oldname %s newname: %s\n", packageName, path, newfilename)
		}
		// return "file already exists. use --force to replace", errors.New("duplicate .deb in incoming/")
		me.argv.GoodExit("file already exists. TODO: fix this to use --force to replace")
	}
	// file can be moved
	os.Rename(fullname, newfilename)
	log.Printf("%s  moved incoming oldname %s newname: %s\n", packageName, fullname, newfilename)
	return nil
}