<commit_before>// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

/**
 * Server/client environment: argument handling, config file parsing,
 * thread wrappers, startup time
 */
#ifndef SYSCOIN_UTIL_SYSTEM_H
#define SYSCOIN_UTIL_SYSTEM_H

#if defined(HAVE_CONFIG_H)
#include <config/syscoin-config.h>
#endif

#include <attributes.h>
#include <compat.h>
#include <compat/assumptions.h>
#include <fs.h>
#include <logging.h>
#include <optional.h>
#include <sync.h>
#include <tinyformat.h>
#include <util/memory.h>
#include <util/settings.h>
#include <util/threadnames.h>
#include <util/time.h>

#include <exception>
#include <map>
#include <set>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>

#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
// SYSCOIN
#include <ctpl.h>
#include <amount.h>
class JSONRPCRequest;
extern bool fMasternodeMode;
extern bool bGethTestnet;
extern bool fZMQEthStatus;
extern bool fDisableGovernance;
extern bool fRegTest;
extern uint32_t fGethSyncHeight;
extern uint32_t fGethCurrentHeight;
extern std::string exePath;
extern std::string fGethSyncStatus;
extern bool fGethSynced;
extern bool fLoaded;
extern bool bb;
extern pid_t gethPID;
extern pid_t relayerPID;
extern int64_t nLastGethHeaderTime;
extern bool fAssetIndex;
extern int32_t DEFAULT_MN_COLLATERAL_REQUIRED;
extern CAmount nMNCollateralRequired;
extern std::vector<JSONRPCRequest> vecTPSRawTransactions;
typedef struct {
    // Values from /proc/meminfo, in KiB or converted to MiB.
    long MemTotalKiB;
    int MemTotalMiB;
    int MemAvailableMiB; // -1 means no data available
    int SwapTotalMiB;
    long SwapTotalKiB;
    int SwapFreeMiB;
    // Calculated percentages
    int MemAvailablePercent; // percent of total memory that is available
    int SwapFreePercent; // percent of total swap that is free
} meminfo_t;

meminfo_t parse_meminfo();

class UniValue;

// Application startup time (used for uptime calculation)
int64_t GetStartupTime();

extern const char * const SYSCOIN_CONF_FILENAME;
extern const char * const SYSCOIN_SETTINGS_FILENAME;
// SYSCOIN
void RenameThreadPool(ctpl::thread_pool& tp, const char* baseName);
void SetupEnvironment();
bool SetupNetworking();

template<typename... Args>
bool error(const char* fmt, const Args&... args)
{
    LogPrintf("ERROR: %s\n", tfm::format(fmt, args...));
    return false;
}

void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
bool FileCommit(FILE *file);
bool TruncateFile(FILE *file, unsigned int length);
int RaiseFileDescriptorLimit(int nMinFD);
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
bool RenameOver(fs::path src, fs::path dest);
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false);
void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name);
bool DirIsWritable(const fs::path& directory);
bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes = 0);

/** Get the size of a file by scanning it.
 *
 * @param[in] path The file path
 * @param[in] max Stop seeking beyond this limit
 * @return The file size or max
 */
std::streampos GetFileSize(const char* path, std::streamsize max = std::numeric_limits<std::streamsize>::max());

/** Release all directory locks. This is used for unit testing only, at runtime
 * the global destructor will take care of the locks.
 */
void ReleaseDirectoryLocks();

bool TryCreateDirectories(const fs::path& p);
fs::path GetDefaultDataDir();
// The blocks directory is always net specific.
const fs::path &GetBlocksDir();
const fs::path &GetDataDir(bool fNetSpecific = true);
// SYSCOIN
fs::path GetGethPidFile();
void KillProcess(const pid_t& pid);
std::string GetGethFilename();
bool CheckSpecs(std::string &errMsg, bool bMiner = false);
bool StartGethNode(const std::string &exePath, pid_t &pid, int websocketport=8646, int ethrpcport=8645, const std::string & mode="light");
bool StopGethNode(pid_t &pid);
fs::path GetRelayerPidFile();
std::string GeteRelayerFilename();
bool StartRelayerNode(const std::string &exePath, pid_t &pid, int rpcport, int websocketport=8646, int ethrpcport=8645);
bool StopRelayerNode(pid_t &pid);
// Return true if -datadir option points to a valid directory or is not specified.
bool CheckDataDirOption();
/** Tests only */
void ClearDatadirCache();
fs::path GetConfigFile(const std::string& confPath);
#ifdef WIN32
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
#endif
#ifndef WIN32
std::string ShellEscape(const std::string& arg);
#endif
#if HAVE_SYSTEM
void runCommand(const std::string& strCommand);
#endif<commit_msg>compile<commit_after>// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

/**
 * Server/client environment: argument handling, config file parsing,
 * thread wrappers, startup time
 */
