Ejemplo n.º 1
0
        public bool Contains(string hash, string path)
        {
            string hashKey = BackupFile.GetFileHash(hash, path);

            return(this.hashesAndFileNames.Contains(hashKey));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fullPath">Full fullPath.</param>
        /// <param name="masterFolder"></param>
        /// <param name="indexFolder"></param>
        /// <returns></returns>
        public BackupFile GetBackupFile(string fullPath, string masterFolder, string indexFolder)
        {
            // we hash the path of the file so we can look it up quickly
            // then we check the ModifiedTime and size
            // if these have changed we redo the hash
            // files with same hash are allowed (Porridge TV ep and movie)
            // files can't have same hash and same filename though

            string hash;

            BackupFile backupFile;

            // if this path is already added then return it
            if (this.paths.Contains(fullPath))
            {
                // check the timestamp against what we have
                backupFile = (BackupFile)this.paths[fullPath];

                var t = backupFile.LastWriteTime;
                var u = Utils.GetFileLastWriteTime(fullPath);

                // if the file on disk is different then check the hash
                if (t != u)
                {
                    // update the timestamp as its changed/missing
                    backupFile.LastWriteTime = u;

                    hash = Utils.GetShortMd5HashFromFile(fullPath);

                    // has the hash changed too?
                    if (hash != backupFile.ContentsHash)
                    {
                        if (this.hashesAndFileNames.Contains(backupFile.Hash))
                        {
                            this.hashesAndFileNames.Remove(backupFile.Hash);
                        }

                        backupFile.ContentsHash = hash;
                        this.hashesAndFileNames.Add(backupFile.Hash, backupFile);

                        // clear the backup details as the master file hash has changed
                        backupFile.BackupDisk        = null;
                        backupFile.BackupDiskChecked = null;
                    }
                }

                // Check the file length
                if (backupFile.Length == 0)
                {
                    backupFile.UpdateFileLength();
                }

                return(backupFile);
            }

            hash = Utils.GetShortMd5HashFromFile(fullPath);

            string hashKey = BackupFile.GetFileHash(hash, fullPath);

            if (this.hashesAndFileNames.Contains(hashKey))
            {
                var b = (BackupFile)this.hashesAndFileNames[hashKey];

                if (this.paths.Contains(b.FullPath))
                {
                    this.paths.Remove(b.FullPath);
                }

                b.SetFullPath(fullPath, masterFolder, indexFolder);
                this.paths.Add(fullPath, b);

                return(b);
            }

            backupFile = new BackupFile(fullPath, masterFolder, indexFolder);
            this.BackupFiles.Add(backupFile);

            this.hashesAndFileNames.Add(backupFile.Hash, backupFile);
            this.paths.Add(fullPath, backupFile);

            return(backupFile);
        }