Skip to content

Commit

Permalink
Merge pull request #62 from artehe/remoted
Browse files Browse the repository at this point in the history
Remoted
  • Loading branch information
artehe authored Oct 24, 2024
2 parents d652f59 + 85214e1 commit a933cf6
Show file tree
Hide file tree
Showing 101 changed files with 4,070 additions and 497 deletions.
26 changes: 0 additions & 26 deletions .vscode/launch.json

This file was deleted.

54 changes: 20 additions & 34 deletions Netimobiledevice/Afc/AfcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,14 @@ public class AfcService : LockdownService

private ulong _packetNumber;

public AfcService(LockdownServiceProvider lockdown, string serviceName, ILogger? logger = null) : base(lockdown, GetServiceName(lockdown, serviceName), logger: logger)
public AfcService(LockdownServiceProvider lockdown, string serviceName, ILogger? logger) : base(lockdown, serviceName, logger: logger)
{
_packetNumber = 0;
}

public AfcService(LockdownServiceProvider client) : this(client, string.Empty) { }
public AfcService(LockdownServiceProvider lockdown, ILogger? logger = null) : this(lockdown, RSD_SERVICE_NAME, logger) { }

private static string GetServiceName(LockdownServiceProvider lockdown, string providedServiceName)
{
string serviceName = providedServiceName;
if (string.IsNullOrEmpty(serviceName)) {
if (lockdown is LockdownClient) {
serviceName = LOCKDOWN_SERVICE_NAME;
}
else {
serviceName = RSD_SERVICE_NAME;
}
}
return serviceName;
}
public AfcService(LockdownClient lockdown, ILogger? logger = null) : this(lockdown, LOCKDOWN_SERVICE_NAME, logger) { }

