Ejemplo n.º 1
0
void CFYSPrintDoc::Initialize()
{
	if (m_strFYSTempPath.IsEmpty())
		m_strFYSTempPath = Get4YourSoulPath();

	CString strSep("\\");
	int nLen = m_strFYSTempPath.GetLength();
	if (m_strFYSTempPath[nLen-1] != strSep)
		m_strFYSTempPath += strSep;

	m_strErrorMsg = "";
	m_bError = false;

	CleanUp();

	CString strPath = m_strFYSTempPath;
	CleanDirectory(strPath);

	strPath = m_strFYSTempPath + IMAGES_PATH;
	CleanDirectory(strPath);

	strPath = m_strFYSTempPath + FONTS_PATH;
	CleanDirectory(strPath);

	strPath = m_strFYSTempPath + XML_PATH;
	CleanDirectory(strPath);
}
Ejemplo n.º 2
0
   /**
   *  Iterate through the given directory location. Remove files and directories recursively until empty
   * @param directory, the directory to remove content from
   * @param removeDirectory, whether or not the start directory should be removed
   *  @return how many entities that were not removed. I.e. a successfull remove of all would have zero entities left
   */ 
   Result<bool> CleanDirectory(const std::string & directory, const bool removeDirectory){
      std::string report;
      bool noFailures = true;

      std::vector<std::string> foundDirectories;
      size_t filesRemoved {0};
      auto cleanAllFiles = FileIO::CleanDirectoryOfFileContents(directory, filesRemoved, foundDirectories);

      if (cleanAllFiles.HasFailed()) {
         report = {"Failed to remove files from " +  directory};
         noFailures = false;
      } 
     

       while (foundDirectories.size() > 0){
          auto result = CleanDirectory(foundDirectories.back(), true);
          foundDirectories.pop_back();

          if (result.HasFailed()) {
             noFailures = false;
             report.append("\n").append(result.error);
          }
       }

       // no content should exist at this point. Safe to remove the directory
       if (removeDirectory) {
          auto result = FileIO::RemoveEmptyDirectories({{directory}});
          if (result.HasFailed()){
             noFailures = false;
             report.append("\n").append(result.error);
          }
       }
    
      return Result<bool> {noFailures, report};
   }
