コード例 #1
0
ファイル: CommandHash.cs プロジェクト: belucha/hashlinkcopy
 protected override void ProcessFile(FileInfo info, int level)
 {
     var hi = new HashInfo(info, CommandHash.HASH_ALG);
     var hf = hi.GetHashPath(HashDir);
     var hInfo = new FileInfo(hf);
     Logger.Root.WriteLine(Verbosity.Debug, "{0}=>{1}", info.FullName, hInfo.FullName);
 }
コード例 #2
0
ファイル: CommandShrink.cs プロジェクト: belucha/hashlinkcopy
 protected override void ProcessFile(FileInfo info, int level)
 {
     var hi = new HashInfo(info, CommandShrink.SHA1);
     var hf = Path.GetFullPath(hi.GetHashPath(HashDir));
     // check if we need to copy the file
     if (!File.Exists(hf))
     {
         Directory.CreateDirectory(Path.GetDirectoryName(hf));
         Monitor.Root.MoveFile(info.FullName, hf, info.Length);
         File.SetAttributes(hf, FileAttributes.Normal);
     }
     else
     {
         var hInfo = new FileInfo(hf);
         if (hInfo.Length != info.Length)
         {
             Monitor.Root.HashCollision(hf, info.FullName);
             return;
         }
         Monitor.Root.DeleteFile(info);
     }
     // create link
     if (Monitor.Root.LinkFile(hf, info.FullName, info.Length)!=0)
         // 10bit link count overrun => move the hash file
         Monitor.Root.MoveFile(hf, info.FullName, info.Length);
     // adjust file attributes and the last write time
     try
     {
         info.LastAccessTimeUtc = info.LastWriteTimeUtc;
         info.Attributes = info.Attributes;
     }
     catch
     {
     }
 }
コード例 #3
0
        protected override void ProcessFile(FileInfo info, int level)
        {
            var hi    = new HashInfo(info, CommandHash.HASH_ALG);
            var hf    = hi.GetHashPath(HashDir);
            var hInfo = new FileInfo(hf);

            Logger.Root.WriteLine(Verbosity.Debug, "{0}=>{1}", info.FullName, hInfo.FullName);
        }
コード例 #4
0
        protected override void ProcessFile(FileInfo info, int level)
        {
            var hi = new HashInfo(info, CommandShrink.SHA1);
            var hf = Path.GetFullPath(hi.GetHashPath(HashDir));

            // check if we need to copy the file
            if (!File.Exists(hf))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(hf));
                Monitor.Root.MoveFile(info.FullName, hf, info.Length);
                File.SetAttributes(hf, FileAttributes.Normal);
            }
            else
            {
                var hInfo = new FileInfo(hf);
                if (hInfo.Length != info.Length)
                {
                    Monitor.Root.HashCollision(hf, info.FullName);
                    return;
                }
                Monitor.Root.DeleteFile(info);
            }
            // create link
            if (Monitor.Root.LinkFile(hf, info.FullName, info.Length) != 0)
            {
                // 10bit link count overrun => move the hash file
                Monitor.Root.MoveFile(hf, info.FullName, info.Length);
            }
            // adjust file attributes and the last write time
            try
            {
                info.LastAccessTimeUtc = info.LastWriteTimeUtc;
                info.Attributes        = info.Attributes;
            }
            catch
            {
            }
        }
コード例 #5
0
        void DoProcessFile(FileInfo sourceFileInfo, int level, HashAlgorithm hashProvider)
        {
            if (Monitor.Root.DryRun)
            {
                return;
            }
            var sourceFilename = sourceFileInfo.FullName;
            // build target file name
            var targetFilename = this.RebasePath(sourceFilename, this.Target);

            // check if previous version of file can be found, that we could link to, this way we avoid to calc the SHA1 and a copy of attributes and
            if (this.usePreviousBackup && this.PreviousBackup != null)
            {
                try
                {
                    var previousBackupFilename = this.RebasePath(sourceFilename, this.PreviousBackup);
                    var previousBackupFileInfo = new FileInfo(previousBackupFilename);
                    if (previousBackupFileInfo.Length == sourceFileInfo.Length && previousBackupFileInfo.LastWriteTimeUtc == sourceFileInfo.LastWriteTimeUtc && previousBackupFileInfo.Attributes == sourceFileInfo.Attributes &&
                        Monitor.Root.LinkFile(previousBackupFilename, targetFilename, sourceFileInfo.Length) == 0)
                    {
                        return; // we successfully linked to the previous file => so we are done
                    }
                }
                catch (FileNotFoundException)
                {
                    // the previous backup file was not found => just continue with normal operation
                }
            }
            // we have no previous directory or the file changed, or linking failed, use hash algorithm
            var hash         = new HashInfo(sourceFileInfo, hashProvider);
            var hashFilename = hash.GetHashPath(this.HashDir);

            // lock the target hash path
            lock (hashFilename)
            {
                // check if we need to copy the file
                int linkError;
                do
                {
                    linkError = Monitor.Root.LinkFile(hashFilename, targetFilename, sourceFileInfo.Length);
                    switch (linkError)
                    {
                    case 0:         // ERROR_SUCCESS
                        break;

                    case 183:       // ERROR_ALREADY_EXISTS
                        return;     // target file already existing

                    case 2:         // ERROR_FILE_NOT_FOUND
                    case 3:         // ERROR_PATH_NOT_FOUND
                        if (linkError == 3)
                        {
                            Monitor.Root.CreateDirectory(Path.GetDirectoryName(hashFilename));
                        }
                        Monitor.Root.CopyFile(sourceFilename, hashFilename, sourceFileInfo.Length);
                        File.SetAttributes(hashFilename, FileAttributes.Normal);
                        // run again
                        break;

                    case 1142:      // ERROR_TOO_MANY_LINKS
                        Monitor.Root.MoveFile(hashFilename, targetFilename, sourceFileInfo.Length);
                        linkError = 0;
                        break;

                    default:
                        throw new System.ComponentModel.Win32Exception(linkError, String.Format("CreateHardLink({0},{1}) returned 0x{2:X8}h", hashFilename, targetFilename));
                    }
                } while (linkError != 0);
                var targetFileInfo = this.checkFileLength ? new FileInfo(targetFilename) : null;
                if (targetFileInfo != null)
                {
                    if (targetFileInfo.Length != sourceFileInfo.Length)
                    {
                        Monitor.Root.HashCollision(hashFilename, targetFilename);
                    }
                }
                // adjust file attributes and the last write time
                // make sure the backed up files have identical attributes and write times as the original
                if (this.restoreAttributes && (targetFileInfo == null || targetFileInfo.Attributes != sourceFileInfo.Attributes))
                {
                    File.SetAttributes(targetFilename, sourceFileInfo.Attributes);
                }
                if (this.restoreLastWriteTime && (targetFileInfo == null || targetFileInfo.LastWriteTimeUtc != sourceFileInfo.LastWriteTimeUtc))
                {
                    File.SetLastWriteTimeUtc(targetFilename, sourceFileInfo.LastWriteTimeUtc);
                }
                if (this.clearArchiveBit)
                {
                    File.SetAttributes(sourceFilename, sourceFileInfo.Attributes & (~FileAttributes.Archive));
                }
            }
        }
