public FileService WriteUsingBufferUntilEnd(IWriteBuffer writeBuffer)
        {
            var bufferLength = writeBuffer.BufferLength();

            _stopWatch.Start();

            using FileStream fs = File.OpenWrite(FullPath);

            for (var i = 0d; i < _fileSize; i += bufferLength)
            {
                var buffer = writeBuffer
                             .WriteUntilEnd()
                             .Buffer();

                var writeLength = bufferLength;

                if ((i + bufferLength) > _fileSize)
                {
                    writeLength = (long)(_fileSize - i);
                }

                fs.Write(buffer, 0, (int)writeLength);

                writeBuffer.Clear();

                _iterations++;
            }

            Close();

            _stopWatch.Stop();

            return(this);
        }
        private void Marshall(Transaction trans, ObjectReference @ref, IWriteBuffer buffer
                              , bool isNew)
        {
            if (!trans.SupportsVirtualFields())
            {
                MarshallIgnore(buffer);
                return;
            }
            ObjectContainerBase stream   = trans.Container();
            HandlerRegistry     handlers = stream._handlers;
            bool migrating = false;

            if (stream._replicationCallState == Const4.New)
            {
                IDb4oReplicationReferenceProvider provider = handlers._replicationReferenceProvider;
                object parentObject = @ref.GetObject();
                IDb4oReplicationReference replicationReference = provider.ReferenceFor(parentObject
                                                                                       );
                if (replicationReference != null)
                {
                    migrating = true;
                    VirtualAttributes va = @ref.ProduceVirtualAttributes();
                    va.i_version  = replicationReference.Version();
                    va.i_uuid     = replicationReference.LongPart();
                    va.i_database = replicationReference.SignaturePart();
                }
            }
            if (@ref.VirtualAttributes() == null)
            {
                @ref.ProduceVirtualAttributes();
                migrating = false;
            }
            Marshall(trans, @ref, buffer, migrating, isNew);
        }
Example #3
0
        protected static void WriteFailed <T, TException>(IWriteBuffer <T> buffer, T item)
            where TException : Exception
        {
            Action act = () => buffer.TryWrite(item);

            act.Should().Throw <TException>();
        }
Example #4
0
        /// <summary>
        /// Requested file data. Read the file and write it to ProjFS.
        /// </summary>
        public HResult GetFileDataCallback(int commandId, string relativePath, ulong byteOffset, uint length, Guid dataStreamId, byte[] contentId, byte[] providerId, uint triggeringProcessId, string triggeringProcessImageFileName)
        {
            var assetPath         = GetAssetPath(relativePath);
            var pakFileSystemInfo = GetSystemInfo(assetPath);

            if (pakFileSystemInfo == null)
            {
                return(HResult.FileNotFound);
            }

            uint dataSize = Convert.ToUInt32(pakFileSystemInfo.Size);

            try
            {
                using (IWriteBuffer writeBuffer = virtualizationInstance.CreateWriteBuffer(dataSize))
                {
                    var data = ReadFile(assetPath);
                    if (data == null)
                    {
                        return(HResult.FileNotFound);
                    }
                    writeBuffer.Stream.Write(data, 0, data.Length);
                    HResult writeResult = virtualizationInstance.WriteFileData(dataStreamId, writeBuffer, 0, dataSize);
                    return(writeResult);
                }
            }
            catch (OutOfMemoryException)
            {
                return(HResult.OutOfMemory);
            }
            catch (Exception)
            {
                return(HResult.InternalError);
            }
        }
Example #5
0
		public virtual void Write(IWriteBuffer buffer, string str)
		{
			int length = str.Length;
			char[] chars = new char[length];
			Sharpen.Runtime.GetCharsForString(str, 0, length, chars, 0);
			for (int i = 0; i < length; i++)
			{
				buffer.WriteByte((byte)(chars[i] & unchecked((int)(0xff))));
			}
		}
