Skip to content

Commit 82e2a11

Browse files
authored
Merge pull request #211 from essentialkaos/develop
Version 2.0.0
2 parents b179ca0 + 507833b commit 82e2a11

44 files changed

Lines changed: 521 additions & 373 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<p align="center"><a href="#readme"><img src=".github/images/card.svg"/></a></p>
22

33
<p align="center">
4-
<a href="https://kaos.sh/g/npck"><img src=".github/images/godoc.svg"/></a>
4+
<a href="https://kaos.sh/g/npck.v2"><img src=".github/images/godoc.svg"/></a>
55
<a href="https://kaos.sh/y/npck"><img src="https://kaos.sh/y/fdbafdcb2caa4516afbd5feabebce511.svg" alt="Codacy" /></a>
66
<a href="https://kaos.sh/c/npck"><img src="https://kaos.sh/c/npck.svg" alt="Coverage Status" /></a>
77
<a href="https://kaos.sh/w/npck/ci"><img src="https://kaos.sh/w/npck/ci.svg" alt="GitHub Actions CI Status" /></a>
@@ -32,7 +32,7 @@ package main
3232

3333
import (
3434
"fmt"
35-
"github.com/essentialkaos/npck"
35+
"github.com/essentialkaos/npck/v2"
3636
)
3737