コード例 #6
0
ファイル: CommandCopy.cs プロジェクト: belucha/hashlinkcopy
 void DoProcessFile(FileInfo sourceFileInfo, int level, HashAlgorithm hashProvider)
 {
     if (Monitor.Root.DryRun) return;
     var sourceFilename = sourceFileInfo.FullName;
     // build target file name
     var targetFilename = this.RebasePath(sourceFilename, this.Target);
     // check if previous version of file can be found, that we could link to, this way we avoid to calc the SHA1 and a copy of attributes and
     if (this.usePreviousBackup && this.PreviousBackup != null)
         try
         {
             var previousBackupFilename = this.RebasePath(sourceFilename, this.PreviousBackup);
             var previousBackupFileInfo = new FileInfo(previousBackupFilename);
             if (previousBackupFileInfo.Length == sourceFileInfo.Length && previousBackupFileInfo.LastWriteTimeUtc == sourceFileInfo.LastWriteTimeUtc && previousBackupFileInfo.Attributes == sourceFileInfo.Attributes
                 && Monitor.Root.LinkFile(previousBackupFilename, targetFilename, sourceFileInfo.Length) == 0)
                 return; // we successfully linked to the previous file => so we are done
         }
         catch (FileNotFoundException)
         {
             // the previous backup file was not found => just continue with normal operation
         }
     // we have no previous directory or the file changed, or linking failed, use hash algorithm
     var hash = new HashInfo(sourceFileInfo, hashProvider);
     var hashFilename = hash.GetHashPath(this.HashDir);
     // lock the target hash path
     lock (hashFilename)
     {
         // check if we need to copy the file
         int linkError;
         do
         {
             linkError = Monitor.Root.LinkFile(hashFilename, targetFilename, sourceFileInfo.Length);
             switch (linkError)
             {
                 case 0:     // ERROR_SUCCESS
                     break;
                 case 183:   // ERROR_ALREADY_EXISTS
                     return; // target file already existing
                 case 2:     // ERROR_FILE_NOT_FOUND
                 case 3:     // ERROR_PATH_NOT_FOUND
                     if (linkError == 3)
                         Monitor.Root.CreateDirectory(Path.GetDirectoryName(hashFilename));
                     Monitor.Root.CopyFile(sourceFilename, hashFilename, sourceFileInfo.Length);
                     File.SetAttributes(hashFilename, FileAttributes.Normal);
                     // run again
                     break;
                 case 1142:  // ERROR_TOO_MANY_LINKS
                     Monitor.Root.MoveFile(hashFilename, targetFilename, sourceFileInfo.Length);
                     linkError = 0;
                     break;
                 default:
                     throw new System.ComponentModel.Win32Exception(linkError, String.Format("CreateHardLink({0},{1}) returned 0x{2:X8}h", hashFilename, targetFilename));
             }
         } while (linkError != 0);
         var targetFileInfo = this.checkFileLength ? new FileInfo(targetFilename) : null;
         if (targetFileInfo != null)
             if (targetFileInfo.Length != sourceFileInfo.Length)
                 Monitor.Root.HashCollision(hashFilename, targetFilename);
         // adjust file attributes and the last write time
         // make sure the backed up files have identical attributes and write times as the original
         if (this.restoreAttributes && (targetFileInfo == null || targetFileInfo.Attributes != sourceFileInfo.Attributes))
             File.SetAttributes(targetFilename, sourceFileInfo.Attributes);
         if (this.restoreLastWriteTime && (targetFileInfo == null || targetFileInfo.LastWriteTimeUtc != sourceFileInfo.LastWriteTimeUtc))
             File.SetLastWriteTimeUtc(targetFilename, sourceFileInfo.LastWriteTimeUtc);
         if (this.clearArchiveBit)
             File.SetAttributes(sourceFilename, sourceFileInfo.Attributes & (~FileAttributes.Archive));
     }
 }