Exemplo n.º 1
0
        public static void Run7zip(FileDetail fd, FolderList fl)
        {
            if (System.IO.File.Exists(fd.CompressedName))
            {
                Debug.Print("Deleting existing compressed file: {0}", fd.CompressedName);
                FileHandler.DeleteFile(fd.CompressedName);
            }

            string password = Program.MainForm.config.PasswordText;

            if (!string.IsNullOrEmpty(password))
            {
                password = "******" + password + " -mhe";
            }

            CompressionLevel compLevel = (CompressionLevel)Program.MainForm.config.CompressionLevel;

            RunProcess(Program.MainForm.config.Executable7zip, string.Format(@"a -t7z -y ""{0}"" ""{1}"" {2} -{3}", fd.CompressedName, fd.OriginalName, password, compLevel));

            if (File.Exists(fd.CompressedName))
            {
                FileInfo cFi = new System.IO.FileInfo(fd.CompressedName);
                //fd.CompressedHash = Crypto.GetSHA512Hash(cFi.FullName);
                fd.CompressedTime = cFi.LastWriteTimeUtc;
                fd.CompressedSize = cFi.Length;
            }
        }
Exemplo n.º 2
0
        //private void HandleRename()
        //{
        //    foreach (var item in filesRenamed)
        //    {
        //        FileDetail fd = FileDetail.ReturnObject(item.Key);
        //        string oCompressed = fd.CompressedName;

        //        fd.OriginalName = item.Value;
        //        System.IO.File.Move(oCompressed, fd.CompressedName);

        //        Debug.Print("Renamed file {0} to {1}", oCompressed, fd.CompressedName);
        //        Log(System.IO.Path.GetDirectoryName(oCompressed), System.IO.Path.GetFileName(oCompressed), string.Format("File renamed to: {0}", fd.CompressedName), LogColor.Amber);
        //    }

        //    filesRenamed.Clear();
        //}

        void SyncWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            CtrlInvoke.ButtonEnable(btnSyncStop, true);
            CtrlInvoke.ButtonEnable(btnSyncStart, false);
            CtrlInvoke.ButtonEnable(btnExtract, false);

            if (this.WindowState == FormWindowState.Minimized)
            {
                notifyIcon1.ShowBalloonTip(5000, "NoPeekCloud", "Synchronizing...", ToolTipIcon.Info);
            }

            ListViewItem runLogItem = new ListViewItem(new[] { DateTime.Now.ToString("dd.MM.yy"), DateTime.Now.ToString("HH:mm:ss"), (string)e.Argument });

            CtrlInvoke.SetLog(lstRunLog, runLogItem);

            List <FileDetail> tempFileDetail = new List <FileDetail>();

            CtrlInvoke.SetText(txtLastRun, DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString());

            Stopwatch stopWatch = new Stopwatch();

            if (string.IsNullOrEmpty(config.PasswordText))
            {
                MsgManager.Show("Password is empty, files will not be encrypted.", "Empty password", MessageBoxIcon.Warning);
            }

            changeTimer = -1;
            int totalCount = 0;

            try
            {
                stopWatch.Start();
                CtrlInvoke.SetStatus(statusStrip1, tslStatus, "Renaming files...");
                //HandleRename();

                CtrlInvoke.SetStatus(statusStrip1, tslStatus, "Finding files...");

                int x = 0;

                foreach (FolderList fl in FolderList.list)
                {
                    System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(fl.Source);

                    Regex re = new Regex(@"(\.tmp|^~|^thumbs\.db$|^desktop\.ini$)", RegexOptions.IgnoreCase);
                    fl.SourceFiles.AddRange(d.EnumerateFiles("*.*", System.IO.SearchOption.AllDirectories).Where(f => !re.IsMatch(System.IO.Path.GetFileName(f.FullName))).ToList());

                    System.IO.DirectoryInfo dt = new System.IO.DirectoryInfo(fl.Target);
                    fl.CompressedFiles.AddRange(dt.GetFiles("*.*", System.IO.SearchOption.AllDirectories).Select(path => path.FullName).ToList());

                    totalCount += fl.SourceFiles.Count;
                }

                RemainingTime rt = new RemainingTime(totalCount, 250);

                CtrlInvoke.SetText(txtFileTotal, totalCount.ToString());
                CtrlInvoke.SetStatus(statusStrip1, tslStatus, "Synchronizing...");

                foreach (FolderList fl in FolderList.list)
                {
                    Debug.Print("Working on folder: {0}", fl.Source);
                    Debug.Indent();

                    foreach (var file in fl.SourceFiles)
                    {
                        Debug.Print("Found file: {0}", file.FullName);
                        Debug.Indent();

                        CtrlInvoke.SetText(txtFileCurrent, x.ToString());
                        CtrlInvoke.SetText(txtProgressFolder, file.DirectoryName);
                        CtrlInvoke.SetText(txtProgressFile, file.Name);

                        FileDetail fd = new FileDetail();
                        fd.OriginalName = file.FullName;
                        fd.OriginalPath = fl.Source;
                        fd.OriginalHash = Crypto.GetSHA512Hash(file.FullName);
                        fd.OriginalTime = file.LastWriteTimeUtc;
                        fd.OriginalSize = file.Length;

                        //fd.CompressedName = fd.CompressedNamePath;
                        if (System.IO.File.Exists(fd.CompressedName))
                        {
                            System.IO.FileInfo cFi = new System.IO.FileInfo(fd.CompressedName);
                            //fd.CompressedHash = Crypto.GetSHA512Hash(cFi.FullName);
                            fd.CompressedTime = cFi.LastWriteTimeUtc;
                            fd.CompressedSize = cFi.Length;
                        }

                        fl.CompressedFiles.Remove(fd.CompressedName);

                        if (FileDetail.ObjectExists(file.FullName) > 0)
                        {
                            Debug.Print("File exists, checking details...");
                            FileDetail fd2 = FileDetail.ReturnObject(file.FullName);

                            if (fd.OriginalHash == fd2.OriginalHash && fd.OriginalSize == fd2.OriginalSize && fd.OriginalTime == fd2.OriginalTime)
                            {
                                Debug.Print("Files are equal, check if compressed file is correct...");
                                //if (fd.CompressedHash == fd2.CompressedHash && fd.CompressedSize == fd2.CompressedSize && fd.CompressedTime == fd2.CompressedTime && !config.ForceCompressedCreation)
                                if (fd.CompressedSize == fd2.CompressedSize && fd.CompressedTime == fd2.CompressedTime && !config.ForceCompressedCreation)
                                {
                                    Debug.Print("Compressed file checks out, do nothing...");
                                }
                                else
                                {
                                    if (!config.ForceCompressedCreation)
                                    {
                                        Debug.Print("Compressed file does not match, recreate!");
                                        //if (fd.CompressedHash != fd2.CompressedHash) Debug.Print("Hash is different: {0} <> {1}", fd.CompressedHash, fd2.CompressedHash);
                                        if (fd.CompressedSize != fd2.CompressedSize)
                                        {
                                            Debug.Print("Size is different: {0} <> {1}", fd.CompressedSize, fd2.CompressedSize);
                                        }
                                        if (fd.CompressedTime != fd2.CompressedTime)
                                        {
                                            Debug.Print("Time is different: {0} <> {1}", fd.CompressedTime, fd2.CompressedTime);
                                        }

                                        logWriter.FileLog(file.DirectoryName, file.Name, "Compressed file does not match, recreate!", LogColor.Blue);
                                    }
                                    else
                                    {
                                        Debug.Print("Forcing creation of compressed file!");
                                        logWriter.FileLog(file.DirectoryName, file.Name, "Forcing creation of compressed file!", LogColor.Blue);
                                    }
                                    ProcHandler.Run7zip(fd, fl);
                                }
                            }
                            else
                            {
                                Debug.Print("Files differ, do something!");
                                if (fd.OriginalHash != fd2.OriginalHash)
                                {
                                    Debug.Print("Hash is different: {0} <> {1}", fd.OriginalHash, fd2.OriginalHash);
                                }
                                if (fd.OriginalSize != fd2.OriginalSize)
                                {
                                    Debug.Print("Size is different: {0} <> {1}", fd.OriginalSize, fd2.OriginalSize);
                                }
                                if (fd.OriginalTime != fd2.OriginalTime)
                                {
                                    Debug.Print("Time is different: {0} <> {1}", fd.OriginalTime, fd2.OriginalTime);
                                }

                                logWriter.FileLog(file.DirectoryName, file.Name, "Files differ, do something!", LogColor.Amber);
                                ProcHandler.Run7zip(fd, fl);
                            }
                        }
                        else
                        {
                            Debug.Print("File not seen before, encrypt and add to list.");
                            logWriter.FileLog(file.DirectoryName, file.Name, "File not seen before, encrypt and add.", LogColor.Green);

                            //FileDetail.list.Add(fd);
                            ProcHandler.Run7zip(fd, fl);
                        }

                        tempFileDetail.Add(fd);

                        x++;
                        SyncWorker.ReportProgress(Function.ReturnPercent(x, totalCount));

                        if (SyncWorker.CancellationPending)
                        {
                            Debug.Unindent();
                            break;
                        }

                        Debug.Unindent();
                        CtrlInvoke.SetText(txtRemainingTime, rt.Calculate(stopWatch.ElapsedMilliseconds, x));
                    }

                    if (!SyncWorker.CancellationPending)
                    {
                        foreach (string deleteFile in fl.CompressedFiles)
                        {
                            Debug.Print("Compressed file {0} not in use, delete.", deleteFile);
                            logWriter.FileLog(System.IO.Path.GetDirectoryName(deleteFile), System.IO.Path.GetFileName(deleteFile), "Compressed file not in use, delete.", LogColor.Red);

                            FileHandler.DeleteFile(deleteFile);
                        }

                        foreach (var directory in System.IO.Directory.GetDirectories(fl.Target, "*", SearchOption.AllDirectories))
                        {
                            if (System.IO.Directory.GetFiles(directory).Length == 0 && System.IO.Directory.GetDirectories(directory).Length == 0)
                            {
                                Debug.Print("Deleting empty folder {0}.", directory);
                                logWriter.FileLog(directory, string.Empty, "Deleting empty folder", LogColor.Red);
                                FileHandler.DeleteFolder(directory);
                            }
                        }
                    }

                    else
                    {
                        Debug.Unindent();
                        break;
                    }

                    Debug.Unindent();
                }

                if (!SyncWorker.CancellationPending)
                {
                    List <FileDetail> deleteResult = FileDetail.list.Except(tempFileDetail).ToList();
                    foreach (FileDetail fd in deleteResult)
                    {
                        Debug.Print("FileDetail entry {0} points to no file that does not exist. Delete.", fd.OriginalName);
                        logWriter.FileLog(System.IO.Path.GetDirectoryName(fd.OriginalName), System.IO.Path.GetFileName(fd.OriginalName), "FileDetail entry not in use, delete.", LogColor.Red);
                    }
                }
            }
            catch (Exception exp)
            {
                MsgManager.LaunchExceptionReporter(exp);
            }
            finally
            {
                try
                {
                    if (System.IO.File.Exists(FileDetail.FileName))
                    {
                        System.IO.File.Copy(FileDetail.FileName, currentFolder + "\\FileDetails.bak", true);
                    }

                    if (!SyncWorker.CancellationPending)
                    {
                        XML.SerializeList <FileDetail>(FileDetail.FileName, tempFileDetail);
                        FileDetail.list = tempFileDetail;
                    }
                }
                catch (Exception exp2)
                {
                    MsgManager.LaunchExceptionReporter(exp2);
                }
                finally
                {
                    stopWatch.Stop();
                    CtrlInvoke.SetText(txtSyncStopWatch, stopWatch.Elapsed.ToString());
                    CtrlInvoke.SetText(txtSyncNumberOfFiles, totalCount.ToString());

                    foreach (FolderList fl in FolderList.list)
                    {
                        fl.SourceFiles.Clear();
                        fl.CompressedFiles.Clear();
                    }
                }
            }
        }