@@ -16,7 +16,7 @@ import (
1616// possibly writing each file to a tar writer stream. By default StreamFile is used, which will result in all files
1717// being written. A custom writeFunc can be passed so that each file may be written, modified+written, or skipped
1818// depending on the custom logic.
19- func Stream (w io.Writer , dir , relativePath string , writeFunc func (f os.FileInfo , shardRelativePath , fullPath string , tw * tar.Writer ) error ) error {
19+ func Stream (w io.Writer , dir , relativePath string , bufSize uint64 , writeFunc func (f os.FileInfo , shardRelativePath , fullPath string , tw * tar.Writer , bufSize uint64 ) error ) error {
2020 tw := tar .NewWriter (w )
2121 defer tw .Close ()
2222
@@ -41,30 +41,31 @@ func Stream(w io.Writer, dir, relativePath string, writeFunc func(f os.FileInfo,
4141 return err
4242 }
4343
44- return writeFunc (f , filepath .Join (relativePath , subDir ), path , tw )
44+ return writeFunc (f , filepath .Join (relativePath , subDir ), path , tw , bufSize )
4545 })
4646}
4747
4848// Generates a filtering function for Stream that checks an incoming file, and only writes the file to the stream if
4949// its mod time is later than since. Example: to tar only files newer than a certain datetime, use
5050// tar.Stream(w, dir, relativePath, SinceFilterTarFile(datetime))
51- func SinceFilterTarFile (since time.Time ) func (f os.FileInfo , shardRelativePath , fullPath string , tw * tar.Writer ) error {
52- return func (f os.FileInfo , shardRelativePath , fullPath string , tw * tar.Writer ) error {
51+ func SinceFilterTarFile (since time.Time ) func (f os.FileInfo , shardRelativePath , fullPath string , tw * tar.Writer , bufSize uint64 ) error {
52+ return func (f os.FileInfo , shardRelativePath , fullPath string , tw * tar.Writer , bufSize uint64 ) error {
5353 if f .ModTime ().After (since ) {
54- return StreamFile (f , shardRelativePath , fullPath , tw )
54+ return StreamFile (f , shardRelativePath , fullPath , tw , bufSize )
5555 }
5656 return nil
5757 }
5858}
5959
6060// stream a single file to tw, extending the header name using the shardRelativePath
61- func StreamFile (f os.FileInfo , shardRelativePath , fullPath string , tw * tar.Writer ) error {
62- return StreamRenameFile (f , f .Name (), shardRelativePath , fullPath , tw )
61+ func StreamFile (f os.FileInfo , shardRelativePath , fullPath string , tw * tar.Writer , bufSize uint64 ) error {
62+ return StreamRenameFile (f , f .Name (), shardRelativePath , fullPath , tw , bufSize )
6363}
6464
6565// / Stream a single file to tw, using tarHeaderFileName instead of the actual filename
6666// e.g., when we want to write a *.tmp file using the original file's non-tmp name.
67- func StreamRenameFile (f os.FileInfo , tarHeaderFileName , relativePath , fullPath string , tw * tar.Writer ) error {
67+ func StreamRenameFile (f os.FileInfo , tarHeaderFileName , relativePath , fullPath string , tw * tar.Writer , bufSize uint64 ) error {
68+ buf := make ([]byte , bufSize )
6869 h , err := tar .FileInfoHeader (f , f .Name ())
6970 if err != nil {
7071 return err
@@ -86,7 +87,7 @@ func StreamRenameFile(f os.FileInfo, tarHeaderFileName, relativePath, fullPath s
8687
8788 defer fr .Close ()
8889
89- _ , err = io .CopyN (tw , fr , h . Size )
90+ _ , err = io .CopyBuffer (tw , fr , buf )
9091
9192 return err
9293}
0 commit comments