Fixed problem with utf characters in the %APPDATA% path on Windows.

Added function for opening files on Windows and Linux - RsDirUtil::rs_fopen.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4124 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2011-04-03 23:11:38 +00:00
parent bc78397a64
commit bc113326e4
17 changed files with 109 additions and 119 deletions

View File

@ -718,7 +718,7 @@ bool CacheStrapper::CacheExist(CacheData& data){
std::string filename = data.path + "/" + data.name; std::string filename = data.path + "/" + data.name;
FILE* file = NULL; FILE* file = NULL;
file = fopen(filename.c_str(), "r"); file = RsDirUtil::rs_fopen(filename.c_str(), "r");
if(file == NULL) if(file == NULL)
return false; return false;

View File

@ -978,7 +978,7 @@ int FileIndex::saveIndex(const std::string& filename, std::string &fileHash, uin
/* finally, save to file */ /* finally, save to file */
FILE *file = fopen(filenametmp.c_str(), "wb"); FILE *file = RsDirUtil::rs_fopen(filenametmp.c_str(), "wb");
if (file == NULL) if (file == NULL)
{ {
std::cerr << "FileIndex::saveIndex error opening file for writting: " << filename << ". Giving up." << std::endl; std::cerr << "FileIndex::saveIndex error opening file for writting: " << filename << ". Giving up." << std::endl;

View File

@ -682,7 +682,7 @@ bool ftController::moveFile(const std::string& source,const std::string& dest)
bool ftController::copyFile(const std::string& source,const std::string& dest) bool ftController::copyFile(const std::string& source,const std::string& dest)
{ {
FILE *in = fopen64(source.c_str(),"rb") ; FILE *in = RsDirUtil::rs_fopen(source.c_str(),"rb") ;
if(in == NULL) if(in == NULL)
{ {
@ -690,7 +690,7 @@ bool ftController::copyFile(const std::string& source,const std::string& dest)
return false ; return false ;
} }
FILE *out = fopen64(dest.c_str(),"wb") ; FILE *out = RsDirUtil::rs_fopen(dest.c_str(),"wb") ;
if(out == NULL) if(out == NULL)
{ {
@ -1040,13 +1040,7 @@ bool ftController::FileRequest(const std::string& fname, const std::string& has
destination = dest + "/" + fname; destination = dest + "/" + fname;
// create void file with the target name. // create void file with the target name.
#ifdef WINDOWS_SYS FILE *f = RsDirUtil::rs_fopen(destination.c_str(),"w") ;
std::wstring destinationW;
librs::util::ConvertUtf8ToUtf16(destination, destinationW);
FILE *f = _wfopen(destinationW.c_str(), L"w");
#else
FILE *f = fopen64(destination.c_str(),"w") ;
#endif
if(f == NULL) if(f == NULL)
std::cerr << "Could not open file " << destination << " for writting." << std::endl ; std::cerr << "Could not open file " << destination << " for writting." << std::endl ;
else else

View File

@ -215,7 +215,7 @@ bool ftExtraList::moveExtraFile(std::string fname, std::string hash, uint64_t si
} }
std::string path = destpath + '/' + fname; std::string path = destpath + '/' + fname;
if (0 == rename(it->second.info.path.c_str(), path.c_str())) if (RsDirUtil::renameFile(it->second.info.path, path))
{ {
/* rename */ /* rename */
it->second.info.path = path; it->second.info.path = path;
@ -427,13 +427,7 @@ bool ftExtraList::loadList(std::list<RsItem *>& load)
} }
/* open file */ /* open file */
#ifdef WINDOWS_SYS FILE *fd = RsDirUtil::rs_fopen(fi->file.path.c_str(), "rb");
std::wstring filepathW;
librs::util::ConvertUtf8ToUtf16(fi->file.path, filepathW);
FILE *fd = _wfopen(filepathW.c_str(), L"rb");
#else
FILE *fd = fopen64(fi->file.path.c_str(), "rb");
#endif
if (fd == NULL) if (fd == NULL)
{ {
delete (*it); delete (*it);

View File

@ -251,13 +251,7 @@ int ftFileCreator::locked_initializeFileAttrs()
* attempt to open file * attempt to open file
*/ */
#ifdef WINDOWS_SYS fd = RsDirUtil::rs_fopen(file_name.c_str(), "r+b");
std::wstring wfile_name;
librs::util::ConvertUtf8ToUtf16(file_name, wfile_name);
fd = _wfopen(wfile_name.c_str(), L"r+b");
#else
fd = fopen64(file_name.c_str(), "r+b");
#endif
if (!fd) if (!fd)
{ {
@ -268,11 +262,7 @@ int ftFileCreator::locked_initializeFileAttrs()
std::cerr << std::endl; std::cerr << std::endl;
/* try opening for write */ /* try opening for write */
#ifdef WINDOWS_SYS fd = RsDirUtil::rs_fopen(file_name.c_str(), "w+b");
fd = _wfopen(wfile_name.c_str(), L"w+b");
#else
fd = fopen64(file_name.c_str(), "w+b");
#endif
if (!fd) if (!fd)
{ {
std::cerr << "ftFileCreator::initializeFileAttrs()"; std::cerr << "ftFileCreator::initializeFileAttrs()";

View File

@ -305,24 +305,14 @@ int ftFileProvider::initializeFileAttrs()
* attempt to open file * attempt to open file
*/ */
#ifdef WINDOWS_SYS fd = RsDirUtil::rs_fopen(file_name.c_str(), "r+b");
std::wstring wfile_name;
librs::util::ConvertUtf8ToUtf16(file_name, wfile_name);
fd = _wfopen(wfile_name.c_str(), L"r+b");
#else
fd = fopen64(file_name.c_str(), "r+b");
#endif
if (!fd) if (!fd)
{ {
std::cerr << "ftFileProvider::initializeFileAttrs() Failed to open (r+b): "; std::cerr << "ftFileProvider::initializeFileAttrs() Failed to open (r+b): ";
std::cerr << file_name << std::endl; std::cerr << file_name << std::endl;
/* try opening read only */ /* try opening read only */
#ifdef WINDOWS_SYS fd = RsDirUtil::rs_fopen(file_name.c_str(), "rb");
fd = _wfopen(wfile_name.c_str(), L"rb");
#else
fd = fopen64(file_name.c_str(), "rb");
#endif
if (!fd) if (!fd)
{ {
std::cerr << "ftFileProvider::initializeFileAttrs() Failed to open (rb): "; std::cerr << "ftFileProvider::initializeFileAttrs() Failed to open (rb): ";

View File

@ -36,6 +36,7 @@
#include "authgpg.h" #include "authgpg.h"
#include "pqi/p3connmgr.h" #include "pqi/p3connmgr.h"
#include "serialiser/rsconfigitems.h" #include "serialiser/rsconfigitems.h"
#include "util/rsdir.h"
/******************** notify of new Cert **************************/ /******************** notify of new Cert **************************/
#include "pqinotify.h" #include "pqinotify.h"
@ -178,7 +179,7 @@ static int initLib = 0;
SSL_CTX_set_cipher_list(sslctx, "DEFAULT"); SSL_CTX_set_cipher_list(sslctx, "DEFAULT");
// certificates (Set Local Server Certificate). // certificates (Set Local Server Certificate).
FILE *ownfp = fopen(cert_file, "r"); FILE *ownfp = RsDirUtil::rs_fopen(cert_file, "r");
if (ownfp == NULL) if (ownfp == NULL)
{ {
std::cerr << "Couldn't open Own Certificate!" << std::endl; std::cerr << "Couldn't open Own Certificate!" << std::endl;
@ -201,7 +202,7 @@ static int initLib = 0;
mOwnPublicKey = X509_get_pubkey(x509); mOwnPublicKey = X509_get_pubkey(x509);
// get private key // get private key
FILE *pkfp = fopen(priv_key_file, "rb"); FILE *pkfp = RsDirUtil::rs_fopen(priv_key_file, "rb");
if (pkfp == NULL) if (pkfp == NULL)
{ {
std::cerr << "Couldn't Open PrivKey File!" << std::endl; std::cerr << "Couldn't Open PrivKey File!" << std::endl;

View File

@ -265,8 +265,8 @@ bool p3ConfigMgr::backedUpFileSave(const std::string& fname, const std::string&
#endif #endif
// open file from which to collect buffer // open file from which to collect buffer
file = fopen(fname.c_str(), "rb"); file = RsDirUtil::rs_fopen(fname.c_str(), "rb");
sign = fopen(sign_fname.c_str(), "rb"); sign = RsDirUtil::rs_fopen(sign_fname.c_str(), "rb");
// if failed then create files // if failed then create files
if((file == NULL) || (sign == NULL)){ if((file == NULL) || (sign == NULL)){
@ -274,8 +274,8 @@ bool p3ConfigMgr::backedUpFileSave(const std::string& fname, const std::string&
std::cerr << "p3Config::backedUpFileSave() failed to open meta files " << fname << std::endl; std::cerr << "p3Config::backedUpFileSave() failed to open meta files " << fname << std::endl;
#endif #endif
file = fopen(fname.c_str(), "wb"); file = RsDirUtil::rs_fopen(fname.c_str(), "wb");
sign = fopen(sign_fname.c_str(), "wb"); sign = RsDirUtil::rs_fopen(sign_fname.c_str(), "wb");
if((file == NULL) || (sign == NULL)){ if((file == NULL) || (sign == NULL)){
std::cerr << "p3Config::backedUpFileSave() failed to open backup meta files" << fname_backup << std::endl; std::cerr << "p3Config::backedUpFileSave() failed to open backup meta files" << fname_backup << std::endl;
@ -319,8 +319,8 @@ bool p3ConfigMgr::backedUpFileSave(const std::string& fname, const std::string&
if((size_file) > 0 && (size_sign > 0)){ if((size_file) > 0 && (size_sign > 0)){
// now write actual back-up file // now write actual back-up file
file = fopen(fname_backup.c_str(), "wb"); file = RsDirUtil::rs_fopen(fname_backup.c_str(), "wb");
sign = fopen(sign_fname_backup.c_str(), "wb"); sign = RsDirUtil::rs_fopen(sign_fname_backup.c_str(), "wb");
if((file == NULL) || (sign == NULL)){ if((file == NULL) || (sign == NULL)){
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
@ -629,8 +629,8 @@ bool p3ConfigMgr::checkForGlobalSigConfig()
std::string fName = basedir + "/" + metafname; std::string fName = basedir + "/" + metafname;
std::string sigName = basedir + "/" + metasigfname; std::string sigName = basedir + "/" + metasigfname;
metaFile = fopen(fName.c_str(), "r"); metaFile = RsDirUtil::rs_fopen(fName.c_str(), "r");
metaSig = fopen(sigName.c_str(), "r"); metaSig = RsDirUtil::rs_fopen(sigName.c_str(), "r");
// check if files exist // check if files exist
if((metaFile != NULL) && (metaSig != NULL)) if((metaFile != NULL) && (metaSig != NULL))
@ -1060,7 +1060,7 @@ bool p3Config::backedUpFileSave(const std::string& cfg_fname, const std::string&
#endif #endif
// open file from which to collect buffer // open file from which to collect buffer
cfg_file = fopen(cfg_fname.c_str(), "rb"); cfg_file = RsDirUtil::rs_fopen(cfg_fname.c_str(), "rb");
// if it fails to open, create file,but back-up file will now be empty // if it fails to open, create file,but back-up file will now be empty
if(cfg_file == NULL){ if(cfg_file == NULL){
@ -1068,7 +1068,7 @@ bool p3Config::backedUpFileSave(const std::string& cfg_fname, const std::string&
std::cerr << "p3Config::backedUpFileSave() fopen failed for file: " << cfg_fname << std::endl; std::cerr << "p3Config::backedUpFileSave() fopen failed for file: " << cfg_fname << std::endl;
#endif #endif
cfg_file = fopen(cfg_fname.c_str(), "wb"); cfg_file = RsDirUtil::rs_fopen(cfg_fname.c_str(), "wb");
if(cfg_file == NULL) if(cfg_file == NULL)
{ {
@ -1116,7 +1116,7 @@ bool p3Config::backedUpFileSave(const std::string& cfg_fname, const std::string&
{ {
// now write actual back-up file // now write actual back-up file
cfg_file = fopen(cfg_fname_backup.c_str(), "wb"); cfg_file = RsDirUtil::rs_fopen(cfg_fname_backup.c_str(), "wb");
if(cfg_file == NULL){ if(cfg_file == NULL){
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG

View File

@ -23,13 +23,10 @@
* *
*/ */
#include "pqi/pqibin.h" #include "pqi/pqibin.h"
#include "pqi/authssl.h" #include "pqi/authssl.h"
#include "util/rsnet.h" #include "util/rsnet.h"
#include "util/rsdir.h"
// #define DEBUG_PQIBIN // #define DEBUG_PQIBIN
@ -40,23 +37,23 @@ BinFileInterface::BinFileInterface(const char *fname, int flags)
if ((bin_flags & BIN_FLAGS_READABLE) && if ((bin_flags & BIN_FLAGS_READABLE) &&
(bin_flags & BIN_FLAGS_WRITEABLE)) (bin_flags & BIN_FLAGS_WRITEABLE))
{ {
buf = fopen(fname, "rb+"); buf = RsDirUtil::rs_fopen(fname, "rb+");
/* if the file don't exist */ /* if the file don't exist */
if (!buf) if (!buf)
{ {
buf = fopen(fname, "wb+"); buf = RsDirUtil::rs_fopen(fname, "wb+");
} }
} }
else if (bin_flags & BIN_FLAGS_READABLE) else if (bin_flags & BIN_FLAGS_READABLE)
{ {
buf = fopen(fname, "rb"); buf = RsDirUtil::rs_fopen(fname, "rb");
} }
else if (bin_flags & BIN_FLAGS_WRITEABLE) else if (bin_flags & BIN_FLAGS_WRITEABLE)
{ {
// This is enough to remove old file in Linux... // This is enough to remove old file in Linux...
// but not in windows.... (what to do) // but not in windows.... (what to do)
buf = fopen(fname, "wb"); buf = RsDirUtil::rs_fopen(fname, "wb");
fflush(buf); /* this might help windows! */ fflush(buf); /* this might help windows! */
} }
else else
@ -430,7 +427,7 @@ uint64_t BinMemInterface::bytecount()
bool BinMemInterface::writetofile(const char *fname) bool BinMemInterface::writetofile(const char *fname)
{ {
FILE *fd = fopen(fname, "wb"); FILE *fd = RsDirUtil::rs_fopen(fname, "wb");
if (!fd) if (!fd)
{ {
return false; return false;
@ -449,7 +446,7 @@ bool BinMemInterface::writetofile(const char *fname)
bool BinMemInterface::readfromfile(const char *fname) bool BinMemInterface::readfromfile(const char *fname)
{ {
FILE *fd = fopen(fname, "rb"); FILE *fd = RsDirUtil::rs_fopen(fname, "rb");
if (!fd) if (!fd)
{ {
return false; return false;

View File

@ -804,7 +804,7 @@ continue_packet:
// if(pktlen == 17306) // if(pktlen == 17306)
// { // {
// FILE *f = fopen("dbug.packet.bin","w"); // FILE *f = RsDirUtil::rs_fopen("dbug.packet.bin","w");
// fwrite(block,pktlen,1,f) ; // fwrite(block,pktlen,1,f) ;
// fclose(f) ; // fclose(f) ;
// exit(-1) ; // exit(-1) ;

View File

@ -32,6 +32,7 @@
#include "pqi/sslfns.h" #include "pqi/sslfns.h"
#include "pqi/pqi_base.h" #include "pqi/pqi_base.h"
#include "util/rsdir.h"
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <openssl/err.h> #include <openssl/err.h>
@ -115,7 +116,7 @@ X509_REQ *GenerateX509Req(
// open the file. // open the file.
FILE *out; FILE *out;
if (NULL == (out = fopen(pkey_file.c_str(), "w"))) if (NULL == (out = RsDirUtil::rs_fopen(pkey_file.c_str(), "w")))
{ {
fprintf(stderr,"GenerateX509Req: Couldn't Create Key File!"); fprintf(stderr,"GenerateX509Req: Couldn't Create Key File!");
fprintf(stderr," : %s\n", pkey_file.c_str()); fprintf(stderr," : %s\n", pkey_file.c_str());
@ -144,7 +145,7 @@ X509_REQ *GenerateX509Req(
/********** Test Loading the private Key.... ************/ /********** Test Loading the private Key.... ************/
FILE *tst_in = NULL; FILE *tst_in = NULL;
EVP_PKEY *tst_pkey = NULL; EVP_PKEY *tst_pkey = NULL;
if (NULL == (tst_in = fopen(pkey_file.c_str(), "rb"))) if (NULL == (tst_in = RsDirUtil::rs_fopen(pkey_file.c_str(), "rb")))
{ {
fprintf(stderr,"GenerateX509Req() Couldn't Open Private Key"); fprintf(stderr,"GenerateX509Req() Couldn't Open Private Key");
fprintf(stderr," : %s\n", pkey_file.c_str()); fprintf(stderr," : %s\n", pkey_file.c_str());
@ -692,7 +693,7 @@ int LoadCheckX509(const char *cert_file, std::string &issuerName, std::string &l
* and checks the certificate * and checks the certificate
*/ */
FILE *tmpfp = fopen(cert_file, "r"); FILE *tmpfp = RsDirUtil::rs_fopen(cert_file, "r");
if (tmpfp == NULL) if (tmpfp == NULL)
{ {
#ifdef AUTHSSL_DEBUG #ifdef AUTHSSL_DEBUG

View File

@ -36,6 +36,7 @@
#include "util/rsdebug.h" #include "util/rsdebug.h"
#include "util/rsdir.h" #include "util/rsdir.h"
#include "util/rsrandom.h" #include "util/rsrandom.h"
#include "util/folderiterator.h"
#include "retroshare/rsinit.h" #include "retroshare/rsinit.h"
#include "rsserver/rsloginhandler.h" #include "rsserver/rsloginhandler.h"
@ -742,16 +743,20 @@ void RsInit::setupBaseDir()
// use directory "Data" in portable version // use directory "Data" in portable version
RsInitConfig::basedir = "Data"; RsInitConfig::basedir = "Data";
} else { } else {
char *h = getenv("APPDATA"); wchar_t *wh = _wgetenv(L"APPDATA");
std::string h;
librs::util::ConvertUtf16ToUtf8(std::wstring(wh), h);
std::cerr << "retroShare::basedir() -> $APPDATA = "; std::cerr << "retroShare::basedir() -> $APPDATA = ";
std::cerr << h << std::endl; std::cerr << h << std::endl;
char *h2 = getenv("HOMEDRIVE"); char *h2 = getenv("HOMEDRIVE");
std::cerr << "retroShare::basedir() -> $HOMEDRIVE = "; std::cerr << "retroShare::basedir() -> $HOMEDRIVE = ";
std::cerr << h2 << std::endl; std::cerr << h2 << std::endl;
char *h3 = getenv("HOMEPATH"); wchar_t *wh3 = _wgetenv(L"HOMEPATH");
std::string h3;
librs::util::ConvertUtf16ToUtf8(std::wstring(wh3), h3);
std::cerr << "retroShare::basedir() -> $HOMEPATH = "; std::cerr << "retroShare::basedir() -> $HOMEPATH = ";
std::cerr << h3 << std::endl; std::cerr << h3 << std::endl;
if (h == NULL) if (h.empty())
{ {
// generating default // generating default
std::cerr << "load_check_basedir() getEnv Error --Win95/98?"; std::cerr << "load_check_basedir() getEnv Error --Win95/98?";
@ -896,24 +901,34 @@ bool getAvailableAccounts(std::list<accountId> &ids)
* files checked to see if they have changed. (rehashed) * files checked to see if they have changed. (rehashed)
*/ */
struct dirent *dent;
struct stat buf;
/* check for the dir existance */ /* check for the dir existance */
DIR *dir = opendir(RsInitConfig::basedir.c_str()); librs::util::FolderIterator dirIt(RsInitConfig::basedir);
if (!dir) if (!dirIt.isValid())
{ {
std::cerr << "Cannot Open Base Dir - No Available Accounts" << std::endl; std::cerr << "Cannot Open Base Dir - No Available Accounts" << std::endl;
exit(1); exit(1);
} }
while(NULL != (dent = readdir(dir))) struct stat64 buf;
while (dirIt.readdir())
{ {
/* check entry type */ /* check entry type */
std::string fname = dent -> d_name; std::string fname;
dirIt.d_name(fname);
std::string fullname = RsInitConfig::basedir + "/" + fname; std::string fullname = RsInitConfig::basedir + "/" + fname;
#ifdef FIM_DEBUG
std::cerr << "calling stats on " << fullname <<std::endl;
#endif
#ifdef WINDOWS_SYS
std::wstring wfullname;
librs::util::ConvertUtf8ToUtf16(fullname, wfullname);
if (-1 != _wstati64(wfullname.c_str(), &buf))
#else
if (-1 != stat64(fullname.c_str(), &buf))
#endif
if (-1 != stat(fullname.c_str(), &buf))
{ {
#ifdef FIM_DEBUG #ifdef FIM_DEBUG
std::cerr << "buf.st_mode: " << buf.st_mode <<std::endl; std::cerr << "buf.st_mode: " << buf.st_mode <<std::endl;
@ -938,7 +953,8 @@ bool getAvailableAccounts(std::list<accountId> &ids)
} }
} }
} }
closedir(dir) ; /* close directory */
dirIt.closedir();
for(it = directories.begin(); it != directories.end(); it++) for(it = directories.begin(); it != directories.end(); it++)
{ {
@ -1266,7 +1282,7 @@ bool RsInit::GenerateSSLCertificate(std::string gpg_id, std::string org, std
/* Save cert to file */ /* Save cert to file */
// open the file. // open the file.
FILE *out = NULL; FILE *out = NULL;
if (NULL == (out = fopen(cert_name.c_str(), "w"))) if (NULL == (out = RsDirUtil::rs_fopen(cert_name.c_str(), "w")))
{ {
fprintf(stderr,"RsGenerateCert() Couldn't create Cert File"); fprintf(stderr,"RsGenerateCert() Couldn't create Cert File");
fprintf(stderr," : %s\n", cert_name.c_str()); fprintf(stderr," : %s\n", cert_name.c_str());
@ -1306,7 +1322,7 @@ bool RsInit::GenerateSSLCertificate(std::string gpg_id, std::string org, std
std::cerr << "Mv Config Dir from: " << tmpbase << " to: " << finalbase; std::cerr << "Mv Config Dir from: " << tmpbase << " to: " << finalbase;
std::cerr << std::endl; std::cerr << std::endl;
if (0 > rename(tmpbase.c_str(), finalbase.c_str())) if (!RsDirUtil::renameFile(tmpbase, finalbase))
{ {
std::cerr << "rename FAILED" << std::endl; std::cerr << "rename FAILED" << std::endl;
} }
@ -1502,7 +1518,7 @@ bool RsInit::get_configinit(std::string dir, std::string &id)
initfile += configInitFile; initfile += configInitFile;
// open and read in the lines. // open and read in the lines.
FILE *ifd = fopen(initfile.c_str(), "r"); FILE *ifd = RsDirUtil::rs_fopen(initfile.c_str(), "r");
char path[1024]; char path[1024];
int i; int i;
@ -1532,7 +1548,7 @@ bool RsInit::create_configinit(std::string dir, std::string id)
initfile += configInitFile; initfile += configInitFile;
// open and read in the lines. // open and read in the lines.
FILE *ifd = fopen(initfile.c_str(), "w"); FILE *ifd = RsDirUtil::rs_fopen(initfile.c_str(), "w");
if (ifd != NULL) if (ifd != NULL)
{ {

View File

@ -3,6 +3,7 @@
#include <retroshare/rsinit.h> #include <retroshare/rsinit.h>
#include <pqi/authgpg.h> #include <pqi/authgpg.h>
#include "rsloginhandler.h" #include "rsloginhandler.h"
#include "util/rsdir.h"
#ifdef UBUNTU #ifdef UBUNTU
#include <gnome-keyring-1/gnome-keyring.h> #include <gnome-keyring-1/gnome-keyring.h>
@ -195,7 +196,7 @@ bool RsLoginHandler::tryAutoLogin(const std::string& ssl_id,std::string& ssl_pas
/******************** OSX KeyChain stuff *****************************/ /******************** OSX KeyChain stuff *****************************/
#else /* UNIX, but not UBUNTU or APPLE */ #else /* UNIX, but not UBUNTU or APPLE */
FILE* helpFile = fopen(getAutologinFileName.c_str(), "r"); FILE* helpFile = RsDirUtil::rs_fopen(getAutologinFileName.c_str(), "r");
if(helpFile == NULL){ if(helpFile == NULL){
std::cerr << "\nFailed to open help file\n" << std::endl; std::cerr << "\nFailed to open help file\n" << std::endl;
@ -265,7 +266,7 @@ bool RsLoginHandler::tryAutoLogin(const std::string& ssl_id,std::string& ssl_pas
int datalen = 0; int datalen = 0;
/* open the data to the file */ /* open the data to the file */
FILE *fp = fopen(getAutologinFileName(ssl_id).c_str(), "rb"); FILE *fp = RsDirUtil::rs_fopen(getAutologinFileName(ssl_id).c_str(), "rb");
if (fp != NULL) if (fp != NULL)
{ {
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
@ -407,7 +408,7 @@ bool RsLoginHandler::enableAutoLogin(const std::string& ssl_id,const std::string
#else #else
/* WARNING: Autologin is inherently unsafe */ /* WARNING: Autologin is inherently unsafe */
FILE* helpFile = fopen(getAutologinFileName.c_str(), "w"); FILE* helpFile = RsDirUtil::rs_fopen(getAutologinFileName.c_str(), "w");
if(helpFile == NULL){ if(helpFile == NULL){
std::cerr << "\nRsStoreAutoLogin(): Failed to open help file\n" << std::endl; std::cerr << "\nRsStoreAutoLogin(): Failed to open help file\n" << std::endl;
@ -493,7 +494,7 @@ bool RsLoginHandler::enableAutoLogin(const std::string& ssl_id,const std::string
//std::cerr << std::endl; //std::cerr << std::endl;
/* save the data to the file */ /* save the data to the file */
FILE *fp = fopen(getAutologinFileName(ssl_id).c_str(), "wb"); FILE *fp = RsDirUtil::rs_fopen(getAutologinFileName(ssl_id).c_str(), "wb");
if (fp != NULL) if (fp != NULL)
{ {
fwrite(DataOut.pbData, 1, DataOut.cbData, fp); fwrite(DataOut.pbData, 1, DataOut.cbData, fp);
@ -599,7 +600,7 @@ bool RsLoginHandler::clearAutoLogin(const std::string& ssl_id)
std::string passwdfile = getAutologinFileName(ssl_id) ; std::string passwdfile = getAutologinFileName(ssl_id) ;
FILE *fp = fopen(passwdfile.c_str(), "wb"); FILE *fp = RsDirUtil::rs_fopen(passwdfile.c_str(), "wb");
if (fp != NULL) if (fp != NULL)
{ {
@ -626,7 +627,7 @@ bool RsLoginHandler::checkAndStoreSSLPasswdIntoGPGFile(const std::string& ssl_id
// //
std::cerr << "let's store the ssl Password into a pgp ecrypted file" << std::endl; std::cerr << "let's store the ssl Password into a pgp ecrypted file" << std::endl;
FILE *sslPassphraseFile = fopen(getSSLPasswdFileName(ssl_id).c_str(), "r"); FILE *sslPassphraseFile = RsDirUtil::rs_fopen(getSSLPasswdFileName(ssl_id).c_str(), "r");
if(sslPassphraseFile != NULL) // already have it. if(sslPassphraseFile != NULL) // already have it.
{ {
@ -634,7 +635,7 @@ bool RsLoginHandler::checkAndStoreSSLPasswdIntoGPGFile(const std::string& ssl_id
return true ; return true ;
} }
sslPassphraseFile = fopen(getSSLPasswdFileName(ssl_id).c_str(), "w"); sslPassphraseFile = RsDirUtil::rs_fopen(getSSLPasswdFileName(ssl_id).c_str(), "w");
if(sslPassphraseFile == NULL) if(sslPassphraseFile == NULL)
{ {
@ -674,7 +675,7 @@ bool RsLoginHandler::getSSLPasswdFromGPGFile(const std::string& ssl_id,std::stri
// Let's read the password from an encrypted file // Let's read the password from an encrypted file
// Let's check if there's a ssl_passpharese_file that we can decrypt with PGP // Let's check if there's a ssl_passpharese_file that we can decrypt with PGP
// //
FILE *sslPassphraseFile = fopen(getSSLPasswdFileName(ssl_id).c_str(), "r"); FILE *sslPassphraseFile = RsDirUtil::rs_fopen(getSSLPasswdFileName(ssl_id).c_str(), "r");
if (sslPassphraseFile == NULL) if (sslPassphraseFile == NULL)
{ {

View File

@ -2506,7 +2506,7 @@ uint32 TcpStream::int_rbytes()
static FILE *bc_fd = 0; static FILE *bc_fd = 0;
int setupBinaryCheck(std::string fname) int setupBinaryCheck(std::string fname)
{ {
bc_fd = fopen(fname.c_str(), "r"); bc_fd = RsDirUtil::rs_fopen(fname.c_str(), "r");
return 1; return 1;
} }

View File

@ -23,11 +23,9 @@
* *
*/ */
#include "util/rsdebug.h" #include "util/rsdebug.h"
#include "util/rsthreads.h" #include "util/rsthreads.h"
#include "util/rsdir.h"
#include <map> #include <map>
#include <stdio.h> #include <stdio.h>
@ -57,7 +55,7 @@ int setDebugCrashMode(const char *cfile)
RsStackMutex stack(logMtx); /******** LOCKED ****************/ RsStackMutex stack(logMtx); /******** LOCKED ****************/
crashfile = cfile; crashfile = cfile;
/* if the file exists - then we crashed, save it */ /* if the file exists - then we crashed, save it */
FILE *tmpin = fopen(crashfile.c_str(), "r"); FILE *tmpin = RsDirUtil::rs_fopen(crashfile.c_str(), "r");
if (tmpin) if (tmpin)
{ {
/* see how long it is */ /* see how long it is */
@ -71,7 +69,7 @@ int setDebugCrashMode(const char *cfile)
/* go back to the start */ /* go back to the start */
fseek(tmpin, 0, SEEK_SET); fseek(tmpin, 0, SEEK_SET);
FILE *tmpout = fopen(crashfile_save.c_str(), "w"); FILE *tmpout = RsDirUtil::rs_fopen(crashfile_save.c_str(), "w");
int da_size = 10240; int da_size = 10240;
char dataarray[da_size]; /* 10k */ char dataarray[da_size]; /* 10k */
unsigned int da_read = 0; unsigned int da_read = 0;
@ -131,7 +129,7 @@ int clearDebugCrashLog()
debugMode = RS_DEBUG_STDERR; debugMode = RS_DEBUG_STDERR;
/* just open the file, and then close */ /* just open the file, and then close */
FILE *tmpin = fopen(crashfile.c_str(), "w"); FILE *tmpin = RsDirUtil::rs_fopen(crashfile.c_str(), "w");
fclose(tmpin); fclose(tmpin);
return 1; return 1;
@ -147,7 +145,7 @@ int setDebugFile(const char *fname)
int locked_setDebugFile(const char *fname) int locked_setDebugFile(const char *fname)
{ {
if (NULL != (ofd = fopen(fname, "w"))) if (NULL != (ofd = RsDirUtil::rs_fopen(fname, "w")))
{ {
fprintf(stderr, "Logging redirected to %s\n", fname); fprintf(stderr, "Logging redirected to %s\n", fname);
debugMode = RS_DEBUG_LOGFILE; debugMode = RS_DEBUG_LOGFILE;

View File

@ -582,15 +582,8 @@ bool RsDirUtil::getFileHash(const std::string& filepath, std::string &hash, uint
unsigned char sha_buf[SHA_DIGEST_LENGTH]; unsigned char sha_buf[SHA_DIGEST_LENGTH];
unsigned char gblBuf[512]; unsigned char gblBuf[512];
#ifdef WINDOWS_SYS if (NULL == (fd = RsDirUtil::rs_fopen(filepath.c_str(), "rb")))
std::wstring filepathW;
librs::util::ConvertUtf8ToUtf16(filepath, filepathW);
if (NULL == (fd = _wfopen(filepathW.c_str(), L"rb")))
return false; return false;
#else
if (NULL == (fd = fopen64(filepath.c_str(), "rb")))
return false;
#endif
/* determine size */ /* determine size */
fseeko64(fd, 0, SEEK_END); fseeko64(fd, 0, SEEK_END);
@ -647,16 +640,16 @@ bool RsDirUtil::renameFile(const std::string& from, const std::string& to)
{ {
int loops = 0; int loops = 0;
#if defined(WIN32) || defined(MINGW) || defined(__CYGWIN__) #ifdef WINDOWS_SYS
#ifdef WIN_CROSS_UBUNTU std::wstring f;
std::wstring f,t ; librs::util::ConvertUtf8ToUtf16(from, f);
for(int i=0;i<from.size();++i) f.push_back(from[i]) ; std::wstring t;
for(int i=0;i<to.size();++i) t.push_back(to[i]) ; librs::util::ConvertUtf8ToUtf16(to, t);
while (!MoveFileEx(f.c_str(), t.c_str(), MOVEFILE_REPLACE_EXISTING))
#else #else
std::string f(from),t(to) ; std::string f(from),t(to) ;
#endif
while (!MoveFileExA(f.c_str(), t.c_str(), MOVEFILE_REPLACE_EXISTING))
#else
while (rename(from.c_str(), to.c_str()) < 0) while (rename(from.c_str(), to.c_str()) < 0)
#endif #endif
{ {
@ -736,6 +729,20 @@ bool RsDirUtil::createBackup (const std::string& sFilename, unsigned int nCount)
return true; return true;
} }
FILE *RsDirUtil::rs_fopen(const char* filename, const char* mode)
{
#ifdef WINDOWS_SYS
std::wstring wfilename;
librs::util::ConvertUtf8ToUtf16(filename, wfilename);
std::wstring wmode;
librs::util::ConvertUtf8ToUtf16(mode, wmode);
return _wfopen(wfilename.c_str(), wmode.c_str());
#else
return = fopen64(filename, mode);
#endif
}
#if 0 // NOT ENABLED YET! #if 0 // NOT ENABLED YET!
/************************* WIDE STRING ***************************/ /************************* WIDE STRING ***************************/
/************************* WIDE STRING ***************************/ /************************* WIDE STRING ***************************/

View File

@ -91,6 +91,7 @@ bool hashWideFile(std::wstring filepath,
bool getWideFileHash(std::wstring filepath, bool getWideFileHash(std::wstring filepath,
std::string &hash, uint64_t &size); std::string &hash, uint64_t &size);
FILE *rs_fopen(const char* filename, const char* mode);
} }