Пример #1
0
        protected Process LaunchImplementation()
        {
            if (Requirements.Command == "")
            {
                throw new OptionException(Resources.NoRunWithEmptyCommand, "--command");
            }

            using (CreateRunHook())
            {
                try
                {
                    return(Executor.Start(Selections, AdditionalArgs.ToArray()));
                }
                #region Error handling
                catch (KeyNotFoundException ex)
                {
                    // Gracefully handle broken external selections
                    if (SelectionsDocument)
                    {
                        throw new InvalidDataException(ex.Message, ex);
                    }
                    else
                    {
                        throw;
                    }
                }
                #endregion
            }
        }
Пример #2
0
            public override ExitCode Execute()
            {
                var    catalog = GetCatalog();
                string query   = AdditionalArgs.JoinEscapeArguments();

                Handler.Output(Resources.AppList, catalog.Search(query));
                return(ExitCode.OK);
            }
Пример #3
0
        protected Process LaunchImplementation()
        {
            if (Requirements.Command == "")
            {
                throw new OptionException(Resources.NoRunWithEmptyCommand, "command");
            }

            using (CreateRunHook())
                return(Executor.Start(Selections, AdditionalArgs.ToArray()));
        }
Пример #4
0
        public override ExitCode Execute()
        {
            bool removed = false;

            foreach (var digest in AdditionalArgs.Select(x => new ManifestDigest(x)))
            {
                removed |= ImplementationStore.Remove(digest, Handler);
            }
            return(removed ? ExitCode.OK : ExitCode.NoChanges);
        }
Пример #5
0
 public override ExitCode Execute()
 {
     foreach (var digest in AdditionalArgs.Select(x => new ManifestDigest(x)))
     {
         if (!Store.Remove(digest, Handler))
         {
             throw new ImplementationNotFoundException(digest);
         }
     }
     return(ExitCode.OK);
 }
Пример #6
0
        /// <inheritdoc/>
        public override void Parse(IEnumerable <string> args)
        {
            base.Parse(args);

            SetInterfaceUri(GetCanonicalUri(AdditionalArgs[0]));
            AdditionalArgs.RemoveAt(0);

            if (Requirements.InterfaceUri.IsFile && File.Exists(Requirements.InterfaceUri.LocalPath))
            {
                TryParseSelectionsDocument();
            }
        }
Пример #7
0
        protected Process?LaunchImplementation()
        {
            Debug.Assert(Selections != null);

            if (Requirements.Command == "")
            {
                throw new OptionException(Resources.NoRunWithEmptyCommand, "command");
            }

            return(Executor.Inject(Selections, _overrideMain)
                   .AddWrapper(_wrapper)
                   .AddArguments(AdditionalArgs.ToArray())
                   .Start());
        }
Пример #8
0
        private void Remove()
        {
            if (AdditionalArgs.Count < 2)
            {
                throw new OptionException(Resources.MissingArguments + Environment.NewLine + "remove DIGEST+", "");
            }

            foreach (var digest in AdditionalArgs.Skip(1).Select(x => new ManifestDigest(x)))
            {
                if (!Store.Remove(digest, Handler))
                {
                    throw new ImplementationNotFoundException(digest);
                }
            }
        }
Пример #9
0
        /*** FILESYSTEM OPERATION FUNCTIONS ***/

        private void DoCreateDir()
        {
            bool failIfExists = false;

            if (!string.IsNullOrEmpty(AdditionalArgs))
            {
                string[] args = AdditionalArgs.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var arg in args)
                {
                    if (string.Equals(arg, "--failIfExists", StringComparison.OrdinalIgnoreCase))
                    {
                        failIfExists = true;
                    }
                }
            }

            if (FileUtilities.DirectoryExistsNoFollow(PathAsString) || FileUtilities.FileExistsNoFollow(PathAsString))
            {
                if (failIfExists)
                {
                    throw new InvalidOperationException($"Directory creation failed because '{PathAsString}' exists");
                }
            }

            if (OperatingSystemHelper.IsUnixOS)
            {
                Directory.CreateDirectory(PathAsString);
            }
            else
            {
                // .NET's Directory.CreateDirectory first checks for directory existence, so when a directory
                // exists it does nothing; to test a specific Detours access policy, we want to issue a "create directory"
                // operation regardless of whether the directory exists, hence p-invoking Windows native CreateDirectory.
                if (!ExternWinCreateDirectory(PathAsString, IntPtr.Zero))
                {
                    int errorCode = Marshal.GetLastWin32Error();

                    // If we got ERROR_ALREADY_EXISTS (183), keep it quiet --- yes we did not 'write' but the directory
                    // is on the disk after this method returns --- and do not fail the operation.
                    if (errorCode == ERROR_ALREADY_EXISTS)
                    {
                        return;
                    }

                    throw new InvalidOperationException($"Directory creation (native) for path '{PathAsString}' failed with error {errorCode}.");
                }
            }
        }
