Skip to content

Commit

Permalink
Merge pull request #83 from snprajwal/crit-lint
Browse files Browse the repository at this point in the history
Enable linting for crit
  • Loading branch information
rst0git authored Aug 12, 2022
2 parents b7262e1 + b2cfe11 commit 948d84a
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 31 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ image
scripts/*.h
scripts/expected.go
scripts/output.go
magic/magic.go
crit/bin
11 changes: 4 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
run:
skip-dirs:
- rpc
- stats
- crit

linters:
disable-all: false
presets:
- bugs
- performance
- unused
- format

linters-settings:
exhaustive:
default-signifies-exhaustive: true
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ lint:
golangci-lint run ./...

build:
$(MAKE) -C scripts
$(GO) build -v ./...
# Build crit binary
$(MAKE) -C crit bin/crit
Expand Down
52 changes: 41 additions & 11 deletions crit/decode-extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func decodePipesData(
extraSize := p.GetBytes()

if noPayload {
f.Seek(int64(extraSize), 1)
_, err := f.Seek(int64(extraSize), 1)
if err != nil {
return "", err
}
return countBytes(int64(extraSize)), nil
}
extraBuf := make([]byte, extraSize)
Expand All @@ -49,7 +52,10 @@ func decodeSkQueues(
extraSize := p.GetLength()

if noPayload {
f.Seek(int64(extraSize), 1)
_, err := f.Seek(int64(extraSize), 1)
if err != nil {
return "", err
}
return countBytes(int64(extraSize)), nil
}
extraBuf := make([]byte, extraSize)
Expand Down Expand Up @@ -78,7 +84,10 @@ func decodeTcpStream(
outQLen := p.GetOutqLen()

if noPayload {
f.Seek(0, 2)
_, err := f.Seek(0, 2)
if err != nil {
return "", err
}
return countBytes(int64(inQLen + outQLen)), nil
}

Expand Down Expand Up @@ -111,7 +120,10 @@ func decodeBpfmapData(
extraSize := p.GetKeysBytes() + p.GetValuesBytes()

if noPayload {
f.Seek(int64(extraSize), 1)
_, err := f.Seek(int64(extraSize), 1)
if err != nil {
return "", err
}
return countBytes(int64(extraSize)), nil
}
extraBuf := make([]byte, extraSize)
Expand All @@ -137,7 +149,10 @@ func decodeIpcSem(
roundedSize := (extraSize/8 + 1) * 8

if noPayload {
f.Seek(roundedSize, 1)
_, err := f.Seek(roundedSize, 1)
if err != nil {
return "", err
}
return countBytes(extraSize), nil
}
extraPayload := []uint16{}
Expand All @@ -149,7 +164,10 @@ func decodeIpcSem(
}
extraPayload = append(extraPayload, binary.LittleEndian.Uint16(extraBuf))
}
f.Seek(roundedSize-extraSize, 1)
_, err := f.Seek(roundedSize-extraSize, 1)
if err != nil {
return "", err
}
extraJson, err := json.Marshal(extraPayload)
return string(extraJson), err
}
Expand All @@ -169,14 +187,20 @@ func decodeIpcShm(
roundedSize := (extraSize/4 + 1) * 4

if noPayload {
f.Seek(roundedSize, 1)
_, err := f.Seek(roundedSize, 1)
if err != nil {
return "", err
}
return countBytes(extraSize), nil
}
extraBuf := make([]byte, extraSize)
if _, err := f.Read(extraBuf); err != nil {
return "", err
}
f.Seek(roundedSize-extraSize, 1)
_, err := f.Seek(roundedSize-extraSize, 1)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(extraBuf), nil
}

Expand All @@ -200,7 +224,7 @@ func decodeIpcMsg(
for i := 0; i < int(msgQNum); i++ {
n, err := f.Read(sizeBuf)
if n == 0 {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return "", err
Expand All @@ -219,7 +243,10 @@ func decodeIpcMsg(
roundedMsgSize := (msgSize/8 + 1) * 8

if noPayload {
f.Seek(roundedMsgSize, 1)
_, err = f.Seek(roundedMsgSize, 1)
if err != nil {
return "", err
}
totalSize += int64(extraSize) + msgSize
} else {
jsonMsg, err := protojson.Marshal(msg)
Expand All @@ -234,7 +261,10 @@ func decodeIpcMsg(
}
msgData := base64.StdEncoding.EncodeToString(msgDataBuf)
extraPayload = append(extraPayload, msgData)
f.Seek(roundedMsgSize-msgSize, 1)
_, err = f.Seek(roundedMsgSize-msgSize, 1)
if err != nil {
return "", err
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion crit/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package crit
import (
"encoding/base64"
"encoding/binary"
"errors"
"io"
"os"

Expand Down Expand Up @@ -149,7 +150,7 @@ func (img *CriuImage) decodeGhostFile(f *os.File, noPayload bool) error {
for {
n, err := f.Read(sizeBuf)
if n == 0 {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return err
Expand Down
6 changes: 3 additions & 3 deletions crit/explore.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *crit) ExploreFds() ([]*Fd, error) {
return nil, err
}

var fds []*Fd
fds := make([]*Fd, 0)
for _, entry := range psTreeImg.Entries {
process := entry.Message.(*images.PstreeEntry)
pId := process.GetPid()
Expand Down Expand Up @@ -176,7 +176,7 @@ func (c *crit) ExploreMems() ([]*MemMap, error) {
return vmaIdMap[shmId]
}

var memMaps []*MemMap
memMaps := make([]*MemMap, 0)
for _, entry := range psTreeImg.Entries {
process := entry.Message.(*images.PstreeEntry)
pId := process.GetPid()
Expand Down Expand Up @@ -293,7 +293,7 @@ func (c *crit) ExploreRss() ([]*RssMap, error) {
return nil, err
}

var rssMaps []*RssMap
rssMaps := make([]*RssMap, 0)
for _, entry := range psTreeImg.Entries {
process := entry.Message.(*images.PstreeEntry)
pId := process.GetPid()
Expand Down
158 changes: 158 additions & 0 deletions magic/magic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ test:
@rm -f input.h output.go expected.go

clean:
rm -f ../magic/magic.go
rm -f ../magic/magic.go magic.h

.PHONY: magic-gen test clean
11 changes: 11 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
This directory contains scripts used by go-criu.

# `magicgen`
CRIU uses a 32-bit integer value to determine the type of image file. These values are called *magics*, and are defined [here](/~https://github.com/checkpoint-restore/criu/tree/master/criu/include/magic.h). `magicgen.go` processes this header file and generates `../magic/magic.go` with a map of magic names and values.

`Usage: magicgen.go /path/to/magic.h /path/to/magic.go`

A set of Makefile targets are provided for convenience:
- `make` or `make magic-gen`: Generate `../magic/magic.go`
- `make test`: Run unit test and E2E test for `magicgen.go`
- `make clean`: Remove `../magic/magic.go` and `magic.h`
Loading

0 comments on commit 948d84a

Please sign in to comment.