Implement Stat in Xbox FAT plugin.

This commit is contained in:
2019-04-10 20:25:16 +01:00
parent 0d1e171c7d
commit 429a40de12
4 changed files with 88 additions and 6 deletions

View File

@@ -32,6 +32,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DiscImageChef.CommonTypes.Structs;
namespace DiscImageChef.Filesystems.FATX
@@ -66,7 +67,59 @@ namespace DiscImageChef.Filesystems.FATX
stat = null;
if(!mounted) return Errno.AccessDenied;
throw new NotImplementedException();
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)
};
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;
}
uint[] GetClusters(uint startCluster)
@@ -98,5 +151,35 @@ namespace DiscImageChef.Filesystems.FATX
return clusters.ToArray();
}
Errno GetFileEntry(string path, out DirectoryEntry entry)
{
entry = new DirectoryEntry();
string cutPath =
path.StartsWith("/") ? path.Substring(0).ToLower(cultureInfo) : path.ToLower(cultureInfo);
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;
}
}
}