// ReplaceFileTarWrapper converts inputTarStream to a new tar stream. Files in the
// tar stream are modified if they match any of the keys in mods.
ReplaceFileTarWrapper converts inputTarStream to a new tar stream. Files in the
tar stream are modified if they match any of the keys in mods.
// ReplaceFileTarWrapper converts inputTarStream to a new tar stream . Files in the // tar stream are modified if they match any of the keys in mods .
func replaceFileTarWrapper(inputTarStream io.ReadCloser, mods map[string]TarModifierFunc) io.ReadCloser {
        pipeReader, pipeWriter := io.Pipe()
        go func() {
                tarReader := tar.NewReader(inputTarStream)
                tarWriter := tar.NewWriter(pipeWriter)
                defer inputTarStream.Close()
                defer tarWriter.Close()
                modify := func(name string, original *tar.Header, modifier TarModifierFunc, tarReader io.Reader) error {
                        header, data, err := modifier(name, original, tarReader)
                        switch {
                        case err != nil:
                                return err
                        case header == nil:
                                return nil
                        }
                        header.Name = name
                        header.Size = int64(len(data))
                        if err := tarWriter.WriteHeader(header); err != nil {
                                return err
                        }
                        if len(data) != 0 {
                                if _, err := tarWriter.Write(data); err != nil {
                                        return err
                                }
                        }
                        return nil
                }
                var err error
                var originalHeader *tar.Header
                for {
                        originalHeader, err = tarReader.Next()
                        if err == io.EOF {
                                break
                        }
                        if err != nil {
                                pipeWriter.CloseWithError(err)
                                return
                        }
                        modifier, ok := mods[originalHeader.Name]
                        if !ok {
                                if err := tarWriter.WriteHeader(originalHeader); err != nil {
                                        pipeWriter.CloseWithError(err)
                                        return
                                }
                                if _, err := Copy(tarWriter, tarReader); err != nil {
                                        pipeWriter.CloseWithError(err)
                                        return
                                }
                                continue
                        }
                        delete(mods, originalHeader.Name)
                        if err := modify(originalHeader.Name, originalHeader, modifier, tarReader); err != nil {
                                pipeWriter.CloseWithError(err)
                                return
                        }
                }
                for name, modifier := range mods {
                        if err := modify(name, nil, modifier, nil); err != nil {
                                pipeWriter.CloseWithError(err)
                                return
                        }
                }
                pipeWriter.Close()
        }()
        return pipeReader
}
func replaceFileTarWrapper ( inputTarStream io . ReadCloser , mods map [ string ] TarModifierFunc ) io . ReadCloser { pipeReader , pipeWriter := io . Pipe ( ) go func ( ) { tarReader := tar . NewReader ( inputTarStream ) tarWriter := tar . NewWriter ( pipeWriter ) defer inputTarStream . Close ( ) defer tarWriter . Close ( ) modify := func ( name string , original * tar . Header , modifier TarModifierFunc , tarReader io . Reader ) error { header , data , err := modifier ( name , original , tarReader ) switch { case err != nil : return err case header == nil : return nil } header . Name = name header . Size = int64 ( len ( data ) ) if err := tarWriter . WriteHeader ( header ) ; err != nil { return err } if len ( data ) != 0 { if _ , err := tarWriter . Write ( data ) ; err != nil { return err } } return nil } var err error var originalHeader * tar . Header for { originalHeader , err = tarReader . Next ( ) if err == io . EOF { break } if err != nil { pipeWriter . CloseWithError ( err ) return } modifier , ok := mods [ originalHeader . Name ] if ! ok { if err := tarWriter . WriteHeader ( originalHeader ) ; err != nil { pipeWriter . CloseWithError ( err ) return } if _ , err := Copy ( tarWriter , tarReader ) ; err != nil { pipeWriter . CloseWithError ( err ) return } continue } delete ( mods , originalHeader . Name ) if err := modify ( originalHeader . Name , originalHeader , modifier , tarReader ) ; err != nil { pipeWriter . CloseWithError ( err ) return } } for name , modifier := range mods { if err := modify ( name , nil , modifier , nil ) ; err != nil { pipeWriter . CloseWithError ( err ) return } } pipeWriter . Close ( ) } ( ) return pipeReader }
ReplaceFileTarWrapper converts inputTarStream to a new tar stream.
ReplaceFileTarWrapper converts inputTarStream to a new tar stream .
// No modifiers for this file, copy the header and data // Apply the modifiers that haven't matched any files in the archive