@@ -13,7 +13,9 @@ import (
13
13
"strconv"
14
14
"strings"
15
15
16
+ "github.com/IceWhaleTech/CasaOS-Common/utils/logger"
16
17
"github.com/mholt/archiver/v3"
18
+ "go.uber.org/zap"
17
19
)
18
20
19
21
// GetSize get the file size
@@ -400,6 +402,32 @@ func GetCompressionAlgorithm(t string) (string, archiver.Writer, error) {
400
402
return "" , nil , errors .New ("format not implemented" )
401
403
}
402
404
}
405
+ func IsBrokenSymlink (path string ) (bool , error ) {
406
+ info , err := os .Lstat (path )
407
+ if err != nil {
408
+ return false , fmt .Errorf ("error getting file info: %w" , err )
409
+ }
410
+
411
+ // file is not a symlink
412
+ if info .Mode ()& os .ModeSymlink == 0 {
413
+ return false , nil
414
+ }
415
+
416
+ target , err := os .Readlink (path )
417
+ if err != nil {
418
+ return false , fmt .Errorf ("error reading symlink: %w" , err )
419
+ }
420
+
421
+ _ , err = os .Stat (target )
422
+ if os .IsNotExist (err ) {
423
+ return true , nil
424
+ }
425
+ if err != nil {
426
+ return false , fmt .Errorf ("error checking target: %w" , err )
427
+ }
428
+
429
+ return false , nil
430
+ }
403
431
404
432
func AddFile (ar archiver.Writer , path , commonPath string ) error {
405
433
info , err := os .Stat (path )
@@ -441,7 +469,17 @@ func AddFile(ar archiver.Writer, path, commonPath string) error {
441
469
}
442
470
443
471
for _ , name := range names {
444
- err = AddFile (ar , filepath .Join (path , name ), commonPath )
472
+ filePath := filepath .Join (path , name )
473
+ isBroken , err := IsBrokenSymlink (filePath )
474
+ if err != nil {
475
+ logger .Error ("Failed to check symlink" , zap .Any ("name" , filePath ), zap .Error (err ))
476
+ continue
477
+ }
478
+ if isBroken {
479
+ continue
480
+ }
481
+
482
+ err = AddFile (ar , filePath , commonPath )
445
483
if err != nil {
446
484
return err
447
485
}
0 commit comments