1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
//////////////////////////////////////////////////////////////////////////
//
// pgAgent - PostgreSQL Tools
//
// Copyright (C) 2002 - 2024, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// misc.cpp - misc functions
//
//////////////////////////////////////////////////////////////////////////
#include "pgAgent.h"
#include "connection.h"
#include <boost/locale/encoding_utf.hpp>
#if !BOOST_OS_WINDOWS
#include <unistd.h>
#include <stdlib.h>
#endif
#define APPVERSION_STR PGAGENT_VERSION
namespace fs = boost::filesystem;
// In unix.c or win32.c
void usage(const std::string &executable);
std::string getArg(int &argc, char **&argv)
{
std::string res;
if (argv[0][2])
return (argv[0] + 2);
if (argc >= 1)
{
argc--;
argv++;
return argv[0];
}
// very bad!
LogMessage("Invalid command line argument", LOG_ERROR);
return res;
}
void printVersion()
{
printf("PostgreSQL Scheduling Agent\n");
printf("Version: %s\n", APPVERSION_STR);
};
void setOptions(int argc, char **argv, const std::string &executable)
{
while (argc-- > 0)
{
if (argv[0][0] == '-')
{
switch (argv[0][1])
{
case 't':
{
int val = atoi((const char*)getArg(argc, argv).c_str());
if (val > 0)
shortWait = val;
break;
}
case 'r':
{
int val = atoi((const char*)getArg(argc, argv).c_str());
if (val >= 10)
longWait = val;
break;
}
case 'l':
{
int val = atoi((const char*)getArg(argc, argv).c_str());
if (val >= 0 && val <= 2)
minLogLevel = val;
break;
}
case 'v':
{
printVersion();
exit(0);
}
#if !BOOST_OS_WINDOWS
case 'f':
{
runInForeground = true;
break;
}
case 's':
{
logFile = getArg(argc, argv);
break;
}
#endif
default:
{
usage(executable);
exit(1);
}
}
}
else
{
if (!connectString.empty())
connectString += " ";
connectString += *argv;
if (**argv == '"')
connectString = connectString.substr(1, connectString.length() - 2);
}
argv++;
}
}
void WaitAWhile(const bool waitLong)
{
int count;
if (waitLong)
count = longWait;
else
count = shortWait;
while (count--)
{
#ifdef WIN32
CheckForInterrupt();
Sleep(1000);
#else
sleep(1);
#endif
}
}
std::string NumToStr(const long l)
{
return boost::lexical_cast<std::string>(l);
}
#if BOOST_OS_WINDOWS
// This function is used to convert const std::str to std::wstring.
std::wstring s2ws(const std::string &str)
{
using boost::locale::conv::utf_to_utf;
return utf_to_utf<wchar_t>(str.c_str(), str.c_str() + str.size());
}
// This function is used to convert std::wstring to std::str
std::string ws2s(const std::wstring &wstr)
{
using boost::locale::conv::utf_to_utf;
return utf_to_utf<char>(wstr.c_str(), wstr.c_str() + wstr.size());
}
#endif
// Below function will generate random string of given character.
std::string generateRandomString(size_t length)
{
char *str = new char[length];
size_t i = 0;
int r;
str[length - 1] = '\0';
srand(time(NULL));
for(i = 0; i < length - 1; ++i)
{
for(;;)
{
// interval between 'A' and 'z'
r = rand() % 57 + 65;
if((r >= 65 && r <= 90) || (r >= 97 && r <= 122))
{
str[i] = (char)r;
break;
}
}
}
std::string result(str);
if (str != NULL)
{
delete []str;
str = NULL;
}
return result;
}
bool createUniqueTemporaryDirectory(const std::string &prefix, fs::path &uniqueDir)
{
const unsigned short MAX_ATTEMPTS = 100;
unsigned short attempts = 0;
try {
fs::path tempDir = fs::temp_directory_path();
do {
if (attempts++ >= MAX_ATTEMPTS)
return false;
uniqueDir = tempDir / fs::unique_path(
prefix + "%%%%%%%%-%%%%-%%%%-%%%%-%%%%%%%%%%%%"
);
// Check if exists
if (boost::filesystem::is_directory(uniqueDir))
continue;
// Create the directory securely
if (!fs::create_directory(uniqueDir)) {
return false;
}
// Set appropriate permissions (example: owner read/write/execute only)
fs::permissions(uniqueDir, boost::filesystem::owner_all);
return true;
} while (true);
} catch (const fs::filesystem_error &ex) {
return false;
}
}
|