bool CommandRm::exe(tfs::api::FileSystem& fs, string parameter) { FilePath pathGenerator; istringstream input(parameter); string path; string commandType; input >> commandType >>path; /** * if there is only one parameter,it should be the filepath * which will be removed later, or romove the directory */ if(path == ""){ path = commandType; path = pathGenerator.getPath(m_currentDir, path); path = pathGenerator.change(path); return removeFile(fs, path); } else if(commandType == "-rf"){ path = pathGenerator.getPath(m_currentDir, path); path = pathGenerator.change(path); return removeDirectory(fs, path); } cout << "<USAGE> rm <filepath>" << endl; cout << "<USAGE> rm -rf <filepath>" << endl; return false; }
void testChange() { std::string c0("/test/dir/test"); std::string c1("/test/dir/test/"); std::string c2("/root/temp/.././../."); std::string c3("/root/tet/test/./test/../../../"); std::string c4("/./dd/.."); std::string c5("/./1/../2/./../3/./././../3/4/5/../../.."); std::string c6("/test/../test/temp/../../"); // std::string result = m_path.change(c1); TS_ASSERT_EQUALS(result, c0); result = m_path.change(c2); TS_ASSERT_EQUALS(result, "/"); result = m_path.change(c3); TS_ASSERT_EQUALS(result, "/root"); result = m_path.change(c4); TS_ASSERT_EQUALS(result, "/"); result = m_path.change(c0); TS_ASSERT_EQUALS(result, c0); result = m_path.change(c5); TS_ASSERT_EQUALS(result, "/"); result = m_path.change("//"); TS_ASSERT_EQUALS(result, "/"); result = m_path.change(c6); TS_ASSERT_EQUALS(result, "/"); // @fixbug 19 std::string c7("/.."); result = m_path.change(c7); TS_ASSERT_EQUALS(result, "/"); }
bool CommandPut::exe(FileSystem& fs, string parameter) { istringstream input(parameter); string tfsPath; string localPath; int blocksize = -1; // add by fk input >> localPath >> tfsPath >>blocksize; //get the parameters if (blocksize == -1) blocksize = 1; /** * if the path is null, it puts nothing */ if(tfsPath == "" && localPath == ""){ cout << "USAGE: put <local_file> [<tfs_file>]" << endl; cout << "EXAMPLE: put filename1" << endl; cout << " put filename1 ." << endl; cout << " put filename1 ./filename2" << endl; cout << " put /home/abc/filename1" << endl; cout << " put /home/abc/filename1 ./filename2" << endl; return false; } // check local file struct stat fileStat; if (stat(localPath.c_str(), &fileStat) != 0){ cout << "Error: put: no such file " << localPath << endl; return false; } bool bAbsolutePath; string localDir; string localFilename; FilePath::split(localPath, bAbsolutePath, localDir, localFilename); string tfsDir; string tfsFilename; FilePath::split(tfsPath, bAbsolutePath, tfsDir, tfsFilename); if (!bAbsolutePath){ // reletive path FilePath pathGenerator; tfsDir = pathGenerator.getPath(m_currentDir, tfsDir); tfsDir = pathGenerator.change(tfsDir); //change into absolutely path if(tfsDir != "/"){ tfsDir += "/"; } } if (tfsFilename.empty()){ tfsFilename = localFilename; } tfsPath = tfsDir + tfsFilename; cout << "put " << localPath << " " << tfsPath << endl; try{ if( !fs.existDirectory(tfsDir) ){ cout << "Error: no such directory " << tfsDir << endl; return false; } //if the file is exist , the local file will be appended if( !fs.existFile(tfsPath) ){ int chunkSize = (fileStat.st_size > MAX_CHUNK_SIZE) ? MAX_CHUNK_SIZE:DEFAULT_CHUNK_SIZE; chunkSize = chunkSize - chunkSize % blocksize; fs.createFile(tfsPath, 2 , chunkSize); } AppendStream tfsAppend; int bufferSize = (fileStat.st_size < MAX_BUFFER_SIZE) ? MAX_BUFFER_SIZE:DEFAULT_BUFFER_SIZE; tfsAppend.open(tfsPath, bufferSize); ifstream fin(localPath.c_str() , ios::binary); if(!fin){ cout << "ERROR: put: open file " << localPath << endl; return false; } size_t MAXGET = 1024 * 1024 / blocksize * blocksize; if (MAXGET == 0) throw (TFSException("Block Size Exceed 1 M")); char *buffer = new char[MAXGET]; while(fin){ fin.read(buffer, MAXGET); //cout << "read " << fin.gcount() << endl; tfsAppend.append(buffer, fin.gcount()); } } catch(TFSException& ex){ cout << "ERROR: put: Get exception: " << ex.what() << endl; return false; } return true; }