void buffered_string_benchmark(void){ //basic test to see if BufStringAdd is working at all, should print out <123456abcdefg> BufString *a = BufStringNewSize(10/*initial size*/); BufStringAdd(a, "123456"); BufStringAdd(a, "abcdefg"); DEBUG("Test <%s>\n", a->bs_Buffer); for (unsigned int i = 0; i < 25; i++){ struct timeval start_time, end_time; gettimeofday(&start_time, NULL); BufString *b = BufStringNewSize(10/*initial size*/); for (unsigned int j = 0; j < 100000000; j++){ BufStringAdd(b, "123456790"); } // DEBUG("Total buffer size %d", b->bs_Size); gettimeofday(&end_time, NULL); struct timeval duration; timeval_subtract(&duration, &end_time, &start_time); DEBUG("Iteration %d execution time %ld.%04lds\n", i, duration.tv_sec, duration.tv_usec/1000); BufStringDelete(b); } exit(1); }
BufString *Info( File *s, const char *path ) { DEBUG("Info!\n"); BufString *bs = BufStringNew(); int spath = strlen( path ); int rspath = strlen( s->f_Path ); DEBUG("Info!\n"); // user is trying to get access to not his directory DEBUG("Check access for path '%s' in root path '%s' name '%s'\n", path, s->f_Path, s->f_Name ); if( strncmp( path, s->f_Path, rspath ) != 0 ) { BufStringAdd( bs, "{ \"ErrorMessage\": \"No access to file\"}" ); } else { struct stat ls; if( stat( path, &ls ) == 0 ) { FillStat( bs, &ls, s, path ); } else { BufStringAdd( bs, "{ \"ErrorMessage\": \"File or directory do not exist\"}" ); } } DEBUG("Info END\n"); return bs; }
BufString *Info( File *s, const char *path ) { DEBUG("Info!\n"); BufString *bs = BufStringNew(); int spath = strlen( path ); int rspath = strlen( s->f_Path ); BufStringAdd( bs, "ok<!--separate-->"); DEBUG("Info!\n"); // user is trying to get access to not his directory DEBUG("Check access for path '%s' in root path '%s' name '%s'\n", path, s->f_Path, s->f_Name ); int doub = strlen( s->f_Name ); char *comm = NULL; if( ( comm = calloc( rspath + spath + 512, sizeof(char) ) ) != NULL ) { strcpy( comm, s->f_Path ); if( comm[ strlen( comm ) -1 ] != '/' ) { strcat( comm, "/" ); } strcat( comm, &(path[ doub+2 ]) ); DEBUG("PATH created %s\n", comm ); struct stat ls; if( stat( comm, &ls ) == 0 ) { FillStat( bs, &ls, s, comm ); } else { BufStringAdd( bs, "{ \"ErrorMessage\": \"File or directory do not exist\"}" ); } free( comm ); } DEBUG("Info END\n"); return bs; }
BufString *Call( File *s, const char *path, Http *request ) { DEBUG("Info!\n"); BufString *bs = BufStringNew(); BufStringAdd( bs, "fail<!--separate-->"); DEBUG("Info END\n"); return bs; }
void FillStat( BufString *bs, INRAMFile *nf, File *d, const char *path ) { char tmp[ 1024 ]; /* int rootSize = 0; if( d->f_Path != NULL ) { rootSize = strlen( d->f_Path ); } */ int pathSize = strlen( path ); //DEBUG("FILLSTAT path '%s' rootpath '%s' %d\n", path, d->f_Path, path[ strlen( d->f_Path ) ] ); BufStringAdd( bs, "{" ); snprintf( tmp, 1023, " \"Filename\":\"%s\",",nf->nf_Name ); BufStringAdd( bs, tmp ); //DEBUG( "FILLSTAT filename set\n"); /* if( rootSize != pathSize ) { if( nf->nf_Type == INRAM_DIR ) { sprintf( tmp, "\"Path\":\"%s:%s/\",", d->f_Name, &path[ strlen( d->f_Path ) ] ); } else { sprintf( tmp, "\"Path\":\"%s:%s\",", d->f_Name, &path[ strlen( d->f_Path ) ] ); } } else */ { sprintf( tmp, "\"Path\":\"%s\",", path );//d->f_Name ); BufStringAdd( bs, tmp ); } //DEBUG( "FILLSTAT fullname set\n"); if( nf->nf_Type == INRAM_DIR ) { BufStringAdd( bs, "\"Filesize\":\"0\", " ); BufStringAdd( bs, "\"MetaType\":\"Directory\",\"Type\":\"Directory\" }" ); } else { sprintf( tmp, "\"Filesize\": %d,",(int) nf->nf_Data->bs_Size ); BufStringAdd( bs, tmp ); BufStringAdd( bs, "\"MetaType\":\"File\",\"Type\":\"File\" }" ); } //DEBUG( "FILLSTAT END\n"); }
void FillStat( BufString *bs, struct stat *s, File *d, const char *path ) { char tmp[ 1024 ]; int rootSize = strlen( d->f_Path ); int pathSize = strlen( path ); //DEBUG("FILLSTAT path '%s' rootpath '%s' %d\n", path, d->f_Path, path[ strlen( d->f_Path ) ] ); BufStringAdd( bs, "{" ); sprintf( tmp, " \"Filename\":\"%s\",", GetFileName( path ) ); BufStringAdd( bs, tmp ); //DEBUG( "FILLSTAT filename set\n"); if( rootSize != pathSize ) { if( S_ISDIR( s->st_mode ) ) { sprintf( tmp, "\"Path\":\"%s:%s/\",", d->f_Name, &path[ strlen( d->f_Path ) ] ); } else { sprintf( tmp, "\"Path\":\"%s:%s\",", d->f_Name, &path[ strlen( d->f_Path ) ] ); } } else { sprintf( tmp, "\"Path\":\"%s:\",", d->f_Name ); } //DEBUG( "FILLSTAT fullname set\n"); BufStringAdd( bs, tmp ); sprintf( tmp, "\"Filesize\": %d,",(int) s->st_size ); BufStringAdd( bs, tmp ); //DEBUG( "FILLSTAT filesize set\n"); if( S_ISDIR( s->st_mode ) ) { BufStringAdd( bs, "\"MetaType\":\"Directory\",\"Type\":\"Directory\" }" ); } else { BufStringAdd( bs, "\"MetaType\":\"File\",\"Type\":\"File\" }" ); } //DEBUG( "FILLSTAT END\n"); }
BufString *Info( File *s, const char *path ) { DEBUG("[INRAM] Info!\n"); BufString *bs = BufStringNew(); BufStringAdd( bs, "ok<!--separate-->"); DEBUG("Info!\n"); // user is trying to get access to not his directory DEBUG("Check access for path '%s' in root path '%s' name '%s'\n", path, s->f_Path, s->f_Name ); int error = 0; SpecialData *srd = (SpecialData *) s->f_SpecialData; INRAMFile *dir =INRAMFileGetLastPath( srd->root, path, &error ); if( dir != NULL ) { FillStat( bs, dir, s, path ); } else { DEBUG("[INRAM] file stat FAIL %s\n", path ); SpecialData *locsd = (SpecialData *)s->f_SpecialData; SystemBase *l = (SystemBase *)locsd->sb; char buffer[ 256 ]; int size = snprintf( buffer, sizeof(buffer), "{ \"response\": \"%s\", \"code\":\"%d\" }", l->sl_Dictionary->d_Msg[DICT_FILE_OR_DIRECTORY_DO_NOT_EXIST] , DICT_FILE_OR_DIRECTORY_DO_NOT_EXIST ); BufStringAddSize( bs, buffer, size ); //BufStringAdd( bs, "{ \"response\": \"File or directory do not exist\"}" ); } DEBUG("[INRAM] Info END\n"); return bs; }
BufString *Dir( File *s, const char *path ) { BufString *bs = BufStringNew(); DEBUG("\n\n\n\n\n\n"); DEBUG("Dir!\n"); int error = 0; // user is trying to get access to not his directory SpecialData *srd = (SpecialData *) s->f_SpecialData; INRAMFile *dir =INRAMFileGetLastPath( srd->root, path, &error ); DEBUG("Path received, pointer to: %p path %s!\n", dir, path ); if( dir != NULL ) { int pos = 0; BufStringAdd( bs, "ok<!--separate-->"); BufStringAdd( bs, "[" ); INRAMFile *f = dir->nf_Children; // temporary solution, must be fixed char tempString[ 4096 ]; int plen = strlen( path ) - 1; DEBUG("going through children\n"); while ( f != NULL ) { if( plen > 0 && path[ plen ] == '/' ) { sprintf( tempString, "%s:%s%s",s->f_Name, path, f->nf_Name ); } else { sprintf( tempString, "%s:%s/%s",s->f_Name, path, f->nf_Name ); } if( pos != 0 ) { BufStringAdd( bs, "," ); } FillStat( bs, f, s, tempString ); pos++; DEBUG("Dir/PAth added %s\n", tempString ); f = (INRAMFile *) f->node.mln_Succ; } BufStringAdd( bs, "]" ); } else { //BufStringAdd( bs, "fail<!--separate-->Could not open directory."); int pos = 0; BufStringAdd( bs, "ok<!--separate-->"); BufStringAdd( bs, "[]" ); } DEBUG("Dir END %s\n", bs->bs_Buffer); return bs; }
BufString *GetJSONFromStructure( ULONG *descr, void *data ) { BufString *bs = BufStringNew(); if( bs == NULL ) { ERROR("ERROR: bufstring is null\n"); return NULL; } DEBUG("[GetJSONFromStructure] \n"); if( descr == NULL || data == NULL ) { BufStringDelete( bs ); ERROR("Data structure or description was not provided!\n"); return 0; } if( descr[ 0 ] != SQLT_TABNAME ) { BufStringDelete( bs ); ERROR("SQLT_TABNAME was not provided!\n"); return 0; } DEBUG("JSONParse\n"); ULONG *dptr = &descr[ SQL_DATA_STRUCT_START ]; // first 2 entries inform about table, rest information provided is about columns unsigned char *strptr = (unsigned char *)data; // pointer to structure to which will will insert data int opt = 0; BufStringAdd( bs, "{" ); while( dptr[0] != SQLT_END ) { //DEBUG("Found on pos %d tag %d row %s\n", i, dptr[ 0 ], row[ i ] ); switch( dptr[ 0 ] ) { case SQLT_IDINT: // primary key case SQLT_INT: { char tmp[ 256 ]; int tmpint; memcpy( &tmpint, strptr + dptr[2], sizeof( int ) ); if( opt == 0 ) { sprintf( tmp, "\"%s\": %d ", (char *)dptr[ 1 ], tmpint ); BufStringAdd( bs, tmp ); } else { sprintf( tmp, ", \"%s\": %d ", (char *)dptr[ 1 ], tmpint ); BufStringAdd( bs, tmp ); } opt++; } break; case SQLT_STR: { char tmp[ 512 ]; char *tmpchar; memcpy( &tmpchar, strptr+dptr[2], sizeof( char *) ); if( tmpchar != NULL ) { if( opt == 0 ) { sprintf( tmp, "\"%s\": \"%s\" ", (char *)dptr[ 1 ], tmpchar ); BufStringAdd( bs, tmp ); } else { sprintf( tmp, ", \"%s\": \"%s\" ", (char *)dptr[ 1 ], tmpchar ); BufStringAdd( bs, tmp ); } } opt++; } break; case SQLT_TIMESTAMP: { // '2015-08-10 16:28:31' char date[ 512 ]; struct tm *tp = (struct tm *)( strptr+dptr[2]); if( opt == 0 ) { sprintf( date, "\"%s\": \"%4d-%2d-%2d %2d:%2d:%2d\" ", (char *)dptr[ 1 ], tp->tm_year, tp->tm_mon, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec ); BufStringAdd( bs, date ); } else { sprintf( date, ", \"%s\": \"%4d-%2d-%2d %2d:%2d:%2d\" ", (char *)dptr[ 1 ], tp->tm_year, tp->tm_mon, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec ); BufStringAdd( bs, date ); } opt++; } break; } dptr += 3; } BufStringAdd( bs, "}" ); //DEBUG("Object to JSON parse end %s\n", bs->bs_Buffer ); return bs; }
BufString *Dir( File *s, const char *path ) { BufString *bs = BufStringNew(); int rspath = strlen( s->f_Path ); DEBUG("Dir!\n"); // user is trying to get access to not his directory int doub = strlen( s->f_Name ); char *comm = NULL; if( ( comm = calloc( rspath +512, sizeof(char) ) ) != NULL ) { strcpy( comm, s->f_Path ); if( comm[ strlen( comm ) -1 ] != '/' && s->f_Path[ strlen(s->f_Path)-1 ] != '/' ) { DEBUG("Added '/\n"); strcat( comm, "/" ); } strcat( comm, path ); //&(path[ doub+1 ]) ); if( comm[ strlen( comm ) -1 ] != '/' ) { DEBUG("end was not endeed /\n"); strcat( comm, "/" ); } char tempString[ 1024 ]; DIR *d; struct dirent *dir; DEBUG("DIR -> directory '%s' for path '%s' devname '%s' double %d devpath '%s'\n", comm, path, s->f_Name, doub, s->f_Path ); d = opendir( comm ); if( d ) { int pos = 0; BufStringAdd( bs, "ok<!--separate-->"); //BufStringAdd( bs, "ok<!--separate-->[" ); BufStringAdd( bs, "[" ); while ((dir = readdir(d)) != NULL) { if( dir->d_name[ 0 ] == '/' ) { sprintf( tempString, "%s%s", comm, &(dir->d_name[1]) ); } else { sprintf( tempString, "%s%s", comm, dir->d_name ); } struct stat ls; //DEBUG("---------------> %s\n", dir->d_name ); if( stat( tempString, &ls ) == 0 ) { if( !(strcmp( dir->d_name, "." ) == 0 || strcmp( dir->d_name, ".." ) == 0 ) ) { if( pos != 0 ) { BufStringAdd( bs, "," ); } FillStat( bs, &ls, s, tempString ); pos++; } } } BufStringAdd( bs, "]" ); closedir( d ); } else { BufStringAdd( bs, "fail<!--separate-->Could not open directory."); } free( comm ); } DEBUG("Dir END\n"); return bs; }
BufString *AppSessionRemUserByNames( AppSession *as, UserSession *loggedSession, char *userlist ) { BufString *bs = BufStringNew(); if( userlist == NULL ) { DEBUG("Userlist is empty!\n"); BufStringAdd( bs, " {\"removed\": [ ] }" ); return bs; } BufStringAdd( bs, " {\"removed\": [" ); unsigned int i = 0; unsigned int usersi = 0; char *upositions[ 128 ]; memset( upositions, 0, sizeof( upositions ) ); if( as != NULL ) { // remove spaces and 'weird' chars from entry unsigned int i, j=0; DEBUG("[AppSession] user list: %s\n", userlist ); if( userlist != NULL ) { unsigned int listsize = strlen( userlist ); for( i=0 ; i < listsize ; i++ ) { if( userlist[ i ] == '[' || userlist[ i ] == ']' || userlist[ i ] == ' ' || userlist[ i ] == '"' ) { if( userlist[ i ] == ']' ) { userlist[ i ] = 0; DEBUG("End\n"); break; } } else { userlist[ j ] = userlist[ i ]; j++; } } userlist[ j ] = 0; upositions[ 0 ] = userlist; usersi = 1; listsize = strlen( userlist ); for( i = 1 ; i < listsize; i++ ) { if( userlist[ i ] == ',' ) { userlist[ i ] = 0; upositions[ usersi++ ] = &(userlist[ i+1 ]); } } //userlistadded = FCalloc( usersi * 512, sizeof(char) ); } } else { DEBUG("[AppSession] as = NULL\n" ); } // find user sessions by username // and send message pthread_mutex_lock( &as->as_SessionsMut ); UserSession *adminSession = NULL; char tmp[ 1024 ]; int msgsndsize = 0; SASUList *asul = as->as_UserSessionList; SASUList **rementr = NULL; unsigned int rementrnum = 0; int returnEntry = 0; DEBUG("[AppSession] Number of entries in SAS %d\n", as->as_UserNumber ); if( as->as_UserNumber > 0 ) { rementr = FCalloc( as->as_UserNumber+100, sizeof(SASUList *) ); User *assidAdmin = NULL; while( asul != NULL ) { for( i = 0 ; i < usersi ; i++ ) { if( asul->usersession->us_User != NULL ) { DEBUG("[AppSession] Checking user '%s'\n", upositions[ i ] ); if( strcmp( upositions[ i ], asul->usersession->us_User->u_Name ) == 0 ) { char locbuf[ 128 ]; int size = 0; if( returnEntry == 0 ) { size = snprintf( locbuf, sizeof(locbuf), "%s", asul->usersession->us_User->u_Name ); } else { size = snprintf( locbuf, sizeof(locbuf), ",%s", asul->usersession->us_User->u_Name ); } BufStringAddSize( bs, locbuf, size ); returnEntry++; if( asul->usersession == as->as_UserSessionList->usersession ) { DEBUG("[AppSession] Admin will be removed\n"); adminSession = asul->usersession; } rementr[ rementrnum++ ] = asul; } } } asul = (SASUList *) asul->node.mln_Succ; } } pthread_mutex_unlock( &as->as_SessionsMut ); // // we want to remove admin // if( rementr != NULL ) { if( adminSession != NULL ) { asul = as->as_UserSessionList; while( asul != NULL ) { int len = sprintf( tmp, "{\"type\":\"msg\",\"data\": { \"type\":\"%s\", \"data\":{\"type\":\"%lu\", \"data\":{ \"identity\":{\"username\":\"%s\"},\"data\": {\"type\":\"sasid-close\",\"data\":\"%s\"}}}}}", asul->authid, as->as_SASID, loggedSession->us_User->u_Name, asul->usersession->us_User->u_Name ); msgsndsize += WebSocketSendMessageInt( asul->usersession, tmp, len ); asul = (SASUList *) asul->node.mln_Succ; } } else { for( i=0 ; i < rementrnum ; i++ ) { DEBUG("[AppSession] authid %s sasid %lu userptr %p usersessptr %p usersessuser ptr %p\n", rementr[ i ]->authid, as->as_SASID, loggedSession->us_User, rementr[ i ]->usersession, rementr[ i ]->usersession->us_User ); int len = sprintf( tmp, "{\"type\":\"msg\",\"data\": { \"type\":\"%s\", \"data\":{\"type\":\"%lu\", \"data\":{ \"identity\":{\"username\":\"%s\"},\"data\": {\"type\":\"sasid-close\",\"data\":\"%s\"}}}}}", rementr[ i ]->authid, as->as_SASID, loggedSession->us_User->u_Name, rementr[ i ]->usersession->us_User->u_Name ); msgsndsize += WebSocketSendMessageInt( rementr[ i ]->usersession, tmp, len ); AppSessionRemUsersession( as, rementr[ i ]->usersession ); } } FFree( rementr ); } BufStringAdd( bs, "] }" ); return bs; }