5
5
package content
6
6
7
7
import (
8
- "archive/tar"
9
8
"context"
10
9
"io"
11
10
"os"
12
11
13
- "github.com/docker/docker /pkg/archive"
14
- "github.com/docker/docker /pkg/idtools"
12
+ "github.com/containers/storage /pkg/archive"
13
+ "github.com/containers/storage /pkg/idtools"
15
14
"github.com/opentracing/opentracing-go"
16
15
"golang.org/x/xerrors"
17
16
17
+ "github.com/gitpod-io/gitpod/common-go/log"
18
18
"github.com/gitpod-io/gitpod/common-go/tracing"
19
19
carchive "github.com/gitpod-io/gitpod/content-service/pkg/archive"
20
20
)
21
21
22
- // ConvertWhiteout converts whiteout files from the archive
23
- type ConvertWhiteout func (* tar.Header , string ) (bool , error )
24
-
25
22
// BuildTarbal creates an OCI compatible tar file dst from the folder src, expecting the overlay whiteout format
26
23
func BuildTarbal (ctx context.Context , src string , dst string , fullWorkspaceBackup bool , opts ... carchive.TarOption ) (err error ) {
27
24
var cfg carchive.TarConfig
@@ -39,6 +36,10 @@ func BuildTarbal(ctx context.Context, src string, dst string, fullWorkspaceBacku
39
36
return xerrors .Errorf ("Unable to tar files: %v" , err .Error ())
40
37
}
41
38
39
+ if fullWorkspaceBackup {
40
+ log .Warn ("Full workspace backup is disabled." )
41
+ }
42
+
42
43
uidMaps := make ([]idtools.IDMap , len (cfg .UIDMaps ))
43
44
for i , m := range cfg .UIDMaps {
44
45
uidMaps [i ] = idtools.IDMap {
@@ -56,86 +57,25 @@ func BuildTarbal(ctx context.Context, src string, dst string, fullWorkspaceBacku
56
57
}
57
58
}
58
59
59
- var tarout io.ReadCloser
60
- if fullWorkspaceBackup {
61
- tarout , err = archive .TarWithOptions (src , & archive.TarOptions {
62
- UIDMaps : uidMaps ,
63
- GIDMaps : gidMaps ,
64
- InUserNS : true ,
65
- WhiteoutFormat : archive .OverlayWhiteoutFormat ,
66
- })
67
- } else {
68
- tarout , err = TarWithOptions (src , & TarOptions {
69
- UIDMaps : uidMaps ,
70
- GIDMaps : gidMaps ,
71
- })
72
- }
73
-
60
+ tarReader , err := archive .TarWithOptions (src , & archive.TarOptions {
61
+ UIDMaps : uidMaps ,
62
+ GIDMaps : gidMaps ,
63
+ Compression : archive .Uncompressed ,
64
+ })
74
65
if err != nil {
75
- return xerrors . Errorf ( "cannot create tar: %w" , err )
66
+ return
76
67
}
68
+ defer tarReader .Close ()
77
69
78
- fout , err := os .OpenFile (dst , os .O_WRONLY | os .O_CREATE , 0744 )
70
+ tarFile , err := os .OpenFile (dst , os .O_CREATE | os .O_WRONLY , 0755 )
79
71
if err != nil {
80
- return cleanCorruptedTarballAndReturnError ( dst , xerrors .Errorf ("cannot open archive for writing : %w " , err ))
72
+ return xerrors .Errorf ("Unable to create tar file : %v " , err . Error ( ))
81
73
}
82
74
83
- defer fout .Close ()
84
-
85
- _ , err = io .Copy (fout , tarout )
75
+ _ , err = io .Copy (tarFile , tarReader )
86
76
if err != nil {
87
- return cleanCorruptedTarballAndReturnError (dst , xerrors .Errorf ("cannot write tar file: %w" , err ))
88
- }
89
- if err = fout .Sync (); err != nil {
90
- return cleanCorruptedTarballAndReturnError (dst , xerrors .Errorf ("cannot flush tar out stream: %w" , err ))
77
+ return xerrors .Errorf ("Unable create tar file: %v" , err .Error ())
91
78
}
92
79
93
- return nil
94
- }
95
-
96
- // ErrMaxSizeExceeded is emitted by LimitWriter when a write tries to write beyond the max number of bytes allowed
97
- var ErrMaxSizeExceeded = xerrors .Errorf ("maximum size exceeded" )
98
-
99
- // cleanCorruptedTarballAndReturnError cleans up the file located at path dst and returns the error err passed to it
100
- func cleanCorruptedTarballAndReturnError (dst string , err error ) error {
101
- os .Remove (dst )
102
- return err
103
- }
104
-
105
- // newLimitWriter wraps a writer such that a maximum of N bytes can be written. Once that limit is exceeded
106
- // the writer returns io.ErrClosedPipe
107
- func newLimitWriter (out io.Writer , maxSizeBytes int64 ) * limitWriter {
108
- return & limitWriter {
109
- MaxSizeBytes : maxSizeBytes ,
110
- Out : out ,
111
- }
112
- }
113
-
114
- type limitWriter struct {
115
- MaxSizeBytes int64
116
- Out io.Writer
117
- BytesWritten int64
118
-
119
- didMaxOut bool
120
- }
121
-
122
- func (s * limitWriter ) Write (b []byte ) (n int , err error ) {
123
- if s .MaxSizeBytes == 0 {
124
- return s .Out .Write (b )
125
- }
126
-
127
- bsize := int64 (len (b ))
128
- if bsize + s .BytesWritten > s .MaxSizeBytes {
129
- s .didMaxOut = true
130
- return 0 , ErrMaxSizeExceeded
131
- }
132
-
133
- n , err = s .Out .Write (b )
134
- s .BytesWritten += int64 (n )
135
-
136
- return n , err
137
- }
138
-
139
- func (s * limitWriter ) DidMaxOut () bool {
140
- return s .didMaxOut
80
+ return
141
81
}
0 commit comments