mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-04-25 21:30:23 +08:00
Use `golangci-lint fmt` to format code, upgrading `.golangci.yml` to v2 and mirroring the linter configuration used by https://github.com/go-gitea/gitea. `gci` now handles import ordering into standard, project-local, blank, and default groups. Mirrors https://github.com/go-gitea/gitea/pull/37194. Changes: - Upgrade `.golangci.yml` to v2 format with the same linter set as gitea (minus `prealloc`, `unparam`, `testifylint`, `nilnil` which produced too many pre-existing issues) - Add path-based exclusions (`bodyclose`, `gosec` in tests; `gosec:G115`/`G117` globally) - Run lint via `make lint-go` in CI instead of `golangci/golangci-lint-action`, matching the pattern used by other Gitea repos - Apply safe auto-fixes (`modernize`, `perfsprint`, `usetesting`, etc.) - Add explanations to existing `//nolint` directives - Remove dead code (unused `newRemoteReusableWorkflow` and `networkName`), duplicate imports, and shadowed `max` builtins - Replace deprecated `docker/distribution/reference` with `distribution/reference` - Fix `Deprecated:` comment casing and simplify nil/len checks --- This PR was written with the help of Claude Opus 4.7 Reviewed-on: https://gitea.com/gitea/act/pulls/163 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-committed-by: silverwind <me@silverwind.io>
132 lines
2.7 KiB
Go
132 lines
2.7 KiB
Go
package artifactcache
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
)
|
|
|
|
type Storage struct {
|
|
rootDir string
|
|
}
|
|
|
|
func NewStorage(rootDir string) (*Storage, error) {
|
|
if err := os.MkdirAll(rootDir, 0o755); err != nil {
|
|
return nil, err
|
|
}
|
|
return &Storage{
|
|
rootDir: rootDir,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Storage) Exist(id uint64) (bool, error) {
|
|
name := s.filename(id)
|
|
if _, err := os.Stat(name); os.IsNotExist(err) {
|
|
return false, nil
|
|
} else if err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (s *Storage) Write(id uint64, offset int64, reader io.Reader) error {
|
|
name := s.tempName(id, offset)
|
|
if err := os.MkdirAll(filepath.Dir(name), 0o755); err != nil {
|
|
return err
|
|
}
|
|
file, err := os.Create(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
_, err = io.Copy(file, reader)
|
|
return err
|
|
}
|
|
|
|
func (s *Storage) Commit(id uint64, size int64) (int64, error) {
|
|
defer func() {
|
|
_ = os.RemoveAll(s.tempDir(id))
|
|
}()
|
|
|
|
name := s.filename(id)
|
|
tempNames, err := s.tempNames(id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(name), 0o755); err != nil {
|
|
return 0, err
|
|
}
|
|
file, err := os.Create(name)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer file.Close()
|
|
|
|
var written int64
|
|
for _, v := range tempNames {
|
|
f, err := os.Open(v)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n, err := io.Copy(file, f)
|
|
_ = f.Close()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
written += n
|
|
}
|
|
|
|
// If size is less than 0, it means the size is unknown.
|
|
// We can't check the size of the file, just skip the check.
|
|
// It happens when the request comes from old versions of actions, like `actions/cache@v2`.
|
|
if size >= 0 && written != size {
|
|
_ = file.Close()
|
|
_ = os.Remove(name)
|
|
return 0, fmt.Errorf("broken file: %v != %v", written, size)
|
|
}
|
|
|
|
return written, nil
|
|
}
|
|
|
|
func (s *Storage) Serve(w http.ResponseWriter, r *http.Request, id uint64) {
|
|
name := s.filename(id)
|
|
http.ServeFile(w, r, name)
|
|
}
|
|
|
|
func (s *Storage) Remove(id uint64) {
|
|
_ = os.Remove(s.filename(id))
|
|
_ = os.RemoveAll(s.tempDir(id))
|
|
}
|
|
|
|
func (s *Storage) filename(id uint64) string {
|
|
return filepath.Join(s.rootDir, fmt.Sprintf("%02x", id%0xff), strconv.FormatUint(id, 10))
|
|
}
|
|
|
|
func (s *Storage) tempDir(id uint64) string {
|
|
return filepath.Join(s.rootDir, "tmp", strconv.FormatUint(id, 10))
|
|
}
|
|
|
|
func (s *Storage) tempName(id uint64, offset int64) string {
|
|
return filepath.Join(s.tempDir(id), fmt.Sprintf("%016x", offset))
|
|
}
|
|
|
|
func (s *Storage) tempNames(id uint64) ([]string, error) {
|
|
dir := s.tempDir(id)
|
|
files, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var names []string
|
|
for _, v := range files {
|
|
if !v.IsDir() {
|
|
names = append(names, filepath.Join(dir, v.Name()))
|
|
}
|
|
}
|
|
return names, nil
|
|
}
|