2017-05-27 20:02:57 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : DataFile.cs
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2017-05-27 20:02:57 +01:00
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Core algorithms.
|
2017-05-27 20:02:57 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Abstracts writing to files.
|
2017-05-27 20:02:57 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2017-12-20 02:08:37 +00:00
|
|
|
|
// it under the terms of the GNU General public License as
|
2017-05-27 20:02:57 +01:00
|
|
|
|
// 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
|
2017-12-20 02:08:37 +00:00
|
|
|
|
// GNU General public License for more details.
|
2017-05-27 20:02:57 +01:00
|
|
|
|
//
|
2017-12-20 02:08:37 +00:00
|
|
|
|
// You should have received a copy of the GNU General public License
|
2017-05-27 20:02:57 +01:00
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2017-05-27 20:02:57 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 03:50:57 +00:00
|
|
|
|
|
2017-05-27 20:02:57 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using DiscImageChef.Console;
|
|
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Core
|
|
|
|
|
|
{
|
|
|
|
|
|
public class DataFile
|
|
|
|
|
|
{
|
2017-06-07 23:24:30 +01:00
|
|
|
|
FileStream dataFs;
|
2017-05-27 20:02:57 +01:00
|
|
|
|
|
|
|
|
|
|
public DataFile(string outputFile)
|
|
|
|
|
|
{
|
|
|
|
|
|
dataFs = new FileStream(outputFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(dataFs != null) dataFs.Close();
|
2017-05-27 20:02:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int Read(byte[] array, int offset, int count)
|
|
|
|
|
|
{
|
|
|
|
|
|
return dataFs.Read(array, offset, count);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-20 05:48:09 +01:00
|
|
|
|
public long Seek(ulong block, ulong blockSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
return dataFs.Seek((long)(block * blockSize), SeekOrigin.Begin);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public long Seek(ulong offset, SeekOrigin origin)
|
|
|
|
|
|
{
|
|
|
|
|
|
return dataFs.Seek((long)offset, origin);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-27 20:02:57 +01:00
|
|
|
|
public long Seek(long offset, SeekOrigin origin)
|
|
|
|
|
|
{
|
|
|
|
|
|
return dataFs.Seek(offset, origin);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Write(byte[] data)
|
|
|
|
|
|
{
|
2017-06-07 23:24:30 +01:00
|
|
|
|
Write(data, 0, data.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Write(byte[] data, int offset, int count)
|
|
|
|
|
|
{
|
|
|
|
|
|
dataFs.Write(data, offset, count);
|
2017-05-27 20:02:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void WriteAt(byte[] data, ulong block, uint blockSize)
|
2017-06-07 23:24:30 +01:00
|
|
|
|
{
|
|
|
|
|
|
WriteAt(data, block, blockSize, 0, data.Length);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void WriteAt(byte[] data, ulong block, uint blockSize, int offset, int count)
|
2017-05-27 20:02:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
dataFs.Seek((long)(block * blockSize), SeekOrigin.Begin);
|
2017-06-07 23:24:30 +01:00
|
|
|
|
dataFs.Write(data, offset, count);
|
2017-05-27 20:02:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
public long Position
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return dataFs.Position; }
|
|
|
|
|
|
}
|
2017-06-07 22:27:32 +01:00
|
|
|
|
|
2017-05-27 20:02:57 +01:00
|
|
|
|
public static void WriteTo(string who, string outputPrefix, string outputSuffix, string what, byte[] data)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!string.IsNullOrEmpty(outputPrefix) && !string.IsNullOrEmpty(outputSuffix))
|
|
|
|
|
|
WriteTo(who, outputPrefix + outputSuffix, data, what);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
public static void WriteTo(string who, string filename, byte[] data, string whatWriting = null,
|
|
|
|
|
|
bool overwrite = false)
|
2017-05-27 20:02:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
if(!string.IsNullOrEmpty(filename))
|
|
|
|
|
|
{
|
2017-06-20 06:42:09 +01:00
|
|
|
|
if(File.Exists(filename))
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(overwrite) File.Delete(filename);
|
2017-06-20 06:42:09 +01:00
|
|
|
|
else
|
2017-05-27 20:02:57 +01:00
|
|
|
|
{
|
2017-06-20 06:42:09 +01:00
|
|
|
|
DicConsole.ErrorWriteLine("Not overwriting file {0}", filename);
|
|
|
|
|
|
return;
|
2017-05-27 20:02:57 +01:00
|
|
|
|
}
|
2017-06-20 06:42:09 +01:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.DebugWriteLine(who, "Writing " + whatWriting + " to {0}", filename);
|
|
|
|
|
|
FileStream outputFs = new FileStream(filename, FileMode.CreateNew);
|
|
|
|
|
|
outputFs.Write(data, 0, data.Length);
|
|
|
|
|
|
outputFs.Close();
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
catch { DicConsole.ErrorWriteLine("Unable to write file {0}", filename); }
|
2017-05-27 20:02:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|