Example #6
0
 public virtual void WriteLengthAndString(IWriteBuffer buffer, string str)
 {
     if (str == null)
     {
         buffer.WriteInt(0);
         return;
     }
     buffer.WriteInt(str.Length);
     Write(buffer, str);
 }
Example #7
0
 public virtual void Write(IWriteBuffer buffer, string str)
 {
     var length = str.Length;
     var chars = new char[length];
     Runtime.GetCharsForString(str, 0, length, chars, 0);
     for (var i = 0; i < length; i++)
     {
         buffer.WriteByte((byte) (chars[i] & unchecked(0xff)));
     }
 }
        internal override void Marshall(Transaction trans, ObjectReference @ref, IWriteBuffer
                                        buffer, bool isMigrating, bool isNew)
        {
            VirtualAttributes   attr      = @ref.VirtualAttributes();
            ObjectContainerBase container = trans.Container();
            bool doAddIndexEntry          = isNew && container.MaintainsIndices();
            int  dbID           = 0;
            bool linkToDatabase = (attr != null && attr.i_database == null) ? true : !isMigrating;

            if (linkToDatabase)
            {
                Db4oDatabase db = ((IInternalObjectContainer)container).Identity();
                if (db == null)
                {
                    // can happen on early classes like Metaxxx, no problem
                    attr = null;
                }
                else
                {
                    if (attr.i_database == null)
                    {
                        attr.i_database = db;
                        // TODO: Should be check for ! client instead of instanceof
                        if (container is LocalObjectContainer)
                        {
                            attr.i_uuid     = container.GenerateTimeStampId();
                            doAddIndexEntry = true;
                        }
                    }
                    db = attr.i_database;
                    if (db != null)
                    {
                        dbID = db.GetID(trans);
                    }
                }
            }
            else
            {
                if (attr != null)
                {
                    dbID = attr.i_database.GetID(trans);
                }
            }
            buffer.WriteInt(dbID);
            if (attr == null)
            {
                buffer.WriteLong(0);
                return;
            }
            buffer.WriteLong(attr.i_uuid);
            if (doAddIndexEntry)
            {
                AddIndexEntry(trans, @ref.GetID(), attr.i_uuid);
            }
        }
Example #9
0
        public virtual void Write(IWriteBuffer buffer, string str)
        {
            int length = str.Length;

            char[] chars = new char[length];
            Sharpen.Runtime.GetCharsForString(str, 0, length, chars, 0);
            for (int i = 0; i < length; i++)
            {
                buffer.WriteByte((byte)(chars[i] & unchecked ((int)(0xff))));
            }
        }
Example #10
0
        public virtual void Write(IWriteBuffer buffer, string str)
        {
            var length = str.Length;
            var chars  = new char[length];

            Runtime.GetCharsForString(str, 0, length, chars, 0);
            for (var i = 0; i < length; i++)
            {
                buffer.WriteByte((byte)(chars[i] & unchecked (0xff)));
            }
        }
		/// <summary>
		/// Note the different implementation when compared to LatinStringIO and UnicodeStringIO:
		/// Instead of writing the length of the string, UTF8StringIO writes the length of the
		/// byte array.
		/// </summary>
		/// <remarks>
		/// Note the different implementation when compared to LatinStringIO and UnicodeStringIO:
		/// Instead of writing the length of the string, UTF8StringIO writes the length of the
		/// byte array.
		/// </remarks>
		public override void WriteLengthAndString(IWriteBuffer buffer, string str)
		{
			if (str == null)
			{
				buffer.WriteInt(0);
				return;
			}
			byte[] bytes = Encode(str);
			buffer.WriteInt(bytes.Length);
			buffer.WriteBytes(bytes);
		}
Example #12
0
 /// <summary>
 /// Note the different implementation when compared to LatinStringIO and UnicodeStringIO:
 /// Instead of writing the length of the string, UTF8StringIO writes the length of the
 /// byte array.
 /// </summary>
 /// <remarks>
 /// Note the different implementation when compared to LatinStringIO and UnicodeStringIO:
 /// Instead of writing the length of the string, UTF8StringIO writes the length of the
 /// byte array.
 /// </remarks>
 public override void WriteLengthAndString(IWriteBuffer buffer, string str)
 {
     if (str == null)
     {
         buffer.WriteInt(0);
         return;
     }
     byte[] bytes = Encode(str);
     buffer.WriteInt(bytes.Length);
     buffer.WriteBytes(bytes);
 }
