| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"reflect" |
| 9 |
|
| 10 |
"golang.org/x/sync/errgroup" |
| 11 |
) |
| 12 |
|
| 13 |
// UnTar implements the untar command, decoding a catar file and writing the |
| 14 |
// contained tree to a target directory. |
| 15 |
// |
| 16 |
// Filesystems implementing FilesystemFinalizer have Finalize() called on them |
| 17 |
// once the end of the archive is reached. That doesn't happen when UnTar |
| 18 |
// returns an error, so the caller should still close the filesystem to have |
| 19 |
// any deferred operations applied to what was extracted. |
| 20 |
func UnTar(ctx context.Context, r io.Reader, fs FilesystemWriter) error { |
| 21 |
dec := NewArchiveDecoder(r) |
| 22 |
loop: |
| 23 |
for { |
| 24 |
// See if we're meant to stop |
| 25 |
select { |
| 26 |
case <-ctx.Done(): |
| 27 |
return Interrupted{} |
| 28 |
default: |
| 29 |
} |
| 30 |
c, err := dec.Next() |
| 31 |
if err != nil { |
| 32 |
return err |
| 33 |
} |
| 34 |
switch n := c.(type) { |
| 35 |
case NodeDirectory: |
| 36 |
err = fs.CreateDir(n) |
| 37 |
case NodeFile: |
| 38 |
err = fs.CreateFile(n) |
| 39 |
case NodeDevice: |
| 40 |
err = fs.CreateDevice(n) |
| 41 |
case NodeSymlink: |
| 42 |
err = fs.CreateSymlink(n) |
| 43 |
case nil: |
| 44 |
break loop |
| 45 |
default: |
| 46 |
err = fmt.Errorf("unsupported type %s", reflect.TypeOf(c)) |
| 47 |
} |
| 48 |
if err != nil { |
| 49 |
return err |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Filesystems that defer directory metadata (permissions, timestamps) |
| 54 |
// until a directory has been fully populated need to be told that no |
| 55 |
// further entries are coming. |
| 56 |
if f, ok := fs.(FilesystemFinalizer); ok { |
| 57 |
return f.Finalize() |
| 58 |
} |
| 59 |
return nil |
| 60 |
} |
| 61 |
|
| 62 |
// UnTarIndex takes an index file (of a chunked catar), re-assembles the catar |
| 63 |
// and decodes it on-the-fly into the target directory 'dst'. Uses n goroutines |
| 64 |
// to retrieve and decompress the chunks. |
| 65 |
func UnTarIndex(ctx context.Context, fs FilesystemWriter, index Index, s Store, n int, pb ProgressBar) error { |
| 66 |
type requestJob struct { |
| 67 |
chunk IndexChunk // requested chunk |
| 68 |
data chan ([]byte) // channel for the (decompressed) chunk |
| 69 |
} |
| 70 |
var ( |
| 71 |
req = make(chan requestJob) |
| 72 |
assemble = make(chan chan []byte, n) |
| 73 |
) |
| 74 |
g, ctx := errgroup.WithContext(ctx) |
| 75 |
|
| 76 |
// Initialize and start progress bar if one was provided |
| 77 |
pb.SetTotal(len(index.Chunks)) |
| 78 |
pb.Start() |
| 79 |
defer pb.Finish() |
| 80 |
|
| 81 |
// Use a pipe as input to untar and write the chunks into that (in the right |
| 82 |
// order of course) |
| 83 |
r, w := io.Pipe() |
| 84 |
|
| 85 |
// Workers - getting chunks from the store |
| 86 |
for range n { |
| 87 |
g.Go(func() error { |
| 88 |
for r := range req { |
| 89 |
// Pull the chunk from the store |
| 90 |
chunk, err := s.GetChunk(r.chunk.ID) |
| 91 |
if err != nil { |
| 92 |
close(r.data) |
| 93 |
return err |
| 94 |
} |
| 95 |
b, err := chunk.Data() |
| 96 |
if err != nil { |
| 97 |
close(r.data) |
| 98 |
return err |
| 99 |
} |
| 100 |
// Might as well verify the chunk size while we're at it |
| 101 |
if r.chunk.Size != uint64(len(b)) { |
| 102 |
close(r.data) |
| 103 |
return fmt.Errorf("unexpected size for chunk %s", r.chunk.ID) |
| 104 |
} |
| 105 |
r.data <- b |
| 106 |
close(r.data) |
| 107 |
} |
| 108 |
return nil |
| 109 |
}) |
| 110 |
} |
| 111 |
|
| 112 |
// Feeder - requesting chunks from the workers and handing a result data channel |
| 113 |
// to the assembler |
| 114 |
g.Go(func() error { |
| 115 |
loop: |
| 116 |
for _, c := range index.Chunks { |
| 117 |
data := make(chan []byte, 1) |
| 118 |
select { |
| 119 |
case <-ctx.Done(): |
| 120 |
break loop |
| 121 |
case req <- requestJob{chunk: c, data: data}: // request the chunk |
| 122 |
select { |
| 123 |
case <-ctx.Done(): |
| 124 |
break loop |
| 125 |
case assemble <- data: // and hand over the data channel to the assembler |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
close(req) // tell the workers this is it |
| 130 |
close(assemble) // tell the assembler we're done |
| 131 |
return nil |
| 132 |
}) |
| 133 |
|
| 134 |
// Assembler - Read from data channels push the chunks into the pipe that untar reads from |
| 135 |
g.Go(func() error { |
| 136 |
defer w.Close() // No more chunks to come, stop the untar |
| 137 |
loop: |
| 138 |
for { |
| 139 |
select { |
| 140 |
case data := <-assemble: |
| 141 |
if data == nil { |
| 142 |
break loop |
| 143 |
} |
| 144 |
pb.Increment() |
| 145 |
b := <-data |
| 146 |
if _, err := io.Copy(w, bytes.NewReader(b)); err != nil { |
| 147 |
return err |
| 148 |
} |
| 149 |
case <-ctx.Done(): |
| 150 |
break loop |
| 151 |
} |
| 152 |
} |
| 153 |
return nil |
| 154 |
}) |
| 155 |
|
| 156 |
// UnTar - Read from the pipe that Assembler pushes into |
| 157 |
g.Go(func() error { |
| 158 |
err := UnTar(ctx, r, fs) |
| 159 |
if err != nil { |
| 160 |
// If an error has occurred during the UnTar, we need to stop the Assembler. |
| 161 |
// If we don't, then it would stall on writing to the pipe. |
| 162 |
r.CloseWithError(err) |
| 163 |
} |
| 164 |
return err |
| 165 |
}) |
| 166 |
|
| 167 |
return g.Wait() |
| 168 |
} |