Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OTPaths Class Reference

#include <OTPaths.hpp>

Public Member Functions

EXPORT ~OTPaths ()
 

Static Public Member Functions

static EXPORT const OTStringAppBinaryFolder ()
 
static EXPORT void SetAppBinaryFolder (OTString strLocation)
 
static EXPORT const OTStringHomeFolder ()
 
static EXPORT void SetHomeFolder (OTString strLocation)
 
static EXPORT const OTStringAppDataFolder ()
 
static EXPORT const OTStringGlobalConfigFile ()
 
static EXPORT const OTStringPrefixFolder ()
 
static EXPORT const OTStringScriptsFolder ()
 
static EXPORT bool LoadSetPrefixFolder (OTSettings &config=s_settings, const OTString &strPrefixFolder="")
 
static EXPORT bool LoadSetScriptsFolder (OTSettings &config=s_settings, const OTString &strScriptsFolder="", const bool &bIsRelative=true)
 
static EXPORT bool Get (OTSettings &config, const OTString &strSection, const OTString &strKey, OTString &out_strVar, bool &out_bIsRelative, bool &out_bKeyExist)
 
static EXPORT bool Set (OTSettings &config, const OTString &strSection, const OTString &strKey, const OTString &strValue, const bool &bIsRelative, bool &out_bIsNewOrUpdated, const OTString &strComment="")
 
static EXPORT bool FixPath (const OTString &strPath, OTString &out_strFixedPath, const bool &bIsFolder)
 
static EXPORT bool PathExists (const OTString &strPath)
 
static EXPORT bool FileExists (const OTString &strFilePath, int64_t &nFileLength)
 
static EXPORT bool FolderExists (const OTString &strFolderPath)
 
static EXPORT bool ConfirmCreateFolder (const OTString &strExactPath, bool &out_Exists, bool &out_IsNew)
 
static EXPORT bool ToReal (const OTString &strExactPath, OTString &out_strCanonicalPath)
 
static EXPORT bool GetExecutable (OTString &strExecutablePath)
 
static EXPORT bool GetCurrentWorking (OTString &strCurrentWorkingPath)
 
static EXPORT bool GetHomeFromSystem (OTString &out_strHomeFolder)
 
static EXPORT bool AppendFolder (OTString &out_strPath, const OTString &strBasePath, const OTString &strFolderName)
 
static EXPORT bool AppendFile (OTString &out_strPath, const OTString &strBasePath, const OTString &strFileName)
 
static EXPORT bool RelativeToCanonical (OTString &out_strCanonicalPath, const OTString &strBasePath, const OTString &strRelativePath)
 
static EXPORT bool BuildFolderPath (const OTString &strFolderPath, bool &out_bFolderCreated)
 
static EXPORT bool BuildFilePath (const OTString &strFolderPath, bool &out_bFolderCreated)
 

Detailed Description

Definition at line 144 of file OTPaths.hpp.

Constructor & Destructor Documentation

opentxs::OTPaths::~OTPaths ( )

Definition at line 226 of file OTPaths.cpp.

227 {
228 }

Member Function Documentation

const OTString & opentxs::OTPaths::AppBinaryFolder ( )
static

Definition at line 230 of file OTPaths.cpp.

231 {
232  return OTPaths::s_strAppBinaryFolder;
233 }
const OTString & opentxs::OTPaths::AppDataFolder ( )
static

Definition at line 254 of file OTPaths.cpp.

255 {
256  if (s_strAppDataFolder.Exists())
257  return s_strAppDataFolder; // already got it, just return it.
258 
259  OTString strHomeDataFolder(OTPaths::HomeFolder()),
260  strAppDataFolder(""); // eg. /home/user/ (the folder that the OT
261  // appdata folder will be in.)
262 
263  if (!strHomeDataFolder.Exists() && !GetHomeFromSystem(strHomeDataFolder)) {
264  OT_FAIL;
265  }
266 
267  // now lets change all the '\' into '/'
268  // then check that the path /home/user indeed exists, and is a folder.
269 
270  FixPath(strHomeDataFolder, strHomeDataFolder, true);
271  if (!PathExists(strHomeDataFolder)) OT_FAIL;
272 
273  // ok, we have the HomeData Folder, lets append our OT folder to it.
274 
275  if (!AppendFolder(strAppDataFolder, strHomeDataFolder, OT_APPDATA_DIR))
276  OT_FAIL;
277 
278  bool bFolderCreated;
279  if (!BuildFolderPath(strAppDataFolder, bFolderCreated)) OT_FAIL;
280 
281  s_strAppDataFolder = strAppDataFolder; // all good lets set it.
282 
283  return s_strAppDataFolder;
284 }
static EXPORT bool BuildFolderPath(const OTString &strFolderPath, bool &out_bFolderCreated)
Definition: OTPaths.cpp:1320
EXPORT bool Exists() const
Definition: OTString.cpp:1035
static EXPORT bool AppendFolder(OTString &out_strPath, const OTString &strBasePath, const OTString &strFolderName)
Definition: OTPaths.cpp:1212
static EXPORT bool FixPath(const OTString &strPath, OTString &out_strFixedPath, const bool &bIsFolder)
Definition: OTPaths.cpp:759
#define OT_APPDATA_DIR
Definition: OTPaths.cpp:179
static EXPORT bool GetHomeFromSystem(OTString &out_strHomeFolder)
Definition: OTPaths.cpp:1155
#define OT_FAIL
Definition: Assert.hpp:139
static EXPORT const OTString & HomeFolder()
Definition: OTPaths.cpp:240
static EXPORT bool PathExists(const OTString &strPath)
Definition: OTPaths.cpp:802
bool opentxs::OTPaths::AppendFile ( OTString out_strPath,
const OTString strBasePath,
const OTString strFileName 
)
static

Definition at line 1245 of file OTPaths.cpp.