Пример #10
0
    /// <summary>
    /// Creates a new run command.
    /// </summary>
    /// <param name="handler">A callback object used when the the user needs to be asked questions or informed about download and IO tasks.</param>
    public Run(ICommandHandler handler)
        : base(handler, outputOptions: false)
    {
        Options.Add("m|main=", () => Resources.OptionMain, newMain => _overrideMain     = newMain);
        Options.Add("w|wrapper=", () => Resources.OptionWrapper, newWrapper => _wrapper = newWrapper);
        Options.Add("no-wait", () => Resources.OptionNoWait, _ => _noWait = true);

        // Work-around to disable interspersed arguments (needed for passing arguments through to sub-processes)
        Options.Add("<>", value =>
        {
            AdditionalArgs.Add(value);

            // Stop using options parser, treat everything from here on as unknown
            Options.Clear();
        });
    }
Пример #11
0
        /// <inheritdoc/>
        public override void Parse(IEnumerable <string> args)
        {
            if (Options.Parse(args).Count != 0)
            {
                throw new OptionException(Resources.TooManyArguments + "\n" + AdditionalArgs.JoinEscapeArguments(), "");
            }

            Requirements.InterfaceUri = Config.SelfUpdateUri;

            // Pass in the installation directory to the updater as an argument
            AdditionalArgs.Add(Locations.InstallBase);

            if (_restartCentral)
            {
                AdditionalArgs.Add("--restart-central");
            }
        }
Пример #12
0
        /// <inheritdoc/>
        public Run([NotNull] ICommandHandler handler) : base(handler)
        {
            //Options.Remove("xml");
            //Options.Remove("show");

            Options.Add("m|main=", () => Resources.OptionMain, newMain => Executor.Main             = newMain);
            Options.Add("w|wrapper=", () => Resources.OptionWrapper, newWrapper => Executor.Wrapper = newWrapper);
            Options.Add("no-wait", () => Resources.OptionNoWait, _ => NoWait = true);

            // Work-around to disable interspersed arguments (needed for passing arguments through to sub-processes)
            Options.Add("<>", value =>
            {
                AdditionalArgs.Add(value);

                // Stop using options parser, treat everything from here on as unknown
                Options.Clear();
            });
        }
Пример #13
0
    /// <inheritdoc/>
    public override ExitCode Execute()
    {
        if (Requirements.Command == "")
        {
            throw new OptionException(Resources.NoRunWithEmptyCommand, "command");
        }

        Solve();

        DownloadUncachedImplementations();

        Handler.CancellationToken.ThrowIfCancellationRequested();
        Handler.DisableUI();

        var process = Executor.Inject(Selections, _overrideMain)
                      .AddWrapper(_wrapper)
                      .AddArguments(AdditionalArgs.ToArray())
                      .SetEnvironmentVariable(ZeroInstallEnvironment.FeedUriName, Requirements.InterfaceUri.ToStringRfc())
                      .SetCallbackEnvironmentVariables()
                      .Start();

        CloseUI(process);

        BackgroundUpdate();
        BackgroundSelfUpdate();

        if (process == null)
        {
            Log.Warn("No process launched");
            return(ExitCode.OK);
        }
        else if (_noWait)
        {
            Log.Debug("Not waiting for program to exit");
            return(WindowsUtils.IsWindows ? (ExitCode)process.Id : ExitCode.OK);
        }
        else
        {
            Log.Debug("Waiting for program to exit");
            return((ExitCode)process.WaitForExitCode());
        }
    }
Пример #14
0
            /// <summary>
            /// Returns the default <see cref="IStore"/> or a <see cref="CompositeStore"/> as specifief by the <see cref="CliCommand.AdditionalArgs"/>.
            /// </summary>
            protected IStore GetEffectiveStore()
            {
                if (AdditionalArgs.Count == 0)
                {
                    return(Store);
                }
                else
                {
                    foreach (string path in AdditionalArgs)
                    {
                        if (!Directory.Exists(path))
                        {
                            throw new DirectoryNotFoundException(string.Format(Resources.FileOrDirNotFound, path));
                        }
                    }

                    return(new CompositeStore(
                               AdditionalArgs.Select(x => (IStore) new DirectoryStore(x, useWriteProtection: false))));
                }
            }
Пример #15
0
        /// <inheritdoc/>
        public override void Parse(IEnumerable <string> args)
        {
            base.Parse(args);

            Requirements.InterfaceUri = GetCanonicalUri(AdditionalArgs[0]);
            AdditionalArgs.RemoveAt(0);

            if (_version != null)
            {
                Requirements.ExtraRestrictions[Requirements.InterfaceUri] = _version;
            }
            else if (_notBefore != null || _before != null)
            {
                Requirements.ExtraRestrictions[Requirements.InterfaceUri] = new VersionRange(_notBefore, _before);
            }

            if (Requirements.InterfaceUri.IsFile && File.Exists(Requirements.InterfaceUri.LocalPath))
            {
                TryParseSelectionsDocument();
            }
        }
