Skip to content

Commit 871c111

Browse files
[fix] Exclude POSIX special files from inventory (#275)
1 parent ea78822 commit 871c111

3 files changed

Lines changed: 95 additions & 65 deletions

File tree

src/ByteSync.Client/Services/Inventories/InventoryBuilder.cs

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ private void ProcessSubDirectories(InventoryPart inventoryPart, DirectoryInfo di
271271
if (IsReparsePoint(subDirectory))
272272
{
273273
RecordSkippedEntry(inventoryPart, subDirectory, SkipReason.Symlink, FileSystemEntryKind.Symlink);
274+
274275
continue;
275276
}
276277
}
@@ -377,57 +378,8 @@ private bool ShouldIgnoreHiddenDirectory(DirectoryInfo directoryInfo)
377378
{
378379
var isRoot = IsRootPath(inventoryPart, fileInfo);
379380

380-
var entryKind = PosixFileTypeClassifier.ClassifyPosixEntry(fileInfo.FullName);
381-
if (entryKind == FileSystemEntryKind.Symlink)
381+
if (TryHandleFileSkip(inventoryPart, fileInfo, isRoot))
382382
{
383-
RecordSkippedEntry(inventoryPart, fileInfo, SkipReason.Symlink, FileSystemEntryKind.Symlink);
384-
385-
return;
386-
}
387-
388-
if (IsPosixSpecialFile(entryKind))
389-
{
390-
AddPosixSpecialFileAndLog(inventoryPart, fileInfo, entryKind);
391-
392-
return;
393-
}
394-
395-
if (!isRoot && ShouldIgnoreHiddenFile(fileInfo))
396-
{
397-
RecordSkippedEntry(inventoryPart, fileInfo, SkipReason.Hidden, FileSystemEntryKind.RegularFile);
398-
399-
return;
400-
}
401-
402-
if (!isRoot)
403-
{
404-
var systemSkipReason = GetSystemSkipReason(fileInfo);
405-
if (systemSkipReason.HasValue)
406-
{
407-
RecordSkippedEntry(inventoryPart, fileInfo, systemSkipReason.Value, FileSystemEntryKind.RegularFile);
408-
409-
return;
410-
}
411-
}
412-
413-
if (IsReparsePoint(fileInfo))
414-
{
415-
RecordSkippedEntry(inventoryPart, fileInfo, SkipReason.Symlink, FileSystemEntryKind.Symlink);
416-
417-
return;
418-
}
419-
420-
if (!FileSystemInspector.Exists(fileInfo))
421-
{
422-
RecordSkippedEntry(inventoryPart, fileInfo, SkipReason.NotFound);
423-
424-
return;
425-
}
426-
427-
if (FileSystemInspector.IsOffline(fileInfo) || IsRecallOnDataAccess(fileInfo))
428-
{
429-
RecordSkippedEntry(inventoryPart, fileInfo, SkipReason.Offline, FileSystemEntryKind.RegularFile);
430-
431383
return;
432384
}
433385

@@ -454,6 +406,65 @@ private bool ShouldIgnoreHiddenDirectory(DirectoryInfo directoryInfo)
454406
}
455407
}
456408

