void SnapOsUnixInit() { Username = Environment.UserName; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { try { var(lsbReleaseExitCode, lsbReleaseStdOutput) = OsProcessManager .RunAsync(new ProcessStartInfoBuilder("lsb_release").Add("-a"), CancellationToken.None).GetAwaiter().GetResult(); if (lsbReleaseExitCode == 0 && lsbReleaseStdOutput != null) { var(distroId, _, _, _) = ParseLsbRelease(lsbReleaseStdOutput); DistroType = distroId == "Ubuntu" ? SnapOsDistroType.Ubuntu : SnapOsDistroType.Unknown; } else { DistroType = SnapOsDistroType.Unknown; } } catch (Exception e) { _logger.Warn("Exception thrown while executing 'lsb_release'", e); } return; } throw new PlatformNotSupportedException(); }
public List <SnapOsProcess> GetProcesses() { var processes = EnumerateProcesses().Select(x => { var processNameValid = !string.IsNullOrWhiteSpace(x.processName); return(OsProcessManager.Build(x.pid, x.processName, !processNameValid ? null : Filesystem.PathGetDirectoryName(x.processName), !processNameValid ? null : Filesystem.PathGetFileName(x.processName))); }).ToList(); return(processes); }
public List <SnapOsProcess> GetProcesses() { var processes = Process.GetProcesses().Select(process => OsProcessManager.Build(process.Id, process.ProcessName)).ToList(); return(processes); }
public async Task CreateShortcutsForExecutableAsync(SnapOsShortcutDescription shortcutDescription, ILog logger = null, CancellationToken cancellationToken = default) { if (shortcutDescription == null) { throw new ArgumentNullException(nameof(shortcutDescription)); } var exeName = Filesystem.PathGetFileName(shortcutDescription.ExeAbsolutePath); if (Username == null) { _logger?.Error($"Unable to create shortcut because username is null. Executable: {exeName}"); return; } logger?.Info($"Creating shortcuts for executable: {shortcutDescription.ExeAbsolutePath}"); var autoStartEnabled = shortcutDescription.ShortcutLocations.HasFlag(SnapShortcutLocation.Startup); var desktopEnabled = shortcutDescription.ShortcutLocations.HasFlag(SnapShortcutLocation.Desktop); var startMenuEnabled = shortcutDescription.ShortcutLocations.HasFlag(SnapShortcutLocation.StartMenu); var desktopShortcutUtf8Content = BuildDesktopShortcut(shortcutDescription, shortcutDescription.NuspecReader.GetDescription()); if (desktopShortcutUtf8Content == null) { _logger?.Warn( $"Unknown error while building desktop shortcut for exe: {exeName}. Distro: {DistroType}. Maybe unsupported distro?"); return; } var applicationsDirectoryAbsolutePath = Filesystem.PathCombine($"/home/{Username}", ".local/share/applications"); var autoStartDirectoryAbsolutePath = Filesystem.PathCombine($"/home/{Username}", ".config/autostart"); var autoStartShortcutAbsolutePath = Filesystem.PathCombine(autoStartDirectoryAbsolutePath, $"{exeName}.desktop"); var desktopShortcutAbsolutePath = Filesystem.PathCombine(applicationsDirectoryAbsolutePath, $"{exeName}.desktop"); if (startMenuEnabled) { _logger?.Warn("Creating start menu shortcuts is not supported on this OS."); } if (autoStartEnabled) { if (Filesystem.DirectoryCreateIfNotExists(autoStartDirectoryAbsolutePath)) { _logger?.Info($"Created autostart directory: {autoStartDirectoryAbsolutePath}"); } if (Filesystem.FileDeleteIfExists(autoStartShortcutAbsolutePath)) { _logger?.Info($"Deleted existing auto start shortcut: {autoStartShortcutAbsolutePath}"); } _logger?.Info($"Creating autostart shortcut: {autoStartShortcutAbsolutePath}. " + $"Absolute path: {shortcutDescription.ExeAbsolutePath}."); await Filesystem.FileWriteUtf8StringAsync(desktopShortcutUtf8Content, autoStartShortcutAbsolutePath, cancellationToken); _logger?.Info($"Attempting to mark shortcut as trusted: {autoStartShortcutAbsolutePath}."); var trustedSuccess = await OsProcessManager.ChmodExecuteAsync(autoStartShortcutAbsolutePath, cancellationToken); _logger?.Info($"Shortcut marked as trusted: {(trustedSuccess ? "yes" : "no")}"); } if (desktopEnabled) { if (!Filesystem.DirectoryExists(applicationsDirectoryAbsolutePath)) { _logger?.Error($"Applications directory does not exist. Desktop shortcut will not be created. Path: {applicationsDirectoryAbsolutePath}"); goto next; } if (Filesystem.FileDeleteIfExists(desktopShortcutAbsolutePath)) { _logger?.Info($"Deleted existing shortcut: {desktopShortcutAbsolutePath}"); } _logger?.Info($"Creating desktop shortcut: {desktopShortcutAbsolutePath}. " + $"Auto startup: {autoStartEnabled}. " + $"Absolute path: {shortcutDescription.ExeAbsolutePath}."); await Filesystem.FileWriteUtf8StringAsync(desktopShortcutUtf8Content, desktopShortcutAbsolutePath, cancellationToken); _logger?.Info($"Attempting to mark shortcut as trusted: {desktopShortcutAbsolutePath}."); var trustedSuccess = await OsProcessManager.ChmodExecuteAsync(desktopShortcutAbsolutePath, cancellationToken); _logger?.Info($"Shortcut marked as trusted: {(trustedSuccess ? "yes" : "no")}"); } next :; }