@@ -765,7 +765,7 @@ func (fsys *DeepFS) Open(name string) (fs.File, error) {
765
765
return nil , & fs.PathError {Op : "open" , Path : name , Err : fmt .Errorf ("%w: %s" , fs .ErrInvalid , name )}
766
766
}
767
767
name = path .Join (filepath .ToSlash (fsys .Root ), name )
768
- realPath , innerPath := fsys .splitPath (name )
768
+ realPath , innerPath := fsys .SplitPath (name )
769
769
if innerPath != "" {
770
770
if innerFsys := fsys .getInnerFsys (realPath ); innerFsys != nil {
771
771
return innerFsys .Open (innerPath )
@@ -779,7 +779,7 @@ func (fsys *DeepFS) Stat(name string) (fs.FileInfo, error) {
779
779
return nil , & fs.PathError {Op : "stat" , Path : name , Err : fmt .Errorf ("%w: %s" , fs .ErrInvalid , name )}
780
780
}
781
781
name = path .Join (filepath .ToSlash (fsys .Root ), name )
782
- realPath , innerPath := fsys .splitPath (name )
782
+ realPath , innerPath := fsys .SplitPath (name )
783
783
if innerPath != "" {
784
784
if innerFsys := fsys .getInnerFsys (realPath ); innerFsys != nil {
785
785
return fs .Stat (innerFsys , innerPath )
@@ -798,7 +798,7 @@ func (fsys *DeepFS) ReadDir(name string) ([]fs.DirEntry, error) {
798
798
return nil , & fs.PathError {Op : "readdir" , Path : name , Err : fmt .Errorf ("%w: %s" , fs .ErrInvalid , name )}
799
799
}
800
800
name = path .Join (filepath .ToSlash (fsys .Root ), name )
801
- realPath , innerPath := fsys .splitPath (name )
801
+ realPath , innerPath := fsys .SplitPath (name )
802
802
if innerPath != "" {
803
803
if innerFsys := fsys .getInnerFsys (realPath ); innerFsys != nil {
804
804
return fs .ReadDir (innerFsys , innerPath )
@@ -811,7 +811,7 @@ func (fsys *DeepFS) ReadDir(name string) ([]fs.DirEntry, error) {
811
811
// make sure entries that appear to be archive files indicate they are a directory
812
812
// so the fs package will try to walk them
813
813
for i , entry := range entries {
814
- if slices . Contains ( archiveExtensions , strings . ToLower ( path . Ext ( entry .Name ()) )) {
814
+ if PathIsArchive ( entry .Name ()) {
815
815
entries [i ] = alwaysDirEntry {entry }
816
816
}
817
817
}
@@ -840,7 +840,7 @@ func (fsys *DeepFS) getInnerFsys(realPath string) fs.FS {
840
840
return nil
841
841
}
842
842
843
- // splitPath splits a file path into the "real" path and the "inner" path components,
843
+ // SplitPath splits a file path into the "real" path and the "inner" path components,
844
844
// where the split point is the first extension of an archive filetype like ".zip" or
845
845
// ".tar.gz" that occurs in the path.
846
846
//
@@ -851,7 +851,7 @@ func (fsys *DeepFS) getInnerFsys(realPath string) fs.FS {
851
851
// If no archive extension is found in the path, only the realPath is returned.
852
852
// If the input path is precisely an archive file (i.e. ends with an archive file
853
853
// extension), then innerPath is returned as "." which indicates the root of the archive.
854
- func (* DeepFS ) splitPath (path string ) (realPath , innerPath string ) {
854
+ func (* DeepFS ) SplitPath (path string ) (realPath , innerPath string ) {
855
855
if len (path ) < 2 {
856
856
realPath = path
857
857
return
@@ -870,20 +870,20 @@ func (*DeepFS) splitPath(path string) (realPath, innerPath string) {
870
870
871
871
for {
872
872
part := strings .TrimRight (strings .ToLower (path [start :end ]), " " )
873
- for _ , ext := range archiveExtensions {
874
- if strings .HasSuffix (part , ext ) {
875
- // we've found an archive extension, so the path until the end of this segment is
876
- // the "real" OS path, and what remains (if anything( is the path within the archive
877
- realPath = filepath .Clean (filepath .FromSlash (path [:end ]))
878
- if end < len (path ) {
879
- innerPath = path [end + 1 :]
880
- } else {
881
- // signal to the caller that this is an archive,
882
- // even though it is the very root of the archive
883
- innerPath = "."
884
- }
885
- return
873
+ if PathIsArchive (part ) {
874
+ // we've found an archive extension, so the path until the end of this segment is
875
+ // the "real" OS path, and what remains (if anything( is the path within the archive
876
+ realPath = filepath .Clean (filepath .FromSlash (path [:end ]))
877
+
878
+ if end < len (path ) {
879
+ innerPath = path [end + 1 :]
880
+ } else {
881
+ // signal to the caller that this is an archive,
882
+ // even though it is the very root of the archive
883
+ innerPath = "."
886
884
}
885
+ return
886
+
887
887
}
888
888
889
889
// advance to the next segment, or end of string
@@ -936,6 +936,22 @@ var archiveExtensions = []string{
936
936
".tar.lz" ,
937
937
}
938
938
939
+ // PathIsArchive returns true if the path ends with an archive file (i.e.
940
+ // whether the path traverse to an archive) solely by lexical analysis (no
941
+ // reading the files or headers is performed).
942
+ func PathIsArchive (path string ) bool {
943
+ // normalize the extension
944
+ path = strings .ToLower (path )
945
+ for _ , ext := range archiveExtensions {
946
+ // Check the full ext
947
+ if strings .HasSuffix (path , ext ) {
948
+ return true
949
+ }
950
+ }
951
+
952
+ return false
953
+ }
954
+
939
955
// PathContainsArchive returns true if the path contains an archive file (i.e.
940
956
// whether the path traverses into an archive) solely by lexical analysis (no
941
957
// reading of files or headers is performed). Such a path is not typically
0 commit comments