<commit_before>﻿/*
    Copyright (c) 2017 <NAME> (https://github.com/Klocman/)
    Apache License Version 2.0
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Klocman.Extensions;
using Klocman.Native;
using Klocman.Tools;
using UninstallTools.Junk.Confidence;
using UninstallTools.Junk.Containers;
using UninstallTools.Properties;

namespace UninstallTools.Junk.Finders.Misc
{
    public class ShortcutJunk : JunkCreatorBase
    {
        private ICollection<Shortcut> _links;

        public override void Setup(ICollection<ApplicationUninstallerEntry> allUninstallers)
        {
            base.Setup(allUninstallers);

            _links = GetShortcuts();
        }

        private static IEnumerable<string> GetLnkFilesSafe(CSIDL directory, SearchOption option)
        {
            try
            {
                return Directory.GetFiles(WindowsTools.GetEnvironmentPath(directory), "*.lnk", option);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Debug.Fail(ex.ToString());
            }
            return Enumerable.Empty<string>();
        }

        private static List<Shortcut> GetShortcuts()
        {
            var syspath = WindowsTools.GetEnvironmentPath(CSIDL.CSIDL_WINDOWS);

            var results = new List<Shortcut>();
            foreach (var linkFilename in
                GetLnkFilesSafe(CSIDL.CSIDL_PROGRAMS, SearchOption.AllDirectories)
                .Concat(GetLnkFilesSafe(CSIDL.CSIDL_COMMON_PROGRAMS, SearchOption.AllDirectories))
                .Concat(GetLnkFilesSafe(CSIDL.CSIDL_DESKTOPDIRECTORY, SearchOption.TopDirectoryOnly))
                .Concat(GetLnkFilesSafe(CSIDL.CSIDL_COMMON_DESKTOPDIRECTORY, SearchOption.TopDirectoryOnly))
                .Distinct())
            {
                try
                {
                    var target = WindowsTools.ResolveShortcut(linkFilename);

                    if (string.IsNullOrEmpty(target) || target.Contains(syspath, StringComparison.InvariantCultureIgnoreCase))
                        continue;

                    results.Add(new Shortcut(linkFilename, target));
                }
                catch (Exception ex)
                {
                    var failMessage = "Failed to resolve shortcut: " + linkFilename;
                    Console.WriteLine(failMessage);
                    Console.WriteLine(ex);
                    Debug.Fail(failMessage);
                }
            }

            return results;
        }

        public override IEnumerable<IJunkResult> FindJunk(ApplicationUninstallerEntry target)
        {
            var results = new List<FileSystemJunk>();

            if (!string.IsNullOrEmpty(target.InstallLocation))
            {
                results.AddRange(GetLinksPointingToLocation(entry => entry.InstallLocation, target)
                    .DoForEach(x => x.Confidence.Add(ConfidenceRecords.ExplicitConnection)));
            }

            if (!string.IsNullOrEmpty(target.UninstallerFullFilename))
            {
                results.AddRange(GetLinksPointingToLocation(entry => entry.UninstallerFullFilename, target)
                    .DoForEach(x => x.Confidence.Add(ConfidenceRecords.ExplicitConnection)));
            }

            if (!string.IsNullOrEmpty(target.UninstallerLocation))
            {
                var exceptUninstallerShortcut = GetLinksPointingToLocation(entry => entry.UninstallerLocation, target)
                    .Where(possibleResult => results.All(result => !PathTools.PathsEqual(result.Path, possibleResult.Path)))
                    .ToList();

                results.AddRange(exceptUninstallerShortcut);
            }

            // Remove shortcuts that we aren't sure about
            foreach (var junkNode in results.ToList())
            {
                var name = Path.GetFileNameWithoutExtension(junkNode.Path.Name);
                junkNode.Confidence.AddRange(ConfidenceGenerators.GenerateConfidence(name, target));

                if (junkNode.Confidence.IsEmpty)
                    results.Remove(junkNode);
            }

            return results.Cast<IJunkResult>();
        }
<commit_msg>Fixed junk scan for steam apps detecting shortcuts of other steam apps<commit_after>﻿/*
    Copyright (c) 2017 <NAME> (https://github.com/Klocman/)
    Apache License Version 2.0
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Klocman.Extensions;
using Klocman.Native;
using Klocman.Tools;
using UninstallTools.Junk.Confidence;
using UninstallTools.Junk.Containers;
using UninstallTools.Properties;

namespace UninstallTools.Junk.Finders.Misc
{
    public class ShortcutJunk : JunkCreatorBase
    {
        private ICollection<Shortcut> _links;

        public override void Setup(ICollection<ApplicationUninstallerEntry> allUninstallers)
        {
            base.Setup(allUninstallers);

            _links = GetShortcuts();
        }

        private static IEnumerable<string> GetLnkFilesSafe(CSIDL directory, SearchOption option)
        {
            try
            {
                return Directory.GetFiles(WindowsTools.GetEnvironmentPath(directory), "*.lnk", option);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Debug.Fail(ex.ToString());
            }
            return Enumerable.Empty<string>();
        }

        private static List<Shortcut> GetShortcuts()
        {
            var syspath = WindowsTools.GetEnvironmentPath(CSIDL.CSIDL_WINDOWS);

            var results = new List<Shortcut>();
            foreach (var linkFilename in
                GetLnkFilesSafe(CSIDL.CSIDL_PROGRAMS, SearchOption.AllDirectories)
                .Concat(GetLnkFilesSafe(CSIDL.CSIDL_COMMON_PROGRAMS, SearchOption.AllDirectories))
                .Concat(GetLnkFilesSafe(CSIDL.CSIDL_DESKTOPDIRECTORY, SearchOption.TopDirectoryOnly))
                .Concat(GetLnkFilesSafe(CSIDL.CSIDL_COMMON_DESKTOPDIRECTORY, SearchOption.TopDirectoryOnly))
                .Distinct())
            {
                try
                {
                    var target = WindowsTools.ResolveShortcut(linkFilename);

                    if (string.IsNullOrEmpty(target) || target.Contains(syspath, StringComparison.InvariantCultureIgnoreCase))
                        continue;

                    results.Add(new Shortcut(linkFilename, target));
                }
                catch (Exception ex)
                {
                    var failMessage = "Failed to resolve shortcut: " + linkFilename;
                    Console.WriteLine(failMessage);
                    Console.WriteLine(ex);
                    Debug.Fail(failMessage);
                }
            }

            return results;
        }

        public override IEnumerable<IJunkResult> FindJunk(ApplicationUninstallerEntry target)
        {
            var results = new List<FileSystemJunk>();

            if (!string.IsNullOrEmpty(target.InstallLocation))
            {
                results.AddRange(GetLinksPointingToLocation(entry => entry.InstallLocation, target)
                    .DoForEach(x => x.Confidence.Add(ConfidenceRecords.ExplicitConnection)));
            }

            if (target.UninstallerKind == UninstallerType.Steam)
            {
                results.AddRange(GetLinksPointingToSteamApp(target));
            }
            else
            {
                if (!string.IsNullOrEmpty(target.UninstallerFullFilename))
                {
                    results.AddRange(GetLinksPointingToLocation(entry => entry.UninstallerFullFilename, target)
                        .DoForEach(x => x.Confidence.Add(ConfidenceRecords.ExplicitConnection)));
                }

                if (!string.IsNullOrEmpty(target.UninstallerLocation))
                {
                    var exceptUninstallerShortcut = GetLinksPointingToLocation(entry => entry.UninstallerLocation, target)
                        .Where(possibleResult => results.All(result => !PathTools.PathsEqual(result.Path, possibleResult.Path)))
                        .ToList();

                    results.AddRange(exceptUninstallerShortcut);
                }
            }

            // Remove shortcuts that we aren't sure about
            foreach (var junkNode in results.ToList())
            {
                var name = Path.GetFileNameWithoutExtension(junkNode.Path.Name);
                junkNode.Confidence.AddRange(ConfidenceGenerators.GenerateConfidence(name, target));

                if (junkNode.Confidence.IsEmpty)
                    results.Remove(junkNode);
            }

            return results.Cast<IJunkResult>();
        }

        /// <summary>
        /// Avoids marking all steam application shortcuts as junk (they use same exe path as uninstall commands)
        /// </summary>
        private IEnumerable<FileSystemJunk> GetLinksPointingToSteamApp(ApplicationUninstallerEntry target)
        {
            Debug.Assert(target.UninstallerKind == UninstallerType.Steam);

            var appId = System.Text.RegularExpressions.Regex.Replace(target.RegistryKeyName, @"[^0-9]", "");

            if (!string.IsNullOrEmpty(appId))
            {
                foreach (var source in _links)
                {
                    if (source.LinkTarget.Contains(appId, StringComparison.Ordinal)
                        && source.LinkTarget.Contains("steam", StringComparison.OrdinalIgnoreCase))
                    {
                        var result = CreateJunkNode(source, target);
                        yield return result;
                    }
                }
            }
        }

{'fraction_non_alphanumeric': 0.08536709894822833, 'fraction_numerical': 0.0014295925661186562, 'mean_word_length': 1.901925925925926, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 40, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3830593', 'n_tokens_mistral': 2571, 'n_tokens_neox': 2424, 'n_words': 417}