Exemple #1
0
char *loadPrefFile()
{
	if(!checkFileExist(SETTINGS_FILE) && checkFileExist(SETTINGS_FILE".tmp"))
		rename(SETTINGS_FILE".tmp", SETTINGS_FILE);

	size_t filesize = getFileSize(SETTINGS_FILE);

    if(filesize == 0)
		return NULL;
	
    char * output = calloc(filesize + 10, sizeof(char));
    if(output == NULL)
        return NULL;

	AESDecrypt(SETTINGS_PASSWORD, SETTINGS_FILE, output, OUTPUT_IN_MEMORY);

    if(output[0] != '<')
    {
#ifdef EXTENSIVE_LOGGING
		logR("Incorrect settings decryption: %s", output);
#else
		logR("Incorrect settings decryption");
#endif
		
        free(output);
		output = NULL;
    }

	return output;
}
Exemple #2
0
void dumpTagCat(TAG_VERBOSE * tags, uint nbTags, CATEGORY_VERBOSE * category, uint nbCat)
{
	MUTEX_LOCK(concurentColdUpdate);
	
	//Delete a temporary file if Rak crashed while working on it
	if(checkFileExist(WIP_TAG_DB))
		remove(WIP_TAG_DB);
	
	//Open the database
	sqlite3 * newDB = NULL;
	if(sqlite3_open(WIP_TAG_DB, &newDB) != SQLITE_OK)
	{
		logR("Couldn't open the temporary database");
		return;
	}
	else
		createTagsTable(newDB);
	
	//Dump the content using our helpers
	tagUpdateCachedEntryWithRequest(tagUpdateQuery(newDB, false), tags, nbTags);
	catUpdateCachedEntryWithRequest(catUpdateQuery(newDB, false), category, nbCat);
	
	//Closing the DB, then swapping the files
	sqlite3_close(newDB);
	
	remove(OLD_TAG_DB);
	rename(TAG_DB, OLD_TAG_DB);
	rename(WIP_TAG_DB, TAG_DB);
	
	MUTEX_UNLOCK(concurentColdUpdate);
}
Exemple #3
0
bool createTagsTable(sqlite3 * mainCache)
{
	if(mainCache == NULL)
	{
		logR("Initialization is incomplete!");
		return false;
	}
	
	sqlite3_stmt * request = createRequest(mainCache, "CREATE TABLE "TABLE_TAGS" ("DBNAMETOID(RDB_tagID)" INTEGER PRIMARY KEY NOT NULL, "DBNAMETOID(RDB_tagName)" TEXT NOT NULL); CREATE INDEX hopperIsGud ON "TABLE_TAGS"("DBNAMETOID(RDB_tagID)");");

	if(request == NULL || sqlite3_step(request) != SQLITE_DONE)
	{
		logR("Initialization error small");
		
		destroyRequest(request);

		return false;
	}
	
	destroyRequest(request);
	
	if((request = createRequest(mainCache, "CREATE TABLE "TABLE_CATEGORY" ("DBNAMETOID(RDB_CAT_ID)" INTEGER PRIMARY KEY NOT NULL, "DBNAMETOID(RDB_CAT_rootID)" INTEGER, "DBNAMETOID(RDB_CAT_name)" TEXT NOT NULL); CREATE INDEX gdbOverlldb ON "TABLE_CATEGORY"("DBNAMETOID(RDB_CAT_ID)", "DBNAMETOID(RDB_CAT_rootID)");")) == NULL || sqlite3_step(request) != SQLITE_DONE)
	{
		logR("Initialization error big");
		
		destroyRequest(request);
		
		return false;
	}
	
	destroyRequest(request);
	
	if((request = createRequest(mainCache, "CREATE TABLE "TABLE_TAG_VERSION" (`"VERSION_COLUMN_NAME"` INTEGER);")) == NULL || sqlite3_step(request) != SQLITE_DONE)
	{
		logR("Initialization error WTF");
		
		destroyRequest(request);
		
		return false;
	}
	
	destroyRequest(request);
	
	return true;
}
Exemple #4
0
bool openRarArchiveFromFile(ARCHIVE * archive, const char * path)
{
	archive->fileHandle = ar_open_file(path);
	if(archive->fileHandle == NULL)
	{
		logR("Couldn't open RAR archive's file");
		return false;
	}
	
	archive->archive = ar_open_rar_archive(archive->fileHandle);
	if(archive->archive == NULL)
	{
		logR("Couldn't open RAR archive");
		return false;
	}
	
	return true;
}
Exemple #5
0
void removeFromPref(char* flag)
{
    uint i = 0, length = 0, OBLength, CBLength;
    char *newPrefs = NULL, *prefs = NULL, *prefsBak, openingBracket[16], closingBracket[16];
	
	OBLength = (uint) snprintf(openingBracket, sizeof(openingBracket), "<%s>", flag);
	CBLength = (uint) snprintf(closingBracket, sizeof(closingBracket), "</%s>", flag);

    prefsBak = prefs = loadPrefFile();
    if(prefs == NULL)
    {
        remove(SETTINGS_FILE);
        return;
    }
	
    length = strlen(prefs);
    newPrefs = calloc(length + 1, sizeof(char));
    if(newPrefs == NULL)
    {
        free(prefs);
        return;
    }

    while(*prefs && i < length)
    {
		//Are we in front of the bracket we want to remove?
        if(!strncmp(prefs, openingBracket, OBLength))
        {
			//Skip the opening bracket, and start looking for the closing backet
            prefs += OBLength - 1;
            while(strncmp(++prefs, closingBracket, CBLength));
			
			//Look for the next opening backet
			prefs += CBLength - 1;
            while(*++prefs && *prefs != '<');
        }
        else
            newPrefs[i++] = *prefs++;
    }
	
#ifdef EXTENSIVE_LOGGING
	if(i == 0)
	{
		logR("Uh? Deleting everything, WTF...");
	}
#endif
	
	newPrefs[i] = 0;
    AESEncrypt(SETTINGS_PASSWORD, newPrefs, SETTINGS_FILE".tmp", INPUT_IN_MEMORY);
	remove(SETTINGS_FILE);
	rename(SETTINGS_FILE".tmp", SETTINGS_FILE);

    free(newPrefs);
    free(prefsBak);
}
// once we're no longer in a sig handler this is called by Loop.cpp
// if g_log.needsPrinting() is true
void Log::printBuf ( ) {
	// not in sig handler
	if ( g_inSigHandler ) return;
	// was there a problem?
	if ( s_problem  ) fprintf(stderr,"Log::printBuf: had problem. '%c'\n",
				  s_problem);
	// or overflow?
	if ( s_overflow ) fprintf(stderr,"Log::printBuf: had overflow.\n");
	// point to the buf
	char *p    = s_buf;
	char *pend = s_ptr;
	// reset everything
	s_overflow      = false;
	s_problem       = '\0';
	m_needsPrinting = false;
	// bail if nothing to print, maybe first msg overflowed?
	if ( s_buf == s_ptr ) return;
	// we cannot be interrupted in here
	//bool flipped = false;
	//if ( g_interruptsOn ) {
	//	flipped = true;
	//	g_loop.interruptsOff();
	//}
	// reset buffer here
	s_ptr           = s_buf;
	// now print all log msgs we got while in a signal handler
 loop:
	// sanity check
	if ( p + 8 > pend ) {
		fprintf(stderr,"Had error in Log.cpp: breech of log buffer4.");
		s_ptr = s_buf;
		return;
	}
	// first 4 bytes are the size of the string arguments
	long stringSizes;
	memcpy ( (char *)&stringSizes , p , 4 );
	p += 4;
	// then the type of the msg
	long type;
	memcpy ( (char *)&type , p , 4 );
	p += 4;
	// then the format string
	char *format = p;
	// its length including the \0
	//long flen = gbstrlen ( format ) + 1;
	long flen = 0;
	char *q = format;
	while ( q < pend && *q ) q++;
	if ( q >= pend ) {
		fprintf(stderr,"Had error in Log.cpp: breech of log buffer3.");
		s_ptr = s_buf;
		return;
	}
	flen = q - p + 1;
	p += flen;
	// skip the string arguments now
	p += stringSizes;
	// sanity check
	if ( p + 8 + 4 > pend || p < s_buf ) {
		fprintf(stderr,"Had error in Log.cpp: breech of log buffer2.");
		s_ptr = s_buf;
		return;
	}
	// get time
	long long now ;
	memcpy ( (char *)&now , p , 8 );
	p += 8;
	// get size of args
	long apsize ;
	memcpy ( (char *)&apsize , p , 4 );
	p += 4;
	// dword align
	long rem = ((unsigned long)p) % 4;
	if ( rem > 0 ) p +=  4 - rem;
	// get va_list... needs to be word aligned!!
	va_list ap = (char*)(void*)p;
	p += apsize;
	// . sanity check
	// . i've seen this happen a lot lately since i started logging cancel
	//   acks perhaps?
	if ( p > pend || p < s_buf ) {
		fprintf(stderr,"Had error in Log.cpp: breech of log buffer.");
		s_ptr = s_buf;
		return;
	}
	// print msg into this buf
	char buf[1024*4];
	// print it into our buf now
	vsnprintf ( buf , 1024*4 , format , ap );
	// pass buf to g_log
	logR ( now , type , buf , true );
	// if not done loop back
	if ( p < pend ) goto loop;
	// turn 'em back on if we turned them off
	//if ( flipped ) g_loop.interruptsOn();
}
Exemple #7
0
sqlite3* getPtrRecentDB()
{
	sqlite3 * internalDB = NULL;
	bool initialCheckRequired = !recentMutexInitialized;
	
	if(!recentMutexInitialized)
	{
		MUTEX_CREATE(recentMutex);
		recentMutexInitialized = true;
	}
	
	MUTEX_LOCK(recentMutex);
	
	if(sqlite3_open("recent.db", &internalDB) != SQLITE_OK)
	{
		logR("Couldn't open the recent database, abort :(");
		internalDB = NULL;
	}
	else if(initialCheckRequired)
	{
		uint retValue = checkRecentDBValid(internalDB);
		
		if(retValue != RECENT_CHECK_RETVAL_OK)
		{
			if(retValue & RECENT_CHECK_RETVAL_INVALID_PROJ)
			{
				//On la détruit, et on recrée
				sqlite3_stmt * request = createRequest(internalDB, "DROP TABLE IF EXISTS `"PROJECT_TABLE"`");
				sqlite3_step(request);
				destroyRequest(request);
				
				request = createRequest(internalDB, "CREATE TABLE "PROJECT_TABLE" ("DBNAMETOID(RDB_REC_lastRead)" INTEGER, "DBNAMETOID(RDB_REC_lastDL)" INTEGER, "DBNAMETOID(RDB_REC_repo)" INTEGER, "DBNAMETOID(RDB_REC_projectID)" INTEGER, "DBNAMETOID(RDB_isLocal)" INTEGER);");
				if(request == NULL || sqlite3_step(request) != SQLITE_DONE)
				{
					destroyRequest(request);
					sqlite3_close(internalDB);
					internalDB = NULL;
				}
				else
					destroyRequest(request);
			}
			
			if(internalDB != NULL && retValue & RECENT_CHECK_RETVAL_INVALID_STATE)
			{
				//On la détruit, et on recrée
				sqlite3_stmt * request = createRequest(internalDB, "DROP TABLE IF EXISTS `"STATE_TABLE"`");
				sqlite3_step(request);
				destroyRequest(request);
				
				request = createRequest(internalDB, "CREATE TABLE "STATE_TABLE" ("DBNAMETOID(RDB_REC_repo)" INTEGER, "DBNAMETOID(RDB_REC_projectID)" INTEGER, "DBNAMETOID(RDB_isLocal)" INTEGER, "DBNAMETOID(RDB_REC_lastIsTome)" INTEGER, "DBNAMETOID(RDB_REC_lastCTID)" INTEGER, "DBNAMETOID(RDB_REC_lastPage)" INTEGER, "DBNAMETOID(RDB_REC_wasLastPageOfCT)" INTEGER, "DBNAMETOID(RDB_REC_lastZoom)" FLOAT, "DBNAMETOID(RDB_REC_lastScrollerX)" FLOAT, "DBNAMETOID(RDB_REC_lastScrollerY)" FLOAT, "DBNAMETOID(RDB_REC_lastChange)" INTEGER, PRIMARY KEY("DBNAMETOID(RDB_REC_repo)", "DBNAMETOID(RDB_REC_projectID)", "DBNAMETOID(RDB_isLocal)", "DBNAMETOID(RDB_REC_lastIsTome)")) WITHOUT ROWID;");
				if(request == NULL || sqlite3_step(request) != SQLITE_DONE)
				{
					destroyRequest(request);
					sqlite3_close(internalDB);
					internalDB = NULL;
				}
				else
					destroyRequest(request);
			}
		}
	}
	
	if(internalDB == NULL)
		MUTEX_UNLOCK(recentMutex);
	
	return internalDB;
}
bool MDLInstallation(void *buf, size_t sizeBuf, PROJECT_DATA *projectDB, uint chapitre, uint tome, bool subFolder, bool haveToPutTomeAsReadable)
{
    bool wentFine = true;
    char temp[600], basePath[500], *encodedPath = getPathForProject(*projectDB);
	
	if(encodedPath == NULL)
		return true;
	
    /*Récupération des valeurs envoyés*/
	
    if(tome != INVALID_VALUE)
    {
		if(subFolder)
		{
			if(chapitre % 10)
				snprintf(basePath, 500, PROJECT_ROOT"%s/"VOLUME_PREFIX"%u/"CHAPTER_PREFIX"%u.%u/", encodedPath, tome, chapitre / 10, chapitre % 10);
			else
				snprintf(basePath, 500, PROJECT_ROOT"%s/"VOLUME_PREFIX"%u/"CHAPTER_PREFIX"%u/", encodedPath, tome, chapitre / 10);
		}
		else
		{
			if(chapitre % 10)
				snprintf(basePath, 500, PROJECT_ROOT"%s/"VOLUME_PREFIX"%u/"VOLUME_PRESHARED_DIR"/"CHAPTER_PREFIX"%u.%u/", encodedPath, tome, chapitre / 10, chapitre % 10);
			else
				snprintf(basePath, 500, PROJECT_ROOT"%s/"VOLUME_PREFIX"%u/"VOLUME_PRESHARED_DIR"/"CHAPTER_PREFIX"%u/", encodedPath, tome, chapitre / 10);
		}
    }
    else
    {
        if(chapitre % 10)
            snprintf(basePath, 500, PROJECT_ROOT"%s/"CHAPTER_PREFIX"%u.%u/", encodedPath, chapitre / 10, chapitre % 10);
        else
            snprintf(basePath, 500, PROJECT_ROOT"%s/"CHAPTER_PREFIX"%u/", encodedPath, chapitre / 10);
    }
	
    snprintf(temp, 600, "%s/"CONFIGFILE, basePath);
    if(!checkFileExist(temp))
    {
		//Décompression dans le repertoire de destination
		
        mkdirR(basePath);
        if(!checkDirExist(basePath))
            createPath(basePath);
		
        //On crée un message pour ne pas lire un chapitre en cours d'install
        char installingFile[600];
        snprintf(installingFile, sizeof(installingFile), "%sinstalling", basePath);
        FILE* ressources = fopen(installingFile, "w+");

		if(ressources != NULL)
            fclose(ressources);
		
        wentFine &= decompressChapter(buf, sizeBuf, basePath, *projectDB, chapitre / 10);

		remove(installingFile);
		
		if(wentFine && haveToPutTomeAsReadable)
			setTomeReadable(*projectDB, tome);

		if(!subFolder && !wentFine)
		{
			logR("Archive Corrompue: %ls - %d - %d", projectDB->repo->name, projectDB->projectID, chapitre);
			removeFolder(basePath);
		}
    }

	if(wentFine)
	{
		//Add a flag signaling the file wasn't read yet
		if(tome != INVALID_VALUE)
			snprintf(basePath, 500, PROJECT_ROOT"%s/"VOLUME_PREFIX"%u/", encodedPath, tome);
		
		finishInstallationAtPath(basePath);
	}

	free(encodedPath);
    return wentFine;
}
bool MDLTelechargement(DATA_MOD_DL* input, uint currentPos, uint nbElem)
{
    bool output = false;
	int ret_value = CODE_RETOUR_OK;
	uint i;
	char firstTwentyBytesOfArchive[20];
	
    /**Téléchargement**/
    TMP_DL dataDL;
    dataDL.URL = MDL_craftDownloadURL(*input->todoList);
	
    if(dataDL.URL == NULL)
    {
        logR("Memory error");
        ret_value = CODE_RETOUR_INTERNAL_FAIL;
    }
    else
    {
        do
        {
            dataDL.buf = calloc(1, sizeof(DATA_DL_OBFS));
			
			//La structure est supposée contenir un double pointeur mais ici un triple
            ret_value = downloadChapter(&dataDL, input->todoList, currentPos, nbElem);
            free(dataDL.URL);
			
			if(ret_value != CODE_RETOUR_OK)
			{
				if(dataDL.buf != NULL)
				{
					free(((DATA_DL_OBFS *) dataDL.buf)->data);
					free(((DATA_DL_OBFS *) dataDL.buf)->mask);
					free(dataDL.buf);
				}
                if(ret_value != CODE_RETOUR_DL_CLOSE)
                    output = true;
				
				break;
			}
			
			for(i = 0; i < 19 && dataDL.buf != NULL && ((DATA_DL_OBFS *) dataDL.buf)->data != NULL && ((DATA_DL_OBFS *) dataDL.buf)->mask != NULL; i++)
				firstTwentyBytesOfArchive[i] = ~(((DATA_DL_OBFS *) dataDL.buf)->data[i] ^ ((DATA_DL_OBFS *) dataDL.buf)->mask[i]);
			firstTwentyBytesOfArchive[i] = 0;
			
            if(dataDL.length < 50 && dataDL.buf != NULL && isPaidProject(*input->todoList->datas))
            {
                /*Output du RSP, à gérer*/
#ifdef EXTENSIVE_LOGGING
                logR("Begins with %s", firstTwentyBytesOfArchive);
#endif
                if(dataDL.buf != NULL)
                {
                    if(!strcmp(firstTwentyBytesOfArchive, "invalid_data") || !strcmp(firstTwentyBytesOfArchive, "internal_error") ||
                       !strcmp(firstTwentyBytesOfArchive, "bad_login_infos") || !strcmp(firstTwentyBytesOfArchive, "token_invalid"))
                    {
                        free(((DATA_DL_OBFS *) dataDL.buf)->data);
                        free(((DATA_DL_OBFS *) dataDL.buf)->mask);
                        free(dataDL.buf);
						dataDL.buf = NULL;
						
                        dataDL.URL = MDL_craftDownloadURL(*input->todoList);
                        continue;
                    }
                    free(((DATA_DL_OBFS *) dataDL.buf)->data);
					free(((DATA_DL_OBFS *) dataDL.buf)->mask);
					free(dataDL.buf);
					dataDL.buf = NULL;
                }
                output = true;
            }
			
			//If some data are missing, or if this isn't a valid zip archive, and if this is not a redirection
            else if(dataDL.buf == NULL || ((DATA_DL_OBFS *) dataDL.buf)->data == NULL || ((DATA_DL_OBFS *) dataDL.buf)->mask == NULL || dataDL.length < 50 || (!isZIP(firstTwentyBytesOfArchive) && strncmp(firstTwentyBytesOfArchive, "http://", 7) && strncmp(firstTwentyBytesOfArchive, "https://", 8)))
            {
                if(dataDL.buf != NULL)
				{
					free(((DATA_DL_OBFS *) dataDL.buf)->data);
					free(((DATA_DL_OBFS *) dataDL.buf)->mask);
					free(dataDL.buf);
					dataDL.buf = NULL;
				}
                if(ret_value != CODE_RETOUR_DL_CLOSE)
                    output = true;
            }
			
			//Redirection
            else if(!strncmp(firstTwentyBytesOfArchive, "http://", 7) || !strncmp(firstTwentyBytesOfArchive, "https://", 8))
            {
                //Redirection
				dataDL.URL = malloc(dataDL.length + 10);
				if(dataDL.URL == NULL)
				{
					output = true;
				}
				else
				{
					for(i = 0; i < dataDL.length; i++)
						dataDL.URL[i] = ~((DATA_DL_OBFS *) dataDL.buf)->data[i] ^ ((DATA_DL_OBFS *) dataDL.buf)->mask[i];
					dataDL.URL[i] = 0;
					continue;
				}
            }
            else // Archive pas corrompue, installation
            {
                input->buf = dataDL.buf;
                input->length = dataDL.current_pos;
            }
        }while(0);
    }
	
    if(ret_value == CODE_RETOUR_INTERNAL_FAIL)
    {
        output = true;
    }
    return output;
}
Exemple #10
0
void initializeTags(void * mainCache)
{
	MUTEX_CREATE(concurentColdUpdate);
	
	sqlite3 * coldDB = NULL;
	
	//Create the tables in the main cache
	createTagsTable(mainCache);
	
	if((!checkFileExist(TAG_DB) || sqlite3_open(TAG_DB , &coldDB) != SQLITE_OK) && (!checkFileExist(WIP_TAG_DB) || sqlite3_open(WIP_TAG_DB, &coldDB) != SQLITE_OK))
	{
		//Error, we should reset it with the version we ship with then trigger an update
		resetTagsToLocal();
		
		if(!checkFileExist(TAG_DB) || sqlite3_open(TAG_DB , &coldDB) != SQLITE_OK)
		{
			alertExit("We have significant issues setting up our environment, this may be caused by permission issues, please contact us at [email protected]");
		}
	}
	
	sqlite3_stmt * requestRead, *requestWrite;
	
	//Build the tag base
	if((requestRead = createRequest(coldDB, "SELECT "DBNAMETOID(RDB_tagID)", "DBNAMETOID(RDB_tagName)" FROM "TABLE_TAGS)) != NULL)
	{
		requestWrite = tagUpdateQuery(mainCache, false);
		
		if(requestWrite != NULL)
		{
			while(sqlite3_step(requestRead) == SQLITE_ROW)
			{
				sqlite3_bind_int(requestWrite, 1, sqlite3_column_int(requestRead, 0));
				sqlite3_bind_text(requestWrite, 2, (void *) sqlite3_column_text(requestRead, 1), -1, SQLITE_STATIC);
				
				if(sqlite3_step(requestWrite) != SQLITE_DONE)
				{
#ifdef EXTENSIVE_LOGGING
					uint ID = (uint32_t) sqlite3_column_int(requestRead, 0);
					const unsigned char * text = sqlite3_column_text(requestRead, 1);
					
					if(text == NULL)
						logR("Error building the tag DB for ID %d: no text!", ID);
					else
						logR("Error building the tag DB for ID %d of text %s!", ID, text);
#endif
				}
				sqlite3_reset(requestWrite);
			}
			destroyRequest(requestWrite);
		}
		destroyRequest(requestRead);
	}
	
	//Build the category base
	if((requestRead = createRequest(coldDB, "SELECT "DBNAMETOID(RDB_CAT_ID)", "DBNAMETOID(RDB_CAT_rootID)", "DBNAMETOID(RDB_CAT_name)" FROM "TABLE_CATEGORY)) != NULL)
	{
		requestWrite = catUpdateQuery(mainCache, false);
		
		if(requestWrite != NULL)
		{
			
			while(sqlite3_step(requestRead) == SQLITE_ROW)
			{
				sqlite3_bind_int(requestWrite, 1, sqlite3_column_int(requestRead, 0));
				sqlite3_bind_int(requestWrite, 2, sqlite3_column_int(requestRead, 1));
				sqlite3_bind_text(requestWrite, 3, (void *) sqlite3_column_text(requestRead, 2), -1, SQLITE_STATIC);
				
				for(byte i = 0; i < 32; i++)
				{
					sqlite3_bind_int(requestWrite, i + 4, sqlite3_column_int(requestRead, i + 3));
				}
				
				if(sqlite3_step(requestWrite) != SQLITE_DONE)
				{
#ifdef EXTENSIVE_LOGGING
					uint ID = (uint32_t) sqlite3_column_int(requestRead, 0);
					const unsigned char * text = sqlite3_column_text(requestRead, 2);
					
					if(text == NULL)
						logR("Error building the category DB for ID %d: no text!", ID);
					else
						logR("Error building the category DB for ID %d of text %s!", ID, text);
#endif
				}
				sqlite3_reset(requestWrite);
			}
			destroyRequest(requestWrite);
		}
		destroyRequest(requestRead);
	}
	sqlite3_close(coldDB);
}