Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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());
        }
        private List <BackupData> GetBackupData(DirectoryInfo dir, List <BackupData> dat)
        {
            if (dat == null)
            {
                dat = new List <BackupData>();
            }

            if (Program.TERMINATE_BACKUP)
            {
                Program.BACKUP_STATE = "Terminated";
                return(new List <BackupData>());
            }

            try
            {
                FileInfo[] files = dir.GetFiles();
                totalItems += files.Length;
                foreach (FileInfo file in files)
                {
                    Program.BACKUP_FILE = file.FullName;
                    if (Program.TERMINATE_BACKUP)
                    {
                        Program.BACKUP_STATE = "Terminated";
                        return(new List <BackupData>());
                    }
                    BackupData backup   = new BackupData();
                    bool       sizeDiff = false;
                    totalCopySize  += file.Length;
                    backup.filename = file.Name;
                    backup.path     = file.DirectoryName;
                    backup.length   = file.Length;
                    FileInfo corr = BackupUtil.FindCorrespondingFile(backup, driveData);
                    backup.fileHash = BackupUtil.ComputeHash(file);
                    if (!sizeDiff && corr != null && corr.Exists)
                    {
                        backup.backupHash = BackupUtil.FindUncompressedHash(corr);
                    }
                    scannedItems++;
                    Program.BACKUP_PROGRESS = (scannedItems * 100) / totalItems;
                    dat.Add(backup);
                    Program.BACKUP_STATE = "Collecting file data... (" + dat.Count + ")";
                }
            } catch (System.UnauthorizedAccessException)
            {
            }

            try
            {
                DirectoryInfo[] dirs = dir.GetDirectories();
                totalItems += dirs.Length;
                foreach (DirectoryInfo val in dirs)
                {
                    if (Program.TERMINATE_BACKUP)
                    {
                        Program.BACKUP_STATE = "Terminated";
                        return(new List <BackupData>());
                    }

                    if (val.Name == "__filesync")
                    {
                        continue;
                    }

                    scannedItems++;
                    Program.BACKUP_PROGRESS = (scannedItems * 100) / totalItems;
                    dat = GetBackupData(val, dat);
                }
            } catch (System.UnauthorizedAccessException)
            {
            }


            return(dat);
        }