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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-29 17:34:38 +00:00
|
|
|
// Copyright © 2011-2019 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;
|
2018-06-25 19:08:16 +01:00
|
|
|
using DiscImageChef.CommonTypes.Structs;
|
2019-03-01 07:35:22 +00:00
|
|
|
using DiscImageChef.Helpers;
|
2016-10-07 00:41:59 +01:00
|
|
|
|
|
|
|
|
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>
|
2017-12-24 02:37:41 +00:00
|
|
|
/// Solves a symbolic link.
|
2016-10-07 00:41:59 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">Link path.</param>
|
|
|
|
|
/// <param name="dest">Link destination.</param>
|
2017-12-26 08:17:28 +00:00
|
|
|
public Errno ReadLink(string path, out string dest)
|
2016-10-07 00:41:59 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
dest = null;
|
2017-12-22 07:05:08 +00:00
|
|
|
return !mounted ? Errno.AccessDenied : Errno.NotSupported;
|
2016-10-07 00:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-24 02:37:41 +00:00
|
|
|
/// Lists contents from a directory.
|
2016-10-07 00:41:59 +01:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">Directory path.</param>
|
|
|
|
|
/// <param name="contents">Directory contents.</param>
|
2017-12-26 08:17:28 +00:00
|
|
|
public Errno ReadDir(string path, out List<string> contents)
|
2016-10-07 00:41:59 +01:00
|
|
|
{
|
2017-12-26 08:17:28 +00:00
|
|
|
contents = null;
|
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();
|
2018-06-22 08:08:38 +01:00
|
|
|
ulong lba = (ulong)(vtoc.catalogTrack * sectorsPerTrack + vtoc.catalogSector);
|
2016-10-07 00:41:59 +01:00
|
|
|
totalFileEntries = 0;
|
2018-06-22 08:08:38 +01:00
|
|
|
catalogCache = new Dictionary<string, ushort>();
|
|
|
|
|
fileTypeCache = new Dictionary<string, byte>();
|
|
|
|
|
fileSizeCache = new Dictionary<string, int>();
|
|
|
|
|
lockedFiles = new List<string>();
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
if(lba == 0 || lba > device.Info.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
|
2019-03-01 07:35:22 +00:00
|
|
|
CatalogSector catSector = Marshal.ByteArrayToStructureLittleEndian<CatalogSector>(catSectorB);
|
2016-10-07 00:41:59 +01:00
|
|
|
|
2017-12-24 02:37:41 +00:00
|
|
|
foreach(FileEntry entry in catSector.entries.Where(entry => entry.extentTrack > 0))
|
|
|
|
|
{
|
2017-12-21 07:08:26 +00:00
|
|
|
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];
|
2018-06-22 08:08:38 +01:00
|
|
|
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-26 08:01:40 +00:00
|
|
|
string filename = StringHandlers.SpacePaddedToString(filenameB, Encoding);
|
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-26 06:05:12 +00:00
|
|
|
if(lba > device.Info.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
|
|
|
}
|