diff options
author | nulltoken <emeric.fermas@gmail.com> | 2013-08-13 17:54:05 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2013-08-15 22:24:00 +0400 |
commit | 7ea504a6ef5bec3e4abcf68b3baa403b36886981 (patch) | |
tree | 18cee0a2825a33f4b5cf17b99f3cbfc033e9bc1e /LibGit2Sharp/OdbBackend.cs | |
parent | 7eb9ccfbffe2b309d91186f6850e41f997fa974d (diff) |
Refactor OdbBackEnd
Diffstat (limited to 'LibGit2Sharp/OdbBackend.cs')
-rw-r--r-- | LibGit2Sharp/OdbBackend.cs | 368 |
1 files changed, 197 insertions, 171 deletions
diff --git a/LibGit2Sharp/OdbBackend.cs b/LibGit2Sharp/OdbBackend.cs index 1fe11ec4..23ed4adc 100644 --- a/LibGit2Sharp/OdbBackend.cs +++ b/LibGit2Sharp/OdbBackend.cs @@ -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,50 +219,53 @@ namespace LibGit2Sharp len_p = UIntPtr.Zero; type_p = GitObjectType.Bad; - OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend; + OdbBackend odbBackend = MarshalOdbBackend(backend); + if (odbBackend == null) + { + return (int)GitErrorCode.Error; + } + + Stream dataStream = null; - if (odbBackend != null) + try { - Stream dataStream = null; ObjectType objectType; + int toReturn = odbBackend.Read(oid.Id, out dataStream, out objectType); - try + if (toReturn != 0) { - int toReturn = odbBackend.Read(oid.Id, out dataStream, out objectType); + return toReturn; + } - if (0 == toReturn) - { - // Caller is expected to give us back a stream created with the Allocate() method. - UnmanagedMemoryStream memoryStream = dataStream as UnmanagedMemoryStream; + // Caller is expected to give us back a stream created with the Allocate() method. + var memoryStream = dataStream as UnmanagedMemoryStream; - if (null == memoryStream) - { - return (int)GitErrorCode.Error; - } + if (memoryStream == null) + { + return (int)GitErrorCode.Error; + } - len_p = new UIntPtr((ulong)memoryStream.Capacity); - type_p = objectType.ToGitObjectType(); + len_p = new UIntPtr((ulong)memoryStream.Capacity); + type_p = objectType.ToGitObjectType(); - memoryStream.Seek(0, SeekOrigin.Begin); - buffer_p = new IntPtr(memoryStream.PositionPointer); - } + memoryStream.Seek(0, SeekOrigin.Begin); + buffer_p = new IntPtr(memoryStream.PositionPointer); - return toReturn; - } - catch (Exception ex) - { - Proxy.giterr_set_str(GitErrorCategory.Odb, ex); - } - finally + } + catch (Exception ex) + { + Proxy.giterr_set_str(GitErrorCategory.Odb, ex); + return (int)GitErrorCode.Error; + } + finally + { + if (dataStream != null) { - if (null != dataStream) - { - dataStream.Dispose(); - } + dataStream.Dispose(); } } - return (int)GitErrorCode.Error; + return (int)GitErrorCode.Ok; } private unsafe static int ReadPrefix( @@ -264,61 +282,64 @@ 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; - Stream dataStream = null; - ObjectType objectType; - - try - { - // The length of short_oid is described in characters (40 per full ID) vs. bytes (20) - // which is what we care about. - var oidLen = (int)len; - - // Ensure we allocate enough space to cope with odd-sized prefix - int arraySize = (oidLen + 1) >> 1; - var shortOidArray = new byte[arraySize]; - Array.Copy(short_oid.Id, shortOidArray, arraySize); + return (int)GitErrorCode.Error; + } - int toReturn = odbBackend.ReadPrefix(shortOidArray, oidLen, out oid, out dataStream, out objectType); + Stream dataStream = null; - if (0 == toReturn) - { - // Caller is expected to give us back a stream created with the Allocate() method. - UnmanagedMemoryStream memoryStream = dataStream as UnmanagedMemoryStream; + try + { + // The length of short_oid is described in characters (40 per full ID) vs. bytes (20) + // which is what we care about. + var oidLen = (int)len; - if (null == memoryStream) - { - return (int)GitErrorCode.Error; - } + // Ensure we allocate enough space to cope with odd-sized prefix + int arraySize = (oidLen + 1) >> 1; + var shortOidArray = new byte[arraySize]; + Array.Copy(short_oid.Id, shortOidArray, arraySize); - out_oid.Id = oid; - len_p = new UIntPtr((ulong)memoryStream.Capacity); - type_p = objectType.ToGitObjectType(); + byte[] oid; + ObjectType objectType; - memoryStream.Seek(0, SeekOrigin.Begin); - buffer_p = new IntPtr(memoryStream.PositionPointer); - } + int toReturn = odbBackend.ReadPrefix(shortOidArray, oidLen, out oid, out dataStream, out objectType); + if (toReturn != 0) + { return toReturn; } - catch (Exception ex) + + // Caller is expected to give us back a stream created with the Allocate() method. + var memoryStream = dataStream as UnmanagedMemoryStream; + + if (memoryStream == null) { - Proxy.giterr_set_str(GitErrorCategory.Odb, ex); + return (int)GitErrorCode.Error; } - finally + + out_oid.Id = oid; + len_p = new UIntPtr((ulong)memoryStream.Capacity); + type_p = objectType.ToGitObjectType(); + + memoryStream.Seek(0, SeekOrigin.Begin); + buffer_p = new IntPtr(memoryStream.PositionPointer); + } + catch (Exception ex) + { + Proxy.giterr_set_str(GitErrorCategory.Odb, ex); + return (int)GitErrorCode.Error; + } + finally + { + if (null != dataStream) { - if (null != dataStream) - { - dataStream.Dispose(); - } + 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; + OdbBackend odbBackend = MarshalOdbBackend(backend); + if (odbBackend == null) + { + return (int)GitErrorCode.Error; + } - if (odbBackend != null) + try { int length; ObjectType objectType; + int toReturn = odbBackend.ReadHeader(oid.Id, out length, out objectType); - try + if (toReturn != 0) { - int toReturn = odbBackend.ReadHeader(oid.Id, out length, out objectType); - - if (0 == toReturn) - { - len_p = new UIntPtr((uint)length); - type_p = objectType.ToGitObjectType(); - } - return toReturn; } - catch (Exception ex) - { - Proxy.giterr_set_str(GitErrorCategory.Odb, ex); - } + + 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,27 +387,29 @@ namespace LibGit2Sharp UIntPtr len, GitObjectType type) { - OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend; + if (len.ToUInt64() > long.MaxValue) + { + return (int)GitErrorCode.Error; + } - ObjectType objectType = type.ToObjectType(); + OdbBackend odbBackend = MarshalOdbBackend(backend); + if (odbBackend == null) + { + return (int)GitErrorCode.Error; + } - if (odbBackend != null && - len.ToUInt64() < long.MaxValue) + try { - try + using (var stream = new UnmanagedMemoryStream((byte*)data, (long)len.ToUInt64())) { - using (UnmanagedMemoryStream stream = new UnmanagedMemoryStream((byte*)data, (long)len.ToUInt64())) - { - return odbBackend.Write(oid.Id, stream, (long)len.ToUInt64(), objectType); - } - } - catch (Exception ex) - { - Proxy.giterr_set_str(GitErrorCategory.Odb, ex); + return odbBackend.Write(oid.Id, stream, (long)len.ToUInt64(), type.ToObjectType()); } } - - return (int)GitErrorCode.Error; + catch (Exception ex) + { + Proxy.giterr_set_str(GitErrorCategory.Odb, ex); + return (int)GitErrorCode.Error; + } } private static int WriteStream( @@ -396,33 +420,36 @@ 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) + try { OdbBackendStream stream; + int toReturn = odbBackend.WriteStream((long)length.ToUInt64(), objectType, out stream); - try - { - int toReturn = odbBackend.WriteStream((long)length.ToUInt64(), objectType, out stream); - - if (0 == toReturn) - { - stream_out = stream.GitOdbBackendStreamPointer; - } - - return toReturn; - } - catch (Exception ex) + if (toReturn == 0) { - Proxy.giterr_set_str(GitErrorCategory.Odb, ex); + stream_out = stream.GitOdbBackendStreamPointer; } - } - return (int)GitErrorCode.Error; + return toReturn; + } + catch (Exception ex) + { + Proxy.giterr_set_str(GitErrorCategory.Odb, ex); + return (int)GitErrorCode.Error; + } } private static int ReadStream( @@ -432,51 +459,50 @@ namespace LibGit2Sharp { stream_out = IntPtr.Zero; - OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend; + OdbBackend odbBackend = MarshalOdbBackend(backend); + if (odbBackend == null) + { + return (int)GitErrorCode.Error; + } - if (odbBackend != null) + try { OdbBackendStream stream; + int toReturn = odbBackend.ReadStream(oid.Id, out stream); - try + if (toReturn == 0) { - int toReturn = odbBackend.ReadStream(oid.Id, out stream); - - if (0 == toReturn) - { - stream_out = stream.GitOdbBackendStreamPointer; - } - - return toReturn; + stream_out = stream.GitOdbBackendStreamPointer; } - catch (Exception ex) - { - Proxy.giterr_set_str(GitErrorCategory.Odb, ex); - } - } - return (int)GitErrorCode.Error; + return toReturn; + } + 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) { - try - { - return odbBackend.Exists(oid.Id); - } - catch (Exception ex) - { - Proxy.giterr_set_str(GitErrorCategory.Odb, ex); - } + return false; // Weird } - return false; + try + { + return odbBackend.Exists(oid.Id); + } + catch (Exception ex) + { + Proxy.giterr_set_str(GitErrorCategory.Odb, ex); + return false; + } } private static int Foreach( @@ -484,39 +510,39 @@ namespace LibGit2Sharp 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) { - try - { - return odbBackend.ForEach(new ForeachState(cb, data).ManagedCallback); - } - catch (Exception ex) - { - Proxy.giterr_set_str(GitErrorCategory.Odb, ex); - } + return (int)GitErrorCode.Error; } - return (int)GitErrorCode.Error; + try + { + return odbBackend.ForEach(new ForeachState(cb, data).ManagedCallback); + } + 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; + OdbBackend odbBackend = MarshalOdbBackend(backend); + if (odbBackend == null) + { + return; + } - if (odbBackend != null) + try { - try - { - odbBackend.Dispose(); - } - catch (Exception ex) - { - Proxy.giterr_set_str(GitErrorCategory.Odb, ex); - } + odbBackend.Dispose(); + } + catch (Exception ex) + { + Proxy.giterr_set_str(GitErrorCategory.Odb, ex); } } |