Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Change default encoding to UTF-8, Fix incomplete reads in SyncService #224

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SharpAdbClient/AdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class AdbClient : IAdbClient
/// <summary>
/// The default encoding
/// </summary>
public const string DefaultEncoding = "ISO-8859-1";
public const string DefaultEncoding = "UTF-8";

/// <summary>
/// The port at which the Android Debug Bridge server listens by default.
Expand Down
4 changes: 2 additions & 2 deletions SharpAdbClient/AdbSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ public virtual void SendSyncRequest(SyncCommand command, string path)
throw new ArgumentNullException(nameof(path));
}

this.SendSyncRequest(command, path.Length);

byte[] pathBytes = AdbClient.Encoding.GetBytes(path);
this.SendSyncRequest(command, pathBytes.Length);

this.Write(pathBytes);
}

Expand Down
62 changes: 39 additions & 23 deletions SharpAdbClient/SyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,13 @@ public void Push(Stream stream, string remotePath, int permissions, DateTimeOffs
if (result == SyncCommand.FAIL)
{
var message = this.Socket.ReadSyncString();

throw new AdbException(message);
}
else if (result != SyncCommand.OKAY)
else if (result == SyncCommand.OKAY)
{
this.Socket.ReadSyncString();
}
else
{
throw new AdbException($"The server sent an invali repsonse {result}");
}
Expand Down Expand Up @@ -249,12 +252,13 @@ public void Pull(string remoteFilepath, Stream stream, IProgress<int> progress,

if (response == SyncCommand.DONE)
{
this.Socket.ReadSyncString();
break;
}
else if (response == SyncCommand.FAIL)
{
var message = this.Socket.ReadSyncString();
throw new AdbException($"Failed to pull '{remoteFilepath}'. {message}");
throw new AdbException(message);
}
else if (response != SyncCommand.DATA)
{
Expand Down Expand Up @@ -293,22 +297,26 @@ public void Pull(string remoteFilepath, Stream stream, IProgress<int> progress,
/// <inheritdoc/>
public FileStatistics Stat(string remotePath)
{
// create the stat request message.
this.Socket.SendSyncRequest(SyncCommand.STAT, remotePath);

if (this.Socket.ReadSyncResponse() != SyncCommand.STAT)
var response = this.Socket.ReadSyncResponse();
if (response == SyncCommand.STAT)
{
FileStatistics value = new FileStatistics
{
Path = remotePath
};
this.ReadStatistics(value);
return value;
}
else if (response == SyncCommand.FAIL)
{
var message = this.Socket.ReadSyncString();
throw new AdbException(message);
}
else
{
throw new AdbException($"The server returned an invalid sync response.");
}

// read the result, in a byte array containing 3 ints
// (mode, size, time)
FileStatistics value = new FileStatistics();
value.Path = remotePath;

this.ReadStatistics(value);

return value;
}

/// <inheritdoc/>
Expand All @@ -323,20 +331,28 @@ public IEnumerable<FileStatistics> GetDirectoryListing(string remotePath)
{
var response = this.Socket.ReadSyncResponse();

if (response == SyncCommand.DONE)
if (response == SyncCommand.DENT)
{
FileStatistics entry = new FileStatistics();
this.ReadStatistics(entry);
entry.Path = this.Socket.ReadSyncString();
value.Add(entry);
}
else if (response == SyncCommand.DONE)
{
var ignoreBuf = new byte[16];
this.Socket.Read(ignoreBuf);
break;
}
else if (response != SyncCommand.DENT)
else if (response == SyncCommand.FAIL)
{
var message = this.Socket.ReadSyncString();
throw new AdbException(message);
}
else
{
throw new AdbException($"The server returned an invalid sync response.");
}

FileStatistics entry = new FileStatistics();
this.ReadStatistics(entry);
entry.Path = this.Socket.ReadSyncString();

value.Add(entry);
}

return value;
Expand Down