1247 {
1248  if (!strBasePath.Exists()) {
1249  otErr << __FUNCTION__ << ": Null: "
1250  << "strBasePath"
1251  << " passed in!\n";
1252  OT_FAIL;
1253  }
1254  if (!strFileName.Exists()) {
1255  otErr << __FUNCTION__ << ": Null: "
1256  << "strFileName"
1257  << " passed in!\n";
1258  OT_FAIL;
1259  }
1260 
1261  OTString l_strBasePath_fix(""), l_strFileName_fix("");
1262 
1263  if (!FixPath(strBasePath, l_strBasePath_fix, true)) return false;
1264  if (!FixPath(strFileName, l_strFileName_fix, false)) return false;
1265 
1266  std::string l_strBasePath(l_strBasePath_fix.Get()),
1267  l_strFileName(l_strFileName_fix.Get());
1268 
1269  l_strBasePath.append(l_strFileName);
1270 
1271  const OTString l_strPath(l_strBasePath);
1272 
1273  out_strPath = l_strPath;
1274  return true;
1275 }
static EXPORT bool FixPath(const OTString &strPath, OTString &out_strFixedPath, const bool &bIsFolder)
Definition: OTPaths.cpp:759
#define OT_FAIL
Definition: Assert.hpp:139
OTLOG_IMPORT OTLogStream otErr
bool opentxs::OTPaths::AppendFolder ( OTString out_strPath,
const OTString strBasePath,
const OTString strFolderName 
)
static

Definition at line 1212 of file OTPaths.cpp.

1214 {
1215  if (!strBasePath.Exists()) {
1216  otErr << __FUNCTION__ << ": Null: "
1217  << "strBasePath"
1218  << " passed in!\n";
1219  OT_FAIL;
1220  }
1221  if (!strFolderName.Exists()) {
1222  otErr << __FUNCTION__ << ": Null: "
1223  << "strFolderName"
1224  << " passed in!\n";
1225  OT_FAIL;
1226  }
1227 
1228  OTString l_strBasePath_fix(""), l_strFolderName_fix("");
1229 
1230  if (!FixPath(strBasePath, l_strBasePath_fix, true)) return false;
1231  if (!FixPath(strFolderName, l_strFolderName_fix, true)) return false;
1232 
1233  std::string l_strBasePath(l_strBasePath_fix.Get()),
1234  l_strFolderName(l_strFolderName_fix.Get());
1235 
1236  l_strBasePath.append(l_strFolderName);
1237 
1238  const OTString l_strPath(l_strBasePath);
1239 
1240  out_strPath = l_strPath;
1241  return true;
1242 }
static EXPORT bool FixPath(const OTString &strPath, OTString &out_strFixedPath, const bool &bIsFolder)
Definition: OTPaths.cpp:759
#define OT_FAIL
Definition: Assert.hpp:139
OTLOG_IMPORT OTLogStream otErr
bool opentxs::OTPaths::BuildFilePath ( const OTString strFolderPath,
bool &  out_bFolderCreated 
)
static

Definition at line 1369 of file OTPaths.cpp.

1371 {
1372  out_bFolderCreated = false;
1373 
1374  OTString l_strFilePath_fix(""), l_strFilePath_real("");
1375 
1376  if (!ToReal(strFolderPath, l_strFilePath_real))
1377  return false; // path to real
1378 
1379  if (!FixPath(l_strFilePath_real, l_strFilePath_fix, false))
1380  return false; // real to fixed real
1381 
1382  std::string l_strFilePath(l_strFilePath_fix.Get()); // fixed real path.
1383 
1384  std::vector<std::string> vFolders;
1385 
1386  split_byChar(vFolders, l_strFilePath, "/", split::no_empties);
1387 
1388  size_t nSize = vFolders.size();
1389 
1390  std::string l_strPathPart("");
1391  bool l_FolderExists(false), l_bBuiltFolder(false);
1392 
1393  const bool bLog(OTLog::IsInitialized());
1394 
1395  for (size_t i = 0; i < nSize; i++) {
1396 #ifndef _WIN32 // aka UNIX
1397  if (0 == i) l_strPathPart += "/"; // add annother / for root.
1398 #endif
1399 
1400  l_strPathPart += vFolders[i];
1401 
1402  if ((i + 1) == nSize) continue; // file should be skipped
1403 
1404  l_strPathPart += "/"; // is a folder, so should append /
1405 
1406  if (0 == i) continue; // / or x:/ should be skiped.
1407 
1408  OTString strPathPart(l_strPathPart);
1409  if (!ConfirmCreateFolder(strPathPart, l_FolderExists, l_bBuiltFolder))
1410  return false;
1411  if (bLog && l_bBuiltFolder)
1412  otOut << __FUNCTION__ << ": Made new folder: " << l_strPathPart
1413  << "";
1414 
1415  if (!out_bFolderCreated && l_bBuiltFolder) out_bFolderCreated = true;
1416  }
1417  return true;
1418 }
OTLOG_IMPORT OTLogStream otOut
static EXPORT bool ToReal(const OTString &strExactPath, OTString &out_strCanonicalPath)
Definition: OTPaths.cpp:985
static EXPORT bool FixPath(const OTString &strPath, OTString &out_strFixedPath, const bool &bIsFolder)
Definition: OTPaths.cpp:759
static EXPORT bool IsInitialized()
Definition: OTLog.cpp:371
static EXPORT bool ConfirmCreateFolder(const OTString &strExactPath, bool &out_Exists, bool &out_IsNew)
Definition: OTPaths.cpp:921
bool opentxs::OTPaths::BuildFolderPath ( const OTString strFolderPath,
bool &  out_bFolderCreated 
)
static

Definition at line 1320 of file OTPaths.cpp.

