Skip to content

Commit 06c3720

Browse files
IsFile should return true for special files
1 parent d0cf161 commit 06c3720

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

utils/filesystem/files.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"io"
14+
"io/fs"
1415
"os"
1516
"os/user"
1617
"path/filepath"
@@ -777,7 +778,7 @@ func (fs *VFS) IsFile(path string) (result bool, err error) {
777778
if err != nil {
778779
return
779780
}
780-
result = IsRegularFile(fi)
781+
result = IsRegularFile(fi) || IsSpecialFile(fi)
781782
return
782783
}
783784

@@ -788,6 +789,17 @@ func IsRegularFile(fi os.FileInfo) bool {
788789
return fi.Mode().IsRegular()
789790
}
790791

792+
func IsSpecialFile(fi os.FileInfo) bool {
793+
if fi == nil {
794+
return false
795+
}
796+
mode := fi.Mode()
797+
isSocket := mode&fs.ModeSocket != 0
798+
isDevice := mode&fs.ModeDevice != 0
799+
isNamedPipe := mode&fs.ModeNamedPipe != 0
800+
return isSocket || isDevice || isNamedPipe
801+
}
802+
791803
func (fs *VFS) IsLink(path string) (result bool, err error) {
792804
err = fs.checkWhetherUnderlyingResourceIsClosed()
793805
if err != nil {

utils/filesystem/files_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"fmt"
1111
"io"
12+
"net"
1213
"os"
1314
"path/filepath"
1415
"reflect"
@@ -22,6 +23,7 @@ import (
2223
"github.com/spf13/afero"
2324
"github.com/stretchr/testify/assert"
2425
"github.com/stretchr/testify/require"
26+
"golang.org/x/sys/unix"
2527

2628
"github.com/ARM-software/golang-utils/utils/collection"
2729
"github.com/ARM-software/golang-utils/utils/commonerrors"
@@ -546,6 +548,50 @@ func TestConvertPaths(t *testing.T) {
546548
}
547549
}
548550

551+
func TestIsFile(t *testing.T) {
552+
if platform.IsWindows() {
553+
t.Skip("Windows doesn't have features such as named pipes or unix sockets")
554+
}
555+
for _, fsType := range FileSystemTypes {
556+
t.Run(fmt.Sprint(fsType), func(t *testing.T) {
557+
fs := NewFs(fsType)
558+
559+
if fsType == InMemoryFS {
560+
t.Skip("In-memory file system won't have hardware devices or special files")
561+
}
562+
563+
b, err := fs.IsFile("/dev/null")
564+
require.NoError(t, err)
565+
assert.True(t, b)
566+
567+
tmpDir := t.TempDir()
568+
569+
fifoPath := filepath.Join(tmpDir, faker.Word())
570+
require.NoError(t, err)
571+
defer func() { _ = fs.Rm(fifoPath) }()
572+
err = unix.Mkfifo(fifoPath, 0666)
573+
require.NoError(t, err)
574+
b, err = fs.IsFile(fifoPath)
575+
require.NoError(t, err)
576+
assert.True(t, b)
577+
err = fs.Rm(fifoPath)
578+
require.NoError(t, err)
579+
580+
socketPath := filepath.Join(tmpDir, faker.Word())
581+
require.NoError(t, err)
582+
defer func() { _ = fs.Rm(socketPath) }()
583+
l, err := net.Listen("unix", socketPath)
584+
require.NoError(t, err)
585+
defer func() { _ = l.Close() }()
586+
b, err = fs.IsFile(socketPath)
587+
require.NoError(t, err)
588+
assert.True(t, b)
589+
err = fs.Rm(socketPath)
590+
require.NoError(t, err)
591+
})
592+
}
593+
}
594+
549595
func TestLink(t *testing.T) {
550596
if platform.IsWindows() {
551597
fmt.Println("In order to run TestLink on Windows, Developer mode must be enabled: https://github.com/golang/go/pull/24307")

0 commit comments

Comments
 (0)