Skip to content

Commit

Permalink
added IFileCabBase.Processed
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Nov 13, 2018
1 parent a06cae4 commit 784432e
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 122 deletions.
198 changes: 82 additions & 116 deletions CabinetManager/CabManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,7 @@ public void SetCancellationToken(CancellationToken? cancelToken) {

/// <inheritdoc cref="ICabManager.PackFileSet"/>
public int PackFileSet(IEnumerable<IFileToAddInCab> filesToPackIn) {
int nbFilesProcessed = 0;
foreach (var cabGroupedFiles in filesToPackIn.GroupBy(f => f.CabPath)) {
try {
using (var cfCabinet = new CfCabinet(cabGroupedFiles.Key, _cancelToken)) {
cfCabinet.OnProgress += OnProgressionEvent;
try {
foreach (var fileToAddInCab in cabGroupedFiles) {
if (!File.Exists(fileToAddInCab.SourcePath)) {
continue;
}
var fileRelativePath = fileToAddInCab.RelativePathInCab.NormalizeRelativePath();
cfCabinet.AddExternalFile(fileToAddInCab.SourcePath, fileRelativePath);
nbFilesProcessed++;
OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(cabGroupedFiles.Key, fileRelativePath));
}
cfCabinet.Save(_compressionType);
} finally {
cfCabinet.OnProgress -= OnProgressionEvent;
}
}
} catch (OperationCanceledException) {
throw;
} catch (Exception e) {
throw new CabException($"Failed to pack to {cabGroupedFiles.Key}.", e);
}
OnProgress?.Invoke(this, CabProgressionEventArgs.NewCompletedCabinet(cabGroupedFiles.Key));
}
return nbFilesProcessed;
return DoAction(filesToPackIn, Action.Archive);
}

/// <inheritdoc cref="ICabManager.ListFiles"/>
Expand All @@ -96,120 +69,113 @@ public IEnumerable<IFileInCab> ListFiles(string cabPath) {

/// <inheritdoc cref="ICabManager.ExtractFileSet"/>
public int ExtractFileSet(IEnumerable<IFileInCabToExtract> filesToExtract) {
int nbFilesProcessed = 0;
foreach (var cabGroupedFiles in filesToExtract.GroupBy(f => f.CabPath)) {
if (!File.Exists(cabGroupedFiles.Key)) {
continue;
}
try {
// create all necessary extraction folders
foreach (var extractDirGroupedFiles in cabGroupedFiles.GroupBy(f => Path.GetDirectoryName(f.ExtractionPath))) {
if (!Directory.Exists(extractDirGroupedFiles.Key) && !string.IsNullOrWhiteSpace(extractDirGroupedFiles.Key)) {
Directory.CreateDirectory(extractDirGroupedFiles.Key);
}
}
using (var cfCabinet = new CfCabinet(cabGroupedFiles.Key, _cancelToken)) {
cfCabinet.OnProgress += OnProgressionEvent;
try {
foreach (var fileInCabToExtract in cabGroupedFiles) {
var fileRelativePath = fileInCabToExtract.RelativePathInCab.NormalizeRelativePath();
if (cfCabinet.ExtractToFile(fileRelativePath, fileInCabToExtract.ExtractionPath)) {
nbFilesProcessed++;
OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(cabGroupedFiles.Key, fileRelativePath));
}
}
} finally {
cfCabinet.OnProgress -= OnProgressionEvent;
}
}
} catch (OperationCanceledException) {
throw;
} catch (Exception e) {
throw new CabException($"Failed to extract files from {cabGroupedFiles.Key}.", e);
}
OnProgress?.Invoke(this, CabProgressionEventArgs.NewCompletedCabinet(cabGroupedFiles.Key));
}
return nbFilesProcessed;
return DoAction(filesToExtract, Action.Extract);
}

