Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //######### REMOVE COMMAND ##########
- RmCommand::RmCommand(string args):BaseCommand(args){}
- void RmCommand::execute(FileSystem & fs){
- string path = getArgs();
- // can't delete root or current directory, TODO: check if relative path correct..
- if(path == fs.getWorkingDirectory().getAbsolutePath() || path == fs.getRootDirectory().getAbsolutePath()){
- cout << "Canβt remove directory" << endl;
- return;
- }
- Directory* fatherDirectory = nullptr;
- int lastSlashLoc = path.find_last_of("/");
- string newPath = path.substr(0, lastSlashLoc);// New path of Father directory of 'gonna-be-deleted' file/dir
- string baseName = path.substr(lastSlashLoc+1); // name of 'gonna-be-deleted' file/dir
- fatherDirectory = getDirByPath(fs, newPath);
- if(fatherDirectory == nullptr)
- cout << "invalid path" << endl;
- BaseFile* child = fatherDirectory->getChildByName(baseName);
- if(child==nullptr){ cout << "file/directory not found" << endl;}
- else if(child->IsFile()){ // if file
- delete ((File*)child);
- } else if(!child->IsFile()){// if dir
- delete ((Directory*)child);
- }
- fatherDirectory = nullptr;
- child = nullptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement