From 704d06ba87ac8bc4af4abc1ed88efed82df7127d Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 20 Nov 2024 15:57:34 -0600 Subject: [PATCH] UpdateGoPath() (probably without memory corruption) --- forgeConfig/Makefile | 2 +- forgeConfig/main.go | 13 +++++-------- repo.proto | 2 +- repos.go | 17 +++++++++++++++++ update.go | 16 +++++++++------- 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/forgeConfig/Makefile b/forgeConfig/Makefile index afabd5b..942ee78 100644 --- a/forgeConfig/Makefile +++ b/forgeConfig/Makefile @@ -14,7 +14,7 @@ add: ./forgeConfig --add --name 'foo' --gopath 'go.wit.com/apps/foo' update: - ./forgeConfig --update --name 'foo' --gopath 'go.wit.com/apps/foonew' + ./forgeConfig --update --name 'foo' --gopath 'more stuff but not memory corruption?' corruptMemory: ./forgeConfig --update --name 'foo' --gopath 'blah' diff --git a/forgeConfig/main.go b/forgeConfig/main.go index 8e08878..20df841 100644 --- a/forgeConfig/main.go +++ b/forgeConfig/main.go @@ -23,25 +23,22 @@ func main() { loop := repos.SortByName() // get the list of repos for loop.Scan() { r := loop.Repo() - log.Info("repo:", r.Name, r.Gopath) + log.Info("repo:", r.Name, r.GoPath) } os.Exit(0) } if argv.Update { - r := repos.FindByName(argv.Name) // find the repo - if r == nil { - log.Info("rep:", argv.Name, "not found") - os.Exit(-1) + if repos.UpdateGoPath(argv.Name, argv.GoPath) { + // save updated config file + repos.ConfigSave() } - r.Gopath = argv.GoPath - repos.ConfigSave() os.Exit(0) } if argv.Add { log.Info("going to add a new repo", argv.Name, argv.GoPath) new1 := new(forgepb.Repo) new1.Name = argv.Name - new1.Gopath = argv.GoPath + new1.GoPath = argv.GoPath if repos.Append(new1) { log.Info("added", new1.Name, "ok") } else { diff --git a/repo.proto b/repo.proto index f1d307b..4e07d79 100644 --- a/repo.proto +++ b/repo.proto @@ -19,7 +19,7 @@ message Repo { bool readonly = 6; // if you have write access to the repo bool private = 7; // if the repo can be published string debname = 8; // this is the actual .deb name of the package - string gopath = 9; // Examples: 'go.wit.com/apps/go-clone' or "~/mythings" or "/home/src/foo" + string goPath = 9; // Examples: 'go.wit.com/apps/go-clone' or "~/mythings" or "/home/src/foo" google.protobuf.Timestamp verstamp = 10; // the git commit timestamp of the version } diff --git a/repos.go b/repos.go index 07ee8e2..4222ab9 100644 --- a/repos.go +++ b/repos.go @@ -118,6 +118,23 @@ func (a ByRepoName) Len() int { return len(a) } func (a ByRepoName) Less(i, j int) bool { return a[i].Name < a[j].Name } func (a ByRepoName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (all *Repos) DeleteByName(name string) *Repo { + reposLock.Lock() + defer reposLock.Unlock() + + var newr Repo + + for i, _ := range all.Repos { + if all.Repos[i].Name == name { + newr = *all.Repos[i] + all.Repos[i] = all.Repos[len(all.Repos)-1] + all.Repos = all.Repos[:len(all.Repos)-1] + return &newr + } + } + return nil +} + // safely returns a slice of pointers to the Repo protobufs func (r *Repos) selectAllRepos() []*Repo { reposLock.RLock() diff --git a/update.go b/update.go index 2ad64cf..f1f1af3 100644 --- a/update.go +++ b/update.go @@ -1,11 +1,13 @@ package forgepb -import ( - "os" -) +func (all *Repos) UpdateGoPath(name string, gopath string) bool { + oldr := all.DeleteByName(name) + if oldr == nil { + // nothing to update + return false + } -func (repos *Repos) UpdateGoPath(r *Repo, gopath string) { - r.Gopath = gopath - repos.ConfigSave() - os.Exit(0) + // update gopath and append it back to the list + oldr.GoPath = gopath + return all.Append(oldr) }