Example #13
0
 public static void WriteEnd(IWriteBuffer buffer)
 {
     if (Deploy.debug && Deploy.brackets)
     {
         if (buffer is MarshallingContext)
         {
             ((MarshallingContext)buffer).DebugWriteEnd(Const4.Yapend);
             return;
         }
         buffer.WriteByte(Const4.Yapend);
     }
 }
Example #14
0
 public static void WriteLong(IWriteBuffer buffer, long val)
 {
     if (Deploy.debug && Deploy.debugLong)
     {
         var l_s = "                                " + val;
         new LatinStringIO().Write(buffer, Runtime.Substring(l_s, l_s.Length - Const4
                                                             .LongBytes));
     }
     for (var i = 0; i < Const4.LongBytes; i++)
     {
         buffer.WriteByte((byte)(val >> ((Const4.LongBytes - 1 - i) * 8)));
     }
 }
Example #15
0
 public static void WriteLong(IWriteBuffer buffer, long val)
 {
     if (Deploy.debug && Deploy.debugLong)
     {
         var l_s = "                                " + val;
         new LatinStringIO().Write(buffer, Runtime.Substring(l_s, l_s.Length - Const4
             .LongBytes));
     }
     for (var i = 0; i < Const4.LongBytes; i++)
     {
         buffer.WriteByte((byte) (val >> ((Const4.LongBytes - 1 - i)*8)));
     }
 }
Example #16
0
        private static void RunLoop(IWriteBuffer <char> writeBuffer, ILogger logger)
        {
            logger.LogInformation("Press 'esc' to exit");
            logger.LogInformation("Press any key to stream it to the output file:");

            while (true)
            {
                ConsoleKeyInfo keyPress = Console.ReadKey();
                if (keyPress.Key == ConsoleKey.Escape)
                {
                    return;
                }

                writeBuffer.TryWrite(keyPress.KeyChar);
            }
        }
Example #17
0
		internal override void Marshall(Transaction trans, ObjectReference @ref, IWriteBuffer
			 buffer, bool isMigrating, bool isNew)
		{
			VirtualAttributes attr = @ref.VirtualAttributes();
			if (!isMigrating)
			{
				attr.i_version = trans.Container().GenerateTimeStampId();
			}
			if (attr == null)
			{
				buffer.WriteLong(0);
			}
			else
			{
				buffer.WriteLong(attr.i_version);
			}
		}
Example #18
0
        internal override void Marshall(Transaction trans, ObjectReference @ref, IWriteBuffer
                                        buffer, bool isMigrating, bool isNew)
        {
            VirtualAttributes attr = @ref.VirtualAttributes();

            if (!isMigrating)
            {
                attr.i_version = trans.Container().GenerateTimeStampId();
            }
            if (attr == null)
            {
                buffer.WriteLong(0);
            }
            else
            {
                buffer.WriteLong(attr.i_version);
            }
        }
Example #19
0
        private static void Setup(
            out List <string> logLines,
            out IWriteBuffer <char> writeBuffer,
            out BlockingLoggingProducer blockingLoggingProducer,
            out ProducerWrapper <char> producer,
            CancellationTokenSource cts = null)
        {
            CancellationToken token = cts == null ? CancellationToken.None : cts.Token;

            logLines = new List <string>();
            ILogger log = MemoryLog.Create(logLines);

            // use a regular logging producer unless an override is passed in
            var factory = new BlockingLoggingProducerFactory(log);

            blockingLoggingProducer = factory.Instance;
            var channel = new UnboundedChannelFacade <char>();

            producer    = new ProducerWrapper <char>(factory, token, channel);
            writeBuffer = channel;
        }
 internal override void MarshallIgnore(IWriteBuffer buffer)
 {
 }
		private static void WriteGuid(object obj, IWriteBuffer context)
		{
			Guid id = (Guid)obj;
			context.WriteBytes(id.ToByteArray());
		}
		private void WriteNullBitmap(IWriteBuffer context, BitMap4 bitMap)
		{
			context.WriteBytes(bitMap.Bytes());
		}
