Skip to content

Commit

Permalink
fix an issue with opening too many files and outsource version compar…
Browse files Browse the repository at this point in the history
…ison to a more full-featured library
  • Loading branch information
neutralinsomniac committed Oct 29, 2020
1 parent 7ac8325 commit 5acbb30
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 55 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/neutralinsomniac/obsdpkgup

go 1.13

require suah.dev/protect v1.0.0
require (
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2
suah.dev/protect v1.0.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2 h1:YocNLcTBdEdvY3iDK6jfWXvEaM5OCKkjxPKoJRdB3Gg=
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2/go.mod h1:76rfSfYPWj01Z85hUf/ituArm797mNKcvINh1OlsZKo=
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3 h1:5B6i6EAiSYyejWfvc5Rc9BbI3rzIsrrXfAQBWnYfn+w=
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
suah.dev/protect v1.0.0 h1:X8pzDvDIZIiugmkmr6DES6JFO1XUdJWi34Ffmk6CMZY=
Expand Down
53 changes: 4 additions & 49 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
"time"

"github.com/mcuadros/go-version"
"suah.dev/protect"
)

Expand Down Expand Up @@ -85,11 +85,12 @@ func parseLocalPkgInfoToPkgList() PkgList {

f, err := os.Open(fmt.Sprintf("%s%s/+CONTENTS", pkgDbPath, pkgdir))
checkAndExit(err)
defer f.Close()

contents, err := ioutil.ReadAll(f)
checkAndExit(err)

f.Close()

matches := sigRe.FindAll(contents, -1)

var data_to_hash []byte
Expand Down Expand Up @@ -146,53 +147,7 @@ func min(a, b int) int {
var numberRe = regexp.MustCompile(`^\d+`)

func compareVersionString(v1, v2 string) int {
// early escape
if v1 == v2 {
return 0
}

v1s := strings.Split(v1, ".")
v2s := strings.Split(v2, ".")
min := min(len(v1s), len(v2s))

for i := 0; i < min; i++ {
// first, snag and compare the int portions
v1str := numberRe.FindString(v1s[i])
v2str := numberRe.FindString(v2s[i])
if v1str != "" && v2str != "" {
v1num, _ := strconv.Atoi(v1str)
v2num, _ := strconv.Atoi(v2str)
if v1num > v2num {
return -1
} else if v1num < v2num {
return 1
}
}

// now try alphanumeric
if v1s[i] > v2s[i] {
return -1
} else if v1s[i] < v2s[i] {
return 1
}

// now try length
if len(v1s[i]) > len(v2s[i]) {
return -1
} else if len(v1s[i]) < len(v2s[i]) {
return 1
}
}

// if we got here, then we have a complete prefix match up to the common length of the split arrays.
// return the difference in lengths to make sure that one array isn't longer than the other (to account for e.g. 81.0->81.0.2)
if len(v2s) > len(v1s) {
return 1
} else if len(v1s) > len(v2s) {
return -1
} else {
return 0
}
return version.CompareSimple(v2, v1)
}

type SysInfo struct {
Expand Down
15 changes: 10 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ func TestVersionComparison(t *testing.T) {
t.Errorf("expected: %d, got: %d", expected, res)
}

res = compareVersionString("80.1", "80.2")
res = compareVersionString("1.14.7v3", "1.14.7p0v3")
if res != expected {
t.Errorf("expected: %d, got: %d", expected, res)
}

res = compareVersionString("80.0.0", "80.0.1")
res = compareVersionString("80.1", "80.2")
if res != expected {
t.Errorf("expected: %d, got: %d", expected, res)
}

res = compareVersionString("80.1", "81.2")
res = compareVersionString("80.0.0", "80.0.1")
if res != expected {
t.Errorf("expected: %d, got: %d", expected, res)
}

res = compareVersionString("80.1a", "80.1b")
res = compareVersionString("80.1", "81.2")
if res != expected {
t.Errorf("expected: %d, got: %d", expected, res)
}

res = compareVersionString("80.1a", "80.1aa")
res = compareVersionString("80.1a", "80.1b")
if res != expected {
t.Errorf("expected: %d, got: %d", expected, res)
}
Expand Down Expand Up @@ -72,6 +72,11 @@ func TestVersionComparison(t *testing.T) {
t.Errorf("expected: %d, got: %d", expected, res)
}

res = compareVersionString("80.1a", "80.1aa")
if res != expected {
t.Errorf("expected: %d, got: %d", expected, res)
}

expected = 0
res = compareVersionString("80.1.1", "80.1.1")
if res != expected {
Expand Down

0 comments on commit 5acbb30

Please sign in to comment.