Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update aptutil to golang 1.17 #62

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ version: 2.1
executors:
golang:
docker:
- image: quay.io/cybozu/golang:1.15-focal
- image: quay.io/cybozu/golang:1.17-focal
jobs:
lint:
executor: golang
steps:
- checkout
- run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.33.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.45.2
golangci-lint run --new-from-rev=HEAD~
test:
executor: golang
steps:
- checkout
- run: go test -race -v ./...
- run: make test
build:
executor: golang
steps:
- checkout
- run: go build ./...
- run: make build

workflows:
version: 2
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ tags
pkg/
*.tgz

# Ignore go.sum
/go.sum
cover.out
10 changes: 9 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
linters:
enable:
- dupl
- forbidigo
- goconst
- gofmt
- golint
- revive
- typecheck
- unparam
linters-settings:
forbidigo:
forbid:
# https://go.dev/doc/go1.16#ioutil
- ^ioutil\..*
goconst:
ignore-tests: true
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test:
go test -count 1 -race -v ./... -coverprofile cover.out

build:
go build ./...
6 changes: 3 additions & 3 deletions apt/fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func (fi *FileInfo) Same(t *FileInfo) bool {
if fi.size != t.size {
return false
}
if fi.md5sum != nil && bytes.Compare(fi.md5sum, t.md5sum) != 0 {
if fi.md5sum != nil && !bytes.Equal(fi.md5sum, t.md5sum) {
return false
}
if fi.sha1sum != nil && bytes.Compare(fi.sha1sum, t.sha1sum) != 0 {
if fi.sha1sum != nil && !bytes.Equal(fi.sha1sum, t.sha1sum) {
return false
}
if fi.sha256sum != nil && bytes.Compare(fi.sha256sum, t.sha256sum) != 0 {
if fi.sha256sum != nil && !bytes.Equal(fi.sha256sum, t.sha256sum) {
return false
}
return true
Expand Down
25 changes: 12 additions & 13 deletions cacher/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cacher
import (
"context"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -205,7 +204,7 @@ func (c *Cacher) maintRelease(ctx context.Context, p string, withGPG bool) {
defer ticker.Stop()

if log.Enabled(log.LvDebug) {
log.Debug("maintRelease", map[string]interface{}{
_ = log.Debug("maintRelease", map[string]interface{}{
"path": p,
})
}
Expand All @@ -226,7 +225,7 @@ func (c *Cacher) maintRelease(ctx context.Context, p string, withGPG bool) {
}

func closeRespBody(r *http.Response) {
io.Copy(ioutil.Discard, r.Body)
_, _ = io.Copy(io.Discard, r.Body)
r.Body.Close()
}

Expand Down Expand Up @@ -306,7 +305,7 @@ func (c *Cacher) download(ctx context.Context, p string, u *url.URL, valid *apt.
}
resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
log.Warn("GET failed", map[string]interface{}{
_ = log.Warn("GET failed", map[string]interface{}{
"url": u.String(),
"error": err.Error(),
})
Expand All @@ -326,7 +325,7 @@ func (c *Cacher) download(ctx context.Context, p string, u *url.URL, valid *apt.

tempfile, err := storage.TempFile()
if err != nil {
log.Warn("GET failed", map[string]interface{}{
_ = log.Warn("GET failed", map[string]interface{}{
"url": u.String(),
"error": err.Error(),
})
Expand All @@ -339,22 +338,22 @@ func (c *Cacher) download(ctx context.Context, p string, u *url.URL, valid *apt.

fi, err := apt.CopyWithFileInfo(tempfile, resp.Body, p)
if err != nil {
log.Warn("GET failed", map[string]interface{}{
_ = log.Warn("GET failed", map[string]interface{}{
"url": u.String(),
"error": err.Error(),
})
return
}
err = tempfile.Sync()
if err != nil {
log.Warn("tempfile.Sync failed", map[string]interface{}{
_ = log.Warn("tempfile.Sync failed", map[string]interface{}{
"url": u.String(),
"error": err.Error(),
})
return
}
if valid != nil && !valid.Same(fi) {
log.Warn("downloaded data is not valid", map[string]interface{}{
_ = log.Warn("downloaded data is not valid", map[string]interface{}{
"url": u.String(),
})
return
Expand All @@ -365,15 +364,15 @@ func (c *Cacher) download(ctx context.Context, p string, u *url.URL, valid *apt.
if t := strings.SplitN(path.Clean(p), "/", 2); len(t) == 2 && apt.IsMeta(t[1]) {
_, err = tempfile.Seek(0, io.SeekStart)
if err != nil {
log.Error("failed to reset tempfile offset", map[string]interface{}{
_ = log.Error("failed to reset tempfile offset", map[string]interface{}{
"error": err.Error(),
})
return
}

fil, _, err = apt.ExtractFileInfo(t[1], tempfile)
if err != nil {
log.Error("invalid meta data", map[string]interface{}{
_ = log.Error("invalid meta data", map[string]interface{}{
"path": p,
"error": err.Error(),
})
Expand All @@ -389,7 +388,7 @@ func (c *Cacher) download(ctx context.Context, p string, u *url.URL, valid *apt.
// both have the same set of FileInfo, storage.Insert need to be
// guarded by c.fiLock.
if err := storage.Insert(tempfile.Name(), fi); err != nil {
log.Error("could not save an item", map[string]interface{}{
_ = log.Error("could not save an item", map[string]interface{}{
"path": p,
"error": err.Error(),
})
Expand All @@ -408,7 +407,7 @@ func (c *Cacher) download(ctx context.Context, p string, u *url.URL, valid *apt.
}
}
c.info[p] = fi
log.Info("downloaded and cached", map[string]interface{}{
_ = log.Info("downloaded and cached", map[string]interface{}{
"path": p,
})
}
Expand Down Expand Up @@ -446,7 +445,7 @@ RETRY:
return http.StatusOK, f, nil
case ErrNotFound:
default:
log.Error("lookup failure", map[string]interface{}{
_ = log.Error("lookup failure", map[string]interface{}{
"error": err.Error(),
})
return http.StatusInternalServerError, nil, err
Expand Down
2 changes: 1 addition & 1 deletion cacher/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c cacheHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p := path.Clean(r.URL.Path[1:])

if log.Enabled(log.LvDebug) {
log.Debug("request path", map[string]interface{}{
_ = log.Debug("request path", map[string]interface{}{
"path": p,
})
}
Expand Down
20 changes: 10 additions & 10 deletions cacher/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cacher

import (
"container/heap"
"io/ioutil"
"io"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -124,11 +124,11 @@ func (cm *Storage) maint() {
delete(cm.cache, e.Path())
cm.used -= e.Size()
if err := os.Remove(filepath.Join(cm.dir, e.FilePath())); err != nil {
log.Warn("Storage.maint", map[string]interface{}{
_ = log.Warn("Storage.maint", map[string]interface{}{
"error": err.Error(),
})
}
log.Info("removed", map[string]interface{}{
_ = log.Info("removed", map[string]interface{}{
"path": e.Path(),
})
}
Expand All @@ -141,7 +141,7 @@ func readData(path string) ([]byte, error) {
}
defer f.Close()

return ioutil.ReadAll(f)
return io.ReadAll(f)
}

// Load loads existing items in filesystem.
Expand Down Expand Up @@ -179,7 +179,7 @@ func (cm *Storage) Load() error {
cm.lclock++
cm.lru = append(cm.lru, e)
cm.cache[subpath] = e
log.Debug("Storage.Load", map[string]interface{}{
_ = log.Debug("Storage.Load", map[string]interface{}{
"path": subpath,
})
return nil
Expand All @@ -200,7 +200,7 @@ func (cm *Storage) Load() error {
// opens the file for reading and writing,
// and returns the resulting *os.File.
func (cm *Storage) TempFile() (*os.File, error) {
return ioutil.TempFile(cm.dir, "_tmp")
return os.CreateTemp(cm.dir, "_tmp")
}

// Insert inserts or updates a cache item.
Expand Down Expand Up @@ -241,15 +241,15 @@ func (cm *Storage) Insert(filename string, fi *apt.FileInfo) error {
if !os.IsNotExist(err) {
return err
}
log.Warn("cache file was removed already", map[string]interface{}{
_ = log.Warn("cache file was removed already", map[string]interface{}{
"path": p,
})
}
cm.used -= existing.Size()
heap.Remove(cm, existing.index)
delete(cm.cache, p)
if log.Enabled(log.LvDebug) {
log.Debug("deleted existing item", map[string]interface{}{
_ = log.Debug("deleted existing item", map[string]interface{}{
"path": p,
})
}
Expand Down Expand Up @@ -344,15 +344,15 @@ func (cm *Storage) Delete(p string) error {
if !os.IsNotExist(err) {
return err
}
log.Warn("cached file was already removed", map[string]interface{}{
_ = log.Warn("cached file was already removed", map[string]interface{}{
"path": p,
})
}

cm.used -= e.Size()
heap.Remove(cm, e.index)
delete(cm.cache, p)
log.Info("deleted item", map[string]interface{}{
_ = log.Info("deleted item", map[string]interface{}{
"path": p,
})
return nil
Expand Down
28 changes: 14 additions & 14 deletions cacher/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cacher

import (
"bytes"
"io/ioutil"
"io"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -36,7 +36,7 @@ func insert(cm *Storage, data []byte, path string) (*apt.FileInfo, error) {

func testStorageInsertWorksCorrectly(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "gotest")
dir, err := os.MkdirTemp("", "gotest")
if err != nil {
t.Fatal(err)
}
Expand All @@ -60,19 +60,19 @@ func testStorageInsertWorksCorrectly(t *testing.T) {

func testStorageInsertOverwrite(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "gotest")
dir, err := os.MkdirTemp("", "gotest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
cm := NewStorage(dir, 0)

fi, err := insert(cm, []byte("a"), "path/to/a")
_, err = insert(cm, []byte("a"), "path/to/a")
if err != nil {
t.Fatal(err)
}

fi, err = insert(cm, []byte("a"), "path/to/a")
fi, err := insert(cm, []byte("a"), "path/to/a")
if err != nil {
t.Fatal(err)
}
Expand All @@ -89,7 +89,7 @@ func testStorageInsertOverwrite(t *testing.T) {

func testStorageInsertReturnsErrorAgainstBadPath(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "gotest")
dir, err := os.MkdirTemp("", "gotest")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func testStorageInsertReturnsErrorAgainstBadPath(t *testing.T) {

func testStorageInsertPurgesFilesAllowingLRU(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "gotest")
dir, err := os.MkdirTemp("", "gotest")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -220,27 +220,27 @@ func TestStorageLoad(t *testing.T) {
"ghij": {'g', 'h', 'i', 'j'},
}

dir, err := ioutil.TempDir("", "gotest")
dir, err := os.MkdirTemp("", "gotest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

for k, v := range files {
err := ioutil.WriteFile(filepath.Join(dir, k+fileSuffix), v, 0644)
err := os.WriteFile(filepath.Join(dir, k+fileSuffix), v, 0644)
if err != nil {
t.Fatal(err)
}
}

// dummy should be ignored as it does not have a proper suffix.
err = ioutil.WriteFile(filepath.Join(dir, "dummy"), []byte{'d'}, 0644)
err = os.WriteFile(filepath.Join(dir, "dummy"), []byte{'d'}, 0644)
if err != nil {
t.Fatal(err)
}

cm := NewStorage(dir, 0)
cm.Load()
_ = cm.Load()

l := cm.ListAll()
if len(l) != len(files) {
Expand Down Expand Up @@ -284,12 +284,12 @@ func TestStorageLoad(t *testing.T) {
t.Fatal(err)
}

data, err := ioutil.ReadAll(fGHIJ)
data, err := io.ReadAll(fGHIJ)
fGHIJ.Close()
if err != nil {
t.Fatal(err)
}
if bytes.Compare(files["ghij"], data) != 0 {
t.Error(`bytes.Compare(files["ghij"], data) != 0`)
if !bytes.Equal(files["ghij"], data) {
t.Error(`!bytes.Equal(files["ghij"], data)`)
}
}
Loading