/// <inheritdoc cref="ICabManager.DeleteFileSet"/>
public int DeleteFileSet(IEnumerable<IFileInCabToDelete> filesToDeleteIn) {
int nbFilesProcessed = 0;
foreach (var cabGroupedFiles in filesToDeleteIn.GroupBy(f => f.CabPath)) {
if (!File.Exists(cabGroupedFiles.Key)) {
continue;
}
try {
using (var cfCabinet = new CfCabinet(cabGroupedFiles.Key, _cancelToken)) {
cfCabinet.OnProgress += OnProgressionEvent;
try {
foreach (var fileToAddInCab in cabGroupedFiles) {
var fileRelativePath = fileToAddInCab.RelativePathInCab.NormalizeRelativePath();
if (cfCabinet.DeleteFile(fileRelativePath)) {
nbFilesProcessed++;
OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(cabGroupedFiles.Key, fileRelativePath));
}
}
cfCabinet.Save(_compressionType);
} finally {
cfCabinet.OnProgress -= OnProgressionEvent;
}
}
} catch (OperationCanceledException) {
throw;
} catch (Exception e) {
throw new CabException($"Failed to delete files from {cabGroupedFiles.Key}.", e);
}
OnProgress?.Invoke(this, CabProgressionEventArgs.NewCompletedCabinet(cabGroupedFiles.Key));
}
return nbFilesProcessed;
return DoAction(filesToDeleteIn, Action.Delete);
}

/// <inheritdoc cref="ICabManager.MoveFileSet"/>
public int MoveFileSet(IEnumerable<IFileInCabToMove> filesToMove) {
return DoAction(filesToMove, Action.Move);
}

/// <summary>
/// Returns a string representation of a given cabinet file.
/// </summary>
/// <param name="cabPath"></param>
/// <returns></returns>
public string ToString(string cabPath) {
if (!File.Exists(cabPath)) {
return string.Empty;
}
using (var cfCabinet = new CfCabinet(cabPath, _cancelToken)) {
return cfCabinet.GetStringFullRepresentation();
}
}

private int DoAction(IEnumerable<IFileCabBase> filesIn, Action action) {
var files = filesIn.ToList();
files.ForEach(f => f.Processed = false);

int nbFilesProcessed = 0;
foreach (var cabGroupedFiles in filesToMove.GroupBy(f => f.CabPath)) {
if (!File.Exists(cabGroupedFiles.Key)) {
foreach (var groupedFiles in files.GroupBy(f => f.CabPath)) {
if (action != Action.Archive && !File.Exists(groupedFiles.Key)) {
continue;
}
try {
using (var cfCabinet = new CfCabinet(cabGroupedFiles.Key, _cancelToken)) {
try {
if (action == Action.Extract) {
// create all necessary extraction folders
foreach (var extractDirGroupedFiles in groupedFiles.GroupBy(f => Path.GetDirectoryName(((IFileInCabToExtract) f).ExtractionPath))) {
if (!Directory.Exists(extractDirGroupedFiles.Key) && !string.IsNullOrWhiteSpace(extractDirGroupedFiles.Key)) {
Directory.CreateDirectory(extractDirGroupedFiles.Key);
}
}
}
using (var cfCabinet = new CfCabinet(groupedFiles.Key, _cancelToken)) {
cfCabinet.OnProgress += OnProgressionEvent;
try {
foreach (var fileToAddInCab in cabGroupedFiles) {
var fileRelativePath = fileToAddInCab.RelativePathInCab.NormalizeRelativePath();
if (cfCabinet.MoveFile(fileRelativePath, fileToAddInCab.NewRelativePathInCab.NormalizeRelativePath())) {
nbFilesProcessed++;
OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(cabGroupedFiles.Key, fileRelativePath));
foreach (var file in groupedFiles) {
var fileRelativePath = file.RelativePathInCab.NormalizeRelativePath();
switch (action) {
case Action.Archive:
var fileToArchive = (IFileToAddInCab) file;
if (File.Exists(fileToArchive.SourcePath)) {
cfCabinet.AddExternalFile(fileToArchive.SourcePath, fileRelativePath);
nbFilesProcessed++;
file.Processed = true;
OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath));
}
break;
case Action.Extract:
if (cfCabinet.ExtractToFile(fileRelativePath, ((IFileInCabToExtract) file).ExtractionPath)) {
nbFilesProcessed++;
file.Processed = true;
OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath));
}
break;
case Action.Delete:
if (cfCabinet.DeleteFile(fileRelativePath)) {
nbFilesProcessed++;
file.Processed = true;
OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath));
}
break;
case Action.Move:
if (cfCabinet.MoveFile(fileRelativePath, ((IFileInCabToMove) file).NewRelativePathInCab.NormalizeRelativePath())) {
nbFilesProcessed++;
file.Processed = true;
OnProgress?.Invoke(this, CabProgressionEventArgs.NewProcessedFile(groupedFiles.Key, fileRelativePath));
}
break;
default:
throw new ArgumentOutOfRangeException(nameof(action), action, null);
}
}
cfCabinet.Save(_compressionType);
if (action != Action.Extract) {
cfCabinet.Save(_compressionType);
}
} finally {
cfCabinet.OnProgress -= OnProgressionEvent;
}
}
} catch (OperationCanceledException) {
throw;
} catch (Exception e) {
throw new CabException($"Failed to move files from {cabGroupedFiles.Key}.", e);
throw new CabException($"Failed to {action} files in {groupedFiles.Key}.", e);
}
OnProgress?.Invoke(this, CabProgressionEventArgs.NewCompletedCabinet(cabGroupedFiles.Key));
OnProgress?.Invoke(this, CabProgressionEventArgs.NewCompletedCabinet(groupedFiles.Key));
}
return nbFilesProcessed;
}

