This commit is contained in:
nulltoken 2013-08-13 15:54:05 +02:00
Родитель 7eb9ccfbff
Коммит 7ea504a6ef
1 изменённых файлов: 209 добавлений и 183 удалений

Просмотреть файл

@ -193,6 +193,21 @@ namespace LibGit2Sharp
public static readonly GitOdbBackend.foreach_callback ForEachCallback = Foreach;
public static readonly GitOdbBackend.free_callback FreeCallback = Free;
private static OdbBackend MarshalOdbBackend(IntPtr backendPtr)
{
var intPtr = Marshal.ReadIntPtr(backendPtr, GitOdbBackend.GCHandleOffset);
var odbBackend = GCHandle.FromIntPtr(intPtr).Target as OdbBackend;
if (odbBackend == null)
{
Proxy.giterr_set_str(GitErrorCategory.Reference, "Cannot retrieve the managed OdbBackend.");
return null;
}
return odbBackend;
}
private unsafe static int Read(
out IntPtr buffer_p,
out UIntPtr len_p,
@ -204,23 +219,28 @@ namespace LibGit2Sharp
len_p = UIntPtr.Zero;
type_p = GitObjectType.Bad;
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;
if (odbBackend != null)
OdbBackend odbBackend = MarshalOdbBackend(backend);
if (odbBackend == null)
{
return (int)GitErrorCode.Error;
}
Stream dataStream = null;
ObjectType objectType;
try
{
ObjectType objectType;
int toReturn = odbBackend.Read(oid.Id, out dataStream, out objectType);
if (0 == toReturn)
if (toReturn != 0)
{
// Caller is expected to give us back a stream created with the Allocate() method.
UnmanagedMemoryStream memoryStream = dataStream as UnmanagedMemoryStream;
return toReturn;
}
if (null == memoryStream)
// Caller is expected to give us back a stream created with the Allocate() method.
var memoryStream = dataStream as UnmanagedMemoryStream;
if (memoryStream == null)
{
return (int)GitErrorCode.Error;
}
@ -230,24 +250,22 @@ namespace LibGit2Sharp
memoryStream.Seek(0, SeekOrigin.Begin);
buffer_p = new IntPtr(memoryStream.PositionPointer);
}
return toReturn;
}
catch (Exception ex)
{
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
return (int)GitErrorCode.Error;
}
finally
{
if (null != dataStream)
if (dataStream != null)
{
dataStream.Dispose();
}
}
}
return (int)GitErrorCode.Error;
return (int)GitErrorCode.Ok;
}
private unsafe static int ReadPrefix(
@ -264,13 +282,13 @@ namespace LibGit2Sharp
len_p = UIntPtr.Zero;
type_p = GitObjectType.Bad;
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;
if (odbBackend != null)
OdbBackend odbBackend = MarshalOdbBackend(backend);
if (odbBackend == null)
{
byte[] oid;
return (int)GitErrorCode.Error;
}
Stream dataStream = null;
ObjectType objectType;
try
{
@ -283,14 +301,20 @@ namespace LibGit2Sharp
var shortOidArray = new byte[arraySize];
Array.Copy(short_oid.Id, shortOidArray, arraySize);
byte[] oid;
ObjectType objectType;
int toReturn = odbBackend.ReadPrefix(shortOidArray, oidLen, out oid, out dataStream, out objectType);
if (0 == toReturn)
if (toReturn != 0)
{
// Caller is expected to give us back a stream created with the Allocate() method.
UnmanagedMemoryStream memoryStream = dataStream as UnmanagedMemoryStream;
return toReturn;
}
if (null == memoryStream)
// Caller is expected to give us back a stream created with the Allocate() method.
var memoryStream = dataStream as UnmanagedMemoryStream;
if (memoryStream == null)
{
return (int)GitErrorCode.Error;
}
@ -302,12 +326,10 @@ namespace LibGit2Sharp
memoryStream.Seek(0, SeekOrigin.Begin);
buffer_p = new IntPtr(memoryStream.PositionPointer);
}
return toReturn;
}
catch (Exception ex)
{
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
return (int)GitErrorCode.Error;
}
finally
{
@ -316,9 +338,8 @@ namespace LibGit2Sharp
dataStream.Dispose();
}
}
}
return (int)GitErrorCode.Error;
return (int)GitErrorCode.Ok;
}
private static int ReadHeader(
@ -330,32 +351,33 @@ namespace LibGit2Sharp
len_p = UIntPtr.Zero;
type_p = GitObjectType.Bad;
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;
if (odbBackend != null)
OdbBackend odbBackend = MarshalOdbBackend(backend);
if (odbBackend == null)
{
int length;
ObjectType objectType;
return (int)GitErrorCode.Error;
}
try
{
int length;
ObjectType objectType;
int toReturn = odbBackend.ReadHeader(oid.Id, out length, out objectType);
if (0 == toReturn)
if (toReturn != 0)
{
len_p = new UIntPtr((uint)length);
type_p = objectType.ToGitObjectType();
return toReturn;
}
return toReturn;
len_p = new UIntPtr((uint)length);
type_p = objectType.ToGitObjectType();
}
catch (Exception ex)
{
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
return (int)GitErrorCode.Error;
}
return (int)GitErrorCode.Error;
return (int)GitErrorCode.Ok;
}
private static unsafe int Write(
@ -365,28 +387,30 @@ namespace LibGit2Sharp
UIntPtr len,
GitObjectType type)
{
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;
ObjectType objectType = type.ToObjectType();
if (odbBackend != null &&
len.ToUInt64() < long.MaxValue)
if (len.ToUInt64() > long.MaxValue)
{
return (int)GitErrorCode.Error;
}
OdbBackend odbBackend = MarshalOdbBackend(backend);
if (odbBackend == null)
{
return (int)GitErrorCode.Error;
}
try
{
using (UnmanagedMemoryStream stream = new UnmanagedMemoryStream((byte*)data, (long)len.ToUInt64()))
using (var stream = new UnmanagedMemoryStream((byte*)data, (long)len.ToUInt64()))
{
return odbBackend.Write(oid.Id, stream, (long)len.ToUInt64(), objectType);
return odbBackend.Write(oid.Id, stream, (long)len.ToUInt64(), type.ToObjectType());
}
}
catch (Exception ex)
{
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}
return (int)GitErrorCode.Error;
}
}
private static int WriteStream(
out IntPtr stream_out,
@ -396,20 +420,25 @@ namespace LibGit2Sharp
{
stream_out = IntPtr.Zero;
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;
if (length.ToUInt64() > long.MaxValue)
{
return (int)GitErrorCode.Error;
}
OdbBackend odbBackend = MarshalOdbBackend(backend);
if (odbBackend == null)
{
return (int)GitErrorCode.Error;
}
ObjectType objectType = type.ToObjectType();
if (odbBackend != null &&
length.ToUInt64() < long.MaxValue)
{
OdbBackendStream stream;
try
{
OdbBackendStream stream;
int toReturn = odbBackend.WriteStream((long)length.ToUInt64(), objectType, out stream);
if (0 == toReturn)
if (toReturn == 0)
{
stream_out = stream.GitOdbBackendStreamPointer;
}
@ -419,11 +448,9 @@ namespace LibGit2Sharp
catch (Exception ex)
{
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}
return (int)GitErrorCode.Error;
}
}
private static int ReadStream(
out IntPtr stream_out,
@ -432,17 +459,18 @@ namespace LibGit2Sharp
{
stream_out = IntPtr.Zero;
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;
if (odbBackend != null)
OdbBackend odbBackend = MarshalOdbBackend(backend);
if (odbBackend == null)
{
OdbBackendStream stream;
return (int)GitErrorCode.Error;
}
try
{
OdbBackendStream stream;
int toReturn = odbBackend.ReadStream(oid.Id, out stream);
if (0 == toReturn)
if (toReturn == 0)
{
stream_out = stream.GitOdbBackendStreamPointer;
}
@ -452,20 +480,20 @@ namespace LibGit2Sharp
catch (Exception ex)
{
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}
return (int)GitErrorCode.Error;
}
}
private static bool Exists(
IntPtr backend,
ref GitOid oid)
{
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;
if (odbBackend != null)
OdbBackend odbBackend = MarshalOdbBackend(backend);
if (odbBackend == null)
{
return false; // Weird
}
try
{
return odbBackend.Exists(oid.Id);
@ -473,21 +501,21 @@ namespace LibGit2Sharp
catch (Exception ex)
{
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}
return false;
}
}
private static int Foreach(
IntPtr backend,
GitOdbBackend.foreach_callback_callback cb,
IntPtr data)
{
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;
if (odbBackend != null)
OdbBackend odbBackend = MarshalOdbBackend(backend);
if (odbBackend == null)
{
return (int)GitErrorCode.Error;
}
try
{
return odbBackend.ForEach(new ForeachState(cb, data).ManagedCallback);
@ -495,20 +523,19 @@ namespace LibGit2Sharp
catch (Exception ex)
{
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}
return (int)GitErrorCode.Error;
}
}
private static void Free(
IntPtr backend)
{
GCHandle gcHandle = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset));
OdbBackend odbBackend = gcHandle.Target as OdbBackend;
if (odbBackend != null)
OdbBackend odbBackend = MarshalOdbBackend(backend);
if (odbBackend == null)
{
return;
}
try
{
odbBackend.Dispose();
@ -518,7 +545,6 @@ namespace LibGit2Sharp
Proxy.giterr_set_str(GitErrorCategory.Odb, ex);
}
}
}
private class ForeachState
{