예제 #1
0
        public bool Verify(BlobSyncInfo syncInfo, bool verbose)
        {
            foreach (var item in syncInfo.AllRemoteItems)
            {
                if (verbose)
                {
                    Console.WriteLine("Verifying " + item.Name);
                    Console.WriteLine("  [REMOTE: " + Convert.ToBase64String(item.Properties.ContentHash) + " " + item.Properties.ContentLength + "]");
                }

                // does the file exist?
                var localPath = Path.Combine(LocalPath, item.Name);
                if (File.Exists(localPath))
                {
                    var fileInfo = GetFileData(LocalPath, localPath);
                    if (verbose)
                    {
                        Console.WriteLine("  [LOCAL:  " + fileInfo.MD5 + " " + fileInfo.Length + " " + fileInfo.LastModified + " " + localPath + "]");
                    }
                    if (fileInfo.MD5 != Convert.ToBase64String(item.Properties.ContentHash) || fileInfo.Length != item.Properties.ContentLength)
                    {
                        if (verbose)
                        {
                            Console.WriteLine("  STILL DIFFERS!");
                        }
                        return(false);
                    }
                }
                else
                {
                    if (verbose)
                    {
                        Console.WriteLine("  NOT FOUND!");
                    }
                    return(false);
                }
            }
            return(true);
        }
예제 #2
0
        public async Task <BlobSyncInfo> GetSyncInfoAsync(bool verbose)
        {
            BlobServiceClient client;

            if (ConnectionUrl != null)
            {
                client = new BlobServiceClient(new Uri(ConnectionUrl));
            }
            else
            {
                client = new BlobServiceClient(ConnectionString);
            }
            var container = client.GetBlobContainerClient(ContainerName);

            var blobs         = ListBlobsAsync(container, "");
            var syncInfo      = new BlobSyncInfo();
            var seenBlobNames = new HashSet <string>();

            // look at all blobs and place them in the correct category
            await foreach (var item in blobs)
            {
                syncInfo.AllRemoteItems.Add(item);
                seenBlobNames.Add(item.Name);

                if (verbose)
                {
                    Console.WriteLine(item.Name);
                    Console.WriteLine("  [REMOTE: " + Convert.ToBase64String(item.Properties.ContentHash) + " " + item.Properties.ContentLength + " " + item.Properties.LastModified + "]");
                }

                // does the file exist?
                var localPath = Path.Combine(LocalPath, item.Name);
                if (File.Exists(localPath))
                {
                    // is the same? look at length and md5
                    var fileInfo = GetFileData(LocalPath, localPath);
                    if (verbose)
                    {
                        Console.Write("  [LOCAL:  " + fileInfo.MD5 + " " + fileInfo.Length + " " + fileInfo.LastModified + " " + localPath + "]: ");
                    }
                    if (fileInfo.MD5 == Convert.ToBase64String(item.Properties.ContentHash) &&
                        fileInfo.Length == item.Properties.ContentLength)
                    {
                        if (verbose)
                        {
                            Console.WriteLine("MATCH");
                        }
                        var blockBlob = container.GetBlobClient(item.Name);
                        syncInfo.Identical.Add((blockBlob, fileInfo));
                    }
                    else
                    {
                        if (verbose)
                        {
                            Console.WriteLine("DIFFER");
                        }
                        var blockBlob = container.GetBlobClient(item.Name);
                        syncInfo.Differs.Add((blockBlob, fileInfo));
                    }
                }
                else
                {
                    if (verbose)
                    {
                        Console.WriteLine("  [LOCAL:  NOT FOUND]");
                    }
                    // file does not exist locally
                    var blockBlob = container.GetBlobClient(item.Name);
                    syncInfo.OnlyRemote.Add(blockBlob);
                }
            }

            // look at all files, identify those that have no corresponding blob and put them in the onlyLocal category
            var options = new EnumerationOptions
            {
                RecurseSubdirectories = true
            };

            foreach (var filePath in Directory.EnumerateFiles(LocalPath, "*", options))
            {
                var fileName = Path.GetRelativePath(LocalPath, filePath).Replace("\\", "/");

                if (!seenBlobNames.Contains(fileName))
                {
                    var fileInfo = GetFileData(LocalPath, Path.Combine(LocalPath, fileName));
                    syncInfo.OnlyLocal.Add(fileInfo);
                }
            }

            return(syncInfo);
        }