/// <summary>
/// Returns a string representation of a given cabinet file.
/// </summary>
/// <param name="cabPath"></param>
/// <returns></returns>
public string ToString(string cabPath) {
if (!File.Exists(cabPath)) {
return string.Empty;
}
using (var cfCabinet = new CfCabinet(cabPath, _cancelToken)) {
return cfCabinet.GetStringFullRepresentation();
}

private enum Action {
Archive,
Extract,
Delete,
Move
}

private void OnProgressionEvent(object sender, CfSaveEventArgs e) {
Expand Down
8 changes: 4 additions & 4 deletions CabinetManager/ICabManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public interface ICabManager {
/// <para>
/// Pack (i.e. add or replace) files into cabinets.
/// Non existing source files will not throw an exception.
/// You can inspect which files are processed with the <see cref="OnProgress"/> event.
/// You can inspect which files are processed with the <see cref="OnProgress"/> event or using <see cref="IFileCabBase.Processed"/>.
/// Packing into an existing cabinet will update it.
/// Packing existing files will update them.
/// </para>
Expand All @@ -78,7 +78,7 @@ public interface ICabManager {
/// Extracts the given files from cabinets.
/// Requesting the extraction from a non existing cabinet will not throw an exception.
/// Requesting the extraction a file that does not exist in the cabinet will not throw an exception.
/// You can inspect which files are processed with the <see cref="OnProgress"/> event.
/// You can inspect which files are processed with the <see cref="OnProgress"/> event or using <see cref="IFileCabBase.Processed"/>.
/// </para>
/// </summary>
/// <param name="filesToExtract"></param>
Expand All @@ -92,7 +92,7 @@ public interface ICabManager {
/// Deletes the given files from cabinets.
/// Requesting the deletion from a non existing cabinet will not throw an exception.
/// Requesting the deletion a file that does not exist in the cabinet will not throw an exception.
/// You can inspect which files are processed with the <see cref="OnProgress"/> event.
/// You can inspect which files are processed with the <see cref="OnProgress"/> event or using <see cref="IFileCabBase.Processed"/>.
/// </para>
/// </summary>
/// <param name="filesToDelete"></param>
Expand All @@ -106,7 +106,7 @@ public interface ICabManager {
/// Moves the given files within cabinets.
/// Requesting the movement from a non existing cabinet will not throw an exception.
/// Requesting the movement a file that does not exist in the cabinet will not throw an exception.
/// You can inspect which files are processed with the <see cref="OnProgress"/> event.
/// You can inspect which files are processed with the <see cref="OnProgress"/> event or using <see cref="IFileCabBase.Processed"/>.
/// </para>
/// </summary>
/// <param name="filesToMove"></param>
Expand Down
5 changes: 5 additions & 0 deletions CabinetManager/IFileCabBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ public interface IFileCabBase {
/// </summary>
string RelativePathInCab { get; }

/// <summary>
/// Boolean set after an archiver action which indicates if this file was actually processed.
/// </summary>
bool Processed { get; set; }

}
}
1 change: 1 addition & 0 deletions CabinetManager/internal/FileInCab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace CabinetManager.@internal {
internal class FileInCab : IFileInCab {
public string CabPath { get; set; }
public string RelativePathInCab { get; set; }
public bool Processed { get; set; }
public ulong SizeInBytes { get; set; }
public DateTime LastWriteTime { get; set; }
public FileAttributes FileAttributes { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions CabinetManagerTest/Tests/ArchiveTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ protected void CreateArchive(ICabManager archiver, List<FileInCab> listFiles) {
RelativePathInCab = "random.name"
});
Assert.AreEqual(modifiedList.Count - 1, archiver.PackFileSet(modifiedList));
Assert.AreEqual(modifiedList.Count - 1, modifiedList.Count(f => f.Processed));

// test the update of archives
modifiedList = listFiles.GetRange(0, 1);
Assert.AreEqual(modifiedList.Count, archiver.PackFileSet(modifiedList));
Assert.AreEqual(modifiedList.Count, modifiedList.Count(f => f.Processed));

foreach (var archive in listFiles.GroupBy(f => f.CabPath)) {
if (Directory.Exists(Path.GetDirectoryName(archive.Key))) {
Expand Down Expand Up @@ -116,6 +118,7 @@ protected void Extract(ICabManager archiver, List<FileInCab> listFiles) {
RelativePathInCab = "random.name"
});
Assert.AreEqual(modifiedList.Count - 1, archiver.ExtractFileSet(modifiedList));
Assert.AreEqual(modifiedList.Count - 1, modifiedList.Count(f => f.Processed));

foreach (var fileToExtract in listFiles) {
Assert.IsTrue(File.Exists(fileToExtract.ExtractionPath), $"Extracted file does not exist : {fileToExtract.ExtractionPath}");
Expand Down Expand Up @@ -154,6 +157,7 @@ protected void DeleteFilesInArchive(ICabManager archiver, List<FileInCab> listFi
RelativePathInCab = "random.name"
});
Assert.AreEqual(modifiedList.Count - 1, archiver.DeleteFileSet(modifiedList));
Assert.AreEqual(modifiedList.Count - 1, modifiedList.Count(f => f.Processed));

foreach (var groupedFiles in listFiles.GroupBy(f => f.CabPath)) {
var files = archiver.ListFiles(groupedFiles.Key);
Expand Down Expand Up @@ -193,6 +197,7 @@ protected void MoveInArchives(ICabManager archiver, List<FileInCab> listFiles) {
archiver.SetCancellationToken(null);

Assert.AreEqual(modifiedList.Count - 1, archiver.MoveFileSet(modifiedList));
Assert.AreEqual(modifiedList.Count - 1, modifiedList.Count(f => f.Processed));

archiver.OnProgress -= ArchiverOnOnProgress;

Expand All @@ -208,6 +213,7 @@ protected void MoveInArchives(ICabManager archiver, List<FileInCab> listFiles) {
});

Assert.AreEqual(modifiedList.Count - 1, archiver.MoveFileSet(modifiedList));
Assert.AreEqual(modifiedList.Count - 1, modifiedList.Count(f => f.Processed));

modifiedList.ForEach(f => {
f.RelativePathInCab = f.NewRelativePathInCab;
Expand Down
1 change: 1 addition & 0 deletions CabinetManagerTest/Tests/FileInCab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace CabinetManagerTest.Tests {
public class FileInCab : IFileInCabToDelete, IFileInCabToExtract, IFileToAddInCab, IFileInCabToMove {
public string CabPath { get; set; }
public string RelativePathInCab { get; set; }
public bool Processed { get; set; }
public ulong SizeInBytes { get; set; }
public DateTime LastWriteTime { get; set; }
public string ExtractionPath { get; set; }
Expand Down
Loading

0 comments on commit 784432e

Please sign in to comment.