private async Task DispatchPacket(AfcOpCode opCode, AfcPacket packet, CancellationToken cancellationToken, ulong? thisLength = null)
{
Expand All @@ -62,7 +50,7 @@ private async Task DispatchPacket(AfcOpCode opCode, AfcPacket packet, Cancellati

private async Task<byte[]> FileRead(ulong handle, ulong size, CancellationToken cancellationToken)
{
List<byte> data = new List<byte>();
List<byte> data = [];
while (size > 0) {
ulong toRead;
if (size > MAXIMUM_READ_SIZE) {
Expand All @@ -87,7 +75,7 @@ private async Task<byte[]> FileRead(ulong handle, ulong size, CancellationToken
data.AddRange(chunk);
}

return data.ToArray();
return [.. data];
}

public async Task<DictionaryNode> GetFileInfo(string filename, CancellationToken cancellationToken)
Expand Down Expand Up @@ -128,17 +116,17 @@ public async Task<DictionaryNode> GetFileInfo(string filename, CancellationToken
private static List<string> ParseFileInfoResponseForMessage(byte[] data)
{
string decodedData = Encoding.UTF8.GetString(data);
List<string> seperatedData = decodedData.Split('\0').ToList();
List<string> seperatedData = [.. decodedData.Split('\0')];
seperatedData.RemoveAt(seperatedData.Count - 1);
return seperatedData;
}

private static Dictionary<string, string> ParseFileInfoResponseToDict(byte[] data)
{
Dictionary<string, string> result = new Dictionary<string, string>();
Dictionary<string, string> result = [];

string decodedData = Encoding.UTF8.GetString(data);
List<string> seperatedData = decodedData.Split('\0').ToList();
List<string> seperatedData = [.. decodedData.Split('\0')];

seperatedData.RemoveAt(seperatedData.Count - 1);
if (seperatedData.Count % 2 != 0) {
Expand Down Expand Up @@ -169,7 +157,7 @@ private async Task<byte[]> RunOperation(AfcOpCode opCode, AfcPacket packet, Canc
byte[] response = await Service.ReceiveAsync(AfcHeader.GetSize(), cancellationToken).ConfigureAwait(false);

AfcError status = AfcError.Success;
byte[] data = Array.Empty<byte>();
byte[] data = [];

if (response.Length > 0) {
AfcHeader header = AfcHeader.FromBytes(response);
Expand All @@ -195,7 +183,7 @@ private async Task<string> ResolvePath(string filename, CancellationToken cancel
DictionaryNode info = await GetFileInfo(filename, cancellationToken).ConfigureAwait(false);
if (info.ContainsKey("st_ifmt") && info["st_ifmt"].AsStringNode().Value == "S_IFLNK") {
string target = info["LinkTarget"].AsStringNode().Value;
if (!target.StartsWith("/", StringComparison.InvariantCulture)) {
if (!target.StartsWith('/')) {
// Relative path
string filePath = Path.GetDirectoryName(filename) ?? string.Empty;
filename = Path.Combine(filePath, target);
Expand Down Expand Up @@ -295,7 +283,7 @@ public async Task FileWrite(ulong handle, byte[] data, CancellationToken cancell
int chunksCount = data.Length / chunkSize;
Logger?.LogDebug("Writing {dataSize} bytes in {chunksCount} chunks", dataSize, chunksCount);

List<byte> writtenData = new List<byte>();
List<byte> writtenData = [];
for (int i = 0; i < chunksCount; i++) {
cancellationToken.ThrowIfCancellationRequested();
Logger?.LogDebug("Writing chunk {i}", i);
Expand Down Expand Up @@ -333,7 +321,7 @@ public async Task FileWrite(ulong handle, byte[] data, CancellationToken cancell

public async Task<List<string>> GetDirectoryList(CancellationToken cancellationToken)
{
List<string> directoryList = new List<string>();
List<string> directoryList = [];
try {
AfcFileInfoRequest request = new AfcFileInfoRequest("/");
byte[] response = await RunOperation(AfcOpCode.ReadDir, request, cancellationToken).ConfigureAwait(false);
Expand All @@ -354,8 +342,8 @@ private async Task<List<string>> ListDirectory(string filename, CancellationToke

private async IAsyncEnumerable<Tuple<string, List<string>, List<string>>> Walk(string directory, [EnumeratorCancellation] CancellationToken cancellationToken)
{
List<string> directories = new List<string>();
List<string> files = new List<string>();
List<string> directories = [];
List<string> files = [];

foreach (string fd in await ListDirectory(directory, cancellationToken).ConfigureAwait(false)) {
if (new string[] { ".", "..", "" }.Contains(fd)) {
Expand Down Expand Up @@ -409,9 +397,7 @@ public async IAsyncEnumerable<string> LsDirectory(string path, [EnumeratorCancel
continue;
}

List<string> results = new List<string>();
results.AddRange(dirs.ToArray());
results.AddRange(files.ToArray());
List<string> results = [.. dirs.ToArray(), .. files.ToArray()];
foreach (string entry in results) {
yield return $"{folder}/{entry}";
}
Expand Down Expand Up @@ -469,20 +455,20 @@ public async Task<List<string>> Rm(string filename, CancellationToken cancellati
{
if (!await Exists(filename, cancellationToken).ConfigureAwait(false)) {
if (!await RmSingle(filename, cancellationToken, force: force).ConfigureAwait(false)) {
return new List<string>() { filename };
return [filename];
}
}

// Single file
if (!await IsDir(filename, cancellationToken).ConfigureAwait(false)) {
if (await RmSingle(filename, cancellationToken, force: force).ConfigureAwait(false)) {
return new List<string>();
return [];
}
return new List<string>() { filename };
return [filename];
}

// Directory Content
List<string> undeletedItems = new List<string>();
List<string> undeletedItems = [];
foreach (string entry in await ListDirectory(filename, cancellationToken).ConfigureAwait(false)) {
string currentFile = $"{filename}/{entry}";
if (await IsDir(currentFile, cancellationToken).ConfigureAwait(false)) {
Expand Down Expand Up @@ -517,7 +503,7 @@ public async Task<List<string>> Rm(string filename, CancellationToken cancellati
throw new AfcException($"Failed to delete paths: {string.Join(", ", undeletedItems)}");
}

return new List<string>();
return [];
}

public async Task SetFileContents(string filename, byte[] data, CancellationToken cancellationToken)
Expand Down
6 changes: 3 additions & 3 deletions Netimobiledevice/Afc/CrashReportsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void Dispose()
/// </summary>
public async Task Clear(CancellationToken cancellationToken = default)
{
List<string> undeletedFiles = new List<string>();
List<string> undeletedFiles = [];
foreach (string filename in await GetCrashReportsList("/", cancellationToken: cancellationToken).ConfigureAwait(false)) {
undeletedFiles.AddRange(await _afcService.Rm(filename, cancellationToken, force: true).ConfigureAwait(false));
}
Expand All @@ -78,7 +78,7 @@ public async Task Clear(CancellationToken cancellationToken = default)
public async Task<List<string>> GetCrashReportsList(string path = "/", int depth = 1, CancellationToken cancellationToken = default)
{
// Get the results then skip the root path '/'
List<string> results = new List<string>();
List<string> results = [];
await foreach (string item in _afcService.LsDirectory(path, cancellationToken, depth).ConfigureAwait(false)) {
results.Add(item);
}
Expand All @@ -95,7 +95,7 @@ public async Task GetCrashReport(string outDir, string entry = "/", bool erase =
{
await _afcService.Pull(entry, outDir, cancellationToken).ConfigureAwait(false);
if (erase) {
string[] paths = new string[] { ".", "/" };
string[] paths = [".", "/"];
if (paths.Contains(entry.Trim())) {
await Clear(cancellationToken).ConfigureAwait(false);
}
Expand Down
12 changes: 3 additions & 9 deletions Netimobiledevice/Afc/HouseArrestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed class HouseArrestService : AfcService

private readonly bool documentsOnly;

public HouseArrestService(LockdownServiceProvider lockdown, string bundleId, bool documentsOnly, ILogger? logger = null) : base(lockdown, GetServiceName(lockdown), logger)
public HouseArrestService(LockdownServiceProvider lockdown, string serviceName, string bundleId, bool documentsOnly = false, ILogger? logger = null) : base(lockdown, serviceName, logger)
{
string cmd = VEND_CONTAINER;
if (documentsOnly) {
Expand All @@ -30,15 +30,9 @@ public HouseArrestService(LockdownServiceProvider lockdown, string bundleId, boo
}
}

public HouseArrestService(LockdownServiceProvider lockdown, string bundleId, ILogger? logger = null) : this(lockdown, bundleId, false, logger) { }
public HouseArrestService(LockdownServiceProvider lockdown, string bundleId, ILogger? logger = null) : this(lockdown, RSD_SERVICE_NAME, bundleId, false, logger) { }

private static string GetServiceName(LockdownServiceProvider lockdown)
{
if (lockdown is LockdownClient) {
return SERVICE_NAME;
}
return RSD_SERVICE_NAME;
}
public HouseArrestService(LockdownClient lockdown, string bundleId, ILogger? logger = null) : this(lockdown, SERVICE_NAME, bundleId, false, logger) { }

public void SendCommand(string bundleId, string cmd = VEND_CONTAINER)
{
Expand Down
17 changes: 5 additions & 12 deletions Netimobiledevice/Afc/Packets/AfcFileInfoRequest.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
using System.Collections.Generic;
using Netimobiledevice.Extentions;
using System.Text;

namespace Netimobiledevice.Afc.Packets
{
internal class AfcFileInfoRequest : AfcPacket
internal class AfcFileInfoRequest(string filename) : AfcPacket
{
public CString Filename { get; set; }
public string Filename { get; set; } = filename;

public override int DataSize => Filename.Length;

public AfcFileInfoRequest(string filename)
{
Filename = new CString(filename);
}

public override byte[] GetBytes()
{
List<byte> bytes = new List<byte>();
bytes.AddRange(Header.GetBytes());
bytes.AddRange(Filename.Bytes);
return bytes.ToArray();
return [.. Header.GetBytes(), .. Filename.AsCString().GetBytes(Encoding.UTF8)];
}
}
}
29 changes: 12 additions & 17 deletions Netimobiledevice/Afc/Packets/AfcFileOpenRequest.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
using System;
using System.Collections.Generic;
using Netimobiledevice.Extentions;
using System;
using System.Text;

namespace Netimobiledevice.Afc.Packets
{
internal class AfcFileOpenRequest : AfcPacket
internal class AfcFileOpenRequest(AfcFileOpenMode mode, string filename) : AfcPacket
{
public AfcFileOpenMode Mode { get; }
public CString Filename { get; }
public AfcFileOpenMode Mode { get; } = mode;
public string Filename { get; } = filename;

public override int DataSize => sizeof(AfcFileOpenMode) + Filename.Length;

public AfcFileOpenRequest(AfcFileOpenMode mode, string filename)
{
Mode = mode;
Filename = new CString(filename);
}
public override int DataSize => sizeof(AfcFileOpenMode) + Filename.AsCString().Length;

public override byte[] GetBytes()
{
List<byte> bytes = new List<byte>();
bytes.AddRange(Header.GetBytes());
bytes.AddRange(BitConverter.GetBytes((ulong) Mode));
bytes.AddRange(Filename.Bytes);
return bytes.ToArray();
return [
.. Header.GetBytes(),
.. BitConverter.GetBytes((ulong) Mode),
.. Filename.AsCString().GetBytes(Encoding.UTF8),
];
}
}
}
19 changes: 6 additions & 13 deletions Netimobiledevice/Afc/Packets/AfcReadDirectoryRequest.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
using System.Collections.Generic;
using Netimobiledevice.Extentions;
using System.Text;

namespace Netimobiledevice.Afc.Packets
{
internal class AfcReadDirectoryRequest : AfcPacket
internal class AfcReadDirectoryRequest(string filename) : AfcPacket
{
public CString Filename { get; }
public string Filename { get; } = filename;

public override int DataSize => Filename.Length;

public AfcReadDirectoryRequest(string filename)
{
Filename = new CString(filename);
}
public override int DataSize => Filename.AsCString().Length;

public override byte[] GetBytes()
{
List<byte> bytes = new List<byte>();
bytes.AddRange(Header.GetBytes());
bytes.AddRange(Filename.Bytes);
return bytes.ToArray();
return [.. Header.GetBytes(), .. Filename.AsCString().GetBytes(Encoding.UTF8)];
}
}
}
19 changes: 6 additions & 13 deletions Netimobiledevice/Afc/Packets/AfcRmRequest.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
using System.Collections.Generic;
using Netimobiledevice.Extentions;
using System.Text;

namespace Netimobiledevice.Afc.Packets
{
internal class AfcRmRequest : AfcPacket
internal class AfcRmRequest(string filename) : AfcPacket
{
public CString Filename { get; }
public string Filename { get; } = filename;

public override int DataSize => Filename.Length;

public AfcRmRequest(string filename)
{
Filename = new CString(filename);
}
public override int DataSize => Filename.AsCString().Length;

public override byte[] GetBytes()
{
List<byte> bytes = new List<byte>();
bytes.AddRange(Header.GetBytes());
bytes.AddRange(Filename.Bytes);
return bytes.ToArray();
return [.. Header.GetBytes(), .. Filename.AsCString().GetBytes(Encoding.UTF8)];
}
}
}
22 changes: 0 additions & 22 deletions Netimobiledevice/Afc/Packets/CString.cs

This file was deleted.

Loading

0 comments on commit a933cf6

Please sign in to comment.