@@ -26,35 +26,37 @@ func Unarchive(tarball, dst string) error {
2626 }
2727 defer archiveFile .Close ()
2828
29- format , input , identifyErr := archives .Identify (context .Background (), tarball , archiveFile )
30- if identifyErr != nil {
31- return fmt .Errorf ("identify format: %w" , identifyErr )
29+ format , input , err := archives .Identify (context .Background (), tarball , archiveFile )
30+ if err != nil {
31+ return fmt .Errorf ("identify format: %w" , err )
3232 }
3333
3434 extractor , ok := format .(archives.Extractor )
3535 if ! ok {
3636 return fmt .Errorf ("unsupported format for extraction" )
3737 }
3838
39- if dirErr := createDirWithPermissions (dst , dirPermissions ); dirErr != nil {
40- return fmt .Errorf ("creating destination directory: %w" , dirErr )
39+ if err := createDirWithPermissions (dst , dirPermissions ); err != nil {
40+ return fmt .Errorf ("creating destination directory: %w" , err )
4141 }
4242
4343 handler := func (ctx context.Context , f archives.FileInfo ) error {
4444 return handleFile (f , dst )
4545 }
4646
47- if extractErr := extractor .Extract (context .Background (), input , handler ); extractErr != nil {
48- return fmt .Errorf ("extracting files: %w" , extractErr )
47+ if err := extractor .Extract (context .Background (), input , handler ); err != nil {
48+ return fmt .Errorf ("extracting files: %w" , err )
4949 }
5050
5151 return nil
5252}
5353
5454func createDirWithPermissions (path string , mode os.FileMode ) error {
55- if err := os .MkdirAll (path , mode ); err != nil {
55+ err := os .MkdirAll (path , mode )
56+ if err != nil {
5657 return fmt .Errorf ("mkdir: %w" , err )
5758 }
59+
5860 return nil
5961}
6062
@@ -68,6 +70,7 @@ func securePath(basePath, relativePath string) (string, error) {
6870 if ! strings .HasPrefix (filepath .Clean (dstPath )+ string (os .PathSeparator ), filepath .Clean (basePath )+ string (os .PathSeparator )) {
6971 return "" , fmt .Errorf ("illegal file path: %s" , dstPath )
7072 }
73+
7174 return dstPath , nil
7275}
7376
@@ -80,16 +83,20 @@ func handleFile(f archives.FileInfo, dst string) error {
8083
8184 // Ensure the parent directory exists
8285 parentDir := filepath .Dir (dstPath )
83- if dirErr := createDirWithPermissions (parentDir , dirPermissions ); dirErr != nil {
86+
87+ dirErr := createDirWithPermissions (parentDir , dirPermissions )
88+ if dirErr != nil {
8489 return dirErr
8590 }
8691
8792 // Handle directories
8893 if f .IsDir () {
8994 // Create the directory with permissions from the archive
90- if dirErr := createDirWithPermissions (dstPath , f .Mode ()); dirErr != nil {
95+ dirErr := createDirWithPermissions (dstPath , f .Mode ())
96+ if dirErr != nil {
9197 return fmt .Errorf ("creating directory: %w" , dirErr )
9298 }
99+
93100 return nil
94101 }
95102
@@ -99,7 +106,9 @@ func handleFile(f archives.FileInfo, dst string) error {
99106 logrus .Warnf ("Ignoring symlink: %s -> %s" , f .NameInArchive , f .LinkTarget )
100107 return nil
101108 }
102- if linkErr := os .Symlink (f .LinkTarget , dstPath ); linkErr != nil {
109+
110+ linkErr := os .Symlink (f .LinkTarget , dstPath )
111+ if linkErr != nil {
103112 return fmt .Errorf ("creating symlink: %w" , linkErr )
104113 }
105114 }
@@ -112,12 +121,15 @@ func handleFile(f archives.FileInfo, dst string) error {
112121
113122 // If parent directory is read-only, temporarily make it writable
114123 if originalMode .Mode ().Perm ()& 0o200 == 0 {
115- if chmodErr := os .Chmod (parentDir , originalMode .Mode ()| 0o200 ); chmodErr != nil {
124+ chmodErr := os .Chmod (parentDir , originalMode .Mode ()| 0o200 )
125+ if chmodErr != nil {
116126 return fmt .Errorf ("chmod parent directory: %w" , chmodErr )
117127 }
128+
118129 defer func () {
119130 // Restore the original permissions after writing
120- if chmodErr := os .Chmod (parentDir , originalMode .Mode ()); chmodErr != nil {
131+ chmodErr := os .Chmod (parentDir , originalMode .Mode ())
132+ if chmodErr != nil {
121133 logrus .Warnf ("Failed to restore original permissions for %s: %v" , parentDir , chmodErr )
122134 }
123135 }()
0 commit comments