409+
private bool TryHandleFileSkip(InventoryPart inventoryPart, FileInfo fileInfo, bool isRoot)
410+
{
411+
var entryKind = PosixFileTypeClassifier.ClassifyPosixEntry(fileInfo.FullName);
412+
if (entryKind == FileSystemEntryKind.Symlink)
413+
{
414+
RecordSkippedEntry(inventoryPart, fileInfo, SkipReason.Symlink, FileSystemEntryKind.Symlink);
415+
416+
return true;
417+
}
418+
419+
if (IsPosixSpecialFile(entryKind))
420+
{
421+
AddPosixSpecialFileAndLog(inventoryPart, fileInfo, entryKind);
422+
423+
return true;
424+
}
425+
426+
if (!isRoot && ShouldIgnoreHiddenFile(fileInfo))
427+
{
428+
RecordSkippedEntry(inventoryPart, fileInfo, SkipReason.Hidden, FileSystemEntryKind.RegularFile);
429+
430+
return true;
431+
}
432+
433+
if (!isRoot)
434+
{
435+
var systemSkipReason = GetSystemSkipReason(fileInfo);
436+
if (systemSkipReason.HasValue)
437+
{
438+
RecordSkippedEntry(inventoryPart, fileInfo, systemSkipReason.Value, FileSystemEntryKind.RegularFile);
439+
440+
return true;
441+
}
442+
}
443+
444+
if (IsReparsePoint(fileInfo))
445+
{
446+
RecordSkippedEntry(inventoryPart, fileInfo, SkipReason.Symlink, FileSystemEntryKind.Symlink);
447+
448+
return true;
449+
}
450+
451+
if (!FileSystemInspector.Exists(fileInfo))
452+
{
453+
RecordSkippedEntry(inventoryPart, fileInfo, SkipReason.NotFound);
454+
455+
return true;
456+
}
457+
458+
if (FileSystemInspector.IsOffline(fileInfo) || IsRecallOnDataAccess(fileInfo))
459+
{
460+
RecordSkippedEntry(inventoryPart, fileInfo, SkipReason.Offline, FileSystemEntryKind.RegularFile);
461+
462+
return true;
463+
}
464+
465+
return false;
466+
}
467+
457468

458469
private void DoAnalyze(InventoryPart inventoryPart, DirectoryInfo directoryInfo, CancellationToken cancellationToken)
459470
{
@@ -480,6 +491,8 @@ private void DoAnalyze(InventoryPart inventoryPart, DirectoryInfo directoryInfo,
480491
if (IsPosixSpecialFile(entryKind))
481492
{
482493
RecordSkippedEntry(inventoryPart, directoryInfo, SkipReason.SpecialPosixFile, entryKind);
494+
_logger.LogWarning("Directory {Directory} is a POSIX special file ({EntryKind}) and will be skipped",
495+
directoryInfo.FullName, entryKind);
483496

484497
return;
485498
}
@@ -610,13 +623,6 @@ private void AddInaccessibleFileAndLog(InventoryPart inventoryPart, FileInfo fil
610623

611624
private void AddPosixSpecialFileAndLog(InventoryPart inventoryPart, FileInfo fileInfo, FileSystemEntryKind entryKind)
612625
{
613-
inventoryPart.IsIncompleteDueToAccess = true;
614-
var relativePath = BuildRelativePath(inventoryPart, fileInfo);
615-
var fileDescription = new FileDescription(inventoryPart, relativePath)
616-
{
617-
IsAccessible = false
618-
};
619-
AddFileSystemDescription(inventoryPart, fileDescription);
620626
RecordSkippedEntry(inventoryPart, fileInfo, SkipReason.SpecialPosixFile, entryKind);
621627
_logger.LogWarning("File {File} is a POSIX special file ({EntryKind}) and will be skipped", fileInfo.FullName, entryKind);
622628
}
@@ -742,4 +748,4 @@ private void AddFileSystemDescription(InventoryPart inventoryPart, FileSystemDes
742748
}
743749
}
744750
}
745-
}
751+
}

tests/ByteSync.Client.IntegrationTests/Services/Inventories/TestInventoryBuilder.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,8 @@ public async Task Test_PosixFifo_IsSkipped()
747747
var sessionSettings = SessionSettings.BuildDefault();
748748
var osPlatform = OperatingSystem.IsMacOS() ? OSPlatforms.MacOs : OSPlatforms.Linux;
749749

750-
inventoryBuilder = BuildInventoryBuilder(sessionSettings, null, null, osPlatform);
750+
var inventoryProcessData = new InventoryProcessData();
751+
inventoryBuilder = BuildInventoryBuilder(sessionSettings, inventoryProcessData, null, osPlatform);
751752
inventoryBuilder.AddInventoryPart(sourceA.FullName);
752753
await inventoryBuilder.BuildBaseInventoryAsync(inventoryAFilePath);
753754

