2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-10-07 00:41:59 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Dir.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Apple DOS filesystem plugin.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Methods to handle Apple DOS filesystem catalog (aka directory).
|
|
|
|
|
//
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-10-07 00:41:59 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2017-12-21 07:08:26 +00:00
|
|
|
using System.Linq;
|
2016-10-07 00:41:59 +01:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Filesystems.AppleDOS
|
|
|
|
|
{
|
2017-12-21 16:27:09 +00:00
|
|
|
public partial class AppleDOS
|
2016-10-07 00:41:59 +01:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Solves a symbolic link.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">Link path.</param>
|
|
|
|
|
/// <param name="dest">Link destination.</param>
|
|
|
|
|
public override Errno ReadLink(string path, ref string dest)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!mounted) return Errno.AccessDenied;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
return Errno.NotSupported;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Lists contents from a directory.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">Directory path.</param>
|
|
|
|
|
/// <param name="contents">Directory contents.</param>
|
|
|
|
|
public override Errno ReadDir(string path, ref List<string> contents)
|
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
if(!mounted) return Errno.AccessDenied;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
if(!string.IsNullOrEmpty(path) && string.Compare(path, "/", StringComparison.OrdinalIgnoreCase) != 0)
|
|
|
|
|
return Errno.NotSupported;
|
|
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
contents = catalogCache.Keys.ToList();
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
if(debug)
|
|
|
|
|
{
|
|
|
|
|
contents.Add("$");
|
|
|
|
|
contents.Add("$Boot");
|
|
|
|
|
contents.Add("$Vtoc");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contents.Sort();
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Errno ReadCatalog()
|
|
|
|
|
{
|
|
|
|
|
MemoryStream catalogMs = new MemoryStream();
|
2017-12-20 17:26:28 +00:00
|
|
|
ulong lba = (ulong)(vtoc.catalogTrack * sectorsPerTrack + vtoc.catalogSector);
|
2016-10-07 00:41:59 +01:00
|
|
|
totalFileEntries = 0;
|
|
|
|
|
catalogCache = new Dictionary<string, ushort>();
|
|
|
|
|
fileTypeCache = new Dictionary<string, byte>();
|
|
|
|
|
fileSizeCache = new Dictionary<string, int>();
|
|
|
|
|
lockedFiles = new List<string>();
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(lba == 0 || lba > device.ImageInfo.Sectors) return Errno.InvalidArgument;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
while(lba != 0)
|
|
|
|
|
{
|
|
|
|
|
usedSectors++;
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] catSectorB = device.ReadSector(lba);
|
2016-10-07 00:41:59 +01:00
|
|
|
totalFileEntries += 7;
|
2017-12-20 17:15:26 +00:00
|
|
|
if(debug) catalogMs.Write(catSectorB, 0, catSectorB.Length);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
// Read the catalog sector
|
2017-12-21 16:07:20 +00:00
|
|
|
CatalogSector catSector;
|
2016-10-07 00:41:59 +01:00
|
|
|
IntPtr catPtr = Marshal.AllocHGlobal(256);
|
2017-12-20 17:15:26 +00:00
|
|
|
Marshal.Copy(catSectorB, 0, catPtr, 256);
|
2016-10-07 00:41:59 +01:00
|
|
|
catSector = (CatalogSector)Marshal.PtrToStructure(catPtr, typeof(CatalogSector));
|
|
|
|
|
Marshal.FreeHGlobal(catPtr);
|
|
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
foreach(FileEntry entry in catSector.entries.Where(entry => entry.extentTrack > 0)) {
|
|
|
|
|
track1UsedByFiles |= entry.extentTrack == 1;
|
|
|
|
|
track2UsedByFiles |= entry.extentTrack == 2;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
byte[] filenameB = new byte[30];
|
|
|
|
|
ushort ts = (ushort)((entry.extentTrack << 8) | entry.extentSector);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
// Apple DOS has high byte set over ASCII.
|
|
|
|
|
for(int i = 0; i < 30; i++) filenameB[i] = (byte)(entry.filename[i] & 0x7F);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
string filename = StringHandlers.SpacePaddedToString(filenameB, CurrentEncoding);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
if(!catalogCache.ContainsKey(filename)) catalogCache.Add(filename, ts);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
if(!fileTypeCache.ContainsKey(filename))
|
|
|
|
|
fileTypeCache.Add(filename, (byte)(entry.typeAndFlags & 0x7F));
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
if(!fileSizeCache.ContainsKey(filename))
|
|
|
|
|
fileSizeCache.Add(filename, entry.length * vtoc.bytesPerSector);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-21 07:08:26 +00:00
|
|
|
if((entry.typeAndFlags & 0x80) == 0x80 && !lockedFiles.Contains(filename))
|
|
|
|
|
lockedFiles.Add(filename);
|
|
|
|
|
}
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-20 17:26:28 +00:00
|
|
|
lba = (ulong)(catSector.trackOfNext * sectorsPerTrack + catSector.sectorOfNext);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(lba > device.ImageInfo.Sectors) break;
|
2016-10-07 00:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(debug) catalogBlocks = catalogMs.ToArray();
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
return Errno.NoError;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|