1322 {
1323  out_bFolderCreated = false;
1324 
1325  OTString l_strFolderPath_fix(""), l_strFolderPath_real("");
1326 
1327  if (!ToReal(strFolderPath, l_strFolderPath_real))
1328  return false; // path to real
1329 
1330  if (!FixPath(l_strFolderPath_real, l_strFolderPath_fix, true))
1331  return false; // real to fixed real
1332 
1333  std::string l_strFolderPath(l_strFolderPath_fix.Get()); // fixed real path.
1334 
1335  std::vector<std::string> vFolders;
1336 
1337  split_byChar(vFolders, l_strFolderPath, "/", split::no_empties);
1338 
1339  size_t nSize = vFolders.size();
1340 
1341  std::string l_strPathPart("");
1342  bool l_FolderExists(false), l_bBuiltFolder(false);
1343 
1344  const bool bLog(OTLog::IsInitialized());
1345 
1346  for (size_t i = 0; i < nSize; i++) {
1347 #ifndef _WIN32 // aka UNIX
1348  if (0 == i) l_strPathPart += "/"; // add annother / for root.
1349 #endif
1350  l_strPathPart += vFolders[i];
1351  l_strPathPart += "/";
1352 
1353  if (0 == i) continue; // / or x:/ should be skiped.
1354 
1355  OTString strPathPart(l_strPathPart);
1356 
1357  if (!ConfirmCreateFolder(strPathPart, l_FolderExists, l_bBuiltFolder))
1358  return false;
1359  if (bLog && l_bBuiltFolder)
1360  otOut << __FUNCTION__ << ": Made new folder: " << l_strPathPart
1361  << "\n";
1362 
1363  if (!out_bFolderCreated && l_bBuiltFolder) out_bFolderCreated = true;
1364  }
1365  return true;
1366 }
OTLOG_IMPORT OTLogStream otOut
static EXPORT bool ToReal(const OTString &strExactPath, OTString &out_strCanonicalPath)
Definition: OTPaths.cpp:985
static EXPORT bool FixPath(const OTString &strPath, OTString &out_strFixedPath, const bool &bIsFolder)
Definition: OTPaths.cpp:759
static EXPORT bool IsInitialized()
Definition: OTLog.cpp:371
static EXPORT bool ConfirmCreateFolder(const OTString &strExactPath, bool &out_Exists, bool &out_IsNew)
Definition: OTPaths.cpp:921
bool opentxs::OTPaths::ConfirmCreateFolder ( const OTString strExactPath,
bool &  out_Exists,
bool &  out_IsNew 
)
static

Definition at line 921 of file OTPaths.cpp.

923 {
924  const bool bExists = (strExactPath.Exists() && !strExactPath.Compare(""));
926  bExists,
927  "OTPaths::ConfirmCreateFolder: Assert failed: no strFolderName\n");
928 
929  std::string l_strExactPath(strExactPath.Get());
930 
931  if ('/' != *l_strExactPath.rbegin()) return false; // not a directory.
932 
933  // Confirm If Directory Exists Already
934  out_Exists = PathExists(strExactPath);
935 
936  if (out_Exists) {
937  out_IsNew = false;
938  return true; // Already Have Folder, lets return true!
939  }
940  else {
941 // It dosn't exist: lets create it.
942 
943 #ifdef _WIN32
944  bool bCreateDirSuccess = (_mkdir(strExactPath.Get()) == 0);
945 #else
946  bool bCreateDirSuccess = (mkdir(strExactPath.Get(), 0700) == 0);
947 #endif
948 
949  if (!bCreateDirSuccess) {
950  otErr << "OTPaths::" << __FUNCTION__ << ": Unable To Confirm "
951  "Created Directory "
952  << strExactPath << ".\n";
953  out_IsNew = false;
954  out_Exists = false;
955  return false;
956  }
957 
958  // At this point if the folder still doesn't exist, nothing we can do.
959  // We
960  // already tried to create the folder, and SUCCEEDED, and then STILL
961  // failed
962  // to find it (if this is still false.)
963  else {
964  bool bCheckDirExist = PathExists(strExactPath);
965 
966  if (!bCheckDirExist) {
967  otErr << "OTPaths::" << __FUNCTION__
968  << ": "
969  "Unable To Confirm Created Directory " << strExactPath
970  << ".\n";
971  out_IsNew = false;
972  out_Exists = false;
973  return false;
974  }
975  else {
976  out_IsNew = true;
977  out_Exists = false;
978  return true; // We have created and checked the Folder
979  }
980  }
981  }
982 }
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
OTLOG_IMPORT OTLogStream otErr
static EXPORT bool PathExists(const OTString &strPath)
Definition: OTPaths.cpp:802
bool opentxs::OTPaths::FileExists ( const OTString strFilePath,
int64_t &  nFileLength 
)
static

Definition at line 842 of file OTPaths.cpp.

843 {
844  if (!strFilePath.Exists()) {
845  otErr << __FUNCTION__ << ": Null: "
846  << "strFilePath"
847  << " passed in!\n";
848  OT_FAIL;
849  }
850 
851  // remove trailing backslash for stat
852  std::string l_strPath(strFilePath.Get());
853  l_strPath = (OTString::replace_chars(l_strPath, "\\", '/')); // all \ to /
854 
855  if ('/' != *l_strPath.rbegin()) {
856 #ifdef _WIN32
857  std::string l_strPath_stat = l_strPath;
858  struct _stat st_buf;
859  memset(&st_buf, 0, sizeof(st_buf));
860  char filename[4086]; // not sure about this buffer,
861  // on windows paths cannot be longer than 4086,
862  // so it should be fine... needs more research.
863  strcpy_s(filename, l_strPath_stat.c_str());
864  _stat(filename, &st_buf);
865 #else
866  struct stat st_buf;
867  memset(&st_buf, 0, sizeof(st_buf));
868  stat(l_strPath.c_str(), &st_buf);
869 #endif
870 
871  // check for file
872  if (S_ISREG(st_buf.st_mode)) {
873  // good we have a file.
874  size_t lFileLength = st_buf.st_size;
875  nFileLength = static_cast<int64_t>(lFileLength);
876  return true;
877  }
878  }
879  return false;
880 }
#define S_ISREG(mode)
Definition: OTPaths.cpp:169
#define OT_FAIL
Definition: Assert.hpp:139
OTLOG_IMPORT OTLogStream otErr
static EXPORT std::string replace_chars(const std::string &str, const std::string &charsFrom, const char &charTo)
Definition: OTString.cpp:420
bool opentxs::OTPaths::FixPath ( const OTString strPath,
OTString out_strFixedPath,
const bool &  bIsFolder 
)
static

Definition at line 759 of file OTPaths.cpp.

