예제 #1
0
파일: MainForm.cs 프로젝트: fsfod/Echo
        private void ThreadedSyncAndScan(object sender, DoWorkEventArgs e)
        {
            // Get the BackgroundWorker that raised this event.
            BackgroundWorker worker = sender as BackgroundWorker;

            Dictionary <string, SyncItem> serverhashes;

            //get the server hashlist
            AddTextToConsole("Getting server hashes (" + Settings.HashServer + ")" + Environment.NewLine);
            try
            {
                serverhashes = Http.GetHashList();
            }
            catch (Exception ex)
            {
                AddTextToConsole("Error Reading Remote Hashes:" + ex.Message + Environment.NewLine);
                return;
            }

            //hash all teh local files.
            AddTextToConsole("Checking Local hashes" + Environment.NewLine);
            SyncList LocalData = new SyncList(Settings.LocalDirectory);

            List <string> FilesToDownload = new List <string>();

            LocalData.saveSyncList(Settings.HashCache);
            //get the delta list
            foreach (KeyValuePair <string, SyncItem> kvp in serverhashes)
            {
                //ignore metadata.
                if (kvp.Key == "__Server" || kvp.Key == "__DateGeneratedUTC")
                {
                    ;
                }
                else
                {
                    //Queue downloads
                    if (!LocalData.HashList.ContainsKey(kvp.Key))
                    {
                        //downlaod files that dont exist.
                        FilesToDownload.Add(kvp.Key);
                    }
                    else
                    {
                        if (kvp.Value.Hash != LocalData.HashList[kvp.Key].Hash)
                        {
                            //download files the dont mathc the hash
                            FilesToDownload.Add(kvp.Key);
                        }
                        else
                        {
                            //if we get here the file exist and is right.
                            //get it out of the hash list so we dont delrte it.
                            LocalData.HashList.Remove(kvp.Key);
                        }
                    }
                }
            }

            //Remove Files if set to
            if (Settings.RemoveLocalFileIfNoRemoteFile)
            {
                AddTextToConsole("Generating list of local files to remove" + Environment.NewLine);
                bool shouldDelete = true;
                //ensure we didnt delte everything accidentally.
                if (LocalData.HashList.Count > Settings.numFilesToRemoveWithNoWarning)
                {
                    shouldDelete = false;
                    if (MessageBox.Show(LocalData.HashList.Count + " Files Are flagged for deletion." + Environment.NewLine + " Delete them?", "Continue?", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        shouldDelete = true;
                        AddTextToConsole("Deleting Files" + Environment.NewLine);
                    }
                    else
                    {
                        AddTextToConsole("Files Will NOT be Removed" + Environment.NewLine);
                    }
                }


                //remove files
                if (shouldDelete && LocalData.HashList.Count > 0)
                {
                    foreach (KeyValuePair <string, SyncItem> kvp in LocalData.HashList)
                    {
                        AddTextToConsole("  " + kvp.Key + Environment.NewLine);

                        File.Delete(Settings.LocalDirectory + kvp.Key);
                    }
                }
            }



            //DOWNLOADS
            AddTextToConsole("Need to download: " + FilesToDownload.Count + " from " + Settings.DownloadType + Environment.NewLine);
            ErrorCount = 0;



            int tasklimit = 10;

            int nextFile = 0;
            GetObjectRequest request;
            AmazonS3Client   s3 = new  AmazonS3Client(Settings.s3IDKey, Settings.s3SecretKey);

            while (nextFile < FilesToDownload.Count || DownloadCount > 0)
            {
                if (DownloadCount < tasklimit && nextFile < FilesToDownload.Count)
                {
                    request            = new GetObjectRequest();
                    request.BucketName = Settings.s3Bucket;
                    request.Key        = FilesToDownload[nextFile].Substring(1); //use substring so we elminate the /
                    request.Timeout    = 1000;                                   //wait 1 minute for a  response.
                    AddTextToConsole("  " + (nextFile + 1) + "/" + FilesToDownload.Count + " " + request.Key + Environment.NewLine);

                    s3.BeginGetObject(request, DownloadFile, s3);
                    nextFile++;
                    DownloadCount++;
                    worker.ReportProgress((int)(((float)nextFile) / ((float)FilesToDownload.Count) * 100.0f));
                }
            }



            if (nextFile - ErrorCount < FilesToDownload.Count)
            {
                AddTextToConsole("WARNING: NOT ALL FILES WERE SUCCESSFULLY DOWNLOADED" + Environment.NewLine + Environment.NewLine);
            }
            AddTextToConsole("Download queue processed. Check log for any errors" + Environment.NewLine);
        }
예제 #2
0
        public static void Main(string[] args)
        {
            Dictionary <string, SyncItem> serverhashes = null;
            SyncList LocalData = null;

            Console.WriteLine("Startup.  ");

            //read teh settings file
            Settings.ReadConfigFile(Settings.CONFIG_FILE_CLIENT);

            Console.WriteLine("Getting file list");
            //get the server hashlist
            try {
                serverhashes = Http.GetHashList();
            } catch (Exception ex) {
                Console.WriteLine("Something went wrong gettin'  our remote hashes. Oops.\n" + ex.Message);
                return;
            }

            Console.WriteLine("getting local files");
            //hash all teh local files.
            try {
                LocalData = new SyncList(Settings.LocalDirectory);
                Console.WriteLine("hashed:" + LocalData.HashList.Count);

                LocalData.saveSyncList(Settings.HashCache);
            } catch (Exception ex) {
                Console.WriteLine("Problems readin' local directory. Oops\n" + ex.Message);
                return;
            }


            List <string> FilesToDownload = new List <string> ();

            //get the delta list
            foreach (KeyValuePair <string, SyncItem> kvp in serverhashes)
            {
                //ignore metadata.
                if (kvp.Key == "__Server" || kvp.Key == "__DateGeneratedUTC")
                {
                    ;
                }
                else
                {
                    //Queue downloads
                    if (!LocalData.HashList.ContainsKey(kvp.Key))
                    {
                        //downlaod files that dont exist.
                        FilesToDownload.Add(kvp.Key);
                        Console.WriteLine("New File: " + kvp.Key);
                    }
                    else
                    {
                        if (kvp.Value.Hash != LocalData.HashList [kvp.Key].Hash)
                        {
                            //download files the dont mathc the hash
                            FilesToDownload.Add(kvp.Key);
                            Console.WriteLine(String.Format("hash mismatch: {0}  ;local{2}, remote {1}", kvp.Key, kvp.Value.Hash, LocalData.HashList [kvp.Key].Hash));
                        }
                        else
                        {
                            //if we get here the file exist and is right.
                            //get it out of the hash list so we dont delrte it.
                            bool okay = LocalData.HashList.Remove(kvp.Key);
                            //Console.WriteLine ("File OK: " + kvp.Key+ "okay"+okay);
                        }
                    }
                }
            }

            //Remove Files if set to
            if (Settings.RemoveLocalFileIfNoRemoteFile)
            {
                bool shouldDelete = true;
                //ensure we didnt delte everything accidentally.
                if (LocalData.HashList.Count > Settings.numFilesToRemoveWithNoWarning)
                {
                    shouldDelete = false;
                    Console.Write(LocalData.HashList.Count + " Files Are flagged for deletion, Remove them (Y/N)");
                    char key = (char)Console.Read();

                    if (key == 'Y' || key == 'y')
                    {
                        shouldDelete = true;
                    }
                }


                //remove files
                if (shouldDelete)
                {
                    foreach (KeyValuePair <string, SyncItem> kvp in LocalData.HashList)
                    {
                        File.Delete(Settings.LocalDirectory + kvp.Key);
                        Console.WriteLine("removed: " + kvp.Key);
                    }
                }
            }

            //DOWNLOADS
            Console.WriteLine("Need to download: " + FilesToDownload.Count + " from " + Settings.DownloadType);
            int progress = 0;



            int tasklimit = 10;

            int nextFile = 0;
            GetObjectRequest request;
            AmazonS3Client   s3 = new  AmazonS3Client(Settings.s3IDKey, Settings.s3SecretKey);

            while (nextFile < FilesToDownload.Count || DownloadCount > 0)
            {
                if (DownloadCount < tasklimit && nextFile < FilesToDownload.Count)
                {
                    request            = new GetObjectRequest();
                    request.BucketName = Settings.s3Bucket;
                    request.Key        = FilesToDownload[nextFile].Substring(1); //use substring so we elminate the /
                    request.Timeout    = 1000;                                   //wait 1 minute for a  response.
                    Console.WriteLine(nextFile + "/" + FilesToDownload.Count + " " + request.Key);

                    s3.BeginGetObject(request, DownloadFile, s3);
                    nextFile++;
                    DownloadCount++;
                }
            }



            Console.WriteLine("Downloaded " + (nextFile - ErrorCount) + "/" + FilesToDownload.Count + " File");

            if (nextFile - ErrorCount != FilesToDownload.Count)
            {
                Console.WriteLine("WARNING: NOT ALL FILES WERE SUCCESSFULLY DOWNLOADED");
                Console.WriteLine("         YOU SHOULD RUN THIS TOOL AGAIN.");
            }
            Console.WriteLine("Press any key to close.");
            Console.ReadKey();
        }