3838
func main() {

bz2/bz2.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,48 @@ package bz2
33

44
// ////////////////////////////////////////////////////////////////////////////////// //
55
// //
6-
// Copyright (c) 2025 ESSENTIAL KAOS //
6+
// Copyright (c) 2026 ESSENTIAL KAOS //
77
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
88
// //
99
// ////////////////////////////////////////////////////////////////////////////////// //
1010

1111
import (
1212
"bufio"
1313
"compress/bzip2"
14-
"fmt"
1514
"io"
1615
"os"
1716
"path/filepath"
1817
"strings"
1918

20-
"github.com/essentialkaos/npck/utils"
19+
"github.com/essentialkaos/npck/v2/utils"
2120
)
2221

2322
// ////////////////////////////////////////////////////////////////////////////////// //
2423

25-
// MaxReadLimit is the maximum read limit for decompression bomb
26-
// protection (default: 1GB)
27-
var MaxReadLimit int64 = 1024 * 1024 * 1024
24+
// DEFAULT_MAX_READ_LIMIT is default the maximum read limit (1GB)
25+
const DEFAULT_MAX_READ_LIMIT int64 = 1024 * 1024 * 1024
26+
27+
// ////////////////////////////////////////////////////////////////////////////////// //
28+
29+
// Options is reader options
30+
type Options struct {
31+
// MaxReadLimit is the maximum read limit for decompression bomb
32+
// protection (default: 1GB)
33+
MaxReadLimit int64
34+
}
2835

2936
// ////////////////////////////////////////////////////////////////////////////////// //
3037

3138
var (
32-
ErrNilReader = fmt.Errorf("Reader can not be nil")
33-
ErrEmptyInput = fmt.Errorf("Path to input file can not be empty")
34-
ErrEmptyOutput = fmt.Errorf("Path to output file can not be empty")
39+
ErrNilReader = utils.ErrNilReader
40+
ErrEmptyInput = utils.ErrEmptyInput
41+
ErrEmptyOutput = utils.ErrEmptyOutput
3542
)
3643

3744
// ////////////////////////////////////////////////////////////////////////////////// //
3845

39-
// Unpacks file to given directory
40-
func Unpack(file, dir string) error {
46+
// Unpack unpacks archive file to given directory
47+
func Unpack(file, dir string, options Options) error {
4148
switch {
4249
case file == "":
4350
return ErrEmptyInput
@@ -54,20 +61,20 @@ func Unpack(file, dir string) error {
5461
return err
5562
}
5663

57-
fd, err := os.OpenFile(file, os.O_RDONLY, 0)
64+
fd, err := os.Open(file)
5865

5966
if err != nil {
6067
return err
6168
}
6269

6370
defer fd.Close()
6471

65-
return Read(bufio.NewReader(fd), path)
72+
return Read(bufio.NewReader(fd), path, options)
6673
}
6774

6875
// Read reads compressed data using given reader and unpacks it to
6976
// the given directory
70-
func Read(r io.Reader, output string) error {
77+
func Read(r io.Reader, output string, options Options) error {
7178
switch {
7279
case r == nil:
7380
return ErrNilReader
@@ -81,11 +88,20 @@ func Read(r io.Reader, output string) error {
8188
return err
8289
}
8390

91+
defer fd.Close()
92+
93+
limit := options.MaxReadLimit
94+
95+
if limit == 0 {
96+
limit = DEFAULT_MAX_READ_LIMIT
97+
}
98+
8499
bw := bufio.NewWriter(fd)
85-
_, err = io.Copy(bw, io.LimitReader(bzip2.NewReader(r), MaxReadLimit))
100+
_, err = io.Copy(bw, io.LimitReader(bzip2.NewReader(r), limit))
86101

87-
bw.Flush()
88-
fd.Close()
102+
if err != nil {
103+
return err
104+
}
89105

90-
return err
106+
return bw.Flush()
91107
}

bz2/bz2_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package bz2
22

33
// ////////////////////////////////////////////////////////////////////////////////// //
44
// //
5-
// Copyright (c) 2025 ESSENTIAL KAOS //
5+
// Copyright (c) 2026 ESSENTIAL KAOS //
66
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
77
// //
88
// ////////////////////////////////////////////////////////////////////////////////// //
@@ -38,7 +38,7 @@ func (s *BZ2Suite) SetUpSuite(c *C) {
3838
}
3939

4040
func (s *BZ2Suite) TestUnpack(c *C) {
41-
err := Unpack("../.testdata/data.txt.bz2", s.Dir)
41+
err := Unpack("../.testdata/data.txt.bz2", s.Dir, Options{})
4242

4343
c.Assert(err, IsNil)
4444

@@ -49,24 +49,24 @@ func (s *BZ2Suite) TestUnpack(c *C) {
4949
}
5050

5151
func (s *BZ2Suite) TestErrors(c *C) {
52-
err := Unpack("", "/_unknown")
52+
err := Unpack("", "/_unknown", Options{})
5353
c.Assert(err, NotNil)
5454

55-
err = Unpack("../.testdata/data.txt.bz2", "")
55+
err = Unpack("../.testdata/data.txt.bz2", "", Options{})
5656
c.Assert(err, NotNil)
5757

58-
err = Unpack("/_unknown", s.Dir)
58+
err = Unpack("/_unknown", s.Dir, Options{})
5959
c.Assert(err, NotNil)
6060

61-
err = Unpack("/_unknown", "/root")
61+
err = Unpack("/_unknown", "/root", Options{})
6262
c.Assert(err, NotNil)
6363

64-
err = Read(nil, "/_unknown")
64+
err = Read(nil, "/_unknown", Options{})
6565
c.Assert(err, NotNil)
6666

67-
err = Read(strings.NewReader(""), "")
67+
err = Read(strings.NewReader(""), "", Options{})
6868
c.Assert(err, NotNil)
6969

70-
err = Read(strings.NewReader(""), "/_unknown")
70+
err = Read(strings.NewReader(""), "/_unknown", Options{})
7171
c.Assert(err, NotNil)
7272
}

bz2/example_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package bz2
22

33
// ////////////////////////////////////////////////////////////////////////////////// //
44
// //
5-
// Copyright (c) 2025 ESSENTIAL KAOS //
5+
// Copyright (c) 2026 ESSENTIAL KAOS //
66
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
77
// //
88
// ////////////////////////////////////////////////////////////////////////////////// //
@@ -16,7 +16,7 @@ import (
1616

1717
func ExampleUnpack() {
1818
file := "file.bz2"
19-
err := Unpack(file, "/home/bob/data")
19+
err := Unpack(file, "/home/bob/data", Options{})
2020

2121
if err != nil {
2222
fmt.Printf("Error: Can't unpack %s: %v\n", file, err)
@@ -28,14 +28,14 @@ func ExampleUnpack() {
2828

2929
func ExampleRead() {
3030
file := "file.bz2"
31-
fd, err := os.OpenFile(file, os.O_RDONLY, 0)
31+
fd, err := os.Open(file)
3232

3333
if err != nil {
3434
fmt.Printf("Error: Can't unpack %s: %v\n", file, err)
3535
return
3636
}
3737

38-
err = Read(fd, "/home/bob/data")
38+
err = Read(fd, "/home/bob/data", Options{MaxReadLimit: 15 * 1024 * 1024})
3939

4040
if err != nil {
4141
fmt.Printf("Error: Can't unpack %s: %v\n", file, err)

example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package npck
22

33
// ////////////////////////////////////////////////////////////////////////////////// //
44
// //
5-
// Copyright (c) 2025 ESSENTIAL KAOS //
5+
// Copyright (c) 2026 ESSENTIAL KAOS //
66
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
77
// //
88
// ////////////////////////////////////////////////////////////////////////////////// //

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
module github.com/essentialkaos/npck
1+
module github.com/essentialkaos/npck/v2
22

33
go 1.24.6
44

55
require (
66
github.com/essentialkaos/check v1.4.1
7-
github.com/essentialkaos/ek/v13 v13.38.3
8-
github.com/klauspost/compress v1.18.3
9-
github.com/pierrec/lz4/v4 v4.1.25
7+
github.com/essentialkaos/ek/v13 v13.38.7
8+
github.com/klauspost/compress v1.18.4
9+
github.com/pierrec/lz4/v4 v4.1.26
1010
github.com/ulikunitz/xz v0.5.15
1111
)
1212

1313
require (
1414
github.com/kr/pretty v0.3.1 // indirect
1515
github.com/kr/text v0.2.0 // indirect
1616
github.com/rogpeppe/go-internal v1.14.1 // indirect
17-
golang.org/x/sys v0.40.0 // indirect
17+
golang.org/x/sys v0.41.0 // indirect
1818
)

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
22
github.com/essentialkaos/check v1.4.1 h1:SuxXzrbokPGTPWxGRnzy0hXvtb44mtVrdNxgPa1s4c8=
33
github.com/essentialkaos/check v1.4.1/go.mod h1:xQOYwFvnxfVZyt5Qvjoa1SxcRqu5VyP77pgALr3iu+M=
4-
github.com/essentialkaos/ek/v13 v13.38.3 h1:gQVNC6RdSBBYFhmtN9QOCZbshm+ib0PO2b9O0C7JMWc=
5-
github.com/essentialkaos/ek/v13 v13.38.3/go.mod h1:qS5hOA6BaVYCS+nstm6l502Ehkb8i5PaN5nd40fbUg0=
6-
github.com/klauspost/compress v1.18.3 h1:9PJRvfbmTabkOX8moIpXPbMMbYN60bWImDDU7L+/6zw=
7-
github.com/klauspost/compress v1.18.3/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
4+
github.com/essentialkaos/ek/v13 v13.38.7 h1:Vl4hPfdXBnF1gxxrXTwsp65+jViqYQmoBd3WANrcD6g=
5+
github.com/essentialkaos/ek/v13 v13.38.7/go.mod h1:TgLsoUWMCtNR8+3Qn9S+ilbtE8DSWbjGwAODTbTBgQY=
6+
github.com/klauspost/compress v1.18.4 h1:RPhnKRAQ4Fh8zU2FY/6ZFDwTVTxgJ/EMydqSTzE9a2c=
7+
github.com/klauspost/compress v1.18.4/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
88
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
99
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
1010
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1111
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
12-
github.com/pierrec/lz4/v4 v4.1.25 h1:kocOqRffaIbU5djlIBr7Wh+cx82C0vtFb0fOurZHqD0=
13-
github.com/pierrec/lz4/v4 v4.1.25/go.mod h1:EoQMVJgeeEOMsCqCzqFm2O0cJvljX2nGZjcRIPL34O4=
12+
github.com/pierrec/lz4/v4 v4.1.26 h1:GrpZw1gZttORinvzBdXPUXATeqlJjqUG/D87TKMnhjY=
13+
github.com/pierrec/lz4/v4 v4.1.26/go.mod h1:EoQMVJgeeEOMsCqCzqFm2O0cJvljX2nGZjcRIPL34O4=
1414
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
1515
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
1616
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
1717
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
1818
github.com/ulikunitz/xz v0.5.15 h1:9DNdB5s+SgV3bQ2ApL10xRc35ck0DuIX/isZvIk+ubY=
1919
github.com/ulikunitz/xz v0.5.15/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
20-
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
21-
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
20+
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
21+
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=

gz/example_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package gz
22

33
// ////////////////////////////////////////////////////////////////////////////////// //
44
// //
5-
// Copyright (c) 2025 ESSENTIAL KAOS //
5+
// Copyright (c) 2026 ESSENTIAL KAOS //
66
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
77
// //
88
// ////////////////////////////////////////////////////////////////////////////////// //
@@ -16,7 +16,7 @@ import (
1616

1717
func ExampleUnpack() {
1818
file := "file.gz"
19-
err := Unpack(file, "/home/bob/data")
19+
err := Unpack(file, "/home/bob/data", Options{})
2020

2121
if err != nil {
2222
fmt.Printf("Error: Can't unpack %s: %v\n", file, err)
@@ -28,14 +28,14 @@ func ExampleUnpack() {
2828

2929
func ExampleRead() {
3030
file := "file.gz"
31-
fd, err := os.OpenFile(file, os.O_RDONLY, 0)
31+
fd, err := os.Open(file)
3232

3333
if err != nil {
3434
fmt.Printf("Error: Can't unpack %s: %v\n", file, err)
3535
return
3636
}
3737

38-
err = Read(fd, "/home/bob/data")
38+
err = Read(fd, "/home/bob/data", Options{MaxReadLimit: 15 * 1024 * 1024})
3939

4040
if err != nil {
4141
fmt.Printf("Error: Can't unpack %s: %v\n", file, err)

0 commit comments

Comments
 (0)