2023-02-01 13:30:39 -05:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package conda
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"archive/zip"
|
|
|
|
"compress/bzip2"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
"code.gitea.io/gitea/modules/validation"
|
Support compression for Actions logs (#31761)
Support compression for Actions logs to save storage space and
bandwidth. Inspired by
https://github.com/go-gitea/gitea/issues/24256#issuecomment-1521153015
The biggest challenge is that the compression format should support
[seekable](https://github.com/facebook/zstd/blob/dev/contrib/seekable_format/zstd_seekable_compression_format.md).
So when users are viewing a part of the log lines, Gitea doesn't need to
download the whole compressed file and decompress it.
That means gzip cannot help here. And I did research, there aren't too
many choices, like bgzip and xz, but I think zstd is the most popular
one. It has an implementation in Golang with
[zstd](https://github.com/klauspost/compress/tree/master/zstd) and
[zstd-seekable-format-go](https://github.com/SaveTheRbtz/zstd-seekable-format-go),
and what is better is that it has good compatibility: a seekable format
zstd file can be read by a regular zstd reader.
This PR introduces a new package `zstd` to combine and wrap the two
packages, to provide a unified and easy-to-use API.
And a new setting `LOG_COMPRESSION` is added to the config, although I
don't see any reason why not to use compression, I think's it's a good
idea to keep the default with `none` to be consistent with old versions.
`LOG_COMPRESSION` takes effect for only new log files, it adds `.zst` as
an extension to the file name, so Gitea can determine if it needs
decompression according to the file name when reading. Old files will
keep the format since it's not worth converting them, as they will be
cleared after #31735.
<img width="541" alt="image"
src="https://github.com/user-attachments/assets/e9598764-a4e0-4b68-8c2b-f769265183c9">
(cherry picked from commit 33cc5837a655ad544b936d4d040ca36d74092588)
Conflicts:
assets/go-licenses.json
go.mod
go.sum
resolved with make tidy
2024-08-08 22:10:30 -04:00
|
|
|
"code.gitea.io/gitea/modules/zstd"
|
2023-02-01 13:30:39 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidStructure = util.SilentWrap{Message: "package structure is invalid", Err: util.ErrInvalidArgument}
|
|
|
|
ErrInvalidName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument}
|
|
|
|
ErrInvalidVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PropertyName = "conda.name"
|
|
|
|
PropertyChannel = "conda.channel"
|
|
|
|
PropertySubdir = "conda.subdir"
|
2023-07-30 19:54:22 -04:00
|
|
|
PropertyMetadata = "conda.metadata"
|
2023-02-01 13:30:39 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Package represents a Conda package
|
|
|
|
type Package struct {
|
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
Subdir string
|
|
|
|
VersionMetadata *VersionMetadata
|
|
|
|
FileMetadata *FileMetadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// VersionMetadata represents the metadata of a Conda package
|
|
|
|
type VersionMetadata struct {
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Summary string `json:"summary,omitempty"`
|
|
|
|
ProjectURL string `json:"project_url,omitempty"`
|
|
|
|
RepositoryURL string `json:"repository_url,omitempty"`
|
|
|
|
DocumentationURL string `json:"documentation_url,omitempty"`
|
|
|
|
License string `json:"license,omitempty"`
|
|
|
|
LicenseFamily string `json:"license_family,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileMetadata represents the metadata of a Conda package file
|
|
|
|
type FileMetadata struct {
|
|
|
|
IsCondaPackage bool `json:"is_conda"`
|
|
|
|
Architecture string `json:"architecture,omitempty"`
|
|
|
|
NoArch string `json:"noarch,omitempty"`
|
|
|
|
Build string `json:"build,omitempty"`
|
|
|
|
BuildNumber int64 `json:"build_number,omitempty"`
|
|
|
|
Dependencies []string `json:"dependencies,omitempty"`
|
|
|
|
Platform string `json:"platform,omitempty"`
|
|
|
|
Timestamp int64 `json:"timestamp,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type index struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Architecture string `json:"arch"`
|
|
|
|
NoArch string `json:"noarch"`
|
|
|
|
Build string `json:"build"`
|
|
|
|
BuildNumber int64 `json:"build_number"`
|
|
|
|
Dependencies []string `json:"depends"`
|
|
|
|
License string `json:"license"`
|
|
|
|
LicenseFamily string `json:"license_family"`
|
|
|
|
Platform string `json:"platform"`
|
|
|
|
Subdir string `json:"subdir"`
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type about struct {
|
|
|
|
Description string `json:"description"`
|
|
|
|
Summary string `json:"summary"`
|
|
|
|
ProjectURL string `json:"home"`
|
|
|
|
RepositoryURL string `json:"dev_url"`
|
|
|
|
DocumentationURL string `json:"doc_url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReaderAndReaderAt interface {
|
|
|
|
io.Reader
|
|
|
|
io.ReaderAt
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParsePackageBZ2 parses the Conda package file compressed with bzip2
|
|
|
|
func ParsePackageBZ2(r io.Reader) (*Package, error) {
|
|
|
|
gzr := bzip2.NewReader(r)
|
|
|
|
|
|
|
|
return parsePackageTar(gzr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParsePackageConda parses the Conda package file compressed with zip and zstd
|
|
|
|
func ParsePackageConda(r io.ReaderAt, size int64) (*Package, error) {
|
|
|
|
zr, err := zip.NewReader(r, size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range zr.File {
|
|
|
|
if strings.HasPrefix(file.Name, "info-") && strings.HasSuffix(file.Name, ".tar.zst") {
|
|
|
|
f, err := zr.Open(file.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
dec, err := zstd.NewReader(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer dec.Close()
|
|
|
|
|
|
|
|
p, err := parsePackageTar(dec)
|
|
|
|
if p != nil {
|
|
|
|
p.FileMetadata.IsCondaPackage = true
|
|
|
|
}
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrInvalidStructure
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePackageTar(r io.Reader) (*Package, error) {
|
|
|
|
var i *index
|
|
|
|
var a *about
|
|
|
|
|
|
|
|
tr := tar.NewReader(r)
|
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hdr.Typeflag != tar.TypeReg {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if hdr.Name == "info/index.json" {
|
|
|
|
if err := json.NewDecoder(tr).Decode(&i); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkName(i.Name) {
|
|
|
|
return nil, ErrInvalidName
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkVersion(i.Version) {
|
|
|
|
return nil, ErrInvalidVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
if a != nil {
|
|
|
|
break // stop loop if both files were found
|
|
|
|
}
|
|
|
|
} else if hdr.Name == "info/about.json" {
|
|
|
|
if err := json.NewDecoder(tr).Decode(&a); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validation.IsValidURL(a.ProjectURL) {
|
|
|
|
a.ProjectURL = ""
|
|
|
|
}
|
|
|
|
if !validation.IsValidURL(a.RepositoryURL) {
|
|
|
|
a.RepositoryURL = ""
|
|
|
|
}
|
|
|
|
if !validation.IsValidURL(a.DocumentationURL) {
|
|
|
|
a.DocumentationURL = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if i != nil {
|
|
|
|
break // stop loop if both files were found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == nil {
|
|
|
|
return nil, ErrInvalidStructure
|
|
|
|
}
|
|
|
|
if a == nil {
|
|
|
|
a = &about{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Package{
|
|
|
|
Name: i.Name,
|
|
|
|
Version: i.Version,
|
|
|
|
Subdir: i.Subdir,
|
|
|
|
VersionMetadata: &VersionMetadata{
|
|
|
|
License: i.License,
|
|
|
|
LicenseFamily: i.LicenseFamily,
|
|
|
|
Description: a.Description,
|
|
|
|
Summary: a.Summary,
|
|
|
|
ProjectURL: a.ProjectURL,
|
|
|
|
RepositoryURL: a.RepositoryURL,
|
|
|
|
DocumentationURL: a.DocumentationURL,
|
|
|
|
},
|
|
|
|
FileMetadata: &FileMetadata{
|
|
|
|
Architecture: i.Architecture,
|
|
|
|
NoArch: i.NoArch,
|
|
|
|
Build: i.Build,
|
|
|
|
BuildNumber: i.BuildNumber,
|
|
|
|
Dependencies: i.Dependencies,
|
|
|
|
Platform: i.Platform,
|
|
|
|
Timestamp: i.Timestamp,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/conda/conda-build/blob/db9a728a9e4e6cfc895637ca3221117970fc2663/conda_build/metadata.py#L1393
|
|
|
|
func checkName(name string) bool {
|
|
|
|
if name == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if name != strings.ToLower(name) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !checkBadCharacters(name, "!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/conda/conda-build/blob/db9a728a9e4e6cfc895637ca3221117970fc2663/conda_build/metadata.py#L1403
|
|
|
|
func checkVersion(version string) bool {
|
|
|
|
if version == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !checkBadCharacters(version, "-")
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkBadCharacters(s, additional string) bool {
|
|
|
|
if strings.ContainsAny(s, "=@#$%^&*:;\"'\\|<>?/ ") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return strings.ContainsAny(s, additional)
|
|
|
|
}
|