Files
Aaru/Aaru.Core/DataFile.cs

159 lines
6.9 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : DataFile.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Abstracts writing to files.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General public License for more details.
//
// You should have received a copy of the GNU General public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2022-12-03 16:07:10 +00:00
// Copyright © 2011-2023 Natalia Portillo
// ****************************************************************************/
using System.Diagnostics.CodeAnalysis;
using System.IO;
2020-02-27 00:33:26 +00:00
using Aaru.Console;
using Aaru.Helpers;
namespace Aaru.Core;
2022-03-06 13:29:38 +00:00
/// <summary>Abstracts a datafile with a block based interface</summary>
2023-10-03 22:57:50 +01:00
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
[SuppressMessage("ReSharper", "UnusedMember.Global")]
2022-03-06 13:29:38 +00:00
public sealed class DataFile
{
2022-03-06 13:29:38 +00:00
readonly FileStream _dataFs;
/// <summary>Opens, or create, a new file</summary>
/// <param name="outputFile">File</param>
public DataFile(string outputFile) =>
_dataFs = new FileStream(outputFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
2023-10-03 22:57:50 +01:00
/// <summary>Current file position</summary>
public long Position => _dataFs.Position;
2022-03-06 13:29:38 +00:00
/// <summary>Closes the file</summary>
public void Close() => _dataFs?.Close();
/// <summary>Reads bytes at current position</summary>
/// <param name="array">Array to place read data within</param>
/// <param name="offset">Offset of <see cref="array" /> where data will be read</param>
/// <param name="count">How many bytes to read</param>
/// <returns>How many bytes were read</returns>
public int Read(byte[] array, int offset, int count) => _dataFs.EnsureRead(array, offset, count);
2022-03-06 13:29:38 +00:00
/// <summary>Seeks to the specified block</summary>
/// <param name="block">Block to seek to</param>
/// <param name="blockSize">Block size in bytes</param>
/// <returns>Position</returns>
public long Seek(ulong block, ulong blockSize) => _dataFs.Seek((long)(block * blockSize), SeekOrigin.Begin);
/// <summary>Seeks to specified byte position</summary>
/// <param name="offset">Byte position</param>
/// <param name="origin">Where to count for position</param>
/// <returns>Position</returns>
public long Seek(ulong offset, SeekOrigin origin) => _dataFs.Seek((long)offset, origin);
/// <summary>Seeks to specified byte position</summary>
/// <param name="offset">Byte position</param>
/// <param name="origin">Where to count for position</param>
/// <returns>Position</returns>
public long Seek(long offset, SeekOrigin origin) => _dataFs.Seek(offset, origin);
/// <summary>Writes data at current position</summary>
/// <param name="data">Data</param>
public void Write(byte[] data) => Write(data, 0, data.Length);
/// <summary>Writes data at current position</summary>
/// <param name="data">Data</param>
/// <param name="offset">Offset of data from where to start taking data to write</param>
/// <param name="count">How many bytes to write</param>
public void Write(byte[] data, int offset, int count) => _dataFs.Write(data, offset, count);
/// <summary>Writes data at specified block</summary>
/// <param name="data">Data</param>
/// <param name="block">Block</param>
/// <param name="blockSize">Bytes per block</param>
2022-03-07 07:36:44 +00:00
public void WriteAt(byte[] data, ulong block, uint blockSize) => WriteAt(data, block, blockSize, 0, data.Length);
2022-03-06 13:29:38 +00:00
/// <summary>Writes data at specified block</summary>
/// <param name="data">Data</param>
/// <param name="block">Block</param>
/// <param name="blockSize">Bytes per block</param>
/// <param name="offset">Offset of data from where to start taking data to write</param>
/// <param name="count">How many bytes to write</param>
public void WriteAt(byte[] data, ulong block, uint blockSize, int offset, int count)
{
2022-03-06 13:29:38 +00:00
_dataFs.Seek((long)(block * blockSize), SeekOrigin.Begin);
_dataFs.Write(data, offset, count);
}
2022-03-06 13:29:38 +00:00
/// <summary>Writes data to a newly created file</summary>
/// <param name="who">Who asked the file to be written (class, plugin, etc.)</param>
/// <param name="data">Data to write</param>
/// <param name="outputPrefix">First part of the file name</param>
/// <param name="outputSuffix">Last part of the file name</param>
/// <param name="whatWriting">What is the data about?</param>
2022-03-07 07:36:44 +00:00
public static void WriteTo(string who, string outputPrefix, string outputSuffix, string whatWriting, byte[] data)
2022-03-06 13:29:38 +00:00
{
if(!string.IsNullOrEmpty(outputPrefix) && !string.IsNullOrEmpty(outputSuffix))
2022-03-06 13:29:38 +00:00
WriteTo(who, outputPrefix + outputSuffix, data, whatWriting);
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
/// <summary>Writes data to a newly created file</summary>
/// <param name="who">Who asked the file to be written (class, plugin, etc.)</param>
/// <param name="filename">Filename to create</param>
/// <param name="data">Data to write</param>
/// <param name="whatWriting">What is the data about?</param>
/// <param name="overwrite">If set to <c>true</c> overwrites the file, does nothing otherwise</param>
public static void WriteTo(string who, string filename, byte[] data, string whatWriting = null,
2023-10-03 22:57:50 +01:00
bool overwrite = false)
2022-03-06 13:29:38 +00:00
{
if(string.IsNullOrEmpty(filename))
return;
2022-03-06 13:29:38 +00:00
if(File.Exists(filename))
2023-10-03 22:57:50 +01:00
{
2022-03-06 13:29:38 +00:00
if(overwrite)
File.Delete(filename);
else
{
AaruConsole.ErrorWriteLine(Localization.Core.Not_overwriting_file_0, filename);
2022-03-06 13:29:38 +00:00
return;
2020-02-29 18:03:35 +00:00
}
2023-10-03 22:57:50 +01:00
}
2022-03-06 13:29:38 +00:00
try
{
AaruConsole.DebugWriteLine(who, string.Format(Localization.Core.Writing_0_to_1, whatWriting, filename));
2022-03-06 13:29:38 +00:00
var outputFs = new FileStream(filename, FileMode.CreateNew);
outputFs.Write(data, 0, data.Length);
outputFs.Close();
}
catch
{
AaruConsole.ErrorWriteLine(Localization.Core.Unable_to_write_file_0, filename);
}
}
2017-12-19 20:33:03 +00:00
}