Skip to content

Commit

Permalink
Merge pull request #7 from MShekow/fix/skip-unsupported-filemodes
Browse files Browse the repository at this point in the history
Skip files with unsupported mode, print a warning instead
  • Loading branch information
MShekow authored Jan 9, 2023
2 parents fe86ebb + ac57b7b commit e3b146c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Directory Checksum

![Coverage](https://img.shields.io/badge/Coverage-92.7%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-83.2%25-brightgreen)
![CI](https://github.com/MShekow/directory-checksum/actions/workflows/ci.yml/badge.svg)
![CD](https://github.com/MShekow/directory-checksum/actions/workflows/cd.yml/badge.svg)

Expand Down
36 changes: 35 additions & 1 deletion directory_checksum/fs_scanner.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package directory_checksum

import (
"fmt"
"github.com/go-errors/errors"
"github.com/spf13/afero"
"io/fs"
Expand Down Expand Up @@ -30,10 +31,37 @@ func bendRelativePath(relativePath, absoluteRootPath string) string {
return relativePath
}

// isInvalidFiletype returns true if the provided file mode is of some irregular mode of which a checksum cannot be
// calculated, false otherwise.
func isInvalidFiletype(mode fs.FileMode) bool {
return mode&fs.ModeIrregular == fs.ModeIrregular || mode&fs.ModeCharDevice == fs.ModeCharDevice ||
mode&fs.ModeSocket == fs.ModeSocket || mode&fs.ModeNamedPipe == fs.ModeNamedPipe ||
mode&fs.ModeDevice == fs.ModeDevice
}

func getInvalidFiletypeAsString(mode fs.FileMode) string {
if mode&fs.ModeIrregular == fs.ModeIrregular {
return "non-regular/irregular"
}
if mode&fs.ModeCharDevice == fs.ModeCharDevice {
return "character device"
}
if mode&fs.ModeSocket == fs.ModeSocket {
return "socket"
}
if mode&fs.ModeNamedPipe == fs.ModeNamedPipe {
return "named pipe"
}
if mode&fs.ModeDevice == fs.ModeDevice {
return "device"
}
return ""
}

// ScanDirectory returns the pointer to a (hierarchically-nested) Directory that is constructed from recursively walking
// the directory located at absoluteRootPath.
func ScanDirectory(absoluteRootPath string, filesystemImpl afero.Fs) (*Directory, error) {
// Handle a special case that happens only during unit testing (where root is '\' when executed on Windows)
// Handle a special case that happens only during unit testing (where root is '\' when executed on Windows
if absoluteRootPath != "\\" {
absRootPath, err := filepath.Abs(absoluteRootPath)
if err != nil {
Expand Down Expand Up @@ -62,6 +90,12 @@ func ScanDirectory(absoluteRootPath string, filesystemImpl afero.Fs) (*Directory
fileType = TypeSymlink
}

if isInvalidFiletype(info.Mode()) {
fmt.Printf("WARNING: skipping '%s' because it is of unsupported type: %s\n", relativePath,
getInvalidFiletypeAsString(info.Mode()))
return nil
}

err := directory.Add(relativePath, relativePath, absoluteRootPath, fileType, filesystemImpl)
if err != nil {
return errors.Wrap(err, 0)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"
)

const version = "1.3"
const version = "1.4"

var maxDepth int

Expand Down

0 comments on commit e3b146c

Please sign in to comment.