Example #1
0
        /// <summary>
        /// Compares 2 files to verify that they are the identical
        /// </summary>
        public static bool FilesAreEqual(string firstName, string secondName)
        {
            var firstSize  = NativeMethods.GetFileLength(firstName);
            var secondSize = NativeMethods.GetFileLength(secondName);

            const int BYTES_TO_READ = 1024 * 1024;

            if (firstSize != secondSize)
            {
                return(false);
            }

            int iterations = (int)Math.Ceiling((double)firstSize / BYTES_TO_READ);

            using (FileStream fs1 = LongFile.GetFileStream(firstName, FileAccess.ReadWrite))
                using (FileStream fs2 = LongFile.GetFileStream(secondName, FileAccess.ReadWrite))
                {
                    byte[] one = new byte[BYTES_TO_READ];
                    byte[] two = new byte[BYTES_TO_READ];

                    for (int i = 0; i < iterations; i++)
                    {
                        fs1.Read(one, 0, BYTES_TO_READ);
                        fs2.Read(two, 0, BYTES_TO_READ);

                        if (BitConverter.ToInt64(one, 0) != BitConverter.ToInt64(two, 0))
                        {
                            return(false);
                        }
                    }
                }
            return(true);
        }
Example #2
0
 private static void DeleteDirectories(string[] directories)
 {
     foreach (string directory in directories)
     {
         string[] files = LongDirectory.GetFiles(directory, null, System.IO.SearchOption.TopDirectoryOnly);
         foreach (string file in files)
         {
             LongFile.Delete(file);
         }
         directories = LongDirectory.GetDirectories(directory, null, System.IO.SearchOption.TopDirectoryOnly);
         DeleteDirectories(directories);
         bool ok = NativeMethods.RemoveDirectory(GetWin32LongPath(directory));
         if (!ok)
         {
             ThrowWin32Exception();
         }
     }
 }
Example #3
0
        /// <summary>
        /// CRC hash a file
        /// </summary>
        public static string FileCRC(string filename)
        {
            var sb = new StringBuilder();

            using (var md5 = MD5.Create())
            {
                //This now supports long file names
                //using (var stream = File.OpenRead(filename))
                using (var stream = LongFile.GetFileStream(filename, FileAccess.ReadWrite))
                {
                    byte[] hashBytes = md5.ComputeHash(stream);
                    foreach (byte bt in hashBytes)
                    {
                        sb.Append(bt.ToString("x2"));
                    }
                }
            }
            return(sb.ToString());
        }
Example #4
0
        /// <summary>
        /// Saves a file to storage for a tenant in the specified container
        /// </summary>
        public virtual bool SaveFile(Guid tenantId, string container, string fileName)
        {
            try
            {
                long     fileLength    = 0;
                DateTime creationTime  = DateTime.Now;
                DateTime lastWriteTime = DateTime.Now;
                if (fileName.Length > 260)
                {
                    NativeMethods.GetFileInfo(fileName, out creationTime, out lastWriteTime, out fileLength);
                }
                else
                {
                    var fi = new FileInfo(fileName);
                    fileLength    = fi.Length;
                    creationTime  = fi.CreationTime;
                    lastWriteTime = fi.LastWriteTime;
                }

                //Save the file

                const int blockSize = 1024 * 1024;
                var       count     = (int)Math.Ceiling((fileLength * 1.0) / blockSize);

                var block = new FileInformation
                {
                    Container    = container,
                    FileName     = fileName,
                    TenantID     = tenantId,
                    CRC          = FileUtilities.FileCRC(fileName),
                    Size         = fileLength,
                    CreatedTime  = creationTime.ToUniversalTime(),
                    ModifiedTime = lastWriteTime.ToUniversalTime(),
                };

                //using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var fs = LongFile.GetFileStream(fileName, FileAccess.ReadWrite))
                {
                    FileDataInfo fileInfo = null;
                    RetryHelper.DefaultRetryPolicy(3)
                    .Execute(() =>
                    {
                        using (var factory = GetFactory(_server, _port))
                        {
                            var service = factory.CreateChannel();
                            fileInfo    = service.SendFileStart(block);
                        }
                    });

                    for (var ii = 0; ii < count; ii++)
                    {
                        var bb = new byte[blockSize];
                        var c  = fs.Read(bb, 0, bb.Length);
                        //If last block is smaller then truncate it
                        if (c < blockSize)
                        {
                            bb = bb.Take(c).ToArray();
                        }

                        RetryHelper.DefaultRetryPolicy(3)
                        .Execute(() =>
                        {
                            using (var factory = GetFactory(_server, _port))
                            {
                                var service  = factory.CreateChannel();
                                var wasSaved = service.SendFileData(fileInfo.Token, bb, ii);
                            }
                        });

                        this.OnFileUpload(new FileProgressEventArgs
                        {
                            ChunkIndex  = ii,
                            Container   = container,
                            FileName    = fileName,
                            TotalChunks = count,
                        });
                    }

                    var retval = false;
                    RetryHelper.DefaultRetryPolicy(3)
                    .Execute(() =>
                    {
                        using (var factory = GetFactory(_server, _port))
                        {
                            var service = factory.CreateChannel();
                            retval      = service.SendFileEnd(fileInfo.Token);
                        }
                    });
                    return(retval);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }