コード例 #1
0
        private void driveSettingDelete_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Are you sure you want to delete the backup data\nNote: This will leave the backup files on the computer, but just delete the configuration file.", "Delete Config", MessageBoxButtons.YesNo);

            if (res == DialogResult.Yes)
            {
                DriveBackupData tmp = Program.driveDataList[selectedDrive];
                tmp.backupDataExists = false;
                tmp.backupEnabled    = false;
                tmp.backupList.Clear();
                tmp.defaultBackupLocation = "";

                Program.driveDataList[selectedDrive] = tmp;

                string path = selectedDrive.RootDirectory + "__filesync" + "\\" + Program.GetCPUSerial() + ".cfg";
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                else
                {
                    MessageBox.Show("Could not find file!");
                }

                selectedFolder = null;
                LoadDriveList();
                PopulateBackupDriveTree();
                UpdateUIData();
            }
        }
コード例 #2
0
        private void folderSettingEnable_Click(object sender, EventArgs e)
        {
            if (RecursiveNodeCheck(selectedFolder.node) == false)
            {
                DriveBackupData tmp = Program.driveDataList[selectedDrive];
                tmp.backupList.RemoveAll(str => str.StartsWith(BackupUtil.ConvertDrivePath(selectedFolder.FullName)));
                tmp.backupList.Add(BackupUtil.ConvertDrivePath(selectedFolder.FullName));
                Program.driveDataList[selectedDrive] = tmp;
                Program.SaveDriveData(tmp);

                /*selectedFolder.ImageIndex(1);
                *
                *  bool isExpanded = selectedFolder.node.IsExpanded;
                *  selectedFolder.node.Collapse();
                *  selectedFolder.node.Nodes.Clear();
                *  selectedFolder.node.Nodes.Add(new TreeNode("**Temp", 0, 0));
                *  if(isExpanded)
                *   selectedFolder.node.Expand();*/

                selectedFolder = null;
                LoadDriveList();
                PopulateBackupDriveTree();
                UpdateUIData();
            }
        }
コード例 #3
0
        public BackupListBuilder(DriveBackupData driveData)
        {
            this.driveData = driveData;
            this.data      = new List <BackupData>();

            Debug.WriteLine("Create");
        }
コード例 #4
0
        public static bool BackupFile(BackupData dat, DriveBackupData drive)
        {
            try
            {
                string        pathDir = drive.defaultBackupLocation + dat.path.Substring(2) + "\\" + dat.filename + "\\";
                DirectoryInfo inf     = Directory.CreateDirectory(pathDir);
                //Debug.WriteLine(inf.FullName);
                string safeTime = GetSafeTime(DateTime.Now);
                byte[] buffer   = new byte[16 * 4096];
                int    inBuffer;
                long   bytesRead = 0;
                long   totalBytes;
                using (var inputStream = new FileStream(dat.path + "\\" + dat.filename, FileMode.Open, FileAccess.Read, FileShare.None, buffer.Length * 2))
                    using (var outputStream = File.Create(pathDir + safeTime + ".filesync"))
                    {
                        long             fileSize = inputStream.Length;
                        CompressionLevel compMode = fileSize < COMPRESSION_BYTE_MIN ? CompressionLevel.NoCompression : CompressionLevel.Optimal;
                        Debug.WriteLineIf(compMode == CompressionLevel.NoCompression, "NOCOMPRESSION: " + fileSize);
                        using (GZipStream zipStream = new GZipStream(outputStream, compMode))
                        {
                            totalBytes = inputStream.Length;
                            while ((inBuffer = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                if (Program.TERMINATE_BACKUP)
                                {
                                    zipStream.Close();
                                    return(false);
                                }

                                bytesRead += inBuffer;
                                BACKUP_PROGRESS_CURRENTFILE = (int)((bytesRead * 100) / totalBytes);
                                zipStream.Write(buffer, 0, inBuffer);
                            }

                            zipStream.Close();

                            File.WriteAllBytes(pathDir + safeTime + ".filesync" + ".hash", BitConverter.GetBytes(dat.fileHash));
                        }
                    }

                return(true);
            }
            catch (FileNotFoundException e)
            {
                Program.ThrowErrorMessage(e);
                return(false);
            }
            catch (DirectoryNotFoundException e)
            {
                Program.ThrowErrorMessage(e);
                return(false);
            }
            catch (IOException e)
            {
                Program.ThrowErrorMessage(e);
                return(false);
            }
        }
コード例 #5
0
        private void driveSettingDisableBackup_Click(object sender, EventArgs e)
        {
            DriveBackupData tmp = Program.driveDataList[selectedDrive];

            tmp.backupEnabled = false;
            Program.driveDataList[selectedDrive] = tmp;
            Program.SaveDriveData(tmp);
            UpdateUIData();
        }
コード例 #6
0
        private void LoadDriveList()
        {
            driveListView.Nodes.Clear();
            Dictionary <string, TreeNode> nodes = new Dictionary <string, TreeNode>();

            foreach (KeyValuePair <DriveInfo, DriveBackupData> dat in Program.driveDataList)
            {
                DriveBackupData driveDat = dat.Value;
                DriveInfo       drive    = driveDat.drive;
                TreeNode        tmp      = new TreeNode();
                tmp.Text = "(" + drive.Name + ") " + drive.VolumeLabel;
                tmp.Tag  = driveDat;

                if (driveDat.backupList != null && driveDat.backupList.Count > 0)
                {
                    foreach (var v in driveDat.backupList)
                    {
                        TreeNode tmp2 = new TreeNode();
                        tmp2.Text = BackupUtil.ConvertRealPath(v, drive);
                        tmp.Nodes.Add(tmp2);
                    }
                }
                else
                {
                    TreeNode tmp2 = new TreeNode();
                    tmp2.Text = "No synced folders found";
                    tmp.Nodes.Add(tmp2);
                }

                if (!nodes.ContainsKey(Program.VALID_DRIVE_TYPES[drive.DriveType]))
                {
                    TreeNode driveType = new TreeNode(Program.VALID_DRIVE_TYPES[drive.DriveType]);
                    nodes.Add(Program.VALID_DRIVE_TYPES[drive.DriveType], driveType);
                }

                TreeNode typeNode = nodes[Program.VALID_DRIVE_TYPES[drive.DriveType]];
                typeNode.Nodes.Add(tmp);
            }

            foreach (TreeNode node in nodes.Values)
            {
                driveListView.Nodes.Add(node);
            }
            driveListView.ExpandAll();
            UpdateUIData();
        }
コード例 #7
0
        public static FileInfo FindCorrespondingFile(BackupData dat, DriveBackupData drive)
        {
            string        pathDir = drive.defaultBackupLocation + dat.path.Substring(2) + "\\" + dat.filename + "\\";
            DirectoryInfo dir     = new DirectoryInfo(pathDir);

            if (dir.Exists == false)
            {
                Debug.WriteLine("FIND: NO DIR");
                return(null);
            }
            var files = dir.EnumerateFiles().Where(x => x.Extension == ".filesync").OrderByDescending(x => x.LastWriteTime.Ticks).ToList();

            foreach (var file in files)
            {
                Debug.WriteLine("FIND: " + file.FullName);
            }
            return(files.FirstOrDefault());
        }
コード例 #8
0
        public static void SaveDriveData(DriveBackupData dat)
        {
            string path = dat.drive.RootDirectory + "__filesync";

            if (!Directory.Exists(path))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);
                di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
            }

            string writeFile = GenerateBackupDataFile(dat);

            try
            {
                File.WriteAllText(path + "\\" + GetCPUSerial() + ".cfg", writeFile);
            } catch (IOException e)
            {
                MessageBox.Show("Failed to update config: " + e);
            }
        }
コード例 #9
0
        private void driveSettingBackupPath_Click(object sender, EventArgs e)
        {
            DialogResult res = setBackupLocationBrowser.ShowDialog();

            if (res == DialogResult.OK && !string.IsNullOrWhiteSpace(setBackupLocationBrowser.SelectedPath))
            {
                if (setBackupLocationBrowser.SelectedPath.Substring(0, 3) == selectedDrive.RootDirectory.FullName)
                {
                    MessageBox.Show("Error: Cannot output backup to drive being backed up.");
                    return;
                }
                DriveBackupData tmp = Program.driveDataList[selectedDrive];
                tmp.defaultBackupLocation            = setBackupLocationBrowser.SelectedPath;
                Program.driveDataList[selectedDrive] = tmp;
                Program.SaveDriveData(tmp);
                Program.SetupBackupLocation(setBackupLocationBrowser.SelectedPath);
            }

            LoadDriveList();
            UpdateUIData();
        }
コード例 #10
0
        private void driveSettingSetup_Click(object sender, EventArgs e)
        {
            DialogResult res = setBackupLocationBrowser.ShowDialog();

            if (res == DialogResult.OK && !string.IsNullOrWhiteSpace(setBackupLocationBrowser.SelectedPath))
            {
                //Create file and initial setup.
                if (setBackupLocationBrowser.SelectedPath.Substring(0, 3) == selectedDrive.RootDirectory.FullName)
                {
                    MessageBox.Show("Error: Cannot output backup to drive being backed up.");
                    return;
                }
                DriveBackupData tmp = Program.driveDataList[selectedDrive];
                tmp.backupDataExists                 = true;
                tmp.defaultBackupLocation            = setBackupLocationBrowser.SelectedPath;
                Program.driveDataList[selectedDrive] = tmp;
                Program.SaveDriveData(tmp);
                Program.SetupBackupLocation(setBackupLocationBrowser.SelectedPath);
                MessageBox.Show("Backup system has been set up for " + selectedDrive.Name, "Setup Complete");
            }

            LoadDriveList();
            UpdateUIData();
        }
コード例 #11
0
        public static string GenerateBackupDataFile(DriveBackupData dat)
        {
            string ret = "[FILESYNC]\n" + dat.ToString() + "\n" + dat.backupEnabled.ToString().ToLower() + "\n" + dat.defaultBackupLocation;

            return(ret);
        }
