File indexing completed on 2024-05-05 05:45:56

0001 /*
0002     Copyright Hannah von Reth <vonreth@kde.org>
0003 
0004     Redistribution and use in source and binary forms, with or without
0005     modification, are permitted provided that the following conditions
0006     are met:
0007     1. Redistributions of source code must retain the above copyright
0008        notice, this list of conditions and the following disclaimer.
0009     2. Redistributions in binary form must reproduce the above copyright
0010        notice, this list of conditions and the following disclaimer in the
0011        documentation and/or other materials provided with the distribution.
0012 
0013     THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0014     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0015     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0016     ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0017     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0018     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0019     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0020     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0021     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0022     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0023     SUCH DAMAGE.
0024 */
0025 
0026 #ifndef KSHIMDATA_H
0027 #define KSHIMDATA_H
0028 
0029 #include "kshim.h"
0030 
0031 #include <string>
0032 #include <vector>
0033 
0034 #ifndef WIN32
0035 // don't make const to prevent optimisation
0036 struct KShimPayLoad
0037 {
0038     // an initialised data array starts with an size_t with the size of the payload
0039     // followed by the formated payload
0040     size_t size;
0041     uint8_t cmd[KShimLib::DataStorageSize];
0042 };
0043 #endif
0044 
0045 class KShimData
0046 {
0047 public:
0048     KShimData() = default;
0049     KShimData(const std::filesystem::path &app);
0050     KShimData(const std::vector<uint8_t> &payLoad);
0051 
0052     std::filesystem::path app() const;
0053     std::filesystem::path appAbs() const;
0054     void setApp(const std::filesystem::path &app);
0055 
0056     /***
0057      * If isEnvOverrideEnabled is true this will return either appAbs or the absolute KSHIM_shimname
0058      * env var
0059      */
0060     std::filesystem::path appAbsWithOverride() const;
0061 
0062     const std::vector<KShimLib::string> &args() const;
0063     void setArgs(const std::vector<KShimLib::string_view> &args);
0064     void addArg(const KShimLib::string_view &arg);
0065 
0066     const std::vector<std::pair<KShimLib::string, KShimLib::string>> &env() const;
0067     void setEnv(const std::vector<std::pair<KShimLib::string_view, KShimLib::string_view>> &env);
0068     void addEnvVar(const std::pair<KShimLib::string_view, KShimLib::string_view> &var);
0069 
0070     KShimLib::string formatArgs(const std::vector<KShimLib::string_view> &args) const;
0071 
0072     /***
0073      *
0074      * When enabled the shim will try look for KSHIM_shimname for app()
0075      */
0076     bool isEnvOverrideEnabled() const;
0077     void setEnvOverrideEnabled(bool envOverrideEnabled);
0078 
0079     /***
0080      * Pass the original arg0 to the target process to emulate symlink behaviour
0081      */
0082     bool isKeepArgv0Enabled() const;
0083     void setKeepArgv0Enabled(bool keepArg0Enabled);
0084 
0085     std::vector<uint8_t> toJson() const;
0086 
0087 private:
0088     std::filesystem::path makeAbsouteCommand(const std::filesystem::path &_path) const;
0089 
0090     std::filesystem::path m_app;
0091     std::vector<KShimLib::string> m_args;
0092     std::vector<std::pair<KShimLib::string, KShimLib::string>> m_env;
0093     bool m_envOverrideEnabled = false;
0094     bool m_keepArgv0Enabled = false;
0095 };
0096 
0097 #endif // KSHIMDATA_H