Example #23
0
		internal abstract void Marshall(Transaction trans, ObjectReference @ref, IWriteBuffer
			 buffer, bool migrating, bool isNew);
Example #24
0
 public StreamReaderBridge(StreamReader reader, IWriteBuffer writer)
 {
     _reader = reader;
     _writer = writer;
 }
 internal abstract void MarshallIgnore(IWriteBuffer writer);
Example #26
0
 internal override void Marshall(Transaction trans, ObjectReference @ref, IWriteBuffer
     buffer, bool isMigrating, bool isNew)
 {
     var attr = @ref.VirtualAttributes();
     var container = trans.Container();
     var doAddIndexEntry = isNew && container.MaintainsIndices();
     var dbID = 0;
     var linkToDatabase = (attr != null && attr.i_database == null) ? true : !isMigrating;
     if (linkToDatabase)
     {
         var db = ((IInternalObjectContainer) container).Identity();
         if (db == null)
         {
             // can happen on early classes like Metaxxx, no problem
             attr = null;
         }
         else
         {
             if (attr.i_database == null)
             {
                 attr.i_database = db;
                 // TODO: Should be check for ! client instead of instanceof
                 if (container is LocalObjectContainer)
                 {
                     attr.i_uuid = container.GenerateTimeStampId();
                     doAddIndexEntry = true;
                 }
             }
             db = attr.i_database;
             if (db != null)
             {
                 dbID = db.GetID(trans);
             }
         }
     }
     else
     {
         if (attr != null)
         {
             dbID = attr.i_database.GetID(trans);
         }
     }
     buffer.WriteInt(dbID);
     if (attr == null)
     {
         buffer.WriteLong(0);
         return;
     }
     buffer.WriteLong(attr.i_uuid);
     if (doAddIndexEntry)
     {
         AddIndexEntry(trans, @ref.GetID(), attr.i_uuid);
     }
 }
Example #27
0
 public static void WriteBegin(IWriteBuffer buffer, byte identifier)
 {
 }
