Skip to content

Commit

Permalink
Merge pull request #11 from LTRData/pr/10
Browse files Browse the repository at this point in the history
Pr/10
  • Loading branch information
LTRData authored May 12, 2024
2 parents b2900c2 + e01584d commit d2afda5
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 23 deletions.
20 changes: 13 additions & 7 deletions Library/DiscUtils.Core/Archives/TarFileBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
// Copyright (c) 2008-2024, Kenneth Bell, Olof Lagerkvist and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand All @@ -24,6 +24,7 @@
using DiscUtils.Streams;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;

namespace DiscUtils.Archives;
Expand Down Expand Up @@ -74,6 +75,11 @@ public long TotalSize

public int FileCount => _files.Count;

protected virtual void AddFile(UnixBuildFileRecord file)
{
_files.Add(file);
}

/// <summary>
/// Add a directory to the tar archive.
/// </summary>
Expand Down Expand Up @@ -114,7 +120,7 @@ public void AddDirectory(
/// <param name="buffer">The file data.</param>
public void AddFile(string name, byte[] buffer)
{
_files.Add(new UnixBuildFileRecord(name, buffer));
AddFile(new UnixBuildFileRecord(name, buffer));
}

/// <summary>
Expand All @@ -124,7 +130,7 @@ public void AddFile(string name, byte[] buffer)
/// <param name="sourcefile">The file to add.</param>
public void AddFile(string name, string sourcefile)
{
_files.Add(new UnixBuildFileRecord(name, File.ReadAllBytes(sourcefile)));
AddFile(new UnixBuildFileRecord(name, File.ReadAllBytes(sourcefile)));
}

/// <summary>
Expand All @@ -139,7 +145,7 @@ public void AddFile(string name, string sourcefile)
public void AddFile(
string name, byte[] buffer, int ownerId, int groupId, UnixFilePermissions fileMode, DateTime modificationTime)
{
_files.Add(new UnixBuildFileRecord(name, buffer, fileMode, ownerId, groupId, modificationTime));
AddFile(new UnixBuildFileRecord(name, buffer, fileMode, ownerId, groupId, modificationTime));
}

/// <summary>
Expand All @@ -154,7 +160,7 @@ public void AddFile(
public void AddFile(
string name, string sourcefile, int ownerId, int groupId, UnixFilePermissions fileMode, DateTime modificationTime)
{
_files.Add(new UnixBuildFileRecord(name, File.ReadAllBytes(sourcefile), fileMode, ownerId, groupId, modificationTime));
AddFile(new UnixBuildFileRecord(name, File.ReadAllBytes(sourcefile), fileMode, ownerId, groupId, modificationTime));
}

/// <summary>
Expand All @@ -164,7 +170,7 @@ public void AddFile(
/// <param name="stream">The file data.</param>
public void AddFile(string name, Stream stream)
{
_files.Add(new UnixBuildFileRecord(name, stream));
AddFile(new UnixBuildFileRecord(name, stream));
}

/// <summary>
Expand All @@ -179,7 +185,7 @@ public void AddFile(string name, Stream stream)
public void AddFile(
string name, Stream stream, int ownerId, int groupId, UnixFilePermissions fileMode, DateTime modificationTime)
{
_files.Add(new UnixBuildFileRecord(name, stream, fileMode, ownerId, groupId, modificationTime));
AddFile(new UnixBuildFileRecord(name, stream, fileMode, ownerId, groupId, modificationTime));
}

protected override List<BuilderExtent> FixExtents(out long totalLength)
Expand Down
5 changes: 3 additions & 2 deletions Library/DiscUtils.Core/Archives/UnixBuildFileRecord.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
// Copyright (c) 2008-2024, Kenneth Bell, Olof Lagerkvist and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand All @@ -26,7 +26,8 @@
using System.IO;

namespace DiscUtils.Archives;
internal sealed class UnixBuildFileRecord

public sealed class UnixBuildFileRecord
{
private string _name;
private UnixFilePermissions _fileMode;
Expand Down
4 changes: 3 additions & 1 deletion Library/DiscUtils.Core/IFileSystemBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
// Copyright (c) 2008-2024, Kenneth Bell, Olof Lagerkvist and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -33,6 +33,8 @@ public interface IFileSystemBuilder

string VolumeIdentifier { get; set; }

event EventHandler<ProgressEventArgs> ProgressChanged;

void AddDirectory(string name);

void AddDirectory(string name, DateTime creationTime, DateTime writtenTime, DateTime accessedTime, FileAttributes attributes);
Expand Down
32 changes: 32 additions & 0 deletions Library/DiscUtils.Core/ProgressEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Copyright (c) 2008-2024, Kenneth Bell, Olof Lagerkvist and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

using System;

namespace DiscUtils;

// EventArgs class for progress reporting
public class ProgressEventArgs : EventArgs
{
public long TotalFiles { get; set; }
public long TotalItems { get; set; }
}
8 changes: 4 additions & 4 deletions Library/DiscUtils.Iso9660/BuildDirectoryInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
// Copyright (c) 2008-2024, Kenneth Bell, Olof Lagerkvist and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -41,9 +41,9 @@ public sealed class BuildDirectoryInfo : BuildDirectoryMember
private List<BuildDirectoryMember> _sortedMembers;

internal BuildDirectoryInfo(string name, BuildDirectoryInfo parent)
: base(name, MakeShortDirName(name, parent))
: base(name, MakeShortDirName(name))
{
_parent = parent == null ? this : parent;
_parent = parent ?? this;
HierarchyDepth = parent == null ? 0 : parent.HierarchyDepth + 1;
_members = new(StringComparer.OrdinalIgnoreCase, entry => entry.Name);
}
Expand Down Expand Up @@ -147,7 +147,7 @@ private static int WriteMember(BuildDirectoryMember m, string nameOverride, Enco
return dr.WriteTo(buffer, nameEnc);
}

private static string MakeShortDirName(string longName, BuildDirectoryInfo dir)
private static string MakeShortDirName(string longName)
{
if (IsoUtilities.IsValidDirectoryName(longName))
{
Expand Down
27 changes: 23 additions & 4 deletions Library/DiscUtils.Iso9660/CDBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
// Copyright (c) 2008-2024, Kenneth Bell, Olof Lagerkvist and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -56,6 +56,23 @@ public sealed class CDBuilder : StreamBuilder, IFileSystemBuilder
private readonly List<BuildFileInfo> _files;
private readonly BuildDirectoryInfo _rootDirectory;

// Progress reporting event
public event EventHandler<ProgressEventArgs> ProgressChanged;

private ProgressEventArgs progressEventArgs;

// Method for updating progress
private void UpdateProgress()
{
if (ProgressChanged is not null)
{
progressEventArgs ??= new();
progressEventArgs.TotalFiles = _files.Count;
progressEventArgs.TotalItems = _files.Count + _dirs.Count;
ProgressChanged(this, progressEventArgs);
}
}

/// <summary>
/// Initializes a new instance of the CDBuilder class.
/// </summary>
Expand Down Expand Up @@ -180,6 +197,8 @@ public BuildDirectoryInfo AddDirectory(string name)
private void AddFile(BuildFileInfo fi)
{
_files.Add(fi);

UpdateProgress();
}

/// <summary>
Expand Down Expand Up @@ -311,7 +330,7 @@ protected override List<BuilderExtent> FixExtents(out long totalLength)

_bootEntry.WriteTo(bootCatalog, 0x20);
fixedRegions.Add(new BuilderBufferExtent(bootCatalogPos, bootCatalog));

// Don't add to focus, we already skipped the length of the bootCatalog
}

Expand Down Expand Up @@ -628,11 +647,11 @@ void IFileSystemBuilder.AddFile(string name, byte[] content, DateTime creationTi

void IFileSystemBuilder.AddFile(string name, Stream source, DateTime creationTime, DateTime writtenTime, DateTime accessedTime, FileAttributes attributes) =>
AddFile(name, source).CreationTime = writtenTime;

void IFileSystemBuilder.AddFile(string name, string sourcePath, DateTime creationTime, DateTime writtenTime, DateTime accessedTime, FileAttributes attributes) =>
AddFile(name, sourcePath).CreationTime = writtenTime;

bool IFileSystemBuilder.Exists(string path) => GetFile(path) != null;

public IFileSystem GenerateFileSystem() => new CDReader(Build(), UseJoliet);
}
}
4 changes: 2 additions & 2 deletions Library/DiscUtils.SquashFs/BuilderDirectory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2008-2011, Kenneth Bell
// Copyright (c) 2008-2024, Kenneth Bell, Olof Lagerkvist and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -43,7 +43,7 @@ public BuilderDirectory()

public void AddChild(string name, BuilderNode node)
{
if (name.Contains(@"\\"))
if (name.IndexOfAny(Utilities.PathSeparators) >= 0)
{
throw new ArgumentException("Single level of path must be provided", nameof(name));
}
Expand Down
31 changes: 30 additions & 1 deletion Library/DiscUtils.SquashFs/SquashFileSystemBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2008-2024, Kenneth Bell, Olof Lagerkvist
// Copyright (c) 2008-2024, Kenneth Bell, Olof Lagerkvist and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -45,6 +45,23 @@ public sealed class SquashFileSystemBuilder : StreamBuilder, IFileSystemBuilder

private BuilderDirectory _rootDir;

// Progress reporting event
public event EventHandler<ProgressEventArgs> ProgressChanged;

private ProgressEventArgs progressEventArgs;

// Method for updating progress
private void AddProgress(int newFiles, int newItems)
{
if (ProgressChanged is not null)
{
progressEventArgs ??= new();
progressEventArgs.TotalFiles += newFiles;
progressEventArgs.TotalItems += newItems;
ProgressChanged(this, progressEventArgs);
}
}

/// <summary>
/// Initializes a new instance of the SquashFileSystemBuilder class.
/// </summary>
Expand Down Expand Up @@ -155,7 +172,10 @@ public void AddFile(string path, Stream content, int user, int group, UnixFilePe
user,
group,
DefaultDirectoryPermissions);

dirNode.AddChild(Utilities.GetFileFromPath(path), file);

AddProgress(newFiles: 1, newItems: 1);
}

/// <summary>
Expand Down Expand Up @@ -187,7 +207,10 @@ public void AddFile(string path, byte[] content, int user, int group, UnixFilePe
user,
group,
DefaultDirectoryPermissions);

dirNode.AddChild(Utilities.GetFileFromPath(path), file);

AddProgress(newFiles: 1, newItems: 1);
}

/// <summary>
Expand Down Expand Up @@ -219,7 +242,10 @@ public void AddFile(string path, string contentPath, int user, int group, UnixFi
user,
group,
DefaultDirectoryPermissions);

dirNode.AddChild(Utilities.GetFileFromPath(path), file);

AddProgress(newFiles: 1, newItems: 1);
}

/// <summary>
Expand Down Expand Up @@ -266,6 +292,7 @@ public void AddDirectory(string path, int user, int group, UnixFilePermissions p
user,
group,
permissions);

parentDir.AddChild(Utilities.GetFileFromPath(path), dir);
}

Expand Down Expand Up @@ -535,6 +562,8 @@ private BuilderDirectory CreateDirectory(string path, int user, int group, UnixF
};

currentDir.AddChild(elems[i], nextDir);

AddProgress(newFiles: 0, newItems: 1);
}
else if (nextDir == null)
{
Expand Down
21 changes: 21 additions & 0 deletions Library/DiscUtils.VirtualFileSystem/TarFileSystemBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,31 @@
using DiscUtils.Archives;
using DiscUtils.Internal;
using System.IO;
using System.Collections.Specialized;
using System.Linq;
using LTRData.Extensions.Buffers;

namespace DiscUtils.VirtualFileSystem;
public class TarFileSystemBuilder : TarFileBuilder, IFileSystemBuilder
{
// Progress reporting event
public event EventHandler<ProgressEventArgs> ProgressChanged;

private ProgressEventArgs progressEventArgs;

protected override void AddFile(UnixBuildFileRecord file)
{
base.AddFile(file);

if (ProgressChanged is not null)
{
progressEventArgs ??= new();
progressEventArgs.TotalFiles += file.Name.EndsWith('/') ? 0 : 1;
progressEventArgs.TotalItems = FileCount;
ProgressChanged(this, progressEventArgs);
}
}

public string VolumeIdentifier { get; set; }

public IFileSystem GenerateFileSystem() => new TarFileSystem(Build(), VolumeIdentifier, ownsStream: true);
Expand Down
17 changes: 17 additions & 0 deletions Library/DiscUtils.VirtualFileSystem/VirtualFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ public partial class VirtualFileSystem : DiscFileSystem, IWindowsFileSystem, IUn
{
public delegate Stream FileOpenDelegate(FileMode mode, FileAccess access);

// Progress reporting event
public event EventHandler<ProgressEventArgs> ProgressChanged;

private ProgressEventArgs progressEventArgs;

// Method for updating progress
internal void AddProgress(int newFiles, int newItems)
{
if (ProgressChanged is not null)
{
progressEventArgs ??= new();
progressEventArgs.TotalFiles += newFiles;
progressEventArgs.TotalItems += newItems;
ProgressChanged(this, progressEventArgs);
}
}

public static string GetPathDirectoryName(string path)
{
if (string.IsNullOrEmpty(path))
Expand Down
Loading

0 comments on commit d2afda5

Please sign in to comment.