@@ -3,6 +3,7 @@ package archiver
33import (
44 "archive/tar"
55 "backmeup/config"
6+ "errors"
67 "fmt"
78 "github.com/cheggaaa/pb/v3"
89 "github.com/klauspost/compress/gzip"
@@ -108,56 +109,68 @@ func writeZip(archiveFile *os.File, filesToBackup []BackupFileMetadata) {
108109}
109110
110111func addFileToTar (tw * tar.Writer , path string , pathInArchive string ) error {
111- if stat , err := os .Lstat (path ); err == nil {
112- var linkTarget string
113- // Check if file is symlink
114- if stat .Mode ()& os .ModeSymlink != 0 {
115- var err error
116- linkTarget , err = os .Readlink (path )
117- if err != nil {
118- return fmt .Errorf ("%s: readlink: %v" , stat .Name (), err )
112+ stat , statErr := os .Lstat (path )
113+ if statErr != nil {
114+ return statErr
115+ }
116+ var linkTarget string
117+ // Check if file is symlink
118+ if stat .Mode ()& os .ModeSymlink != 0 {
119+ var err error
120+ linkTarget , err = os .Readlink (path )
121+ if err != nil {
122+ return fmt .Errorf ("%s: readlink: %v" , stat .Name (), err )
123+ }
124+
125+ // In case the user wants to follow symlinks we eval the symlink target
126+ if currentUnitConfig .FollowSymlinks {
127+ linkTargetPath , evalSymlinkErr := filepath .EvalSymlinks (path )
128+ if evalSymlinkErr != nil {
129+ return evalSymlinkErr
119130 }
120131
121- // In case the user wants to follow symlinks we eval the symlink target
122- if currentUnitConfig .FollowSymlinks {
123- if linkTargetPath , err := filepath .EvalSymlinks (path ); err == nil {
124- if linkTargetInfo , statErr := os .Stat (linkTargetPath ); statErr == nil {
125- if linkTargetInfo .Mode ().IsRegular () {
126- // If file is regular, we can simply replace the symlink with the actual file
127- path = linkTargetPath
128- linkTarget = ""
129- stat = linkTargetInfo
130- }
131- }
132- }
132+ linkTargetInfo , linkTargetStatErr := os .Stat (linkTargetPath )
133+ if linkTargetStatErr != nil {
134+ log .Printf ("Can't access link target!" )
135+ return linkTargetStatErr
133136 }
134- }
135137
136- file , err := os .Open (path )
137- if err != nil {
138- return err
138+ if linkTargetInfo .Mode ().IsRegular () {
139+ // If file is regular, we can simply replace the symlink with the actual file
140+ path = linkTargetPath
141+ linkTarget = ""
142+ stat = linkTargetInfo
143+ } else {
144+ log .Printf ("Can't access link target. File is not regular!" )
145+ return errors .New ("file is not regular" )
146+ }
139147 }
140- defer file . Close ()
148+ }
141149
142- // now lets create the header as needed for this file within the tarball
143- header , err := tar .FileInfoHeader (stat , filepath .ToSlash (linkTarget ))
144- if err != nil {
145- return nil
146- }
147- header .Name = pathInArchive
150+ file , err := os .Open (path )
151+ if err != nil {
152+ return err
153+ }
154+ defer file .Close ()
148155
149- // write the header to the tarball archiver
150- if err := tw .WriteHeader (header ); err != nil {
151- return err
152- }
156+ // now lets create the header as needed for this file within the tarball
157+ header , err := tar .FileInfoHeader (stat , filepath .ToSlash (linkTarget ))
158+ if err != nil {
159+ return err
160+ }
161+ header .Name = pathInArchive
153162
154- // Check for regular files
155- if stat .Mode ().IsRegular () {
156- // copy the file data to the tarball
157- _ , err := io .Copy (tw , file )
158- if err != nil {
159- return fmt .Errorf ("%s: copying contents: %w" , file .Name (), err )
160- }
163+ // write the header to the tarball archiver
164+ if err := tw .WriteHeader (header ); err != nil {
165+ return err
166+ }
167+
168+ // Check for regular files
169+ if stat .Mode ().IsRegular () {
170+ // copy the file data to the tarball
171+ _ , err := io .Copy (tw , file )
172+ if err != nil {
173+ return fmt .Errorf ("%s: copying contents: %w" , file .Name (), err )
161174 }
162175 }
163176
0 commit comments