Example #28
0
		public virtual void WriteLengthAndString(IWriteBuffer buffer, string str)
		{
			if (str == null)
			{
				buffer.WriteInt(0);
				return;
			}
			buffer.WriteInt(str.Length);
			Write(buffer, str);
		}
        public HResult GetFileDataCallback(
            int commandId,
            string relativePath,
            ulong byteOffset,
            uint length,
            Guid streamGuid,
            byte[] contentId,
            byte[] providerId,
            uint triggeringProcessId,
            string triggeringProcessImageFileName)
        {
            Console.WriteLine($"GetFileDataCallback: `{relativePath}`");

            if (!this.FileExists(relativePath))
            {
                return(HResult.FileNotFound);
            }

            try
            {
                const int bufferSize = 64 * 1024;
                using (IWriteBuffer writeBuffer = this.virtualizationInstance.CreateWriteBuffer(bufferSize))
                {
                    ulong writeOffset = 0;

                    FileSystemResult hydrateFileResult = this.HydrateFile(
                        relativePath,
                        bufferSize,
                        (readBuffer, bytesToCopy) =>
                    {
                        writeBuffer.Stream.Seek(0, SeekOrigin.Begin);
                        writeBuffer.Stream.Write(readBuffer, 0, (int)bytesToCopy);

                        HResult writeResult = this.virtualizationInstance.WriteFileData(streamGuid, writeBuffer, writeOffset, bytesToCopy);
                        if (writeResult != HResult.Ok)
                        {
                            Console.WriteLine("WriteFile faild: " + writeResult);
                            return(false);
                        }

                        writeOffset += bytesToCopy;

                        return(true);
                    });

                    if (hydrateFileResult != FileSystemResult.Success)
                    {
                        return(HResult.InternalError);
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("IOException in GetFileDataCallback: " + e.Message);
                return(HResult.InternalError);
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine("UnauthorizedAccessException in GetFileDataCallback: " + e.Message);
                return(HResult.InternalError);
            }

            return(HResult.Ok);
        }
Example #30
0
        public static void WriteInt(this IWriteBuffer <char> buffer, int value)
        {
            Span <char> valueBuffer = stackalloc char[20];
            var         written     = CharConverter.IntToUnicode(value, valueBuffer);

            buffer.Write(valueBuffer[0..written]);
        private static void WriteGuid(object obj, IWriteBuffer context)
        {
            Guid id = (Guid)obj;

            context.WriteBytes(id.ToByteArray());
        }
 internal override void MarshallIgnore(IWriteBuffer buffer)
 {
     buffer.WriteInt(0);
     buffer.WriteLong(0);
 }
		internal override void Marshall(Transaction trans, ObjectReference @ref, IWriteBuffer
			 buffer, bool isMigrating, bool isNew)
		{
		}
Example #34
0
 public static void WriteBegin(IWriteBuffer buffer, byte identifier)
 {
 }
Example #35
0
 protected static void InternalWrite(IInternalObjectContainer objectContainer, IWriteBuffer
                                     buffer, string str)
 {
     StringIo(objectContainer).WriteLengthAndString(buffer, str);
 }
 internal abstract void Marshall(Transaction trans, ObjectReference @ref, IWriteBuffer
                                 buffer, bool migrating, bool isNew);
        private static void WriteElementCount(IWriteBuffer context, ICollectionInitializer initializer)
		{
            context.WriteInt(initializer.Count());
		}
Example #38
0
 private static void Write(IWriteBuffer context, DateTimeOffset dateTimeOffset)
 {
     context.WriteLong(dateTimeOffset.Ticks);
     context.WriteLong(dateTimeOffset.Offset.Ticks);
 }
Example #39
0
 public override void Write(IWriteBuffer buffer, string str)
 {
     buffer.WriteBytes(Encode(str));
 }
		public override void Write(IWriteBuffer buffer, string str)
		{
			buffer.WriteBytes(Encode(str));
		}
Example #41
0
		internal override void MarshallIgnore(IWriteBuffer buffer)
		{
			buffer.WriteLong(0);
		}
Example #42
0
		private void Marshall(Transaction trans, ObjectReference @ref, IWriteBuffer buffer
			, bool isNew)
		{
			if (!trans.SupportsVirtualFields())
			{
				MarshallIgnore(buffer);
				return;
			}
			ObjectContainerBase stream = trans.Container();
			HandlerRegistry handlers = stream._handlers;
			bool migrating = false;
			if (stream._replicationCallState == Const4.New)
			{
				IDb4oReplicationReferenceProvider provider = handlers._replicationReferenceProvider;
				object parentObject = @ref.GetObject();
				IDb4oReplicationReference replicationReference = provider.ReferenceFor(parentObject
					);
				if (replicationReference != null)
				{
					migrating = true;
					VirtualAttributes va = @ref.ProduceVirtualAttributes();
					va.i_version = replicationReference.Version();
					va.i_uuid = replicationReference.LongPart();
					va.i_database = replicationReference.SignaturePart();
				}
			}
			if (@ref.VirtualAttributes() == null)
			{
				@ref.ProduceVirtualAttributes();
				migrating = false;
			}
			Marshall(trans, @ref, buffer, migrating, isNew);
		}
		private static void Write(IWriteBuffer context, DateTimeOffset dateTimeOffset)
		{
			context.WriteLong(dateTimeOffset.Ticks);
			context.WriteLong(dateTimeOffset.Offset.Ticks);
		}
Example #44
0
		internal abstract void MarshallIgnore(IWriteBuffer writer);
        internal HResult GetFileDataCallback(
            int commandId,
            string relativePath,
            ulong byteOffset,
            uint length,
            Guid dataStreamId,
            byte[] contentId,
            byte[] providerId,
            uint triggeringProcessId,
            string triggeringProcessImageFileName)
        {
            Log.Information("----> GetFileDataCallback relativePath [{Path}]", relativePath);
            Log.Information("  triggered by [{ProcName} {PID}]", triggeringProcessImageFileName, triggeringProcessId);

            HResult hr = HResult.Ok;

            if (!this.FileExistsInLayer(relativePath))
            {
                hr = HResult.FileNotFound;
            }
            else
            {
                // We'll write the file contents to ProjFS no more than 64KB at a time.
                uint desiredBufferSize = Math.Min(64 * 1024, length);
                try
                {
                    // We could have used VirtualizationInstance.CreateWriteBuffer(uint), but this
                    // illustrates how to use its more complex overload.  This method gets us a
                    // buffer whose underlying storage is properly aligned for unbuffered I/O.
                    using (IWriteBuffer writeBuffer = this.virtualizationInstance.CreateWriteBuffer(
                               byteOffset,
                               desiredBufferSize,
                               out ulong alignedWriteOffset,
                               out uint alignedBufferSize))
                    {
                        // Get the file data out of the layer and write it into ProjFS.
                        hr = this.HydrateFile(
                            relativePath,
                            alignedBufferSize,
                            (readBuffer, bytesToCopy) =>
                        {
                            // readBuffer contains what HydrateFile() read from the file in the
                            // layer.  Now seek to the beginning of the writeBuffer and copy the
                            // contents of readBuffer into writeBuffer.
                            writeBuffer.Stream.Seek(0, SeekOrigin.Begin);
                            writeBuffer.Stream.Write(readBuffer, 0, (int)bytesToCopy);

                            // Write the data from the writeBuffer into the scratch via ProjFS.
                            HResult writeResult = this.virtualizationInstance.WriteFileData(
                                dataStreamId,
                                writeBuffer,
                                alignedWriteOffset,
                                bytesToCopy);

                            if (writeResult != HResult.Ok)
                            {
                                Log.Error("VirtualizationInstance.WriteFileData failed: {Result}", writeResult);
                                return(false);
                            }

                            alignedWriteOffset += bytesToCopy;
                            return(true);
                        });

                        if (hr != HResult.Ok)
                        {
                            return(HResult.InternalError);
                        }
                    }
                }
                catch (OutOfMemoryException e)
                {
                    Log.Error(e, "Out of memory");
                    hr = HResult.OutOfMemory;
                }
                catch (Exception e)
                {
                    Log.Error(e, "Exception");
                    hr = HResult.InternalError;
                }
            }

            Log.Information("<---- return status {Result}", hr);
            return(hr);
        }
Example #46
0
 private static void WriteElementCount(IWriteBuffer context, ICollectionInitializer initializer)
 {
     context.WriteInt(initializer.Count());
 }
		internal override void MarshallIgnore(IWriteBuffer buffer)
		{
		}
Example #48
0
 protected static void InternalWrite(IInternalObjectContainer objectContainer, IWriteBuffer
     buffer, string str)
 {
     StringIo(objectContainer).WriteLengthAndString(buffer, str);
 }
 internal override void Marshall(Transaction trans, ObjectReference @ref, IWriteBuffer
                                 buffer, bool isMigrating, bool isNew)
 {
 }
Example #50
0
 public static void WriteEnd(IWriteBuffer buffer)
 {
     if (Deploy.debug && Deploy.brackets)
     {
         if (buffer is MarshallingContext)
         {
             ((MarshallingContext) buffer).DebugWriteEnd(Const4.Yapend);
             return;
         }
         buffer.WriteByte(Const4.Yapend);
     }
 }
Example #51
0
 private void WriteNullBitmap(IWriteBuffer context, BitMap4 bitMap)
 {
     context.WriteBytes(bitMap.Bytes());
 }