|
1 | 1 | package compression |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "compress/gzip" |
5 | 6 | "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
6 | 9 | "io" |
| 10 | + "os/exec" |
7 | 11 |
|
8 | 12 | "github.com/containerd/containerd/v2/core/content" |
9 | 13 | "github.com/containerd/containerd/v2/core/images" |
@@ -64,3 +68,104 @@ func gzipWriter(comp Config) func(io.Writer) (io.WriteCloser, error) { |
64 | 68 | return gzip.NewWriterLevel(dest, level) |
65 | 69 | } |
66 | 70 | } |
| 71 | + |
| 72 | +func (c igzipType) Compress(ctx context.Context, comp Config) (compressorFunc Compressor, finalize Finalizer) { |
| 73 | + return func(dest io.Writer, _ string) (io.WriteCloser, error) { |
| 74 | + return gzipCmdWriter(ctx, "igzip", comp)(dest) |
| 75 | + }, nil |
| 76 | +} |
| 77 | + |
| 78 | +func (c igzipType) Decompress(ctx context.Context, cs content.Store, desc ocispecs.Descriptor) (io.ReadCloser, error) { |
| 79 | + return decompress(ctx, cs, desc) |
| 80 | +} |
| 81 | + |
| 82 | +func (c igzipType) NeedsConversion(ctx context.Context, cs content.Store, desc ocispecs.Descriptor) (bool, error) { |
| 83 | + return gzipType{}.NeedsConversion(ctx, cs, desc) |
| 84 | +} |
| 85 | + |
| 86 | +func (c igzipType) NeedsComputeDiffBySelf(comp Config) bool { |
| 87 | + return gzipType{}.NeedsComputeDiffBySelf(comp) |
| 88 | +} |
| 89 | + |
| 90 | +func (c igzipType) OnlySupportOCITypes() bool { |
| 91 | + return gzipType{}.OnlySupportOCITypes() |
| 92 | +} |
| 93 | + |
| 94 | +func (c igzipType) MediaType() string { |
| 95 | + return gzipType{}.MediaType() |
| 96 | +} |
| 97 | + |
| 98 | +func (c igzipType) String() string { |
| 99 | + return "igzip" |
| 100 | +} |
| 101 | + |
| 102 | +func (c pigzType) Compress(ctx context.Context, comp Config) (compressorFunc Compressor, finalize Finalizer) { |
| 103 | + return func(dest io.Writer, _ string) (io.WriteCloser, error) { |
| 104 | + return gzipCmdWriter(ctx, "pigz", comp)(dest) |
| 105 | + }, nil |
| 106 | +} |
| 107 | + |
| 108 | +func (c pigzType) Decompress(ctx context.Context, cs content.Store, desc ocispecs.Descriptor) (io.ReadCloser, error) { |
| 109 | + return decompress(ctx, cs, desc) |
| 110 | +} |
| 111 | + |
| 112 | +func (c pigzType) NeedsConversion(ctx context.Context, cs content.Store, desc ocispecs.Descriptor) (bool, error) { |
| 113 | + return gzipType{}.NeedsConversion(ctx, cs, desc) |
| 114 | +} |
| 115 | + |
| 116 | +func (c pigzType) NeedsComputeDiffBySelf(comp Config) bool { |
| 117 | + return gzipType{}.NeedsComputeDiffBySelf(comp) |
| 118 | +} |
| 119 | + |
| 120 | +func (c pigzType) OnlySupportOCITypes() bool { |
| 121 | + return gzipType{}.OnlySupportOCITypes() |
| 122 | +} |
| 123 | + |
| 124 | +func (c pigzType) MediaType() string { |
| 125 | + return gzipType{}.MediaType() |
| 126 | +} |
| 127 | + |
| 128 | +func (c pigzType) String() string { |
| 129 | + return "pigz" |
| 130 | +} |
| 131 | + |
| 132 | +type writeCloserWrapper struct { |
| 133 | + io.Writer |
| 134 | + closer func() error |
| 135 | +} |
| 136 | + |
| 137 | +func (w *writeCloserWrapper) Close() error { |
| 138 | + return w.closer() |
| 139 | +} |
| 140 | + |
| 141 | +func gzipCmdWriter(ctx context.Context, cmd string, comp Config) func(io.Writer) (io.WriteCloser, error) { |
| 142 | + return func(dest io.Writer) (io.WriteCloser, error) { |
| 143 | + reader, writer := io.Pipe() |
| 144 | + args := []string{"-c"} |
| 145 | + if comp.Level != nil { |
| 146 | + args = append(args, fmt.Sprintf("-%d", *comp.Level)) |
| 147 | + } |
| 148 | + command := exec.CommandContext(ctx, cmd, args...) |
| 149 | + command.Stdin = reader |
| 150 | + command.Stdout = dest |
| 151 | + |
| 152 | + var errBuf bytes.Buffer |
| 153 | + command.Stderr = &errBuf |
| 154 | + |
| 155 | + if err := command.Start(); err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + return &writeCloserWrapper{ |
| 160 | + Writer: writer, |
| 161 | + closer: func() error { |
| 162 | + closeErr := writer.Close() |
| 163 | + waitErr := command.Wait() |
| 164 | + if waitErr != nil { |
| 165 | + return fmt.Errorf("%s: %s", waitErr, errBuf.String()) |
| 166 | + } |
| 167 | + return errors.Join(closeErr, waitErr) |
| 168 | + }, |
| 169 | + }, nil |
| 170 | + } |
| 171 | +} |
0 commit comments