コード例 #12
0
        public static async Task <bool> StartBackup(DriveBackupData drive)
        {
            TERMINATE_BACKUP = false;
            try
            {
                ShowNotification("Backup Started", "A backup has started for " + drive.drive.Name + "...");
                if (Properties.Settings.Default.ShowProgress)
                {
                    new MethodInvoker(delegate { OpenProgressWindow(); }).Invoke();
                }
                BACKUP_DRIVE    = drive.drive.Name;
                BACKUP_PROGRESS = 0;
                BACKUP_STATE    = "Collecting File Data...";
                BackupListBuilder build = new BackupListBuilder(drive);
                await build.Start(delegate
                {
                    BACKUP_STATE = "Starting Backup...";
                    List <BackupData> backupList = build.data;
                    int fileCount  = 0;
                    int totalCount = 0;

                    long bytesWritten = 0;
                    foreach (BackupData dat in backupList)
                    {
                        if (TERMINATE_BACKUP)
                        {
                            BACKUP_STATE = "Terminated";
                            return;
                        }
                        BACKUP_STATE = "Syncing (" + totalCount + "/" + backupList.Count + ")...";
                        Debug.WriteLine(dat.fileHash);
                        Debug.WriteLine(dat.backupHash);
                        if (!(dat.fileHash == dat.backupHash))
                        {
                            BACKUP_FILE = "Syncing " + dat.filename;
                            bool ret    = BackupUtil.BackupFile(dat, drive);
                            if (ret == false)
                            {
                                BACKUP_STATE = "FAILED";
                                return;
                            }
                            Debug.WriteLine("Synced " + dat.filename);
                            //BACKUP_FILE = "Syncing " + dat.filename;
                            fileCount++;
                        }
                        else
                        {
                            Debug.WriteLine("Skipped " + dat.filename);
                            BACKUP_FILE = "Skipping " + dat.filename;
                        }
                        totalCount++;
                        bytesWritten   += dat.length;
                        BACKUP_PROGRESS = (int)((bytesWritten * 100) / build.totalCopySize);
                    }
                    BACKUP_PROGRESS = 100;
                    BACKUP_FILE     = "Complete";
                    BACKUP_STATE    = "Complete";
                    if (Properties.Settings.Default.AutoCloseProgress)
                    {
                        if (progressWindow != null && !progressWindow.IsDisposed)
                        {
                            progressWindow.Invoke((EventHandler) delegate
                            {
                                progressWindow.Close();
                            });
                        }
                    }
                    ShowNotification("Backup Finished", "The backup for " + drive.drive.Name + " has finished, " + fileCount + " files were synced.");
                });

                return(true);
            }
            catch (FileNotFoundException e)
            {
                ThrowErrorMessage(e);
                return(false);
            }
            catch (DirectoryNotFoundException e)
            {
                ThrowErrorMessage(e);
                return(false);
            }
            catch (IOException e)
            {
                ThrowErrorMessage(e);
                return(false);
            }
        }
コード例 #13
0
        private static void RefreshDataSet()
        {
            var driveList = DriveInfo.GetDrives().Where(drive => drive.IsReady && VALID_DRIVE_TYPES.ContainsKey(drive.DriveType));

            foreach (DriveInfo drive in driveList)
            {
                bool containsDrive = driveDataList.Any((val) => val.Value.drive.Name == drive.Name);

                if (!containsDrive)
                {
                    DriveBackupData driveDat  = new DriveBackupData(drive);
                    string          checkPath = drive.RootDirectory + "__filesync\\" + GetCPUSerial() + ".cfg";
                    if (File.Exists(checkPath))
                    {
                        StreamReader configData = new StreamReader(checkPath);

                        //Validate file (name contains CPUID)

                        /*
                         * Valid file config
                         * 0: [FILESYNC]
                         * 1: <DATA>
                         * 2: <ENABLED>
                         * 3: <DEFAULT BACKUP LOCATION> (Optional)
                         */

                        if (configData.ReadLine() == "[FILESYNC]")
                        {
                            driveDat.SetData(configData.ReadLine());

                            driveDat.backupEnabled = (configData.ReadLine() == "true" ? true : false);

                            driveDat.backupDataExists = true;

                            string defaultBackup = configData.ReadLine();
                            if (defaultBackup != null)
                            {
                                driveDat.defaultBackupLocation = defaultBackup;
                            }
                        }

                        configData.Close();
                    }

                    driveDataList.Add(driveDat.drive, driveDat);
                    if (driveDat.backupEnabled)
                    {
                        if (!Properties.Settings.Default.AskBeforeBackup)
                        {
                            StartBackup(driveDat);
                        }
                        else
                        {
                            ShowNotification("New Drive Inserted", driveDat.drive.Name + " is ready to be synced!");
                        }
                    }
                }
            }

            ValidateDataSet();

            if (!(mainWindow == null || mainWindow.IsDisposed))
            {
                mainWindow.RefreshAllDataExternal();
            }
        }