@@ -765,11 +766,11 @@ public async Task Test_PosixFifo_IsSkipped()
765766
inventory = inventoryBuilder.Inventory!;
766767
inventory.InventoryParts.Count.Should().Be(1);
767768
inventory.InventoryParts[0].DirectoryDescriptions.Count.Should().Be(0);
768-
inventory.InventoryParts[0].FileDescriptions.Count.Should().Be(2);
769-
770-
var fifoDescription = inventory.InventoryParts[0].FileDescriptions.Single(fd => fd.Name.Equals("pipeA"));
771-
fifoDescription.IsAccessible.Should().BeFalse();
772-
inventory.InventoryParts[0].IsIncompleteDueToAccess.Should().BeTrue();
769+
inventory.InventoryParts[0].FileDescriptions.Count.Should().Be(1);
770+
inventory.InventoryParts[0].FileDescriptions.Single().Name.Should().Be("fileA.txt");
771+
inventory.InventoryParts[0].IsIncompleteDueToAccess.Should().BeFalse();
772+
inventoryProcessData.SkippedEntries.Should()
773+
.ContainSingle(entry => entry.Name == "pipeA" && entry.Reason == SkipReason.SpecialPosixFile);
773774
}
774775

775776
[Test]

tests/ByteSync.Client.UnitTests/Services/Inventories/InventoryBuilderInspectorTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ public async Task Posix_Special_File_Is_Recorded()
339339
await builder.BuildBaseInventoryAsync(invPath);
340340

341341
var part = builder.Inventory.InventoryParts.Single();
342-
part.FileDescriptions.Should().ContainSingle(fd => !fd.IsAccessible);
342+
part.FileDescriptions.Should().BeEmpty();
343+
part.IsIncompleteDueToAccess.Should().BeFalse();
343344
processData.SkippedEntries.Should()
344345
.ContainSingle(e => e.Name == "posix_special.txt" && e.Reason == SkipReason.SpecialPosixFile);
345346
}
@@ -363,6 +364,28 @@ public async Task Posix_Symlink_Directory_Is_Recorded()
363364
processData.SkippedEntries.Should()
364365
.ContainSingle(e => e.Name == "root_posix_symlink" && e.Reason == SkipReason.Symlink);
365366
}
367+
368+
[Test]
369+
public async Task Posix_Special_Directory_Is_Recorded()
370+
{
371+
var insp = new Mock<IFileSystemInspector>(MockBehavior.Strict);
372+
var posix = new Mock<IPosixFileTypeClassifier>(MockBehavior.Strict);
373+
posix.Setup(p => p.ClassifyPosixEntry(It.IsAny<string>())).Returns(FileSystemEntryKind.BlockDevice);
374+
var (builder, processData) = CreateBuilderWithData(insp.Object, posix.Object);
375+
376+
var root = Directory.CreateDirectory(Path.Combine(TestDirectory.FullName, "root_posix_special"));
377+
378+
builder.AddInventoryPart(root.FullName);
379+
var invPath = Path.Combine(TestDirectory.FullName, "inv_posix_special_dir.zip");
380+
await builder.BuildBaseInventoryAsync(invPath);
381+
382+
var part = builder.Inventory.InventoryParts.Single();
383+
part.DirectoryDescriptions.Should().BeEmpty();
384+
part.FileDescriptions.Should().BeEmpty();
385+
part.IsIncompleteDueToAccess.Should().BeFalse();
386+
processData.SkippedEntries.Should()
387+
.ContainSingle(e => e.Name == "root_posix_special" && e.Reason == SkipReason.SpecialPosixFile);
388+
}
366389

367390
[Test]
368391
public async Task Reparse_File_Is_Ignored()

0 commit comments

Comments
 (0)