761 {
762  if (!strPath.Exists()) {
763  otErr << __FUNCTION__ << ": Null: "
764  << "strPath"
765  << " passed in!\n";
766  OT_FAIL;
767  }
768 
769  std::string l_strPath(strPath.Get());
770  // first change all back-slashes to forward slashes:
771  std::string l_strPath_noBackslash(
772  OTString::replace_chars(l_strPath, "\\", '/'));
773 
774  // now we make sure we have the correct trailing "/".
775 
776  if ('/' == *l_strPath_noBackslash.rbegin()) {
777  if (bIsFolder) {
778  out_strFixedPath.Set(l_strPath_noBackslash.c_str());
779  return true;
780  }
781  else {
782  out_strFixedPath.Set(
783  l_strPath_noBackslash.substr(0, l_strPath_noBackslash.size() -
784  1).c_str());
785  return true;
786  }
787  }
788  else {
789  if (bIsFolder) {
790  l_strPath_noBackslash += "/";
791  out_strFixedPath.Set(l_strPath_noBackslash.c_str());
792  return true;
793  }
794  else {
795  out_strFixedPath.Set(l_strPath_noBackslash.c_str());
796  return true;
797  }
798  }
799 }
#define OT_FAIL
Definition: Assert.hpp:139
OTLOG_IMPORT OTLogStream otErr
static EXPORT std::string replace_chars(const std::string &str, const std::string &charsFrom, const char &charTo)
Definition: OTString.cpp:420
bool opentxs::OTPaths::FolderExists ( const OTString strFolderPath)
static

Definition at line 883 of file OTPaths.cpp.

884 {
885  if (!strFolderPath.Exists()) {
886  otErr << __FUNCTION__ << ": Null: "
887  << "strFolderPath"
888  << " passed in!\n";
889  OT_FAIL;
890  }
891 
892  // remove trailing backslash for stat
893  std::string l_strPath(strFolderPath.Get());
894  l_strPath = (OTString::replace_chars(l_strPath, "\\", '/')); // all \ to /
895 
896  if ('/' == *l_strPath.rbegin()) {
897 #ifdef _WIN32
898  std::string l_strPath_stat = l_strPath.substr(0, l_strPath.size() - 1);
899  struct _stat st_buf;
900  memset(&st_buf, 0, sizeof(st_buf));
901  char filename[4086] = ""; // not sure about this buffer,
902  // on windows paths cannot be longer than 4086,
903  // so it should be fine... needs more research.
904  strcpy_s(filename, l_strPath_stat.c_str());
905  _stat(filename, &st_buf);
906 #else
907  struct stat st_buf;
908  memset(&st_buf, 0, sizeof(st_buf));
909  stat(l_strPath.c_str(), &st_buf);
910 #endif
911 
912  if (S_ISDIR(st_buf.st_mode)) {
913  // good we have a directory.
914  return true;
915  }
916  }
917  return false;
918 }
#define OT_FAIL
Definition: Assert.hpp:139
OTLOG_IMPORT OTLogStream otErr
#define S_ISDIR(mode)
Definition: OTPaths.cpp:165
static EXPORT std::string replace_chars(const std::string &str, const std::string &charsFrom, const char &charTo)
Definition: OTString.cpp:420
bool opentxs::OTPaths::Get ( OTSettings config,
const OTString strSection,
const OTString strKey,
OTString out_strVar,
bool &  out_bIsRelative,
bool &  out_bKeyExist 
)
static

Definition at line 625 of file OTPaths.cpp.

628 {
629  if (!strSection.Exists()) {
630  otErr << __FUNCTION__ << ": Null: "
631  << "strSection"
632  << " passed in!\n";
633  OT_FAIL;
634  }
635  if (!strKey.Exists()) {
636  otErr << __FUNCTION__ << ": Null: "
637  << "strKey"
638  << " passed in!\n";
639  OT_FAIL;
640  }
641 
642  out_strVar = "";
643  out_bIsRelative = false;
644  out_bKeyExist = false;
645 
646  const bool bPreLoaded(config.IsLoaded());
647 
648  if (!bPreLoaded) {
649  config.Reset();
650  if (!config.Load()) {
651  OT_FAIL;
652  }
653  }
654 
655  bool bBoolExists(false), bIsRelative(false);
656  OTString strRelativeKey(""), strOutFolder("");
657 
658  strRelativeKey.Format("%s%s", strKey.Get(), OT_CONFIG_ISRELATIVE);
659 
660  if (config.Check_bool(strSection, strRelativeKey, bIsRelative,
661  bBoolExists)) {
662  bool bStringExists = false;
663  if (config.Check_str(strSection, strKey, strOutFolder, bStringExists)) {
664  if (bBoolExists && bStringExists) {
665  if (!bIsRelative) // lets fix the path, so it dosn't matter how
666  // people write it in the config.
667  {
668  if (!ToReal(strOutFolder, strOutFolder)) {
669  OT_FAIL;
670  }
671 
672  if (!FixPath(strOutFolder, strOutFolder, true)) {
673  OT_FAIL;
674  }
675  }
676 
677  out_strVar = strOutFolder;
678  out_bIsRelative = bIsRelative;
679  out_bKeyExist = true;
680  }
681  else {
682  out_strVar = "";
683  out_bIsRelative = false;
684  out_bKeyExist = false;
685  }
686 
687  if (!bPreLoaded) {
688  config.Reset();
689  }
690 
691  return true;
692  }
693  }
694  // if we get here, there has been a error!
695  OT_FAIL;
696 }
static EXPORT bool ToReal(const OTString &strExactPath, OTString &out_strCanonicalPath)
Definition: OTPaths.cpp:985
#define OT_CONFIG_ISRELATIVE
Definition: OTPaths.cpp:199
static EXPORT bool FixPath(const OTString &strPath, OTString &out_strFixedPath, const bool &bIsFolder)
Definition: OTPaths.cpp:759
#define OT_FAIL
Definition: Assert.hpp:139
OTLOG_IMPORT OTLogStream otErr
static EXPORT bool opentxs::OTPaths::GetCurrentWorking ( OTString strCurrentWorkingPath)
static
static EXPORT bool opentxs::OTPaths::GetExecutable ( OTString strExecutablePath)
static
bool opentxs::OTPaths::GetHomeFromSystem ( OTString out_strHomeFolder)
static

