mirror of
https://github.com/ReneLergner/WPinternals.git
synced 2026-06-14 03:16:40 +10:00
DiscUtils: Fat: Merge more upstream changes
This commit is contained in:
@@ -20,20 +20,20 @@
|
|||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using DiscUtils.Streams;
|
||||||
|
|
||||||
namespace DiscUtils.Fat
|
namespace DiscUtils.Fat
|
||||||
{
|
{
|
||||||
using DiscUtils.Streams;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
internal class FatFileStream : SparseStream
|
internal class FatFileStream : SparseStream
|
||||||
{
|
{
|
||||||
private Directory _dir;
|
private readonly Directory _dir;
|
||||||
private long _dirId;
|
private readonly long _dirId;
|
||||||
private ClusterStream _stream;
|
private readonly ClusterStream _stream;
|
||||||
|
|
||||||
private bool didWrite = false;
|
private bool didWrite;
|
||||||
|
|
||||||
public FatFileStream(FatFileSystem fileSystem, Directory dir, long fileId, FileAccess access)
|
public FatFileStream(FatFileSystem fileSystem, Directory dir, long fileId, FileAccess access)
|
||||||
{
|
{
|
||||||
@@ -41,16 +41,10 @@ namespace DiscUtils.Fat
|
|||||||
_dirId = fileId;
|
_dirId = fileId;
|
||||||
|
|
||||||
DirectoryEntry dirEntry = _dir.GetEntry(_dirId);
|
DirectoryEntry dirEntry = _dir.GetEntry(_dirId);
|
||||||
_stream = new ClusterStream(fileSystem, access, (uint)dirEntry.FirstCluster, (uint)dirEntry.FileSize);
|
_stream = new ClusterStream(fileSystem, access, dirEntry.FirstCluster, (uint)dirEntry.FileSize);
|
||||||
_stream.FirstClusterChanged += FirstClusterAllocatedHandler;
|
_stream.FirstClusterChanged += FirstClusterAllocatedHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override long Position
|
|
||||||
{
|
|
||||||
get { return _stream.Position; }
|
|
||||||
set { _stream.Position = value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool CanRead
|
public override bool CanRead
|
||||||
{
|
{
|
||||||
get { return _stream.CanRead; }
|
get { return _stream.CanRead; }
|
||||||
@@ -66,20 +60,23 @@ namespace DiscUtils.Fat
|
|||||||
get { return _stream.CanWrite; }
|
get { return _stream.CanWrite; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<StreamExtent> Extents
|
||||||
|
{
|
||||||
|
get { return new[] { new StreamExtent(0, Length) }; }
|
||||||
|
}
|
||||||
|
|
||||||
public override long Length
|
public override long Length
|
||||||
{
|
{
|
||||||
get { return _stream.Length; }
|
get { return _stream.Length; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public override IEnumerable<StreamExtent> Extents
|
public override long Position
|
||||||
{
|
{
|
||||||
get
|
get { return _stream.Position; }
|
||||||
{
|
set { _stream.Position = value; }
|
||||||
return new StreamExtent[] { new StreamExtent(0, Length) };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Close()
|
protected override void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (_dir.FileSystem.CanWrite)
|
if (_dir.FileSystem.CanWrite)
|
||||||
{
|
{
|
||||||
@@ -99,7 +96,7 @@ namespace DiscUtils.Fat
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
base.Close();
|
base.Dispose(disposing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -138,4 +135,4 @@ namespace DiscUtils.Fat
|
|||||||
_dir.UpdateEntry(_dirId, dirEntry);
|
_dir.UpdateEntry(_dirId, dirEntry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,23 +20,28 @@
|
|||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using DiscUtils.Internal;
|
||||||
|
|
||||||
namespace DiscUtils.Fat
|
namespace DiscUtils.Fat
|
||||||
{
|
{
|
||||||
using DiscUtils.Internal;
|
|
||||||
using System;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
internal sealed class FileName : IEquatable<FileName>
|
internal sealed class FileName : IEquatable<FileName>
|
||||||
{
|
{
|
||||||
public static readonly FileName SelfEntryName = new FileName(new byte[] { 0x2E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, 0);
|
|
||||||
public static readonly FileName ParentEntryName = new FileName(new byte[] { 0x2E, 0x2E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, 0);
|
|
||||||
public static readonly FileName Null = new FileName(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0);
|
|
||||||
|
|
||||||
private const byte SpaceByte = 0x20;
|
private const byte SpaceByte = 0x20;
|
||||||
|
|
||||||
private static readonly byte[] InvalidBytes = new byte[] { 0x22, 0x2A, 0x2B, 0x2C, 0x2E, 0x2F, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x5B, 0x5C, 0x5D, 0x7C };
|
public static readonly FileName SelfEntryName =
|
||||||
|
new FileName(new byte[] { 0x2E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, 0);
|
||||||
|
|
||||||
private byte[] _raw;
|
public static readonly FileName ParentEntryName =
|
||||||
|
new FileName(new byte[] { 0x2E, 0x2E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }, 0);
|
||||||
|
|
||||||
|
public static readonly FileName Null =
|
||||||
|
new FileName(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 0);
|
||||||
|
|
||||||
|
private static readonly byte[] InvalidBytes = { 0x22, 0x2A, 0x2B, 0x2C, 0x2E, 0x2F, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x5B, 0x5C, 0x5D, 0x7C };
|
||||||
|
|
||||||
|
private readonly byte[] _raw;
|
||||||
|
|
||||||
public FileName(byte[] data, int offset)
|
public FileName(byte[] data, int offset)
|
||||||
{
|
{
|
||||||
@@ -56,7 +61,7 @@ namespace DiscUtils.Fat
|
|||||||
byte b = bytes[nameIdx++];
|
byte b = bytes[nameIdx++];
|
||||||
if (b < 0x20 || Contains(InvalidBytes, b))
|
if (b < 0x20 || Contains(InvalidBytes, b))
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Invalid character in file name '" + (char)b + "'", "name");
|
throw new ArgumentException("Invalid character in file name '" + (char)b + "'", nameof(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
_raw[rawIdx++] = b;
|
_raw[rawIdx++] = b;
|
||||||
@@ -64,11 +69,11 @@ namespace DiscUtils.Fat
|
|||||||
|
|
||||||
if (rawIdx > 8)
|
if (rawIdx > 8)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("File name too long '" + name + "'", "name");
|
throw new ArgumentException("File name too long '" + name + "'", nameof(name));
|
||||||
}
|
}
|
||||||
else if (rawIdx == 0)
|
if (rawIdx == 0)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("File name too short '" + name + "'", "name");
|
throw new ArgumentException("File name too short '" + name + "'", nameof(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
while (rawIdx < 8)
|
while (rawIdx < 8)
|
||||||
@@ -86,7 +91,7 @@ namespace DiscUtils.Fat
|
|||||||
byte b = bytes[nameIdx++];
|
byte b = bytes[nameIdx++];
|
||||||
if (b < 0x20 || Contains(InvalidBytes, b))
|
if (b < 0x20 || Contains(InvalidBytes, b))
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Invalid character in file extension '" + (char)b + "'", "name");
|
throw new ArgumentException("Invalid character in file extension '" + (char)b + "'", nameof(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
_raw[rawIdx++] = b;
|
_raw[rawIdx++] = b;
|
||||||
@@ -99,10 +104,20 @@ namespace DiscUtils.Fat
|
|||||||
|
|
||||||
if (nameIdx != bytes.Length)
|
if (nameIdx != bytes.Length)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("File extension too long '" + name + "'", "name");
|
throw new ArgumentException("File extension too long '" + name + "'", nameof(name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Equals(FileName other)
|
||||||
|
{
|
||||||
|
if (other == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return CompareRawNames(this, other) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
public static FileName FromPath(string path, Encoding encoding)
|
public static FileName FromPath(string path, Encoding encoding)
|
||||||
{
|
{
|
||||||
return new FileName(Utilities.GetFileFromPath(path), encoding);
|
return new FileName(Utilities.GetFileFromPath(path), encoding);
|
||||||
@@ -162,16 +177,6 @@ namespace DiscUtils.Fat
|
|||||||
return Equals(other as FileName);
|
return Equals(other as FileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Equals(FileName other)
|
|
||||||
{
|
|
||||||
if (other == null)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return CompareRawNames(this, other) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
public override int GetHashCode()
|
||||||
{
|
{
|
||||||
int val = 0x1A8D3C4E;
|
int val = 0x1A8D3C4E;
|
||||||
@@ -190,7 +195,7 @@ namespace DiscUtils.Fat
|
|||||||
{
|
{
|
||||||
if (a._raw[i] != b._raw[i])
|
if (a._raw[i] != b._raw[i])
|
||||||
{
|
{
|
||||||
return (int)a._raw[i] - (int)b._raw[i];
|
return a._raw[i] - b._raw[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,4 +215,4 @@ namespace DiscUtils.Fat
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user