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 }
void fs_remove_dir(fs_t* fs, inodeid_t dir) { fs_inode_t idir = fs->inode_tab[dir]; int num = idir.size / sizeof(fs_dentry_t); // numero de entradas num directorio fs_dentry_t page[DIR_PAGE_ENTRIES]; for(int i = 0; i < INODE_NUM_BLKS && idir.blocks[i] != 0; i++){ int num_dir_pg_entries; while(num > 0){ block_read(fs->blocks, idir.blocks[i], (char*)page); for(num_dir_pg_entries = 0; num_dir_pg_entries < DIR_PAGE_ENTRIES && num > 0; i++, num --){ inodeid_t entryid = page[num_dir_pg_entries].inodeid; if (fs->inode_tab[entryid].type == FS_FILE) fs_remove_file(fs, entryid); else fs_remove_dir(fs, entryid); } } } BMAP_CLR(fs->inode_bmap, dir); }
int fs_remove(fs_t* fs, inodeid_t dir, char *name) { //checks if the arguments are valid if (fs == NULL || dir>=ITAB_SIZE || name == NULL) { dprintf("[fs_remove] malformed arguments. \n"); return -1; } if (strlen(name) == 0 || strlen(name)+1 > FS_MAX_FNAME_SZ) { dprintf("[fs_remove] file name size error\n"); return -1; } if (!BMAP_ISSET(fs->inode_bmap, dir)) { dprintf("[fs_remove] inode is not being used.\n"); return -1; } inodeid_t entryid = 0; if (!fsi_dir_search(fs, dir, name, &entryid) == 0) { dprintf("[fs_remove] file/dir does not exist\n"); return -1; } fs_inode_t* idir = &fs->inode_tab[dir]; if (idir->type != FS_DIR) { dprintf("[fs_remove] inode is not a directory. \n"); return -1; } fs_dentry_t page[DIR_PAGE_ENTRIES]; int num_dir_entry = 0, block_num = 0; search_dir_entry(fs, dir, entryid, &num_dir_entry, &block_num, (char*)page); fs_dentry_t last_page[DIR_PAGE_ENTRIES]; // array de entradas do ultimo bloco int last_entry_index = (idir->size % BLOCK_SIZE / sizeof(fs_dentry_t)) -1; block_read(fs->blocks,idir->blocks[idir->size/BLOCK_SIZE],(char *)last_page); fs_dentry_t* last_entry = &last_page[last_entry_index]; // ponteiro p/ última posição ocupada do dir page[num_dir_entry] = *last_entry; // mete a ultima entrada na posição da que vai ser removida block_write(fs->blocks, idir->blocks[block_num], (char*)page); // escreve o bloco (page) no disco idir->size -= sizeof(fs_dentry_t); // diminui o tamanho do directório em uma entrada int i, j; if (last_entry_index == 0) { //se for a primeira entrada do bloco, elimina esse bloco for(i = 0, j = 1; idir->blocks[j] != 0; i++, j++); BMAP_CLR(fs->blk_bmap, idir->blocks[i]); idir->blocks[i] = 0; } fs_inode_t ientry = fs->inode_tab[entryid]; // vai buscar o inode do ficheiro/directorio à tabela de inodes if(ientry.type == FS_FILE) fs_remove_file(fs, entryid); else fs_remove_dir(fs, entryid); BMAP_CLR(fs->inode_bmap, entryid); return 0; }
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; }
void test_file_notification(void**) { int temp = 1; const char* test_dir = "t2-output/test_dir"; const char* test_dir_2 = "t2-output/2_test_dir"; const char* test_dir_3 = "t2-output/3_test_dir"; const char* filename_2 = "t2-output/test_dir/test_file_2"; s_filename = "t2-output/test_dir/test_file.dll"; s_filename_2 = "t2-output/2_test_dir/test_file.txt"; s_filename_3 = "t2-output/3_test_dir/test_file.bin"; s_filename_4 = "t2-output/3_test_dir/test_file.so"; s_filename_5 = "t2-output/3_test_dir/test_file.exe"; fs_remove_directory(test_dir); fs_make_directory(test_dir); fs_remove_directory(test_dir_2); fs_make_directory(test_dir_2); fs_remove_directory(test_dir_3); fs_make_directory(test_dir_3); FILE* t = fopen(filename_2, "wb"); fwrite(&temp, 4, 1, t); fclose(t); FileMonitor_addPath(test_dir, "*", fileNotifaction, &s_user_data_1); thread_sleep(200); // Test notification when writing one file t = fopen(s_filename, "wb"); fclose(t); thread_sleep(1000); FileMonitor_update(); thread_sleep(400); assert_int_equal(s_checkPhase, 1); fs_copy_file(filename_2, s_filename); thread_sleep(1000); FileMonitor_update(); thread_sleep(400); assert_int_equal(s_checkPhase, 2); FileMonitor_addPath(test_dir_2, "txt", fileNotifaction2, &s_user_data_2); thread_sleep(1000); fs_copy_file(filename_2, s_filename_2); thread_sleep(1200); FileMonitor_update(); assert_int_equal(s_checkPhase, 3); FileMonitor_removePath(test_dir); // Except no notifactions for this fs_remove_file(s_filename); FileMonitor_update(); thread_sleep(1000); FileMonitor_update(); thread_sleep(400); assert_int_equal(s_checkPhase, 3); FileMonitor_addPath(test_dir_3, "bin;so", fileNotifaction3, &s_user_data_3); thread_sleep(1000); fs_copy_file(filename_2, s_filename_3); thread_sleep(1200); FileMonitor_update(); assert_int_equal(s_checkPhase, 4); thread_sleep(1000); fs_copy_file(filename_2, s_filename_4); thread_sleep(1200); FileMonitor_update(); assert_int_equal(s_checkPhase, 5); thread_sleep(1000); fs_copy_file(filename_2, s_filename_5); thread_sleep(1200); FileMonitor_update(); assert_int_equal(s_checkPhase, 5); FileMonitor_close(); }
int main (int argc, char **argv) { srand(time(NULL)); unsigned char test_data[TEST_DATA_SIZE] = {0}; unsigned char cmp_test_data[TEST_DATA_SIZE] = {0}; createRandomData(test_data,TEST_DATA_SIZE); memcpy(&cmp_test_data, &test_data,TEST_DATA_SIZE); int PASSES = 0; assert (fs_mount() >= 0); assert (fs_create_file("/testDir", DIR_FILE) >= 0); assert (fs_unmount() >= 0); PASS_OK assert (fs_mount() >= 0); assert (fs_create_file("/testDir", DIR_FILE) >= 0); assert (fs_create_file("/testDir/checkMe.txt", REG_FILE) >= 0); assert (fs_unmount() >= 0); PASS_OK assert (fs_mount() >= 0); assert (fs_create_file("/testDir", DIR_FILE) >= 0); assert (fs_create_file("/testDir/checkMe.txt", REG_FILE) >= 0); assert (fs_write_file("/testDir/checkMe.txt", test_data, TEST_DATA_SIZE) >= 0); assert (fs_unmount() >= 0); PASS_OK unsigned char readResults[TEST_DATA_SIZE] = {0}; assert (fs_mount() >= 0); assert (fs_create_file("/testDir", DIR_FILE) >= 0); assert (fs_create_file("/testDir/checkMe.txt", REG_FILE) >= 0); assert (fs_write_file("/testDir/checkMe.txt", test_data, TEST_DATA_SIZE) >= 0); assert (fs_read_file("/testDir/checkMe.txt", readResults, TEST_DATA_SIZE) >= 0); int i = 0; for (;i < TEST_DATA_SIZE; ++i) { assert(readResults[i] == cmp_test_data[i]); } assert (fs_unmount() >= 0); PASS_OK assert (fs_mount() >= 0); assert (fs_create_file("/testDir", DIR_FILE) >= 0); assert (fs_create_file("/testDir/checkMe.txt", REG_FILE) >= 0); assert (fs_create_file("/testDir/secondMe.txt", REG_FILE) >= 0); assert (fs_create_file("/testDir/thirdMe.txt", REG_FILE) >= 0); Directory_t dir; assert (fs_get_directory("/testDir/", &dir) >= 0); assert (dir.size == 3); assert(strncmp(dir.entries[0].filename,"checkMe.txt",strlen("checkMe.txt")) == 0); assert(strncmp(dir.entries[1].filename,"secondMe.txt",strlen("secondMe.txt")) == 0); assert(strncmp(dir.entries[2].filename,"thirdMe.txt",strlen("thirdMe.txt")) == 0); assert (fs_unmount() >= 0); PASS_OK /*NEW TEST ADDED */ assert (fs_mount() >= 0); assert (fs_create_file("/testDir", DIR_FILE) >= 0); assert (fs_create_file("/testDir/checkMe.txt", REG_FILE) >= 0); assert (fs_create_file("/testDir/secondMe.txt", REG_FILE) >= 0); assert (fs_create_file("/testDir/thirdMe.txt", REG_FILE) >= 0); assert (fs_remove_file("/testDir/thirdMe.txt") >= 0); assert (fs_get_directory("/testDir/", &dir) >= 0); assert (dir.size == 2); assert(strncmp(dir.entries[0].filename,"checkMe.txt",strlen("checkMe.txt")) == 0); assert(strncmp(dir.entries[1].filename,"secondMe.txt",strlen("secondMe.txt")) == 0); assert (fs_unmount() >= 0); return 0; }