#ifndef SYSCOIN_UTIL_SYSTEM_H
#define SYSCOIN_UTIL_SYSTEM_H

#if defined(HAVE_CONFIG_H)
#include <config/syscoin-config.h>
#endif

#include <attributes.h>
#include <compat.h>
#include <compat/assumptions.h>
#include <fs.h>
#include <logging.h>
#include <optional.h>
#include <sync.h>
#include <tinyformat.h>
#include <util/memory.h>
#include <util/settings.h>
#include <util/threadnames.h>
#include <util/time.h>

#include <exception>
#include <map>
#include <set>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>

#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
// SYSCOIN
#include <ctpl.h>
#include <amount.h>
class JSONRPCRequest;
extern bool fMasternodeMode;
extern bool bGethTestnet;
extern bool fZMQEthStatus;
extern bool fDisableGovernance;
extern bool fRegTest;
extern uint32_t fGethSyncHeight;
extern uint32_t fGethCurrentHeight;
extern std::string exePath;
extern std::string fGethSyncStatus;
extern bool fGethSynced;
extern bool fLoaded;
extern bool bb;
extern pid_t gethPID;
extern pid_t relayerPID;
extern int64_t nLastGethHeaderTime;
extern bool fAssetIndex;
extern int32_t DEFAULT_MN_COLLATERAL_REQUIRED;
extern CAmount nMNCollateralRequired;
extern std::vector<JSONRPCRequest> vecTPSRawTransactions;
typedef struct {
    // Values from /proc/meminfo, in KiB or converted to MiB.
    long MemTotalKiB;
    int MemTotalMiB;
    int MemAvailableMiB; // -1 means no data available
    int SwapTotalMiB;
    long SwapTotalKiB;
    int SwapFreeMiB;
    // Calculated percentages
    int MemAvailablePercent; // percent of total memory that is available
    int SwapFreePercent; // percent of total swap that is free
} meminfo_t;

meminfo_t parse_meminfo();

class UniValue;

// Application startup time (used for uptime calculation)
int64_t GetStartupTime();

extern const char * const SYSCOIN_CONF_FILENAME;
extern const char * const SYSCOIN_SETTINGS_FILENAME;
// SYSCOIN
void RenameThreadPool(ctpl::thread_pool& tp, const char* baseName);
void SetupEnvironment();
bool SetupNetworking();

template<typename... Args>
bool error(const char* fmt, const Args&... args)
{
    LogPrintf("ERROR: %s\n", tfm::format(fmt, args...));
    return false;
}

void PrintExceptionContinue(const std::exception *pex, const char* pszThread);
bool FileCommit(FILE *file);
bool TruncateFile(FILE *file, unsigned int length);
int RaiseFileDescriptorLimit(int nMinFD);
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
bool RenameOver(fs::path src, fs::path dest);
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false);
void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name);
bool DirIsWritable(const fs::path& directory);
bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes = 0);

/** Get the size of a file by scanning it.
 *
 * @param[in] path The file path
 * @param[in] max Stop seeking beyond this limit
 * @return The file size or max
 */
std::streampos GetFileSize(const char* path, std::streamsize max = std::numeric_limits<std::streamsize>::max());

/** Release all directory locks. This is used for unit testing only, at runtime
 * the global destructor will take care of the locks.
 */
void ReleaseDirectoryLocks();

bool TryCreateDirectories(const fs::path& p);
fs::path GetDefaultDataDir();
// The blocks directory is always net specific.
const fs::path &GetBlocksDir();
const fs::path &GetDataDir(bool fNetSpecific = true);
// SYSCOIN
fs::path GetGethPidFile();
void KillProcess(const pid_t& pid);
std::string GetGethFilename();
bool CheckSpecs(std::string &errMsg, bool bMiner = false);
bool StartGethNode(const std::string &exePath, pid_t &pid, int websocketport=8646, int ethrpcport=8645, const std::string & mode="light");
bool StopGethNode(pid_t &pid);
void DoGethMaintenance();
fs::path GetRelayerPidFile();
std::string GeteRelayerFilename();
bool StartRelayerNode(const std::string &exePath, pid_t &pid, int rpcport, int websocketport=8646, int ethrpcport=8645);
bool StopRelayerNode(pid_t &pid);
// Return true if -datadir option points to a valid directory or is not specified.
bool CheckDataDirOption();
/** Tests only */
void ClearDatadirCache();
fs::path GetConfigFile(const std::string& confPath);
#ifdef WIN32
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
#endif
#ifndef WIN32
std::string ShellEscape(const std::string& arg);
#endif
#if HAVE_SYSTEM
void runCommand(const std::string& strCommand);
#endif
{'fraction_non_alphanumeric': 0.11041495480690222, 'fraction_numerical': 0.010271158586688579, 'mean_word_length': 5.979928315412186, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1022583', 'n_tokens_mistral': 3241, 'n_tokens_neox': 3090, 'n_words': 971}