DECLARE_TEST( fs, directory ) { char* longpath; char* testpath = path_merge( environment_temporary_directory(), string_from_int_static( random64(), 0, 0 ) ); if( !fs_is_file( testpath ) ) fs_remove_file( testpath ); if( !fs_is_directory( testpath ) ) fs_make_directory( testpath ); EXPECT_TRUE( fs_is_directory( testpath ) ); fs_remove_directory( testpath ); EXPECT_FALSE( fs_is_directory( testpath ) ); longpath = path_merge( testpath, string_from_int_static( random64(), 0, 0 ) ); EXPECT_FALSE( fs_is_directory( longpath ) ); fs_make_directory( longpath ); EXPECT_TRUE( fs_is_directory( longpath ) ); fs_remove_directory( testpath ); EXPECT_FALSE( fs_is_directory( testpath ) ); EXPECT_FALSE( fs_is_directory( longpath ) ); string_deallocate( longpath ); string_deallocate( testpath ); return 0; }
DECLARE_TEST( fs, util ) { tick_t systime = time_system(); tick_t lastmod = 0; char* testpath = path_merge( environment_temporary_directory(), string_from_int_static( random64(), 0, 0 ) ); if( !fs_is_directory( environment_temporary_directory() ) ) fs_make_directory( environment_temporary_directory() ); if( fs_is_directory( testpath ) ) fs_remove_directory( testpath ); fs_remove_file( testpath ); EXPECT_EQ( fs_last_modified( testpath ), 0 ); thread_sleep( 1000 ); //For fs time granularity, make sure at least one second passed since systime stream_deallocate( fs_open_file( testpath, STREAM_OUT ) ); EXPECT_TRUE( fs_is_file( testpath ) ); EXPECT_GE( fs_last_modified( testpath ), systime ); fs_remove_file( testpath ); EXPECT_FALSE( fs_is_file( testpath ) ); EXPECT_EQ( fs_last_modified( testpath ), 0 ); stream_deallocate( fs_open_file( testpath, STREAM_OUT ) ); EXPECT_TRUE( fs_is_file( testpath ) ); EXPECT_GE( fs_last_modified( testpath ), systime ); lastmod = fs_last_modified( testpath ); thread_sleep( 5000 ); EXPECT_EQ( fs_last_modified( testpath ), lastmod ); fs_touch( testpath ); EXPECT_GT( fs_last_modified( testpath ), lastmod ); fs_remove_file( testpath ); string_deallocate( testpath ); return 0; }
void PluginHandler_create(bool shadowDirectory) { s_useShadowDir = shadowDirectory; if (!shadowDirectory) return; // Cleanup directory first if (fs_is_file(s_shadowDirName)) fs_remove_file(s_shadowDirName); if (fs_is_directory(s_shadowDirName)) fs_remove_directory(s_shadowDirName); fs_make_directory(s_shadowDirName); // Listen to changes in the current directry for code // TODO: Add this to settings so we can have more dirs here }
font_t font_create(const char* name) { //todo: make this a task FOUNDATION_ASSERT(fs_is_file(name)); stream_t* ttf_stream = fs_open_file(name, STREAM_IN|STREAM_BINARY); FOUNDATION_ASSERT(ttf_stream); const uint64_t ttf_stream_size = stream_size(ttf_stream); unsigned char* ttf_buffer = (unsigned char*) memory_allocate(ttf_stream_size, 4, MEMORY_TEMPORARY); unsigned char* ttf_bitmap = (unsigned char*) memory_allocate(MINT_FONT_BITMAPSIZE*MINT_FONT_BITMAPSIZE, 4, MEMORY_PERSISTENT); const uint64_t read = stream_read(ttf_stream, ttf_buffer, ttf_stream_size); FOUNDATION_ASSERT(read == ttf_stream_size); font_data_t data; stbtt_BakeFontBitmap( ttf_buffer,0, 32.0, ttf_bitmap, MINT_FONT_BITMAPSIZE, MINT_FONT_BITMAPSIZE, 32,96, data.cdata ); TextureCreationInfo info; info.type = Texture2D; info.format = FormatR8; info.width = MINT_FONT_BITMAPSIZE; info.height = MINT_FONT_BITMAPSIZE; data.texture = texture_create(ttf_bitmap, info); array_push_memcpy(s_fontCtx.fonts, &data); memory_deallocate(ttf_bitmap); memory_deallocate(ttf_buffer); stream_deallocate(ttf_stream); return array_size(s_fontCtx.fonts)-1; }
void CUpdater::DoUpdates(CMenus *pMenus) { bool noErrors = true; // Remove Files for (int i=0; i<m_vToRemove.size(); i++) if (fs_is_file(m_vToRemove[i].c_str()) && fs_remove(m_vToRemove[i].c_str()) != 0) noErrors = false; m_vToRemove.clear(); // Download Files for (int i=0; i<m_vToDownload.size(); i++) if (!CHttpDownloader::GetToFile(m_vToDownload[i].c_str(), m_vToDownload[i].c_str())) noErrors = false; m_vToDownload.clear(); if (m_NeedUpdateClient) { #ifdef CONF_FAMILY_WINDOWS #ifdef CONF_PLATFORM_WIN64 if (!CHttpDownloader::GetToFile("http://" UPDATES_HOST UPDATES_BASE_PATH "teeworlds64.exe", "tw_tmp")) #else if (!CHttpDownloader::GetToFile("http://" UPDATES_HOST UPDATES_BASE_PATH "teeworlds.exe", "tw_tmp")) #endif #elif defined(CONF_FAMILY_UNIX) #ifdef CONF_PLATFORM_MACOSX if (!CHttpDownloader::GetToFile("http://" UPDATES_HOST UPDATES_BASE_PATH "teeworlds_mac", "tw_tmp")) #elif defined(CONF_ARCH_IA64) || defined(CONF_ARCH_AMD64) if (!CHttpDownloader::GetToFile("http://" UPDATES_HOST UPDATES_BASE_PATH "teeworlds64", "tw_tmp")) #else if (!CHttpDownloader::GetToFile("http://" UPDATES_HOST UPDATES_BASE_PATH "teeworlds", "tw_tmp")) #endif #endif { noErrors = false; } } if (m_NeedUpdateServer) { #ifdef CONF_FAMILY_WINDOWS #ifdef CONF_PLATFORM_WIN64 if (!CHttpDownloader::GetToFile("http://" UPDATES_HOST UPDATES_BASE_PATH "teeworlds_srv64.exe", "teeworlds_srv.exe")) #else if (!CHttpDownloader::GetToFile("http://" UPDATES_HOST UPDATES_BASE_PATH "teeworlds_srv.exe", "teeworlds_srv.exe")) #endif #elif defined(CONF_FAMILY_UNIX) #ifdef CONF_PLATFORM_MACOSX if (!CHttpDownloader::GetToFile("http://" UPDATES_HOST UPDATES_BASE_PATH "teeworlds_srv_mac", "teeworlds_srv")) #elif defined(CONF_ARCH_IA64) || defined(CONF_ARCH_AMD64) if (!CHttpDownloader::GetToFile("http://" UPDATES_HOST UPDATES_BASE_PATH "teeworlds_srv64", "teeworlds_srv")) #else if (!CHttpDownloader::GetToFile("http://" UPDATES_HOST UPDATES_BASE_PATH "teeworlds_srv", "teeworlds_srv")) #endif #endif { noErrors = false; } } if (noErrors) m_Updated = true; pMenus->SetPopup(CMenus::POPUP_UPDATER_RESULT); }
DECLARE_TEST( fs, file ) { char* testpath = path_merge( environment_temporary_directory(), string_from_int_static( random64(), 0, 0 ) ); char* copypath = path_merge( environment_temporary_directory(), string_from_int_static( random64(), 0, 0 ) ); stream_t* teststream = 0; if( !fs_is_directory( environment_temporary_directory() ) ) fs_make_directory( environment_temporary_directory() ); if( fs_is_directory( testpath ) ) fs_remove_directory( testpath ); fs_remove_file( testpath ); if( fs_is_directory( copypath ) ) fs_remove_directory( copypath ); fs_remove_file( copypath ); teststream = fs_open_file( testpath, STREAM_IN ); EXPECT_EQ( teststream, 0 ); EXPECT_FALSE( fs_is_file( testpath ) ); teststream = fs_open_file( testpath, STREAM_IN | STREAM_OUT ); EXPECT_NE( teststream, 0 ); EXPECT_TRUE( fs_is_file( testpath ) ); stream_deallocate( teststream ); fs_remove_file( testpath ); teststream = fs_open_file( testpath, STREAM_IN ); EXPECT_EQ( teststream, 0 ); EXPECT_FALSE( fs_is_file( testpath ) ); teststream = fs_open_file( testpath, STREAM_OUT ); EXPECT_NE( teststream, 0 ); EXPECT_TRUE( fs_is_file( testpath ) ); stream_deallocate( teststream ); teststream = 0; fs_copy_file( testpath, copypath ); EXPECT_TRUE( fs_is_file( copypath ) ); fs_remove_file( copypath ); EXPECT_FALSE( fs_is_file( copypath ) ); teststream = fs_open_file( testpath, STREAM_OUT ); EXPECT_NE( teststream, 0 ); EXPECT_TRUE( fs_is_file( testpath ) ); stream_write_string( teststream, "testing testing" ); stream_deallocate( teststream ); teststream = 0; fs_copy_file( testpath, copypath ); EXPECT_TRUE( fs_is_file( copypath ) ); fs_remove_file( copypath ); EXPECT_FALSE( fs_is_file( copypath ) ); stream_deallocate( teststream ); string_deallocate( testpath ); string_deallocate( copypath ); return 0; }
DECLARE_TEST( fs, monitor ) { char* testpath = path_merge( environment_temporary_directory(), string_from_int_static( random64(), 0, 0 ) ); char* filetestpath = path_merge( testpath, string_from_int_static( random64(), 0, 0 ) ); stream_t* test_stream; event_stream_t* stream; event_block_t* block; event_t* event; stream = fs_event_stream(); if( fs_is_file( testpath ) ) fs_remove_file( testpath ); if( !fs_is_directory( testpath ) ) fs_make_directory( testpath ); if( fs_is_file( filetestpath ) ) fs_remove_file( filetestpath ); stream_deallocate( fs_open_file( filetestpath, STREAM_OUT ) ); fs_remove_file( filetestpath ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_EQ( event, 0 ); fs_monitor( testpath ); thread_sleep( 1000 ); stream_deallocate( fs_open_file( filetestpath, STREAM_OUT ) ); thread_sleep( 100 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_NE( event, 0 ); EXPECT_EQ( event->system, SYSTEM_FOUNDATION ); EXPECT_EQ( event->id, FOUNDATIONEVENT_FILE_CREATED ); EXPECT_STREQ( event->payload, filetestpath ); event = event_next( block, event ); EXPECT_EQ( event, 0 ); test_stream = fs_open_file( filetestpath, STREAM_IN | STREAM_OUT ); stream_write_string( test_stream, filetestpath ); stream_deallocate( test_stream ); thread_sleep( 100 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_NE( event, 0 ); EXPECT_EQ( event->system, SYSTEM_FOUNDATION ); EXPECT_EQ( event->id, FOUNDATIONEVENT_FILE_MODIFIED ); EXPECT_STREQ( event->payload, filetestpath ); event = event_next( block, event ); EXPECT_EQ( event, 0 ); fs_remove_file( filetestpath ); thread_sleep( 100 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_NE( event, 0 ); EXPECT_EQ( event->system, SYSTEM_FOUNDATION ); EXPECT_EQ( event->id, FOUNDATIONEVENT_FILE_DELETED ); EXPECT_STREQ( event->payload, filetestpath ); event = event_next( block, event ); EXPECT_EQ( event, 0 ); fs_unmonitor( testpath ); thread_sleep( 1000 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_EQ( event, 0 ); stream_deallocate( fs_open_file( filetestpath, STREAM_OUT ) ); thread_sleep( 100 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_EQ( event, 0 ); fs_remove_file( filetestpath ); thread_sleep( 100 ); block = event_stream_process( stream ); event = event_next( block, 0 ); EXPECT_EQ( event, 0 ); fs_remove_directory( testpath ); string_deallocate( testpath ); string_deallocate( filetestpath ); return 0; }
DECLARE_TEST( fs, query ) { uint64_t subpathid = random64(); uint64_t subfileid = random64(); char* testpath = path_merge( environment_temporary_directory(), string_from_int_static( random64(), 0, 0 ) ); char* subtestpath = path_merge( testpath, string_from_int_static( subpathid, 0, 0 ) ); char* filepath[8]; char** subdirs; char** files; int ifp = 0; char* subfilepath; if( fs_is_file( testpath ) ) fs_remove_file( testpath ); if( !fs_is_directory( testpath ) ) fs_make_directory( testpath ); if( !fs_is_directory( subtestpath ) ) fs_make_directory( subtestpath ); for( ifp = 0; ifp < 8; ++ifp ) { filepath[ifp] = path_merge( testpath, string_from_int_static( random64(), 0, 0 ) ); filepath[ifp] = string_append( string_append( filepath[ifp], "." ), string_from_int_static( ifp, 0, 0 ) ); stream_deallocate( fs_open_file( filepath[ifp], STREAM_OUT ) ); } subfilepath = path_merge( subtestpath, string_from_int_static( subfileid, 0, 0 ) ); subfilepath = string_append( subfilepath, ".0" ); stream_deallocate( fs_open_file( subfilepath, STREAM_OUT ) ); files = fs_files( filepath[0] ); EXPECT_EQ( array_size( files ), 0 ); string_array_deallocate( files ); subdirs = fs_subdirs( subtestpath ); EXPECT_EQ( array_size( subdirs ), 0 ); string_array_deallocate( subdirs ); files = fs_files( testpath ); EXPECT_EQ( array_size( files ), 8 ); string_array_deallocate( files ); subdirs = fs_subdirs( testpath ); EXPECT_EQ( array_size( subdirs ), 1 ); string_array_deallocate( subdirs ); files = fs_matching_files( testpath, "*", false ); EXPECT_EQ( array_size( files ), 8 ); string_array_deallocate( files ); files = fs_matching_files( testpath, "*", true ); EXPECT_EQ( array_size( files ), 9 ); string_array_deallocate( files ); files = fs_matching_files( testpath, "*.0", false ); EXPECT_EQ( array_size( files ), 1 ); string_array_deallocate( files ); files = fs_matching_files( testpath, "*.0", true ); EXPECT_EQ( array_size( files ), 2 ); string_array_deallocate( files ); files = fs_matching_files( testpath, "*.1", false ); EXPECT_EQ( array_size( files ), 1 ); string_array_deallocate( files ); files = fs_matching_files( testpath, "*.1", true ); EXPECT_EQ( array_size( files ), 1 ); string_array_deallocate( files ); files = fs_matching_files( testpath, "*.?", true ); EXPECT_EQ( array_size( files ), 9 ); { char* verifypath = string_from_int( subpathid, 0, 0 ); verifypath = path_append( verifypath, string_from_int_static( subfileid, 0, 0 ) ); verifypath = string_append( verifypath, ".0" ); EXPECT_STREQ( files[8], verifypath ); string_deallocate( verifypath ); } string_array_deallocate( files ); fs_remove_directory( testpath ); string_array_deallocate( subdirs ); string_array_deallocate( files ); string_deallocate( subfilepath ); for( ifp = 0; ifp < 8; ++ifp ) string_deallocate( filepath[ifp] ); string_deallocate( subtestpath ); string_deallocate( testpath ); return 0; }