2019-04-06 11:03:43 +01:00
|
|
|
// /***************************************************************************
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : File.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : FATX filesystem plugin.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Methods to handle files.
|
|
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This library 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
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// Copyright © 2011-2019 Natalia Portillo
|
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-04-07 13:31:27 +01:00
|
|
|
using System.Collections.Generic;
|
2019-04-14 11:30:16 +01:00
|
|
|
using System.IO;
|
2019-04-10 20:25:16 +01:00
|
|
|
using System.Linq;
|
2019-04-06 11:03:43 +01:00
|
|
|
using DiscImageChef.CommonTypes.Structs;
|
2019-04-14 11:30:16 +01:00
|
|
|
using FileAttributes = DiscImageChef.CommonTypes.Structs.FileAttributes;
|
2019-04-06 11:03:43 +01:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.Filesystems.FATX
|
|
|
|
|
{
|
|
|
|
|
public partial class XboxFatPlugin
|
|
|
|
|
{
|
2019-04-07 11:41:35 +01:00
|
|
|
public Errno MapBlock(string path, long fileBlock, out long deviceBlock)
|
|
|
|
|
{
|
|
|
|
|
deviceBlock = 0;
|
|
|
|
|
if(!mounted) return Errno.AccessDenied;
|
2019-04-06 11:03:43 +01:00
|
|
|
|
2019-04-14 11:38:20 +01:00
|
|
|
Errno err = Stat(path, out FileEntryInfo stat);
|
|
|
|
|
|
|
|
|
|
if(err != Errno.NoError) return err;
|
|
|
|
|
|
|
|
|
|
uint[] clusters = GetClusters((uint)stat.Inode);
|
|
|
|
|
|
|
|
|
|
if(fileBlock >= clusters.Length) return Errno.InvalidArgument;
|
|
|
|
|
|
|
|
|
|
deviceBlock = (long)(firstClusterSector + (clusters[fileBlock] - 1) * sectorsPerCluster);
|
|
|
|
|
|
|
|
|
|
return Errno.NoError;
|
2019-04-07 11:41:35 +01:00
|
|
|
}
|
2019-04-06 11:03:43 +01:00
|
|
|
|
2019-04-07 11:41:35 +01:00
|
|
|
public Errno GetAttributes(string path, out FileAttributes attributes)
|
|
|
|
|
{
|
|
|
|
|
attributes = new FileAttributes();
|
|
|
|
|
if(!mounted) return Errno.AccessDenied;
|
2019-04-06 11:03:43 +01:00
|
|
|
|
2019-04-10 20:34:32 +01:00
|
|
|
Errno err = Stat(path, out FileEntryInfo stat);
|
|
|
|
|
|
|
|
|
|
if(err != Errno.NoError) return err;
|
|
|
|
|
|
|
|
|
|
attributes = stat.Attributes;
|
|
|
|
|
|
|
|
|
|
return Errno.NoError;
|
2019-04-07 11:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Errno Read(string path, long offset, long size, ref byte[] buf)
|
|
|
|
|
{
|
|
|
|
|
if(!mounted) return Errno.AccessDenied;
|
|
|
|
|
|
2019-04-14 11:30:16 +01:00
|
|
|
Errno err = Stat(path, out FileEntryInfo stat);
|
|
|
|
|
|
|
|
|
|
if(err != Errno.NoError) return err;
|
|
|
|
|
|
2019-04-14 11:57:05 +01:00
|
|
|
if(stat.Attributes.HasFlag(FileAttributes.Directory) && !debug) return Errno.IsDirectory;
|
|
|
|
|
|
2019-04-14 11:30:16 +01:00
|
|
|
if(offset >= stat.Length) return Errno.InvalidArgument;
|
|
|
|
|
|
|
|
|
|
if(size + offset >= stat.Length) size = stat.Length - offset;
|
|
|
|
|
|
|
|
|
|
uint[] clusters = GetClusters((uint)stat.Inode);
|
|
|
|
|
|
|
|
|
|
long firstCluster = offset / bytesPerCluster;
|
|
|
|
|
long offsetInCluster = offset % bytesPerCluster;
|
|
|
|
|
long sizeInClusters = (size + offsetInCluster) / bytesPerCluster;
|
|
|
|
|
if((size + offsetInCluster) % bytesPerCluster > 0) sizeInClusters++;
|
|
|
|
|
|
|
|
|
|
MemoryStream ms = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < sizeInClusters; i++)
|
|
|
|
|
{
|
|
|
|
|
if(i + firstCluster >= clusters.Length) return Errno.InvalidArgument;
|
|
|
|
|
|
|
|
|
|
byte[] buffer =
|
|
|
|
|
imagePlugin.ReadSectors(firstClusterSector + (clusters[i + firstCluster] - 1) * sectorsPerCluster,
|
|
|
|
|
sectorsPerCluster);
|
|
|
|
|
|
|
|
|
|
ms.Write(buffer, 0, buffer.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ms.Position = offsetInCluster;
|
|
|
|
|
buf = new byte[size];
|
|
|
|
|
ms.Read(buf, 0, (int)size);
|
|
|
|
|
|
|
|
|
|
return Errno.NoError;
|
2019-04-07 11:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Errno Stat(string path, out FileEntryInfo stat)
|
|
|
|
|
{
|
|
|
|
|
stat = null;
|
|
|
|
|
if(!mounted) return Errno.AccessDenied;
|
|
|
|
|
|
2019-04-10 20:25:16 +01:00
|
|
|
if(debug && (string.IsNullOrEmpty(path) || path == "$" || path == "/"))
|
|
|
|
|
{
|
|
|
|
|
stat = new FileEntryInfo
|
|
|
|
|
{
|
|
|
|
|
Attributes = FileAttributes.Directory | FileAttributes.System | FileAttributes.Hidden,
|
|
|
|
|
Blocks = GetClusters(superblock.rootDirectoryCluster).Length,
|
|
|
|
|
BlockSize = bytesPerCluster,
|
|
|
|
|
DeviceNo = 0,
|
|
|
|
|
GID = 0,
|
|
|
|
|
Length = GetClusters(superblock.rootDirectoryCluster).Length * bytesPerCluster,
|
|
|
|
|
Inode = superblock.rootDirectoryCluster,
|
|
|
|
|
Links = 1,
|
|
|
|
|
Mode = 0x16D,
|
|
|
|
|
UID = 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Errno err = GetFileEntry(path, out DirectoryEntry entry);
|
|
|
|
|
if(err != Errno.NoError) return err;
|
|
|
|
|
|
|
|
|
|
stat = new FileEntryInfo
|
|
|
|
|
{
|
|
|
|
|
Attributes = new FileAttributes(),
|
|
|
|
|
Blocks = entry.length / bytesPerCluster,
|
|
|
|
|
BlockSize = bytesPerCluster,
|
|
|
|
|
DeviceNo = 0,
|
|
|
|
|
GID = 0,
|
|
|
|
|
Length = entry.length,
|
|
|
|
|
Inode = entry.firstCluster,
|
|
|
|
|
Links = 1,
|
|
|
|
|
Mode = (uint)(entry.attributes.HasFlag(Attributes.Directory) ? 0x16D : 0x124),
|
|
|
|
|
UID = 0,
|
|
|
|
|
CreationTime = DateHandlers.DosToDateTime(entry.creationDate, entry.creationTime).AddYears(20),
|
|
|
|
|
AccessTime = DateHandlers.DosToDateTime(entry.lastAccessDate, entry.lastAccessTime).AddYears(20),
|
|
|
|
|
LastWriteTime = DateHandlers.DosToDateTime(entry.lastWrittenDate, entry.lastWrittenTime)
|
|
|
|
|
.AddYears(20)
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-14 11:31:48 +01:00
|
|
|
if(entry.length % bytesPerCluster > 0) stat.Blocks++;
|
|
|
|
|
|
2019-04-10 20:25:16 +01:00
|
|
|
if(entry.attributes.HasFlag(Attributes.Directory))
|
|
|
|
|
{
|
|
|
|
|
stat.Attributes |= FileAttributes.Directory;
|
|
|
|
|
stat.Blocks = GetClusters(entry.firstCluster).Length;
|
|
|
|
|
stat.Length = stat.Blocks * stat.BlockSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(entry.attributes.HasFlag(Attributes.ReadOnly)) stat.Attributes |= FileAttributes.ReadOnly;
|
|
|
|
|
if(entry.attributes.HasFlag(Attributes.Hidden)) stat.Attributes |= FileAttributes.Hidden;
|
|
|
|
|
if(entry.attributes.HasFlag(Attributes.System)) stat.Attributes |= FileAttributes.System;
|
|
|
|
|
if(entry.attributes.HasFlag(Attributes.Archive)) stat.Attributes |= FileAttributes.Archive;
|
|
|
|
|
|
|
|
|
|
return Errno.NoError;
|
2019-04-07 11:41:35 +01:00
|
|
|
}
|
2019-04-07 13:31:27 +01:00
|
|
|
|
|
|
|
|
uint[] GetClusters(uint startCluster)
|
|
|
|
|
{
|
|
|
|
|
if(startCluster == 0) return null;
|
|
|
|
|
|
|
|
|
|
if(fat16 is null)
|
|
|
|
|
{
|
|
|
|
|
if(startCluster >= fat32.Length) return null;
|
|
|
|
|
}
|
|
|
|
|
else if(startCluster >= fat16.Length) return null;
|
|
|
|
|
|
|
|
|
|
List<uint> clusters = new List<uint>();
|
|
|
|
|
|
|
|
|
|
uint nextCluster = startCluster;
|
|
|
|
|
|
|
|
|
|
if(fat16 is null)
|
|
|
|
|
while((nextCluster & FAT32_MASK) > 0 && (nextCluster & FAT32_MASK) <= FAT32_BAD)
|
|
|
|
|
{
|
|
|
|
|
clusters.Add(nextCluster);
|
|
|
|
|
nextCluster = fat32[nextCluster];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
while(nextCluster > 0 && nextCluster <= FAT16_BAD)
|
|
|
|
|
{
|
|
|
|
|
clusters.Add(nextCluster);
|
|
|
|
|
nextCluster = fat16[nextCluster];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return clusters.ToArray();
|
|
|
|
|
}
|
2019-04-10 20:25:16 +01:00
|
|
|
|
|
|
|
|
Errno GetFileEntry(string path, out DirectoryEntry entry)
|
|
|
|
|
{
|
|
|
|
|
entry = new DirectoryEntry();
|
|
|
|
|
|
|
|
|
|
string cutPath =
|
2019-04-14 11:30:16 +01:00
|
|
|
path.StartsWith("/") ? path.Substring(1).ToLower(cultureInfo) : path.ToLower(cultureInfo);
|
2019-04-10 20:25:16 +01:00
|
|
|
string[] pieces = cutPath.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
|
|
|
|
if(pieces.Length == 0) return Errno.InvalidArgument;
|
|
|
|
|
|
|
|
|
|
string parentPath = string.Join("/", pieces, 0, pieces.Length - 1);
|
|
|
|
|
|
|
|
|
|
Errno err = ReadDir(parentPath, out _);
|
|
|
|
|
|
|
|
|
|
if(err != Errno.NoError) return err;
|
|
|
|
|
|
|
|
|
|
Dictionary<string, DirectoryEntry> parent;
|
|
|
|
|
|
|
|
|
|
if(pieces.Length == 1) parent = rootDirectory;
|
|
|
|
|
else if(!directoryCache.TryGetValue(parentPath, out parent)) return Errno.InvalidArgument;
|
|
|
|
|
|
|
|
|
|
KeyValuePair<string, DirectoryEntry> dirent =
|
|
|
|
|
parent.FirstOrDefault(t => t.Key.ToLower(cultureInfo) == pieces[pieces.Length - 1]);
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrEmpty(dirent.Key)) return Errno.NoSuchFile;
|
|
|
|
|
|
|
|
|
|
entry = dirent.Value;
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
2019-04-06 11:03:43 +01:00
|
|
|
}
|
|
|
|
|
}
|