Definition at line 1155 of file OTPaths.cpp.

1156 {
1157 #ifdef _WIN32
1158 #ifdef _UNICODE
1159  TCHAR szPath[MAX_PATH] = L"";
1160 #else
1161  TCHAR szPath[MAX_PATH] = "";
1162 #endif
1163 
1164  if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1165  nullptr, 0, szPath))) {
1166 #ifdef UNICODE
1167  out_strHomeFolder.Set(utf8util::UTF8FromUTF16(szPath));
1168 #else
1169  out_strHomeFolder.Set(szPath);
1170 #endif
1171  }
1172  else {
1173  out_strHomeFolder.Set("");
1174  return false;
1175  }
1176 #else
1177  out_strHomeFolder.Set(getenv("HOME"));
1178 #endif
1179  return true;
1180 }
EXPORT std::string UTF8FromUTF16(const std::wstring &utf16)
const OTString & opentxs::OTPaths::GlobalConfigFile ( )
static

Definition at line 286 of file OTPaths.cpp.

287 {
288  if (s_strGlobalConfigFile.Exists())
289  return s_strGlobalConfigFile; // got it, lets return it.
290 
291  OTString strGlobalConfigFile("");
292 
293  if (!AppendFile(strGlobalConfigFile, AppDataFolder(),
295  OT_FAIL;
296 
297  s_strGlobalConfigFile = strGlobalConfigFile;
298 
299  return s_strGlobalConfigFile;
300 }
EXPORT bool Exists() const
Definition: OTString.cpp:1035
static EXPORT const OTString & AppDataFolder()
Definition: OTPaths.cpp:254
#define OT_FAIL
Definition: Assert.hpp:139
#define OT_INIT_CONFIG_FILENAME
Definition: OTPaths.cpp:198
static EXPORT bool AppendFile(OTString &out_strPath, const OTString &strBasePath, const OTString &strFileName)
Definition: OTPaths.cpp:1245
const OTString & opentxs::OTPaths::HomeFolder ( )
static

Definition at line 240 of file OTPaths.cpp.

241 {
242  return OTPaths::s_strHomeFolder;
243 }
bool opentxs::OTPaths::LoadSetPrefixFolder ( OTSettings config = s_settings,
const OTString strPrefixFolder = "" 
)
static

Definition at line 331 of file OTPaths.cpp.

335 {
336  /*
337  The prefix path is special.
338 
339  This path is tested if it is different to the
340  one that would be automatically selected by this program
341  (aka either compiled into, or from the registry, or the default user data
342  directory).
343 
344  If the set path is different to what would be supplied and the override path
345  value is set.
346  Then we will use that path.
347 
348  Otherwise, we will update the path in the configuration to link against the
349  updated path.
350 
351  Users will need to set the override path flag in the configuration,
352  if they want to manually set the prefix path.
353  */
354 
355  const bool bPreLoaded(config.IsLoaded());
356 
357  if (!bPreLoaded) {
358  config.Reset();
359  if (!config.Load()) {
360  OT_FAIL;
361  }
362  }
363 
364  {
365  // get default path
366  OTString strDefaultPrefixPath(OT_PREFIX_PATH);
367  {
368  if (!strDefaultPrefixPath.Exists()) {
369  otErr << __FUNCTION__ << ": Error: OT_PREFIX_PATH is not set!";
370  OT_FAIL;
371  }
372 
373 #ifdef _WIN32
374  OTString strTemp;
375  if (OTPaths::Win_GetInstallFolderFromRegistry(strTemp)) {
376  strDefaultPrefixPath = strTemp;
377  }
378 #endif
379 
380  if (!ToReal(strDefaultPrefixPath, strDefaultPrefixPath)) {
381  OT_FAIL;
382  }
383  if (!FixPath(strDefaultPrefixPath, strDefaultPrefixPath, true)) {
384  OT_FAIL;
385  }
386  }
387 
388  OTString strLocalPrefixPath = "";
389  bool bPrefixPathOverride = false;
390 
391  {
392  // now check the configuration to see what values we have:
393  OTString strConfigPath = "";
394 
395  bool bIsNew = false;
396  OTString strPrefixPathOverride("prefix_path_override");
397 
398  if (!config.CheckSet_str("paths", "prefix_path",
399  strDefaultPrefixPath, strConfigPath,
400  bIsNew)) {
401  return false;
402  }
403  if (!config.CheckSet_bool(
404  "paths", strPrefixPathOverride, false, bPrefixPathOverride,
405  bIsNew, "; This will force the prefix not to change")) {
406  return false;
407  }
408 
409  // if the config dosn't have a prefix path set. Lets set the
410  // default.
411  // if a prefix path was passed in, we will override with that later.
412  if (!strConfigPath.Exists() || (3 > strConfigPath.GetLength())) {
413  otErr << __FUNCTION__ << ": Error: Bad "
414  << "prefix_path"
415  << " in config, will reset!";
416 
417  strConfigPath = strDefaultPrefixPath; // set
418  bPrefixPathOverride = false;
419 
420  // lets set the default path, and reset override
421  bool bNewOrUpdate = false;
422  if (!config.Set_str("paths", "prefix_path",
423  strDefaultPrefixPath, bNewOrUpdate)) {
424  return false;
425  }
426  if (!config.Set_bool("paths", strPrefixPathOverride, false,
427  bNewOrUpdate)) {
428  return false;
429  }
430  }
431 
432  strLocalPrefixPath = strConfigPath;
433  }
434 
435  {
436  if (!bPrefixPathOverride) {
437  bool bUpdate = false;
438 
439  // default
440  if (!strLocalPrefixPath.Compare(strDefaultPrefixPath)) {
441  strLocalPrefixPath = strDefaultPrefixPath;
442  bUpdate = true;
443  }
444 
445  // passed in
446  if (strPrefixFolder.Exists() &&
447  (3 < strPrefixFolder.GetLength())) {
448  // a prefix folder was passed in... lets use it, and update
449  // the config if the override isn't set
450  OTString strTmp = strPrefixFolder;
451 
452  if (!ToReal(strTmp, strTmp)) {
453  OT_FAIL;
454  }
455 
456  if (!FixPath(strTmp, strTmp, true)) {
457  OT_FAIL;
458  }
459 
460  if (!strLocalPrefixPath.Compare(strTmp)) {
461  strLocalPrefixPath = strTmp;
462  bUpdate = true;
463  }
464  }
465 
466  // we need to update the path in the config
467  if (bUpdate) {
468  bool bNewOrUpdate = false;
469  if (!config.Set_str("paths", "prefix_path",
470  strLocalPrefixPath, bNewOrUpdate)) {
471  return false;
472  }
473  }
474  }
475  }
476 
477  {
478  if (!strLocalPrefixPath.Exists()) {
479  OT_FAIL;
480  }
481 
482  if (!ToReal(strLocalPrefixPath, strLocalPrefixPath)) {
483  OT_FAIL;
484  }
485  if (!FixPath(strLocalPrefixPath, strLocalPrefixPath, true)) {
486  OT_FAIL;
487  }
488  s_strPrefixFolder = strLocalPrefixPath;
489  }
490  }
491 
492  if (!bPreLoaded) {
493  if (!config.Save()) {
494  OT_FAIL;
495  }
496  config.Reset();
497  }
498  return true;
499 }
static EXPORT bool ToReal(const OTString &strExactPath, OTString &out_strCanonicalPath)
Definition: OTPaths.cpp:985
static EXPORT bool FixPath(const OTString &strPath, OTString &out_strFixedPath, const bool &bIsFolder)
Definition: OTPaths.cpp:759
#define OT_FAIL
Definition: Assert.hpp:139
#define OT_PREFIX_PATH
Definition: OTPaths.cpp:194
OTLOG_IMPORT OTLogStream otErr
bool opentxs::OTPaths::LoadSetScriptsFolder ( OTSettings config = s_settings,
const OTString strScriptsFolder = "",
const bool &  bIsRelative = true 
)
static

Definition at line 504 of file OTPaths.cpp.

508 {
509  const bool bPreLoaded(config.IsLoaded());
510 
511  if (!bPreLoaded) {
512  config.Reset();
513  if (!config.Load()) {
514  OT_FAIL;
515  }
516  }
517 
518  OTString strRelativeKey = "";
519  strRelativeKey.Format("%s%s", "scripts", OT_CONFIG_ISRELATIVE);
520 
521  // local vairables.
522  bool bConfigIsRelative = false;
523  OTString strConfigFolder = "";
524 
525  // lets first check what we have in the configuration:
526  {
527  bool bKeyIsNew = false;
528 
529  if (!config.CheckSet_bool("paths", strRelativeKey, true,
530  bConfigIsRelative, bKeyIsNew)) {
531  return false;
532  }
533  if (!config.CheckSet_str("paths", "scripts", OT_SCRIPTS_DIR,
534  strConfigFolder, bKeyIsNew)) {
535  return false;
536  }
537  }
538 
539  // lets first test if there was a folder passed in
540 
541  if ((strScriptsFolder.Exists()) && (3 < strScriptsFolder.GetLength())) {
542 
543  // we have a folder passed in, lets now check if we need to update
544  // anything:
545 
546  if (bConfigIsRelative != bIsRelative) {
547 
548  bConfigIsRelative = bIsRelative;
549  bool bNewOrUpdated = false;
550 
551  if (!config.Set_bool("paths", strRelativeKey, bConfigIsRelative,
552  bNewOrUpdated)) {
553  return false;
554  }
555  }
556 
557  if (!strConfigFolder.Compare(strScriptsFolder)) {
558 
559  strConfigFolder = strScriptsFolder; // update folder
560  bool bNewOrUpdated = false;
561 
562  if (!config.Set_str("paths", "scripts", strConfigFolder,
563  bNewOrUpdated)) {
564  return false;
565  }
566  }
567  }
568 
569  if (bConfigIsRelative) {
570  if (!FixPath(strConfigFolder, strConfigFolder, true)) {
571  OT_FAIL;
572  }
573 
574  OTString strPrefixScriptPath = "";
575  AppendFolder(strPrefixScriptPath, PrefixFolder(), strConfigFolder);
576 
577  OTString strAppBinaryScriptPath = "";
578 
579  // if the AppBinaryFolder is set, we will attempt to use this script
580  // path instead.
581  // however if the directory dosn't exist, we will default back to
582  // appending to the prefix.
583 
584  // TODO: Make the prefix path set to AppBinaryFolder. (da2ce7)
585 
586  if (AppBinaryFolder().Exists()) {
587  AppendFolder(strAppBinaryScriptPath, AppBinaryFolder(),
588  strConfigFolder);
589  if (!OTPaths::FolderExists(strAppBinaryScriptPath)) {
590  otOut << __FUNCTION__
591  << ": Warning: Cannot Find: " << strAppBinaryScriptPath
592  << ", using default!";
593  strAppBinaryScriptPath = ""; // don't have anything here.
594  }
595  }
596 
597  s_strScriptsFolder = strAppBinaryScriptPath.Exists()
598  ? strAppBinaryScriptPath
599  : strPrefixScriptPath;
600 
601  if (!s_strScriptsFolder.Exists()) OT_FAIL;
602 
603  }
604  else {
605  if (!ToReal(strConfigFolder, strConfigFolder)) {
606  OT_FAIL;
607  }
608 
609  if (!FixPath(strConfigFolder, strConfigFolder, true)) {
610  OT_FAIL;
611  }
612  s_strScriptsFolder = strConfigFolder; // set
613  }
614 
615  if (!bPreLoaded) {
616  if (!config.Save()) {
617  OT_FAIL;
618  }
619  config.Reset();
620  }
621  return true; // success
622 }
OTLOG_IMPORT OTLogStream otOut
static EXPORT bool ToReal(const OTString &strExactPath, OTString &out_strCanonicalPath)
Definition: OTPaths.cpp:985
static EXPORT const OTString & PrefixFolder()
Definition: OTPaths.cpp:302
#define OT_CONFIG_ISRELATIVE
Definition: OTPaths.cpp:199
EXPORT bool Exists() const
Definition: OTString.cpp:1035
static EXPORT bool AppendFolder(OTString &out_strPath, const OTString &strBasePath, const OTString &strFolderName)
Definition: OTPaths.cpp:1212
static EXPORT bool FixPath(const OTString &strPath, OTString &out_strFixedPath, const bool &bIsFolder)
Definition: OTPaths.cpp:759
#define OT_SCRIPTS_DIR
Definition: OTPaths.cpp:206
#define OT_FAIL
Definition: Assert.hpp:139
EXPORT bool Exists(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:584
static EXPORT const OTString & AppBinaryFolder()
Definition: OTPaths.cpp:230
static EXPORT bool FolderExists(const OTString &strFolderPath)
Definition: OTPaths.cpp:883
bool opentxs::OTPaths::PathExists ( const OTString strPath)
static

Definition at line 802 of file OTPaths.cpp.

803 {
804  if (!strPath.Exists()) {
805  otErr << __FUNCTION__ << ": Null: "
806  << "strPath"
807  << " passed in!\n";
808  OT_FAIL;
809  }
810 
811  // remove trailing backslash for stat
812  std::string l_strPath(strPath.Get());
813  l_strPath = (OTString::replace_chars(l_strPath, "\\", '/')); // all \ to /
814 
815  // std::string l_strPath_stat = l_strPath;
816  std::string l_strPath_stat("");
817 
818  // remove last / if it exists (for l_strPath_stat)
819  if ('/' == *l_strPath.rbegin())
820  l_strPath_stat = l_strPath.substr(0, l_strPath.size() - 1);
821  else
822  l_strPath_stat = l_strPath;
823 
824  struct stat st;
825  memset(&st, 0, sizeof(st));
826 
827  if (0 ==
828  stat(l_strPath_stat.c_str(), &st)) // good we have at-least on a node
829  {
830  if ('/' != *l_strPath.rbegin()) {
831  int64_t temp_l = 0;
832  return FileExists(strPath, temp_l);
833  }
834  else {
835  return FolderExists(strPath);
836  }
837  }
838  return false;
839 }
static EXPORT bool FileExists(const OTString &strFilePath, int64_t &nFileLength)
Definition: OTPaths.cpp:842
#define OT_FAIL
Definition: Assert.hpp:139
OTLOG_IMPORT OTLogStream otErr
static EXPORT bool FolderExists(const OTString &strFolderPath)
Definition: OTPaths.cpp:883
static EXPORT std::string replace_chars(const std::string &str, const std::string &charsFrom, const char &charTo)
Definition: OTString.cpp:420
const OTString & opentxs::OTPaths::PrefixFolder ( )
static

Definition at line 302 of file OTPaths.cpp.

303 {
304  if (s_strPrefixFolder.Exists())
305  return s_strPrefixFolder; // got it, lets return it.
306 
307  // lets load from the statndard config, or create the entry.
308  if (LoadSetPrefixFolder())
309  return s_strPrefixFolder;
310  else {
311  OT_FAIL;
312  }
313 }
static EXPORT bool LoadSetPrefixFolder(OTSettings &config=s_settings, const OTString &strPrefixFolder="")
Definition: OTPaths.cpp:331
EXPORT bool Exists() const
Definition: OTString.cpp:1035
#define OT_FAIL
Definition: Assert.hpp:139
bool opentxs::OTPaths::RelativeToCanonical ( OTString out_strCanonicalPath,
const OTString strBasePath,
const OTString strRelativePath 
)
static

Definition at line 1280 of file OTPaths.cpp.

1283 {
1284  if (!strBasePath.Exists()) {
1285  otErr << __FUNCTION__ << ": Null: "
1286  << "strBasePath"
1287  << " passed in!\n";
1288  OT_FAIL;
1289  }
1290  if (!strRelativePath.Exists()) {
1291  otErr << __FUNCTION__ << ": Null: "
1292  << "strRelativePath"
1293  << " passed in!\n";
1294  OT_FAIL;
1295  }
1296 
1297  OTString l_strBasePath_fix("");
1298  if (!FixPath(strBasePath, l_strBasePath_fix, true)) return false;
1299 
1300  if (strRelativePath.Compare(".")) {
1301  out_strCanonicalPath = strBasePath;
1302  return true;
1303  } // if ".", return base path.
1304 
1305  std::string l_strBasePath(l_strBasePath_fix.Get()),
1306  l_strRelativePath(strRelativePath.Get());
1307 
1308  l_strBasePath.append(l_strRelativePath);
1309 
1310  OTString l_strPath(l_strBasePath), l_strCanonicalPath("");
1311 
1312  if (!ToReal(l_strPath, l_strCanonicalPath)) return false;
1313 
1314  out_strCanonicalPath = l_strCanonicalPath;
1315 
1316  return true;
1317 }
static EXPORT bool ToReal(const OTString &strExactPath, OTString &out_strCanonicalPath)
Definition: OTPaths.cpp:985
static EXPORT bool FixPath(const OTString &strPath, OTString &out_strFixedPath, const bool &bIsFolder)
Definition: OTPaths.cpp:759
#define OT_FAIL
Definition: Assert.hpp:139
OTLOG_IMPORT OTLogStream otErr
const OTString & opentxs::OTPaths::ScriptsFolder ( )
static

Definition at line 315 of file OTPaths.cpp.

316 {
317  if (s_strScriptsFolder.Exists())
318  return s_strScriptsFolder; // got it, lets return it.
319 
320  // load it from config (if we already have it set in the config).
321  if (LoadSetScriptsFolder())
322  return s_strScriptsFolder;
323  else {
324  OT_FAIL;
325  }
326 }
EXPORT bool Exists() const
Definition: OTString.cpp:1035
static EXPORT bool LoadSetScriptsFolder(OTSettings &config=s_settings, const OTString &strScriptsFolder="", const bool &bIsRelative=true)
Definition: OTPaths.cpp:504
#define OT_FAIL
Definition: Assert.hpp:139
bool opentxs::OTPaths::Set ( OTSettings config,
const OTString strSection,
const OTString strKey,
const OTString strValue,
const bool &  bIsRelative,
bool &  out_bIsNewOrUpdated,
const OTString strComment = "" 
)
static

Definition at line 699 of file OTPaths.cpp.

703 {
704  if (!strSection.Exists()) {
705  otErr << __FUNCTION__ << ": Null: "
706  << "strSection"
707  << " passed in!\n";
708  OT_FAIL;
709  }
710  if (!strKey.Exists()) {
711  otErr << __FUNCTION__ << ": Null: "
712  << "strKey"
713  << " passed in!\n";
714  OT_FAIL;
715  }
716 
717  out_bIsNewOrUpdated = false;
718 
719  const bool bPreLoaded(config.IsLoaded());
720 
721  if (!bPreLoaded) // we only need to load, if not already loaded.
722  {
723  config.Reset();
724  if (!config.Load()) {
725  OT_FAIL;
726  }
727  }
728 
729  bool bBoolIsNew(false);
730  OTString strRelativeKey("");
731 
732  strRelativeKey.Format("%s%s", strKey.Get(), OT_CONFIG_ISRELATIVE);
733 
734  if (config.Set_bool(strSection, strRelativeKey, bIsRelative, bBoolIsNew,
735  strComment)) {
736  bool bStringIsNew = false;
737  if (config.Set_str(strSection, strKey, strValue, bStringIsNew)) {
738  if (bBoolIsNew && bStringIsNew) // using existing key
739  {
740  out_bIsNewOrUpdated = true;
741  }
742 
743  if (!bPreLoaded) {
744  if (!config.Save()) {
745  OT_FAIL;
746  }
747  config.Reset();
748  }
749 
750  return true;
751  }
752  }
753 
754  // if we get here, there has been a error!
755  OT_FAIL;
756 }
#define OT_CONFIG_ISRELATIVE
Definition: OTPaths.cpp:199
#define OT_FAIL
Definition: Assert.hpp:139
OTLOG_IMPORT OTLogStream otErr
void opentxs::OTPaths::SetAppBinaryFolder ( OTString  strLocation)
static

Definition at line 235 of file OTPaths.cpp.

236 {
237  OTPaths::s_strAppBinaryFolder = strLocation;
238 }
void opentxs::OTPaths::SetHomeFolder ( OTString  strLocation)
static

Definition at line 245 of file OTPaths.cpp.

246 {
247  OTPaths::s_strHomeFolder = strLocation;
248 
249 #ifdef ANDROID
250  OTPaths::s_settings.SetConfigFilePath(GlobalConfigFile());
251 #endif
252 }
static EXPORT const OTString & GlobalConfigFile()
Definition: OTPaths.cpp:286
bool opentxs::OTPaths::ToReal ( const OTString strExactPath,
OTString out_strCanonicalPath 
)
static

Definition at line 985 of file OTPaths.cpp.

987 {
988  if (!strExactPath.Exists()) {
989  otErr << __FUNCTION__ << ": Null: "
990  << "strExactPath"
991  << " passed in!\n";
992  OT_FAIL;
993  }
994 
995 #ifdef _WIN32
996 #ifdef _UNICODE
997 
998  const char* szPath = strExactPath.Get();
999  size_t newsize = strlen(szPath) + 1;
1000  wchar_t* wzPath = new wchar_t[newsize];
1001 
1002  size_t convertedChars = 0;
1003  mbstowcs_s(&convertedChars, wzPath, newsize, szPath, 4096);
1004 
1005  wchar_t szBuf[4096] = L"";
1006 
1007  if (GetFullPathName(wzPath, 4096, szBuf, nullptr)) {
1008  out_strCanonicalPath.Set(utf8util::UTF8FromUTF16(szBuf));
1009  return true;
1010  }
1011  else {
1012  out_strCanonicalPath.Set("");
1013  return false;
1014  }
1015 
1016 #else
1017  char_t szBuf[4096] = "";
1018  char_t const* szPath = strRealPath.Get();
1019 
1020  if (GetFullPathName(szPath, 4096, szBuf, nullptr)) {
1021  out_strCanonicalPath.Set(szBuf);
1022  return true;
1023  }
1024  else {
1025  out_strCanonicalPath.Set("");
1026  return false;
1027  }
1028 
1029 #endif
1030 #else
1031 
1032  char* actualpath = realpath(strExactPath.Get(), NULL);
1033  if (actualpath == NULL) {
1034 
1035  if (errno == ENOTDIR) {
1036  otWarn << "Input value to RealPath is not a directory: (Realpath: "
1037  "skipping)\n";
1038  out_strCanonicalPath.Set(strExactPath);
1039  return true;
1040  }
1041 
1042  if (errno == ENOENT) {
1043  otWarn << "File doesn't exist: (Realpath: skipping)\n";
1044  out_strCanonicalPath.Set(strExactPath);
1045  return true;
1046  }
1047 
1048  OT_ASSERT_MSG((errno != EACCES), "Error (Realpath: EACCES): Unable to "
1049  "build RealPath: access denied");
1050  OT_ASSERT_MSG(
1051  (errno != EINVAL),
1052  "Error (RealPath: EINVAL): Input value into RealPath was nullptr");
1053  OT_ASSERT_MSG(
1054  (errno != ELOOP),
1055  "Error (RealPath: ELOOP): Resloving links resulted in a loop.");
1056  OT_ASSERT_MSG((errno != ENAMETOOLONG),
1057  "Error (RealPath: ENAMETOOLONG): Name too int64_t.");
1058  OT_ASSERT_MSG((errno != ERANGE), "Error (RealPath: ERANGE): Resulting "
1059  "path is too int64_t for the buffer");
1060  OT_ASSERT_MSG((errno != EIO),
1061  "Error (RealPath: EIO): Unable to access path.");
1062 
1063  OT_ASSERT_MSG(
1064  (false),
1065  "Error (RealPath: OTHER): Something bad Happend with 'realpath'.");
1066  }
1067  out_strCanonicalPath.Set(actualpath);
1068  free(actualpath);
1069  return true;
1070 #endif
1071 }
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
#define OT_FAIL
Definition: Assert.hpp:139
EXPORT std::string UTF8FromUTF16(const std::wstring &utf16)
OTLOG_IMPORT OTLogStream otWarn
OTLOG_IMPORT OTLogStream otErr

The documentation for this class was generated from the following files: