int main (int argc, char ** argv ) { // setvbuf(stdin, buff_stdin, _IOFBF, 0x1000); // setvbuf(stdout, buff_stdout, _IOFBF, 0x1000); char *real_obj_name = NULL; int mode = 0; mode = get_work_mode (argc, argv); if ( save_settings_to_fs () < 0 ) return -1; if ( ( real_obj_name = prepare_object (mode)) == NULL ) { return -1; } test_read_dir( "/docs/list.txt" ); docs_to_xml( TEMP_DIR, mode ); do_index_xml (); save_index (); mylistdir( "/" ); return 0; }
// Implements EMPTY directory deletion void myrmdir(char *path){ // if it's an abs path then return to root if (path[0] == '/'){ mychdir("root"); } // if the folder is empty, i.e. the first item is it's list is the ENDOFDIR tag if (strcmp(mylistdir(path)[1], "ENDOFDIR") == 0) { // keep a track of where we start the process as the current dir changes in here, somewhere... int initial_current_dir_index = current_dir_index; int initial_current_dir_first_block = current_dir->first_block; // calculate the parent of the dir to remove and chdir to it char *target = strstr(path, last_entry_in_path(path_to_array(path))); int position = target - path; char parent_path[position+1]; for(int i = 0; i < position + 1; i++){ parent_path[i] = '\0'; } strncpy(parent_path, path, position); if(strcmp(parent_path,"") != 0) { mychdir(parent_path); } // find the entry for the dir to be deleted int entrylist_target = file_entry_index(last_entry_in_path(path_to_array(path))); // find the start of it's block chain int block_chain_target = virtual_disk[current_dir_index].dir.entrylist[entrylist_target].first_block; // clear the dir entry and it's name ready for reuse (still don't have that feature) direntry_t *dir_entry = &virtual_disk[current_dir_index].dir.entrylist[entrylist_target]; dir_entry->first_block = 0; dir_entry->unused = 1; int length = strlen(dir_entry->name); for (int i = 0; i < length; i++){ dir_entry->name[i] = '\0'; } // clear the dirs block chain and update the fat int next_block_chain_target; while(1){ next_block_chain_target = FAT[block_chain_target]; FAT[block_chain_target] = UNUSED; if (next_block_chain_target == ENDOFCHAIN) break; block_chain_target = next_block_chain_target; } copy_fat(FAT); // go back to where we started! current_dir_index = initial_current_dir_index; current_dir->first_block = initial_current_dir_first_block; } else { printf("That directory has content and cannot be deleted.\n"); } }