Beispiel #1
0
        /// <summary>
        /// Handles a specific file along with a group of files already seen with the same size.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="configuration">The configuration.</param>
        /// <param name="stats">The statistics.</param>
        /// <param name="knownWithThisLength">Length of the known with this.</param>
        private static void _HandleFileWithGivenSize(
            FileInfo item,
            Configuration configuration,
            RuntimeStats stats,
            ConcurrentDictionary <string, FileEntry> knownWithThisLength)
        {
            var myKey    = _GenerateKey(item);
            var checksum = knownWithThisLength.GetOrAdd(myKey, new FileEntry(item));

            var isHardLink = false;
            IEnumerable <FileInfo> hardlinks;

            try {
                hardlinks = item.GetHardLinkTargets();
            } catch (Exception e) {
                _RemoveFileEntry(item, knownWithThisLength);
                Console.WriteLine($"[Error] Could not enumerate HardLinks {item.FullName}: {e.Message}");
                return;
            }

            foreach (var target in hardlinks)
            {
                isHardLink = true;
                knownWithThisLength.TryAdd(_GenerateKey(target), new FileEntry(target));
            }

            if (isHardLink)
            {
                stats.HardLinkStats.IncreaseSeen();
                if (configuration.ShowInfoOnly)
                {
                    return;
                }

                _HandleExistingHardLink(item, configuration, stats, knownWithThisLength);
                return;
            }

            string symlink;

            try {
                symlink = item.GetSymbolicLinkTarget();
            } catch (Exception e) {
                _RemoveFileEntry(item, knownWithThisLength);
                Console.WriteLine($"[Error] Could not enumerate SymLink {item.FullName}: {e.Message}");
                return;
            }

            if (symlink != null)
            {
                knownWithThisLength.TryAdd(symlink, new FileEntry(new FileInfo(symlink)));
                stats.SymbolicLinkStats.IncreaseSeen();
                if (configuration.ShowInfoOnly)
                {
                    return;
                }

                _HandleExistingSymbolicLink(item, configuration, stats, knownWithThisLength);
                return;
            }

            if (configuration.ShowInfoOnly)
            {
                return;
            }

            // find matching file in seen list and try to hard or symlink
            var sameFiles =
                knownWithThisLength
                .Where(kvp => kvp.Key != myKey)
                .Where(kvp => kvp.Value.Equals(checksum))
                .Select(kvp => kvp.Key)
            ;

            foreach (var sameFile in sameFiles)
            {
                var temporaryFile = _CreateTemporaryFileInSameDirectory(item);
                temporaryFile.Delete();

                var isSymlink = false;

                try {
                    LinkExtensions.CreateHardLinkFrom((FileInfo)temporaryFile, (string)sameFile);
                } catch (Exception e1) {
                    if (configuration.AlsoTrySymbolicLinks)
                    {
                        isSymlink = true;
                        try {
                            LinkExtensions.CreateSymbolicLinkFrom((FileInfo)temporaryFile, (string)sameFile);
                        } catch (Exception e2) {
                            Console.WriteLine(
                                $"[Warning] Could not Symlink {item.FullName}({FilesizeFormatter.FormatUnit(item.Length, true)}) --> {sameFile}: {e2.Message}");
                            continue;
                        }
                    }
                    else
                    {
                        Console.WriteLine(
                            $"[Warning] Could not Hardlink {item.FullName}({FilesizeFormatter.FormatUnit(item.Length, true)}) --> {sameFile}: {e1.Message}");
                        continue;
                    }
                }

                var isAlreadyDeleted = false;

                try {
                    item.Attributes &= ~FileAttributes.ReadOnly;
                    item.Delete();
                    isAlreadyDeleted = true;
                    File.Move(temporaryFile.FullName, item.FullName);
                } catch {
                    if (isAlreadyDeleted)
                    {
                        // undo file deletion
                        temporaryFile.CopyTo(item.FullName, true);
                    }
                    else
                    {
                        // undo temp file creation
                        temporaryFile.Delete();
                    }

                    throw;
                }

                if (isSymlink)
                {
                    stats.SymbolicLinkStats.IncreaseCreated();
                    if (configuration.SetReadOnlyAttributeOnNewSymbolicLinks)
                    {
                        item.Attributes |= FileAttributes.ReadOnly;
                    }
                }
                else
                {
                    stats.HardLinkStats.IncreaseCreated();
                    if (configuration.SetReadOnlyAttributeOnNewHardLinks)
                    {
                        item.Attributes |= FileAttributes.ReadOnly;
                    }
                }

                Console.WriteLine($"[Info] Created {(isSymlink ? "Symlink" : "Hardlink")} for {item.FullName}({FilesizeFormatter.FormatUnit(item.Length, true)}) --> {sameFile}");
                return;
            }
        }