File tree Expand file tree Collapse file tree 2 files changed +39
-8
lines changed Expand file tree Collapse file tree 2 files changed +39
-8
lines changed Original file line number Diff line number Diff line change 6
6
"fmt"
7
7
"io"
8
8
"os"
9
+ "path"
9
10
"path/filepath"
10
11
"strings"
11
12
"time"
@@ -392,15 +393,18 @@ func isWriteOnly(flag int) bool {
392
393
return flag & os .O_WRONLY != 0
393
394
}
394
395
395
- func isInDir (dir , path string ) bool {
396
- p , err := filepath .Rel (dir , path )
397
- if err != nil {
398
- return false
399
- }
396
+ func isInDir (dir , other string ) bool {
397
+ dir = path .Clean (dir )
398
+ dir = toTrailingSlash (dir )
399
+ other = path .Clean (other )
400
+
401
+ return strings .HasPrefix (other , dir )
402
+ }
400
403
401
- if p == "." || p == ".." {
402
- return false
404
+ func toTrailingSlash (p string ) string {
405
+ if strings .HasSuffix (p , "/" ) {
406
+ return p
403
407
}
404
408
405
- return ! strings . HasPrefix ( p , "../" )
409
+ return p + "/"
406
410
}
Original file line number Diff line number Diff line change @@ -542,3 +542,30 @@ func (s *FilesystemSuite) TestRemoveAll(c *C) {
542
542
c .Assert (os .IsNotExist (err ), Equals , true , comment )
543
543
}
544
544
}
545
+
546
+ func (s * FilesystemSuite ) TestRemoveAllRelative (c * C ) {
547
+ fnames := []string {
548
+ "foo/1" ,
549
+ "foo/2" ,
550
+ "foo/bar/1" ,
551
+ "foo/bar/2" ,
552
+ "foo/bar/baz/1" ,
553
+ "foo/bar/baz/qux/1" ,
554
+ "foo/bar/baz/qux/2" ,
555
+ "foo/bar/baz/qux/3" ,
556
+ }
557
+
558
+ for _ , fname := range fnames {
559
+ f , err := s .Fs .Create (fname )
560
+ c .Assert (err , IsNil )
561
+ c .Assert (f .Close (), IsNil )
562
+ }
563
+
564
+ c .Assert (RemoveAll (s .Fs , "foo/bar/.." ), IsNil )
565
+
566
+ for _ , fname := range fnames {
567
+ _ , err := s .Fs .Stat (fname )
568
+ comment := Commentf ("not removed: %s %s" , fname , err )
569
+ c .Assert (os .IsNotExist (err ), Equals , true , comment )
570
+ }
571
+ }
You can’t perform that action at this time.
0 commit comments