bool CServerSession::OnLogOn( CNcpMessage* pMsg, int nBufSize ) { char path[MAXPATH]; //parse parameters int nFileMode = pMsg->GetRet(); string strUser = (char*)pMsg->GetData(); string strPass = (char*)( pMsg->GetData()+strUser.size()+1 ); //get home path #ifdef _WIN32 GetModuleFileName( NULL, path, ELEMENTS(path) ); char* p = strrchr( path, '\\' ); if( p!=NULL )*p = '\0'; #else getcwd( path, ELEMENTS(path) ); #endif CatDir( path, strUser.c_str(), path, ELEMENTS(path) ); //now path is the current home path. //make home directory if necessary int nRet = S_OK; if( !IsDirExist(path) ) { if( MkDir( path )!=0 ) { nRet = E_ACCES; } } m_strHomeDir = path; m_strCurDir = m_strHomeDir; m_nFileMode = nFileMode; if( SUCCEEDED(nRet) && !IsDirExist(m_strHomeDir.c_str()) ) { nRet = E_NOUSER; } pMsg->Init( CM_ACK, nRet ); pMsg->Send( m_sock ); //switch established if( SUCCEEDED(nRet) ) { m_state = NCPS_ESTABLISHED; if( strcmp( strUser.c_str(), "root" )==0 )m_bRootUsr = true; ASSERT( m_pServApp ); //lock the same user logon from other hosts. m_pLocker = m_pServApp->GetSessionLocker( strUser.c_str() ); m_pLocker->Lock(); } return SUCCEEDED(nRet); }
void Log::logToErrFile(const char* log) { if(m_log_line==0 || m_err_log_file==NULL) { if(m_err_log_file) fclose(m_err_log_file); m_err_log_file=NULL; //日期变了,重设文件名 char new_log_file_name[50] = {0}; if(!IsDirExist("log")) MkDir("log"); std::string log_time=StringReplace(StrTimeStamp(), ":", "_"); sprintf_s(new_log_file_name, sizeof(new_log_file_name), "log\\error_%s.log", log_time.c_str()); fopen_s(&m_err_log_file, new_log_file_name, "a"); if(NULL==m_err_log_file) THROWNEXCEPT("create file:%s error.", new_log_file_name); } size_t count=strlen(log); size_t write_count=fwrite(log, sizeof(char), count, m_err_log_file); if(write_count!=count) THROWNEXCEPT("log to file error."); }
bool MakeDir(const OJString &path) { if (!IsDirExist(path)) { fs::path fullpath(path); sys::error_code err; bool res = fs::create_directory(fullpath, err); if (!(sys::errc::success == err)) { ILogger *logger = LoggerFactory::getLogger(LoggerId::AppInitLoggerId); OJString msg(GetOJString("[filetool] - IMUST::MakeDir - make dir failed: ")); msg += path; msg += GetOJString(" - "); msg += String2OJString(err.message()); logger->logError(msg); return false; } return res; } return true; }
bool MkDir(const char *pszName, mode_t mode) { if(IsDirExist(pszName)) { return 0; } if(IsFileExist(pszName)) { return -2; } const char *p = pszName; if(*p == '/') { p++; } std::string strDir; while(*p != 0) { while((*p != '/') && (*p != 0)) { p++; } strDir.assign(pszName, p - pszName); if(false == IsDirExist(strDir.c_str())) { if(-1 == mkdir(strDir.c_str(),mode)) { return -1; } } if(*p == 0) { break; } p++; } return 0; }
bool PathUtil::EnsureDir(const String &dir) { if(!IsDir(dir)) return false; if(!IsDirExist(dir)) { if(!CreateDir(dir)) return false; } return true; }
int main(int argc,char *argv[]) { if( 2 != argc ) { printf("%s param\n",argv[0]); return -1; } char *path = argv[1]; if(0 == IsDirExist(path)){ printf("path %s is dir\n", path); } else{ printf("path %s is not dir\n", path); } return 0; }
bool PathUtil::CreateDir(const String &dir) { std::vector<std::string> paths; char seps[] = _T("/\\"); char pathBuf[MAX_PATHNAME]; char *token = NULL; memset(pathBuf, 0, MAX_PATHNAME); memcpy(pathBuf, dir.c_str(), dir.length() * sizeof(tchar)); token = strtok(pathBuf, seps); std::string tempDir; while( token != NULL ) { tempDir = token; if(!IsDriveOrRoot(tempDir)) { paths.push_back(tempDir); } token = strtok(NULL, seps); } tempDir = StringUtil::ConvMBS(GetDriveOrRoot(dir)); for(size_t i=0; i < paths.size(); ++i) { tempDir += paths[i]; tempDir += SEPERATOR; if(!IsDirExist(tempDir)) { #if (LORD_PLATFORM == LORD_PLATFORM_WINDOWS) if(mkdir(tempDir.c_str()) != 0) #else if(mkdir(tempDir.c_str(), S_IRWXU|S_IRWXG|S_IRWXO) != 0) #endif return false; } } return true; }
bool CServerSession::OnChDir( CNcpMessage* pMsg, int nBufSize ) { char path[MAXPATH]; char* pNewDir = (char*)pMsg->GetData(); int nRetCode = S_OK; if( strcmp(pNewDir, "~")==0 ) { m_strCurDir = m_strHomeDir; } else { //make the full path if( !IsAbsDir(pNewDir) ) { //this is a relative directory CatDir( m_strCurDir.c_str(), pNewDir, path, ELEMENTS(path) ); } else { //this is a absolute directory strcpy( path, pNewDir ); } if( IsDirExist(path) ) { NormDir( path, path, ELEMENTS(path) ); //the path must be in the home directory if( IsSubDir(path, m_strHomeDir.c_str()) ) { m_strCurDir = path; } else { nRetCode = E_ACCES; } } else { nRetCode = E_NOENT; } } pMsg->Init( CM_ACK, nRetCode ); pMsg->Send( m_sock ); return SUCCEEDED( nRetCode ); }
int erase_dir( const char* path, bool rm_cur ) { #ifdef _WIN32 char buf[_MAX_PATH]; dir_cat( path, "*.*", buf, ELEMENTS(buf) ); struct _finddata_t fileinfo; intptr_t hfind = _findfirst( buf, &fileinfo ); if( hfind==-1 ){ errno = ENOENT; return -1; } do{ char path_file[_MAX_PATH]; dir_cat( path, fileinfo.name, path_file, ELEMENTS(path_file) ); if( fileinfo.attrib & FILE_ATTRIBUTE_DIRECTORY ){ if( strcmp(fileinfo.name,".")!=0 && strcmp(fileinfo.name, "..")!=0 ){ if( erase_dir( path_file, true )!=0 ){ //clear the children path _findclose( hfind ); return -1; } } }else{ if( remove(path_file)!=0 ){ _findclose( hfind ); return -1; } } }while( _findnext( hfind, &fileinfo )==0 ); _findclose( hfind ); #else DIR* dirp = opendir(path); if( dirp==NULL ){ errno = ENOENT; return -1; } struct dirent * dp = readdir(dirp); while( dp ){ char path_file[_MAX_PATH]; dir_cat( path, dp->d_name, path_file, ELEMENTS(path_file) ); if( IsDirExist(path_file) ){ if( strcmp(dp->d_name, ".")!=0 && strcmp(dp->d_name, "..")!=0 ){ if( erase_dir( path_file, true )!=0 ){ closedir(dirp); return -1; } } }else{ if( remove(path_file)!=0 ){ closedir(dirp); return -1; } } dp = readdir(dirp); } closedir(dirp); #endif if( rm_cur ){ return rmdir( path ); } return 0; }
BOOL IsFileExist(LPCTSTR szFilePath) { return IsExist(szFilePath) && !IsDirExist(szFilePath); }
void WriteApplicationFile(char *mappname,char* MessageString) { try { char mmCurentDir[512]; SYSTEMTIME systime; char mdate[255]={0}; char mmdate[255]={0}; char mtime[255]={0}; char mmFileName[512]={0}; bool isFileExist; char mlogtype[1024]={0}; bool isPerDeleted=false; //lstrcpy(mmCurentDir , mAPPLOG); //strcpy(mmCurentDir,SAAZODSetUpPath); lstrcat(mmCurentDir ,"ApplicationLog\\"); //strcat(mmCurentDir , "\\Log"); GetLocalTime(&systime); sprintf(mdate , "%04d-%02d-%02d" ,systime.wYear, systime.wDay , systime.wMonth ); sprintf(mmdate , "%04d%02d%02d" ,systime.wYear, systime.wDay , systime.wMonth); //GetTimeFormat(LOCALE_SYSTEM_DEFAULT , NULL , &systime , "hh':'mm':'ss" , mtime , 255); sprintf(mtime , "%02d:%02d:%02d" , systime.wHour, systime.wMinute , systime.wSecond); sprintf(mmFileName , "%s\\%s.Log" , mmCurentDir , mappname); FILE *fp=NULL; if(IsDirExist(mmFileName) == false) { isFileExist = false; fp=fopen( mmFileName ,"w+"); //for wiping out the contents _fcloseall( ); } else { isFileExist = true; } DWORD dwFileSize = IsFile2MB(mmFileName);// Check for 2 MB (2097152 Bytes) if(dwFileSize >= 2097152) { isPerDeleted = true; isFileExist = false; fp=fopen( mmFileName ,"w+"); //For wiping out the contents _fcloseall( ); } fp = fopen( mmFileName , "a"); if(!(fp == NULL)) { if (isFileExist == false) { fprintf(fp , "#Software: %s\n#Version: 1.0 \n#Date: %s %s\n# \n" , "zSmart.exe", mdate , mtime); fprintf(fp , "----------------------------------------------------------------------\n" ); //fprintf(fp , "#Fields:\tdtime\tSession Id\tSession User Name\tmodule\n"); if(isPerDeleted == true) { fprintf(fp , "Previous content is deleted. Due to the size limit of 2 MB\n" ); fprintf(fp , "----------------------------------------------------------------------\n" ); } } fprintf(fp , "%s %s\t%s \n" , mdate , mtime , MessageString); } else { } _fcloseall( ); } catch(...) { } }
/* convert normal file path to secure storage file path */ int ConvertFileName(int sender_pid, char* dest, const char* src, ssm_flag flag, const char* group_id) { char* if_pointer = NULL; unsigned short h_code = 0; unsigned short h_code2 = 0; unsigned char path_hash[SHA_DIGEST_LENGTH + 1]; char s[33+1]; const char* dir = NULL; char tmp_cmd[32] = {0, }; char tmp_buf[10] = {0, }; const unsigned char exe_path[256] = {0, }; FILE* fp_proc = NULL; char* preserved_dir = NULL; int is_dir_exist = -1; if(!dest || !src) { SLOGE( "[%s] Parameter error in ConvertFileName()...\n", __func__); return SS_FILE_OPEN_ERROR; // file related error } memset(tmp_cmd, 0x00, 32); snprintf(tmp_cmd, 32, "/proc/%d/cmdline", sender_pid); if(!(fp_proc = fopen(tmp_cmd, "r"))) { SLOGE("[%s] file open error: [%s]", __func__, tmp_cmd); return SS_FILE_OPEN_ERROR; } fgets((char*)exe_path, 256, fp_proc); fclose(fp_proc); if(!strncmp(group_id, "NOTUSED", 7)) // don't share { h_code2 = GetHashCode(exe_path); memset(tmp_buf, 0x00, 10); snprintf(tmp_buf, 10, "%u", h_code2); dir = tmp_buf; } else // share dir = group_id; if_pointer = strrchr(src, '/'); if(flag == SSM_FLAG_DATA) // /opt/share/secure-storage/* { // check whether directory is exist or not is_dir_exist = IsDirExist(SS_STORAGE_DEFAULT_PATH); if (is_dir_exist == 0) // SS_STORAGE_FILE_PATH is not exist { SLOGI("[%s] directory [%s] is making now.\n", __func__, SS_STORAGE_DEFAULT_PATH); if(mkdir(SS_STORAGE_DEFAULT_PATH, 0700) < 0) // fail to make directory { SLOGE("[%s] [%s] cannot be made\n", __func__, SS_STORAGE_DEFAULT_PATH); return SS_FILE_OPEN_ERROR; } } else if (is_dir_exist == -1) // Unknown error { SLOGE("[%s] Unknown error in the function IsDirExist().\n", __func__); return SS_PARAM_ERROR; } // TBD strncpy(dest, SS_STORAGE_DEFAULT_PATH, MAX_FILENAME_LEN - 1); strncat(dest, dir, (strlen(dest) - 1)); strncat(dest, "/", 1); // make directory dest[strlen(SS_STORAGE_DEFAULT_PATH) + strlen(dir) + 2] = '\0'; is_dir_exist = IsDirExist(dest); if(is_dir_exist == 0) // not exist { SLOGI("[%s] %s is making now.\n", __func__, dest); if(mkdir(dest, 0700) < 0) // fail to make directory { SLOGE("[%s] [%s] cannot be made\n", __func__, dest); return SS_FILE_OPEN_ERROR; } } strncat(dest, if_pointer + 1, strlen(if_pointer) + 1); strncat(dest, "_", 1); SHA1((unsigned char*)src, (size_t)strlen(src), path_hash); h_code = GetHashCode(path_hash); memset(s, 0x00, 34); snprintf(s, 34, "%u", h_code); strncat(dest, s, strlen(s)); strncat(dest, SS_FILE_POSTFIX, strlen(SS_FILE_POSTFIX)); dest[strlen(SS_STORAGE_DEFAULT_PATH) + strlen(dir) + strlen(if_pointer) + strlen(s) + strlen(SS_FILE_POSTFIX) + 4] = '\0'; } else if(flag == SSM_FLAG_SECRET_PRESERVE) // /tmp/csa/ { preserved_dir = get_preserved_dir(); if(preserved_dir == NULL) // fail to get preserved directory { SLOGE("[%s] fail to get preserved dir\n", __func__); return SS_FILE_OPEN_ERROR; } if(strncmp(src, preserved_dir, strlen(preserved_dir)) == 0) //src[0] == '/') { strncpy(dest, src, MAX_FILENAME_LEN - 1); strncat(dest, SS_FILE_POSTFIX, strlen(SS_FILE_POSTFIX)); dest[strlen(src) + strlen(SS_FILE_POSTFIX)] = '\0'; } else if(if_pointer != NULL) // absolute path == file { strncpy(dest, preserved_dir, MAX_FILENAME_LEN - 1); strncat(dest, if_pointer + 1, strlen(if_pointer) + 1); strncat(dest, SS_FILE_POSTFIX, strlen(SS_FILE_POSTFIX)); dest[strlen(preserved_dir) + strlen(if_pointer) + strlen(SS_FILE_POSTFIX) + 1] = '\0'; } else // relative path == buffer { strncpy(dest, preserved_dir, MAX_FILENAME_LEN - 1); strncat(dest, src, strlen(src)); strncat(dest, SS_FILE_POSTFIX, strlen(SS_FILE_POSTFIX)); dest[strlen(preserved_dir) + strlen(src) + strlen(SS_FILE_POSTFIX)] = '\0'; } free(preserved_dir); } else if(flag == SSM_FLAG_SECRET_OPERATION) // /opt/share/secure-storage/ { if(if_pointer != NULL) // absolute path == input is a file { // check whether directory is exist or not is_dir_exist = IsDirExist(SS_STORAGE_DEFAULT_PATH); if (is_dir_exist == 0) // SS_STORAGE_FILE_PATH is not exist { SLOGI("[%s] %s is making now.\n", __func__, SS_STORAGE_DEFAULT_PATH); if(mkdir(SS_STORAGE_DEFAULT_PATH, 0700) < 0) // fail to make directory { SLOGE("[%s] [%s] cannnot be made\n", __func__, SS_STORAGE_DEFAULT_PATH); return SS_FILE_OPEN_ERROR; } } else if (is_dir_exist == -1) // Unknown error { SLOGE("[%s] Unknown error in the function IsDirExist().\n", __func__); return SS_PARAM_ERROR; } strncpy(dest, SS_STORAGE_DEFAULT_PATH, MAX_FILENAME_LEN - 1); strncat(dest, dir, strlen(dir)); strncat(dest, "/", 1); // make directory dest[strlen(SS_STORAGE_DEFAULT_PATH) + strlen(dir) + 2] = '\0'; is_dir_exist = IsDirExist(dest); if(is_dir_exist == 0) // not exist { SLOGI("[%s] %s is making now.\n", __func__, dest); if(mkdir(dest, 0700) < 0) { SLOGE("[%s] [%s] cannot be made\n", __func__, dest); return SS_FILE_OPEN_ERROR; } } strncat(dest, if_pointer + 1, strlen(if_pointer) + 1); strncat(dest, "_", 1); SHA1((unsigned char*)src, (size_t)strlen(src), path_hash); h_code = GetHashCode(path_hash); memset(s, 0x00, 34); snprintf(s, 34, "%u", h_code); strncat(dest, s, strlen(s)); strncat(dest, SS_FILE_POSTFIX, strlen(SS_FILE_POSTFIX)); dest[strlen(SS_STORAGE_DEFAULT_PATH) + strlen(dir) + strlen(if_pointer) + strlen(s) + strlen(SS_FILE_POSTFIX) + 4] = '\0'; } else // relative path == input is a buffer { // check whether directory is exist or not is_dir_exist = IsDirExist(SS_STORAGE_DEFAULT_PATH); if (is_dir_exist == 0) // SS_STORAGE_BUFFER_PATH is not exist { SLOGI("[%s] %s is making now.\n", __func__, SS_STORAGE_DEFAULT_PATH); if(mkdir(SS_STORAGE_DEFAULT_PATH, 0700) < 0) { SLOGE("[%s] [%s] cannot be made\n", __func__, SS_STORAGE_DEFAULT_PATH); return SS_FILE_OPEN_ERROR; } } else if (is_dir_exist == -1) // Unknown error { SLOGE("[%s] Unknown error in the function IsDirExist().\n", __func__); return SS_PARAM_ERROR; } strncpy(dest, SS_STORAGE_DEFAULT_PATH, MAX_FILENAME_LEN - 1); strncat(dest, dir, strlen(dir)); strncat(dest, "/", 1); // make directory dest[strlen(SS_STORAGE_DEFAULT_PATH) + strlen(dir) + 2] = '\0'; is_dir_exist = IsDirExist(dest); if(is_dir_exist == 0) // not exist { SLOGI("[%s] %s is making now.\n", __func__, dest); if(mkdir(dest, 0700) < 0) { SLOGE("[%s] [%s] cannot be made\n", __func__, dest); return SS_FILE_OPEN_ERROR; } } strncat(dest, src, strlen(src)); strncat(dest, SS_FILE_POSTFIX, strlen(SS_FILE_POSTFIX)); dest[strlen(SS_STORAGE_DEFAULT_PATH) + strlen(dir) + strlen(src) + strlen(SS_FILE_POSTFIX) + 2] = '\0'; } } else { SLOGE("[%s] flag mispatch. cannot convert file name.\n", __func__); return SS_PARAM_ERROR; } return 1; }