summaryrefslogtreecommitdiff
path: root/doIncoming.go
blob: 636aa144a0d9ac2ff9bbe723d94307316f539a16 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main

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

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

func doIncoming(pb *zoopb.Packages) (string, error) {
	os.Chdir(me.pb.BaseDir)

	var counter int
	var incount int
	var newcount int
	err := filepath.Walk("pool", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if !info.IsDir() && strings.HasSuffix(info.Name(), ".deb") {
			if found := me.pb.FindByFilename(path); found != nil {
				// log.Printf("not new file %s\n", path)
				counter += 1
			}
			newcount += 1
			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
			}
			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)
			// 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)
			// }

		}
		return nil
	})

	s := log.Sprintf("scanned %d files. (%d) in incoming. (%d) new files pb.Len(%d)", counter, incount, newcount, pb.Len())
	return s, err
}