Пример #16
0
    /// <inheritdoc/>
    public override ExitCode Execute()
    {
        if (Requirements.Command == "")
        {
            throw new OptionException(Resources.NoRunWithEmptyCommand, "command");
        }

        Solve();

        DownloadUncachedImplementations();

        Handler.CancellationToken.ThrowIfCancellationRequested();
        Handler.DisableUI();

        var process = Executor.Inject(Selections, _overrideMain)
                      .AddWrapper(_wrapper)
                      .AddArguments(AdditionalArgs.ToArray())
                      .Start();

        Handler.CloseUI();

        BackgroundUpdate();
        BackgroundSelfUpdate();

        if (process == null)
        {
            return(ExitCode.OK);
        }
        if (_noWait)
        {
            return(WindowsUtils.IsWindows ? (ExitCode)process.Id : ExitCode.OK);
        }
        else
        {
            Log.Debug("Waiting for application to exit");
            process.WaitForExit();
            return((ExitCode)process.ExitCode);
        }
    }
Пример #17
0
            /// <inheritdoc/>
            public override void Parse(IEnumerable <string> args)
            {
                // NOTE: Does not call base method

                AdditionalArgs.AddRange(Options.Parse(args));
                if (AdditionalArgs.Count != 0)
                {
                    throw new OptionException(Resources.TooManyArguments + Environment.NewLine + AdditionalArgs.JoinEscapeArguments(), null);
                }

                SetInterfaceUri(Config.SelfUpdateUri ?? throw new UriFormatException(Resources.SelfUpdateDisabled));
                if (ProgramUtils.GuiAssemblyName != null)
                {
                    Requirements.Command = Command.NameRunGui;
                }

                // Instruct new version of Zero Install in the cache to deploy itself over the location of the current version
                AdditionalArgs.AddRange(new[] { Self.AltName, Deploy.Name, "--batch", Locations.InstallBase });

                if (_restartCentral)
                {
                    AdditionalArgs.Add("--restart-central");
                }
            }
Пример #18
0
 /// <summary>
 /// Returns the default <see cref="IStore"/> or a <see cref="CompositeStore"/> as specifief by the <see cref="CliCommand.AdditionalArgs"/>.
 /// </summary>
 private IStore GetStore()
 {
     return((AdditionalArgs.Count == 1)
         ? Store
         : new CompositeStore(AdditionalArgs.Skip(1).Select(arg => (IStore) new DirectoryStore(arg))));
 }
Пример #19
0
        /// <inheritdoc/>
        public override void Parse(IReadOnlyList <string> args)
        {
            // NOTE: Does not call base method

            if (Options.Parse(args).Count != 0)
            {
                throw new OptionException(Resources.TooManyArguments + Environment.NewLine + AdditionalArgs.JoinEscapeArguments(), null);
            }

            SetInterfaceUri(Config.SelfUpdateUri ?? throw new UriFormatException(Resources.SelfUpdateDisabled));
            if (WindowsUtils.IsGuiSession)
            {
                Requirements.Command = Command.NameRunGui;
            }

            FeedManager.Refresh = true;
        }
Пример #20
0
        public override ExitCode Execute()
        {
            var manifestDigest = new ManifestDigest(AdditionalArgs[0]);

            try
            {
                string path = AdditionalArgs[1];
                if (Directory.Exists(path))
                {
                    if (AdditionalArgs.Count > 2)
                    {
                        throw new OptionException(Resources.TooManyArguments + Environment.NewLine + AdditionalArgs.Skip(2).JoinEscapeArguments(), null);
                    }
                    ImplementationStore.Add(manifestDigest, builder => Handler.RunTask(new ReadDirectory(Path.GetFullPath(path), builder)));
                    return(ExitCode.OK);
                }

                ImplementationStore.Add(manifestDigest, BuildImplementation);
                return(ExitCode.OK);
            }
            catch (ImplementationAlreadyInStoreException ex)
            {
                Log.Warn(ex.Message, ex);
                return(ExitCode.NoChanges);
            }
        }
Пример #21
0
        public override ExitCode Execute()
        {
            var    manifestDigest = new ManifestDigest(AdditionalArgs[0]);
            string path           = AdditionalArgs[1];

            try
            {
                if (File.Exists(path))
                { // One or more archives (combined/overlay)
                    ImplementationStore.Add(manifestDigest, BuildImplementation);
                    return(ExitCode.OK);
                }
                else if (Directory.Exists(path))
                { // A single directory
                    if (AdditionalArgs.Count > 2)
                    {
                        throw new OptionException(Resources.TooManyArguments + Environment.NewLine + AdditionalArgs.Skip(2).JoinEscapeArguments(), null);
                    }
                    ImplementationStore.Add(manifestDigest, builder => Handler.RunTask(new ReadDirectory(Path.GetFullPath(path), builder)));
                    return(ExitCode.OK);
                }
                else
                {
                    throw new FileNotFoundException(string.Format(Resources.FileOrDirNotFound, path), path);
                }
            }
            catch (ImplementationAlreadyInStoreException ex)
            {
                Log.Warn(ex);
                return(ExitCode.NoChanges);
            }
        }