@@ -23,12 +23,15 @@ import (
2323 "os"
2424 "path/filepath"
2525 "strings"
26+
27+ "github.com/sirupsen/logrus"
2628)
2729
2830// Tar creates a tar archive of the specified path (file or directory)
2931// and returns the content as a stream. For individual files, it preserves
3032// the directory structure relative to the working directory.
3133func Tar (srcPath string , workDir string ) (io.Reader , error ) {
34+ logrus .Infof ("Taring %s, work dir: %s" , srcPath , workDir )
3235 pr , pw := io .Pipe ()
3336
3437 go func () {
@@ -38,6 +41,7 @@ func Tar(srcPath string, workDir string) (io.Reader, error) {
3841
3942 info , err := os .Stat (srcPath )
4043 if err != nil {
44+ logrus .Errorf ("failed to stat source path: %v" , err )
4145 pw .CloseWithError (fmt .Errorf ("failed to stat source path: %w" , err ))
4246 return
4347 }
@@ -53,28 +57,33 @@ func Tar(srcPath string, workDir string) (io.Reader, error) {
5357 // Create a relative path for the tar file header.
5458 relPath , err := filepath .Rel (workDir , path )
5559 if err != nil {
60+ logrus .Errorf ("failed to get relative path: %v" , err )
5661 return fmt .Errorf ("failed to get relative path: %w" , err )
5762 }
5863
5964 header , err := tar .FileInfoHeader (info , "" )
6065 if err != nil {
66+ logrus .Errorf ("failed to create tar header: %v" , err )
6167 return fmt .Errorf ("failed to create tar header: %w" , err )
6268 }
6369
6470 // Set the header name to preserve directory structure.
6571 header .Name = relPath
6672 if err := tw .WriteHeader (header ); err != nil {
73+ logrus .Errorf ("failed to write header: %v" , err )
6774 return fmt .Errorf ("failed to write header: %w" , err )
6875 }
6976
7077 if ! info .IsDir () {
7178 file , err := os .Open (path )
7279 if err != nil {
80+ logrus .Errorf ("failed to open file %s: %v" , path , err )
7381 return fmt .Errorf ("failed to open file %s: %w" , path , err )
7482 }
7583 defer file .Close ()
7684
7785 if _ , err := io .Copy (tw , file ); err != nil {
86+ logrus .Errorf ("failed to write file %s to tar: %v" , path , err )
7887 return fmt .Errorf ("failed to write file %s to tar: %w" , path , err )
7988 }
8089 }
@@ -83,20 +92,23 @@ func Tar(srcPath string, workDir string) (io.Reader, error) {
8392 })
8493
8594 if err != nil {
95+ logrus .Errorf ("failed to walk directory: %v" , err )
8696 pw .CloseWithError (fmt .Errorf ("failed to walk directory: %w" , err ))
8797 return
8898 }
8999 } else {
90100 // For a single file, include the directory structure.
91101 file , err := os .Open (srcPath )
92102 if err != nil {
103+ logrus .Errorf ("failed to open file %s: %v" , srcPath , err )
93104 pw .CloseWithError (fmt .Errorf ("failed to open file: %w" , err ))
94105 return
95106 }
96107 defer file .Close ()
97108
98109 header , err := tar .FileInfoHeader (info , "" )
99110 if err != nil {
111+ logrus .Errorf ("failed to create tar header for file %s: %v" , srcPath , err )
100112 pw .CloseWithError (fmt .Errorf ("failed to create tar header: %w" , err ))
101113 return
102114 }
@@ -105,18 +117,21 @@ func Tar(srcPath string, workDir string) (io.Reader, error) {
105117 // This keeps the directory structure as part of the file path in the tar.
106118 relPath , err := filepath .Rel (workDir , srcPath )
107119 if err != nil {
120+ logrus .Errorf ("failed to get relative path for file %s: %v" , srcPath , err )
108121 pw .CloseWithError (fmt .Errorf ("failed to get relative path: %w" , err ))
109122 return
110123 }
111124
112125 // Use the relative path (including directories) as the header name.
113126 header .Name = relPath
114127 if err := tw .WriteHeader (header ); err != nil {
128+ logrus .Errorf ("failed to write header for file %s: %v" , srcPath , err )
115129 pw .CloseWithError (fmt .Errorf ("failed to write header: %w" , err ))
116130 return
117131 }
118132
119133 if _ , err := io .Copy (tw , file ); err != nil {
134+ logrus .Errorf ("failed to copy file to tar for file %s: %v" , srcPath , err )
120135 pw .CloseWithError (fmt .Errorf ("failed to copy file to tar: %w" , err ))
121136 return
122137 }
@@ -129,10 +144,12 @@ func Tar(srcPath string, workDir string) (io.Reader, error) {
129144// Untar extracts the contents of a tar archive from the provided reader
130145// to the specified destination path.
131146func Untar (reader io.Reader , destPath string ) error {
147+ logrus .Infof ("Untaring archive to %s" , destPath )
132148 tarReader := tar .NewReader (reader )
133149
134150 // Ensure destination directory exists.
135151 if err := os .MkdirAll (destPath , 0755 ); err != nil {
152+ logrus .Errorf ("failed to create destination directory: %v" , err )
136153 return fmt .Errorf ("failed to create destination directory: %w" , err )
137154 }
138155
@@ -142,12 +159,14 @@ func Untar(reader io.Reader, destPath string) error {
142159 break
143160 }
144161 if err != nil {
162+ logrus .Errorf ("error reading tar: %v" , err )
145163 return fmt .Errorf ("error reading tar: %w" , err )
146164 }
147165
148166 // Sanitize file paths to prevent directory traversal.
149167 cleanPath := filepath .Clean (header .Name )
150168 if strings .Contains (cleanPath , ".." ) || strings .HasPrefix (cleanPath , "/" ) || strings .HasPrefix (cleanPath , ":\\ " ) {
169+ logrus .Errorf ("tar file contains invalid path: %s" , cleanPath )
151170 return fmt .Errorf ("tar file contains invalid path: %s" , cleanPath )
152171 }
153172
@@ -156,12 +175,14 @@ func Untar(reader io.Reader, destPath string) error {
156175 // Create directories for all path components.
157176 dirPath := filepath .Dir (targetPath )
158177 if err := os .MkdirAll (dirPath , 0755 ); err != nil {
178+ logrus .Errorf ("failed to create directory %s: %v" , dirPath , err )
159179 return fmt .Errorf ("failed to create directory %s: %w" , dirPath , err )
160180 }
161181
162182 switch header .Typeflag {
163183 case tar .TypeDir :
164184 if err := os .MkdirAll (targetPath , os .FileMode (header .Mode )); err != nil {
185+ logrus .Errorf ("failed to create directory %s: %v" , targetPath , err )
165186 return fmt .Errorf ("failed to create directory %s: %w" , targetPath , err )
166187 }
167188
@@ -172,21 +193,25 @@ func Untar(reader io.Reader, destPath string) error {
172193 os .FileMode (header .Mode ),
173194 )
174195 if err != nil {
196+ logrus .Errorf ("failed to create file %s: %v" , targetPath , err )
175197 return fmt .Errorf ("failed to create file %s: %w" , targetPath , err )
176198 }
177199
178200 if _ , err := io .Copy (file , tarReader ); err != nil {
179201 file .Close ()
202+ logrus .Errorf ("failed to write to file %s: %v" , targetPath , err )
180203 return fmt .Errorf ("failed to write to file %s: %w" , targetPath , err )
181204 }
182205 file .Close ()
183206
184207 case tar .TypeSymlink :
185208 if isRel (header .Linkname , destPath ) && isRel (header .Name , destPath ) {
186209 if err := os .Symlink (header .Linkname , targetPath ); err != nil {
210+ logrus .Errorf ("failed to create symlink %s -> %s: %v" , targetPath , header .Linkname , err )
187211 return fmt .Errorf ("failed to create symlink %s -> %s: %w" , targetPath , header .Linkname , err )
188212 }
189213 } else {
214+ logrus .Errorf ("symlink %s -> %s points outside of destination directory" , targetPath , header .Linkname )
190215 return fmt .Errorf ("symlink %s -> %s points outside of destination directory" , targetPath , header .Linkname )
191216 }
192217
0 commit comments