-
Notifications
You must be signed in to change notification settings - Fork 8
[feature] Detect POSIX special files #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6bf8acf
feat: detect POSIX special files
paul-fresquet 07e22fd
refactor: move FileSystemEntryKind to client
paul-fresquet a033151
refactor: cleanup
paul-fresquet bc1545f
refactor: improve test environment instanciation
paul-fresquet 2b69698
test: fix FIFO inventory expectations
paul-fresquet 3a54c4f
test: align inventory counts for fifo and reparse
paul-fresquet e5d8610
fix: fallback to stat for POSIX mode
paul-fresquet eb9ef8e
fix: classify POSIX types via UnixFileInfo fallback
paul-fresquet edd063b
test: ignore POSIX classification when unknown
paul-fresquet c898665
test: ignore fifo test when classification is unknown
paul-fresquet 66696a7
test: expand POSIX classifier coverage
paul-fresquet fa8367e
refactor: cleanup
paul-fresquet 1c490af
test: cover UnixFileInfo exception path
paul-fresquet 7cc9f7a
test: cover POSIX classifier exception paths
paul-fresquet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/ByteSync.Client/Business/Inventories/FileSystemEntryKind.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| namespace ByteSync.Business.Inventories; | ||
|
|
||
| public enum FileSystemEntryKind | ||
| { | ||
| Unknown = 0, | ||
| RegularFile = 1, | ||
| Directory = 2, | ||
| BlockDevice = 3, | ||
| CharacterDevice = 4, | ||
| Fifo = 5, | ||
| Socket = 6, | ||
| Symlink = 7 | ||
| } |
Large diffs are not rendered by default.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
src/ByteSync.Client/Interfaces/Controls/Inventories/IPosixFileTypeClassifier.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| using ByteSync.Business.Inventories; | ||
|
|
||
| namespace ByteSync.Interfaces.Controls.Inventories; | ||
|
|
||
| public interface IPosixFileTypeClassifier | ||
| { | ||
| FileSystemEntryKind ClassifyPosixEntry(string path); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/ByteSync.Client/Services/Inventories/PosixFileTypeClassifier.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| using ByteSync.Business.Inventories; | ||
| using ByteSync.Interfaces.Controls.Inventories; | ||
| using Mono.Unix; | ||
| using Mono.Unix.Native; | ||
|
|
||
| namespace ByteSync.Services.Inventories; | ||
|
|
||
| public class PosixFileTypeClassifier : IPosixFileTypeClassifier | ||
| { | ||
| private readonly Func<string, UnixFileInfo> _unixFileInfoFactory; | ||
| private readonly Func<string, (bool Success, FilePermissions Type)> _tryGetMode; | ||
|
|
||
| public PosixFileTypeClassifier() | ||
| : this(path => new UnixFileInfo(path), TryGetModeDefault) | ||
| { | ||
| } | ||
|
|
||
| public PosixFileTypeClassifier(Func<string, UnixFileInfo> unixFileInfoFactory) | ||
| : this(unixFileInfoFactory, TryGetModeDefault) | ||
| { | ||
| } | ||
|
|
||
| public PosixFileTypeClassifier(Func<string, UnixFileInfo> unixFileInfoFactory, | ||
| Func<string, (bool Success, FilePermissions Type)> tryGetMode) | ||
| { | ||
| _unixFileInfoFactory = unixFileInfoFactory; | ||
| _tryGetMode = tryGetMode; | ||
| } | ||
|
|
||
| public FileSystemEntryKind ClassifyPosixEntry(string path) | ||
| { | ||
| if (OperatingSystem.IsWindows()) | ||
| { | ||
| return FileSystemEntryKind.Unknown; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var modeResult = _tryGetMode(path); | ||
| if (!modeResult.Success) | ||
| { | ||
| return FileSystemEntryKind.Unknown; | ||
| } | ||
|
|
||
| var entryKind = MapFilePermissions(modeResult.Type); | ||
| if (entryKind == FileSystemEntryKind.RegularFile || entryKind == FileSystemEntryKind.Unknown) | ||
| { | ||
| var unixKind = TryClassifyWithUnixFileInfo(path); | ||
| if (unixKind != FileSystemEntryKind.Unknown) | ||
| { | ||
| return unixKind; | ||
| } | ||
| } | ||
|
|
||
| return entryKind; | ||
| } | ||
| catch (DllNotFoundException) | ||
| { | ||
| return FileSystemEntryKind.Unknown; | ||
| } | ||
| catch (EntryPointNotFoundException) | ||
| { | ||
| return FileSystemEntryKind.Unknown; | ||
| } | ||
| catch (PlatformNotSupportedException) | ||
| { | ||
| return FileSystemEntryKind.Unknown; | ||
| } | ||
| } | ||
|
|
||
| private static (bool Success, FilePermissions Type) TryGetModeDefault(string path) | ||
| { | ||
| if (Syscall.lstat(path, out var stat) != 0) | ||
| { | ||
| if (Syscall.stat(path, out stat) != 0) | ||
| { | ||
| return (false, 0); | ||
| } | ||
| } | ||
|
|
||
| var mode = stat.st_mode; | ||
| var type = mode & FilePermissions.S_IFMT; | ||
|
|
||
| return (true, type); | ||
| } | ||
|
|
||
| private static FileSystemEntryKind MapFilePermissions(FilePermissions type) | ||
| { | ||
| if (type == FilePermissions.S_IFREG) | ||
| { | ||
| return FileSystemEntryKind.RegularFile; | ||
| } | ||
|
|
||
| if (type == FilePermissions.S_IFDIR) | ||
| { | ||
| return FileSystemEntryKind.Directory; | ||
| } | ||
|
|
||
| if (type == FilePermissions.S_IFBLK) | ||
| { | ||
| return FileSystemEntryKind.BlockDevice; | ||
| } | ||
|
|
||
| if (type == FilePermissions.S_IFCHR) | ||
| { | ||
| return FileSystemEntryKind.CharacterDevice; | ||
| } | ||
|
|
||
| if (type == FilePermissions.S_IFIFO) | ||
| { | ||
| return FileSystemEntryKind.Fifo; | ||
| } | ||
|
|
||
| if (type == FilePermissions.S_IFSOCK) | ||
| { | ||
| return FileSystemEntryKind.Socket; | ||
| } | ||
|
|
||
| if (type == FilePermissions.S_IFLNK) | ||
| { | ||
| return FileSystemEntryKind.Symlink; | ||
| } | ||
|
|
||
| return FileSystemEntryKind.Unknown; | ||
| } | ||
|
|
||
| private FileSystemEntryKind TryClassifyWithUnixFileInfo(string path) | ||
| { | ||
| try | ||
| { | ||
| var info = _unixFileInfoFactory(path); | ||
|
|
||
| return info.FileType switch | ||
| { | ||
| FileTypes.BlockDevice => FileSystemEntryKind.BlockDevice, | ||
| FileTypes.CharacterDevice => FileSystemEntryKind.CharacterDevice, | ||
| FileTypes.Fifo => FileSystemEntryKind.Fifo, | ||
| FileTypes.Socket => FileSystemEntryKind.Socket, | ||
| FileTypes.Directory => FileSystemEntryKind.Directory, | ||
| FileTypes.RegularFile => FileSystemEntryKind.RegularFile, | ||
| FileTypes.SymbolicLink => FileSystemEntryKind.Symlink, | ||
| _ => FileSystemEntryKind.Unknown | ||
| }; | ||
| } | ||
| catch (Exception) | ||
| { | ||
| return FileSystemEntryKind.Unknown; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.