Exemple #1
0
        /// <summary>
        /// ValuePath is just the value file name ts.dat and not fully qualified file path
        /// </summary>
        /// <param name="valuePath"></param>
        /// <param name="dbi"></param>
        /// <returns></returns>
        internal ByteValue ReadData(IValue valuePath, DataBlockInfo dbi)
        {
            ByteValue byteValue   = null;
            string    FQValuePath = targetDir + "/" + valuePath;


            if (logger != null)
            {
                logger.Log("Start Synchronizer Simple Download");
            }
            if (null != valuePath && remoteRead)
            {
                if (!synchronizer.DownloadFile(valuePath.ToString(), FQValuePath))
                {
                    return(byteValue);
                }
            }
            if (logger != null)
            {
                logger.Log("End Synchronizer Simple Download");
            }

            if (logger != null)
            {
                logger.Log("Start FileDataStream ReadFromDisk");
            }
            if (null != valuePath)
            {
                FileStream fout = new FileStream(FQValuePath,
                                                 FileMode.OpenOrCreate,
                                                 FileAccess.Read,
                                                 FileShare.ReadWrite);
                fout.Seek(0, SeekOrigin.Begin);
                byte[] bytes = new byte[fout.Length];
                //read file to MemoryStream
                int bytesRead = 0;
                fout.Read(bytes, bytesRead, (int)fout.Length);
                fout.Close();
                byteValue = new ByteValue(bytes);
            }
            if (logger != null)
            {
                logger.Log("End FileDataStream ReadFromDisk");
            }
            if (streamtype == StreamFactory.StreamSecurityType.Secure)
            {
                if (logger != null)
                {
                    logger.Log("Start FileDataStream Decrypt DataBlock");
                }
                if (!hasher.ComputeHash(byteValue.GetBytes()).SequenceEqual(dbi.hashValue))
                {
                    return(null);
                }

                byteValue = new ByteValue(Crypto.DecryptBytesSimple(byteValue.GetBytes(), Crypto.KeyDer(acl_md.encKey), acl_md.IV));
                if (logger != null)
                {
                    logger.Log("End FileDataStream Decrypt DataBlock");
                }
            }
            return(byteValue);
        }
Exemple #2
0
        protected Tuple <Byte[], StrValue> UpdateHelper(IKey key, IValue value, bool IsAppend, long timestamp = -1)
        {
            if (!value.GetType().Equals(typeof(ByteValue)))
            {
                throw new InvalidDataException("Invalid IValue Type.  ByteValue expected.");
            }

            if (logger != null)
            {
                logger.Log("Start FileDataStream Delete Old DataBlock");
            }
            // check if the entry is present, so that the old file can be deleted
            IValue valueDataFilePathOld = base.Get(key);

            // remove old entry/file if present and update has been called
            if (valueDataFilePathOld != null && !IsAppend)
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(valueDataFilePathOld.ToString());
                try
                {
                    fi.Delete();
                }
                catch (System.IO.IOException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            if (logger != null)
            {
                logger.Log("End FileDataStream Delete Old DataBlock");
            }



            if (logger != null)
            {
                logger.Log("Start FileDataStream Construct DataBlock");
            }
            long ts;  // timestamp

            ts = StreamFactory.HighResTick();
            string   dataFilePath         = targetDir + "/" + Convert.ToString(ts) + ".dat";
            StrValue strDataFilePathValue = new StrValue(Convert.ToString(ts) + ".dat");

            if (streamtype == StreamFactory.StreamSecurityType.Secure)
            {
                value = new ByteValue(Crypto.EncryptBytesSimple(value.GetBytes(), Crypto.KeyDer(acl_md.encKey), acl_md.IV));
            }
            else
            {
                value = new ByteValue(value.GetBytes());
            }


            // if (logger != null) logger.Log("Start FileDataStream Construct DBI");
            Byte[] hash;
            if (streamtype == StreamFactory.StreamSecurityType.Secure)
            {
                hash = hasher.ComputeHash(value.GetBytes());
            }
            else
            {
                hash = null;
            }
            // if (logger != null) logger.Log("End FileDataStream Construct DBI");
            if (logger != null)
            {
                logger.Log("End FileDataStream Construct DataBlock");
            }

            if (logger != null)
            {
                logger.Log("Start FileDataStream Update FilePathValue");
            }
            base.UpdateHelper(key, strDataFilePathValue, IsAppend, hash, timestamp);
            if (logger != null)
            {
                logger.Log("End FileDataStream Update FilePathValue");
            }

            // write <val> to file
            if (logger != null)
            {
                logger.Log("Start FileDataStream WriteToDisc DataBlock");
            }
            FileStream fout = new FileStream(dataFilePath,
                                             FileMode.OpenOrCreate,
                                             FileAccess.Write,
                                             FileShare.ReadWrite);

            fout.Write(value.GetBytes(), 0, (int)value.Size());
            fout.Flush(true);
            fout.Close();
            if (logger != null)
            {
                logger.Log("End FileDataStream WriteToDisc DataBlock");
            }

            return(new Tuple <byte[], StrValue>(hash, strDataFilePathValue));
        }