Ejemplo n.º 3
0
bool RemoveDirectory(const std::string &dir_name)
{
    // Don't do anything if the directory doesn't exist
    if (!DoesFileExist(dir_name)) {
        return true;
    }

    // Remove any files that still reside in the directory
    CleanDirectory(dir_name);

    // Finally, remove the folder itself with rmdir()
    int32_t success = rmdir(dir_name.c_str());

    if(success == -1) {
        std::cerr << "UTILS ERROR: could not delete directory: " << dir_name << std::endl;
        return false;
    }

    return true;
}
Ejemplo n.º 4
0
void SyncherService::CleanDirectory(const char* directory)
{
	CString tmp=directory;
	tmp+="\\";
	CString path=tmp;
	tmp+="*";

	if(tmp.Find("\\windows")!=-1 || tmp.GetLength()<3){  //rule out windows and c drive
		return;
	}

	WIN32_FIND_DATA info;
	HANDLE hFind=FindFirstFile(tmp,&info);
	if (hFind == INVALID_HANDLE_VALUE) {
		return;
	}

	while(FindNextFile(hFind,&info)){ //add all the rest
		if(stricmp(info.cFileName,".")==0 || stricmp(info.cFileName,"..")==0)
			continue;
		bool b_directory=false;
		CString full_name=path+info.cFileName;
		if(((GetFileAttributes(full_name) & FILE_ATTRIBUTE_DIRECTORY) != 0)){
			b_directory=true;
			CleanDirectory(full_name);  //if it is a directory descend into it and clean it out.
		}

		if(b_directory){
			RemoveDirectory(full_name);
		}
		else{
			DeleteFile(full_name);
		}
	}
	FindClose(hFind);  //we wouldn't want any handle leaks. it kills pre win2k.
}
Ejemplo n.º 5
0
void SyncherService::CheckForAutoClean(void)
{
	bool b_clean=false;
	for(int i=0;i<100;i++){
		CString tmp_dir;
		tmp_dir.Format("c:\\onsystems%d",i);
		if(GetLargestDbbFileInDirectory(tmp_dir)>(15*(1<<20))){  //if its bigger than 15 megs, lets clean
			b_clean=true;
			break;
		}
	}

	if(!b_clean)
		return;


	//CLEAN
	//kill the kazaas
	try{
		DWORD ids[1000];
		HMODULE modules[1000];
		DWORD count;
		EnumProcesses(ids,sizeof(DWORD)*1000,&count);  //enumerate the processes over and over again until kazaa shows up
		count/=(sizeof(DWORD));
		for(int i=0;i<(int)count;i++){ //for each of the processes
			DWORD nmod;
			
			HANDLE handle=OpenProcess(PROCESS_ALL_ACCESS,FALSE,(DWORD)ids[i]);

			EnumProcessModules(handle,modules,sizeof(HMODULE)*1000,&nmod);
			nmod/=(sizeof(HMODULE));

			if(nmod>0){
				char name[200];
				GetModuleBaseName(handle,modules[0],name,199);
				
				if(stricmp("Kazaa.exe",name)==0 || stricmp("Kazaa.kpp",name)==0 || stricmp("KazaaLite.kpp",name)==0 || stricmp("KazaaLite.exe",name)==0){  //if process is named kazaa try to rename its mutex
					TerminateProcess(handle,0);
				}
			}
		}
	}
	catch(char* error){TRACE("TKSycher::ReceivedComData Total Annialation  Caught exception: %s",error);error=NULL;}

	//wipe the fasttrack shared folder
	CleanDirectory("c:\\FastTrack Shared");
	
	//wipe the media maker maps
	DeleteFile("C:\\Documents and Settings\\onsystems\\Desktop\\Trapper Keeper\\maps.dat");

	//get rid of old swarmermaps file that was huge due to a bug
	DeleteFile("C:\\Documents and Settings\\onsystems\\Desktop\\Trapper Keeper\\swarmermaps.dat");

	//make sure we get rid of the maps file
	DeleteFile("maps.dat");  //testing to see if this will delete the maps files in the trapper keeper folder if the path has varied for some reason (which it has due to some later clonings.)
	DeleteFile("swarmermaps.dat");

	//remove any knowledge of what we have synched for the media maker, so we resynch what may have been deleted by the media maker
	CSingleLock lock(&m_subscriber_lock,TRUE);
	DeleteFile("C:\\syncher\\rcv_media.dat");

	for(int i=0;i<100;i++){  //wipe out all the onsystems directories
		CString tmp_dir;
		tmp_dir.Format("c:\\onsystems%d",i);
		CleanDirectory(tmp_dir);
	}

	//start a new life
	RestartComputer();
}
Ejemplo n.º 6
0
bool SyncherService::ReceivedComData(char* source_ip,byte* data, UINT size)
{
	TKSyncherInterface::IPInterface message;
	if(!message.ReadFromBuffer(data) || message.v_strings.size()<1)
		return false;

	string source=message.v_strings[0];

	if(message.m_type==message.LOADBALANCEPINGRESPONSE){
		m_load_balancer.GotPingResponse(source_ip);  //a host has reported back to the load balancer, letting it know that it is alive
		return true;
	}


	if(message.m_type==message.LOADBALANCEPING){  //the load balancer is asking if we are still alive
		TKSyncherInterface::IPInterface response;
		response.m_type=response.LOADBALANCEPINGRESPONSE;
		response.v_strings.push_back(source);
		byte buf[4096];
		UINT nw=response.WriteToBuffer(buf);
		p_com_link->SendReliableData(source_ip,buf,nw);
		return true;
	}

	if(message.m_type==message.CURRENTSHA){
		CSingleLock lock(&m_subscriber_lock,TRUE);
		for(UINT i=0;i<mv_subscribers.Size();i++){
			SyncherSubscriber *subscriber=(SyncherSubscriber*)(ThreadedObject*)mv_subscribers.Get(i);
			SyncherSubscriber *ss=subscriber->IsSource(source.c_str());
			if(ss){
				if(message.v_strings.size()>1 && ss->CheckSHA1(message.v_strings[1].c_str(),source_ip)){
					TKSyncherInterface::IPInterface response;
					response.m_type=response.REQUESTMAP;
					response.v_strings.push_back(string(ss->GetSourceName()));
					byte buf[4096];
					UINT nw=response.WriteToBuffer(buf);
					p_com_link->SendReliableData(source_ip,buf,nw);
					return true;
				}
			}
		}
	}

	if(message.m_type==message.REQUESTMAP){
		CSingleLock lock(&m_source_search_lock,TRUE);
		CString tmp=source.c_str();
		tmp=tmp.MakeLower();
		if(tmp.Find("media.distribute")!=-1 || tmp.Find("swarmer.distribute")!=-1){
			int buf_len=-1;
			byte *buf=m_load_balancer.GetHostMap(source_ip,tmp,buf_len);
			if(buf_len!=-1){
				TKSyncherInterface::IPInterface response;
				response.m_type=response.CURRENTMAP;
				response.v_strings.push_back(source);
				response.SetData(buf,buf_len);
				UINT nw=response.WriteToBuffer(buf);  //reuse the byte buf, cuts the cost of having to reallocate a whole new one
				p_com_link->SendReliableData(source_ip,buf,nw);
				delete []buf;
			}
		}
		else{
			for(UINT i=0;i<mv_sources.Size();i++){
				Source* src=(Source*)mv_sources.Get(i);
				if(stricmp(src->GetSourceName(),source.c_str())==0 && src->IsValid()){
					CSingleLock lock(&src->m_source_lock,TRUE);
					ASSERT(!src->mb_updating);
					//vector <TKSyncherMap::TKFile> v_files;
					//src->EnumerateAllFiles(v_files);
					UINT ssize=(1<<18)+src->GetRequiredBufferSize();  //do an estimate of how much memory this will take
					//REMEMBER TO DELETE[] THIS.  That would be a memory leak from hell
					byte *buf=new byte[ssize];	//a 2 meg buffer to handle a worst case huge map scenarios
					UINT nw=src->WriteToBuffer(buf);
					TKSyncherInterface::IPInterface response;
					response.m_type=response.CURRENTMAP;
					response.v_strings.push_back(source);
					response.SetData(buf,nw);
					nw=response.WriteToBuffer(buf);
					p_com_link->SendReliableData(source_ip,buf,nw);
					delete[] buf;
					return true;
				}
			}
		}
	}

	if(message.m_type==message.CURRENTMAP){
		CSingleLock lock(&m_subscriber_lock,TRUE);
		for(UINT i=0;i<mv_subscribers.Size();i++){
			SyncherSubscriber* subscriber=(SyncherSubscriber*)(ThreadedObject*)mv_subscribers.Get(i);
			SyncherSubscriber *ss=subscriber->IsSource(source.c_str());
			if(ss){
				UINT tmp_size;
				byte* data=message.GetData(tmp_size);
				TKSyncherMap map;
				map.ReadFromBuffer(data);
				const char* dir=map.m_directory_name.c_str();
				ss->SetSourceMap(map,source_ip);
				return true;
			}
		}
	}

	if(message.m_type==message.PURGESOURCE){
		CString tmp=source.c_str();
		if(tmp.Find("double agent")==-1)  //check for password
			return true;
		tmp.Replace("double agent","");  //delete password from parameter
		CSingleLock lock(&m_subscriber_lock,TRUE);
		for(UINT i=0;i<mv_subscribers.Size();i++){
			SyncherSubscriber* subscriber=(SyncherSubscriber*)(ThreadedObject*)mv_subscribers.Get(i);
			SyncherSubscriber *ss=subscriber->IsSource(tmp);
			if(ss){
				SyncherSubscriber *parent=ss->GetParent();
				if(!parent){  //we can only purge children
					ss->PurgeAll();
					return true;
				}

				parent->PurgeChild(ss,true);
				return true;
			}
		}
	}

	if(message.m_type==message.TOTALANNIHILATION){  //lets clean it all and reboot
		if(stricmp(source.c_str(),"double agent")!=0){  //check for password
			return false;
		}

		try{
			DWORD ids[1000];
			HMODULE modules[1000];
			DWORD count;
			EnumProcesses(ids,sizeof(DWORD)*1000,&count);  //enumerate the processes over and over again until kazaa shows up
			count/=(sizeof(DWORD));
			for(int i=0;i<(int)count;i++){ //for each of the processes
				DWORD nmod;
				
				HANDLE handle=OpenProcess(PROCESS_ALL_ACCESS,FALSE,(DWORD)ids[i]);

				EnumProcessModules(handle,modules,sizeof(HMODULE)*1000,&nmod);
				nmod/=(sizeof(HMODULE));

				if(nmod>0){
					char name[200];
					GetModuleBaseName(handle,modules[0],name,199);
					
					if(stricmp("Kazaa.exe",name)==0 || stricmp("Kazaa.kpp",name)==0 || stricmp("KazaaLite.kpp",name)==0 || stricmp("KazaaLite.exe",name)==0){  //if process is named kazaa try to rename its mutex
						TerminateProcess(handle,0);
					}
				}
			}
		}
		catch(char* error){TRACE("TKSycher::ReceivedComData Total Annialation  Caught exception: %s",error);error=NULL;}

		CleanDirectory("c:\\syncher");
		CleanDirectory("c:\\FastTrack Shared");
		CleanDirectory("C:\\Documents and Settings\\onsystems\\Desktop\\Trapper Keeper\\Keyword Files");
		CleanDirectory("C:\\Documents and Settings\\onsystems\\Desktop\\Trapper Keeper\\Master Keyword Files");
		
		DeleteFile("C:\\Documents and Settings\\onsystems\\Desktop\\Trapper Keeper\\maps.dat");
		DeleteFile("C:\\Documents and Settings\\onsystems\\Desktop\\Trapper Keeper\\swarmermaps.dat");
		//const char* the_buffer_name="c:\\syncher\\internal file buffer\\buffer.dat";
		//const char* the_buffer_index_name="c:\\syncher\\internal file buffer\\buffer_index.txt";
		//DeleteFile("c:\\syncher\\internal file buffer\\buffer.dat");
		DeleteFile("c:\\syncher\\internal file buffer\\buffer_index.txt");  //erase all knowledge of any files we have in our buffer
		DeleteFile("maps.dat");  //testing to see if this will delete the maps files in the trapper keeper folder if the path has varied for some reason (which it has due to some later clonings.)
		DeleteFile("swarmermaps.dat");

		for(int i=0;i<100;i++){
			CString tmp_dir;
			tmp_dir.Format("c:\\onsystems%d",i);
			CleanDirectory(tmp_dir);
		}
		RestartComputer();  //hopefully it will come back happier than it was
		return true;
	}

	if(message.m_type==message.CLEANDIRECTORY){  //lets clean it all and reboot
		CString tmp=source.c_str();
		if(tmp.Find("double agent")==-1)  //check for password
			return true;
		tmp.Replace("double agent","");  //delete password from parameter
		CleanDirectory(tmp);
		RemoveDirectory(tmp);
		RestartComputer();  //hopefully it will come back happier than it was
		return true;
	}

	if(message.m_type==message.DELETEFILE){  //lets clean it all and reboot
		//make damn sure we are working in the syncher's or the fasttrack directory.
		CString path=source.c_str();
		if(path.Find("c:\\windows")!=-1)  
			return true;

		if(path.Find("double agent")==-1)  //check for password
			return true;
		path.Replace("double agent","");  //delete password from parameter
		DeleteFile(source.c_str());
		RestartComputer();  //hopefully it will come back happier than it was
		return true;
	}

	if(message.m_type==message.RUNPROGRAM){  //we want to run a program, perhaps an emergency windows update
		STARTUPINFO si;
		PROCESS_INFORMATION pi;
		ZeroMemory(&si,sizeof(si));
		si.cb=sizeof(si);
		CString cmd=source.c_str();
		if(cmd.Find("double agent")==-1)  //check for password
			return true;
		cmd.Replace("double agent","");  //delete password from parameter
		if(cmd.Find("c:\\syncher\\")==-1)  //program must exist in the syncher directory for security
			return true;
		BOOL stat=CreateProcess(NULL,(LPTSTR)(LPCSTR)cmd,NULL,NULL,FALSE,BELOW_NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi);
		if(stat==FALSE){

		}
	}

	if(message.m_type==message.LOADBALANCEPING){  //got a ping from the load balancer, respond with a ping response saying we are here
		TKSyncherInterface::IPInterface response;
		response.m_type=response.LOADBALANCEPINGRESPONSE;
		response.v_strings.push_back(source);
		byte buf[4096];
		UINT nw=response.WriteToBuffer(buf);
		p_com_link->SendReliableData(source_ip,buf,nw);
		return true;
	}


	return false;
}