Skip to content

Commit f6cfa94

Browse files
aledbfroboquat
authored andcommitted
Refactor backup creation
1 parent 857f98c commit f6cfa94

File tree

4 files changed

+22
-377
lines changed

4 files changed

+22
-377
lines changed

components/content-service/pkg/archive/tar.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ func ExtractTarbal(ctx context.Context, src io.Reader, dst string, opts ...TarOp
118118
if err != nil {
119119
return xerrors.Errorf("tar %s: %s", dst, err.Error()+";"+string(msg))
120120
}
121+
122+
log.WithField("log", string(msg)).Debug("decompressing tar stream log")
123+
121124
<-finished
122125

123126
// lets create a sorted list of pathes and chown depth first.

components/ws-daemon/pkg/content/archive.go

Lines changed: 19 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@
55
package content
66

77
import (
8-
"archive/tar"
98
"context"
109
"io"
1110
"os"
1211

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"
1514
"github.com/opentracing/opentracing-go"
1615
"golang.org/x/xerrors"
1716

17+
"github.com/gitpod-io/gitpod/common-go/log"
1818
"github.com/gitpod-io/gitpod/common-go/tracing"
1919
carchive "github.com/gitpod-io/gitpod/content-service/pkg/archive"
2020
)
2121

22-
// ConvertWhiteout converts whiteout files from the archive
23-
type ConvertWhiteout func(*tar.Header, string) (bool, error)
24-
2522
// BuildTarbal creates an OCI compatible tar file dst from the folder src, expecting the overlay whiteout format
2623
func BuildTarbal(ctx context.Context, src string, dst string, fullWorkspaceBackup bool, opts ...carchive.TarOption) (err error) {
2724
var cfg carchive.TarConfig
@@ -39,6 +36,10 @@ func BuildTarbal(ctx context.Context, src string, dst string, fullWorkspaceBacku
3936
return xerrors.Errorf("Unable to tar files: %v", err.Error())
4037
}
4138

39+
if fullWorkspaceBackup {
40+
log.Warn("Full workspace backup is disabled.")
41+
}
42+
4243
uidMaps := make([]idtools.IDMap, len(cfg.UIDMaps))
4344
for i, m := range cfg.UIDMaps {
4445
uidMaps[i] = idtools.IDMap{
@@ -56,86 +57,25 @@ func BuildTarbal(ctx context.Context, src string, dst string, fullWorkspaceBacku
5657
}
5758
}
5859

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+
})
7465
if err != nil {
75-
return xerrors.Errorf("cannot create tar: %w", err)
66+
return
7667
}
68+
defer tarReader.Close()
7769

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)
7971
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())
8173
}
8274

83-
defer fout.Close()
84-
85-
_, err = io.Copy(fout, tarout)
75+
_, err = io.Copy(tarFile, tarReader)
8676
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())
9178
}
9279

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
14181
}

components/ws-daemon/pkg/content/archive_test.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)