예제 #1
0
        int StreamCreateCallback(out IntPtr git_writestream_out, GitFilter self, IntPtr payload, IntPtr filterSourcePtr, IntPtr git_writestream_next)
        {
            int result = 0;
            var state  = new StreamState();

            try
            {
                Ensure.ArgumentNotZeroIntPtr(filterSourcePtr, "filterSourcePtr");
                Ensure.ArgumentNotZeroIntPtr(git_writestream_next, "git_writestream_next");

                state.thisStream       = new GitWriteStream();
                state.thisStream.close = StreamCloseCallback;
                state.thisStream.write = StreamWriteCallback;
                state.thisStream.free  = StreamFreeCallback;

                state.thisPtr = Marshal.AllocHGlobal(Marshal.SizeOf(state.thisStream));
                Marshal.StructureToPtr(state.thisStream, state.thisPtr, false);

                state.nextPtr    = git_writestream_next;
                state.nextStream = new GitWriteStream();
                Marshal.PtrToStructure(state.nextPtr, state.nextStream);

                state.filterSource = FilterSource.FromNativePtr(filterSourcePtr);
                state.output       = new WriteStream(state.nextStream, state.nextPtr);

                Create(state.filterSource.Path, state.filterSource.Root, state.filterSource.SourceMode);

                if (!activeStreams.TryAdd(state.thisPtr, state))
                {
                    // AFAICT this is a theoretical error that could only happen if we manage
                    // to free the stream pointer but fail to remove the dictionary entry.
                    throw new InvalidOperationException("Overlapping stream pointers");
                }
            }
            catch (Exception exception)
            {
                // unexpected failures means memory clean up required
                if (state.thisPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(state.thisPtr);
                    state.thisPtr = IntPtr.Zero;
                }

                Log.Write(LogLevel.Error, "Filter.StreamCreateCallback exception");
                Log.Write(LogLevel.Error, exception.ToString());
                Proxy.giterr_set_str(GitErrorCategory.Filter, exception);
                result = (int)GitErrorCode.Error;
            }

            git_writestream_out = state.thisPtr;

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Filter"/> class.
        /// And allocates the filter natively.
        /// <param name="name">The unique name with which this filtered is registered with</param>
        /// <param name="attributes">A list of attributes which this filter applies to</param>
        /// </summary>
        protected Filter(string name, IEnumerable <FilterAttributeEntry> attributes)
        {
            Ensure.ArgumentNotNullOrEmptyString(name, "name");
            Ensure.ArgumentNotNull(attributes, "attributes");

            this.name       = name;
            this.attributes = attributes;
            var attributesAsString = string.Join(",", this.attributes.Select(attr => attr.FilterDefinition));

            gitFilter = new GitFilter
            {
                attributes = EncodingMarshaler.FromManaged(Encoding.UTF8, attributesAsString),
                init       = InitializeCallback,
                stream     = StreamCreateCallback,
            };
        }
예제 #3
0
        int StreamCreateCallback(out IntPtr git_writestream_out, GitFilter self, IntPtr payload, IntPtr filterSourcePtr, IntPtr git_writestream_next)
        {
            int result = 0;

            try
            {
                Ensure.ArgumentNotZeroIntPtr(filterSourcePtr, "filterSourcePtr");
                Ensure.ArgumentNotZeroIntPtr(git_writestream_next, "git_writestream_next");

                thisStream       = new GitWriteStream();
                thisStream.close = StreamCloseCallback;
                thisStream.write = StreamWriteCallback;
                thisStream.free  = StreamFreeCallback;
                thisPtr          = Marshal.AllocHGlobal(Marshal.SizeOf(thisStream));
                Marshal.StructureToPtr(thisStream, thisPtr, false);
                nextPtr    = git_writestream_next;
                nextStream = new GitWriteStream();
                Marshal.PtrToStructure(nextPtr, nextStream);
                filterSource = FilterSource.FromNativePtr(filterSourcePtr);
                output       = new WriteStream(nextStream, nextPtr);

                Create(filterSource.Path, filterSource.Root, filterSource.SourceMode);
            }
            catch (Exception exception)
            {
                // unexpected failures means memory clean up required
                if (thisPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(thisPtr);
                    thisPtr = IntPtr.Zero;
                }

                Log.Write(LogLevel.Error, "Filter.StreamCreateCallback exception");
                Log.Write(LogLevel.Error, exception.ToString());
                Proxy.giterr_set_str(GitErrorCategory.Filter, exception);
                result = (int)GitErrorCode.Error;
            }

            git_writestream_out = thisPtr;

            return(result);
        }