Base Framework
testsuite/rm.cpp
/***************************************************************************
The Base Framework
A framework for developing platform independent applications
See COPYRIGHT.txt for details.
This framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the licensing terms refer to the file 'LICENSE'.
***************************************************************************/
#include <base/Application.h>
#include <base/string/FormatOutputStream.h>
#include <base/filesystem/FileSystem.h>
#include <base/filesystem/FolderInfo.h>
using namespace com::azure::dev::base;
class RemoveApplication : public Application {
private:
static const unsigned int MAJOR_VERSION = 1;
static const unsigned int MINOR_VERSION = 1;
enum Command {
COMMAND_HELP,
COMMAND_VERSION,
COMMAND_REMOVE
};
Command command = COMMAND_REMOVE;
bool silent = false;
bool recursive = false;
bool force = false;
public:
RemoveApplication()
: Application("rm")
{
}
void parseArguments() {
Array<String> arguments = getArguments();
while (enu.hasNext()) {
String argument = enu.next();
if (argument == "--help") {
command = COMMAND_HELP;
return;
} else if (argument == "--recursive") {
recursive = true;
} else if (argument == "--force") {
force = true;
} else if (argument == "--silent") {
silent = true;
} else if (argument == "--version") {
command = COMMAND_VERSION;
return;
} else {
paths.append(argument);
}
}
}
void version()
{
fout << getFormalName() << " version "
<< MAJOR_VERSION << '.' << MINOR_VERSION << EOL
<< "The Base Framework (Test Suite)" << EOL
<< ENDL;
}
void help()
{
version();
fout << "Usage: " << getFormalName() << " [options] path(s)" << EOL
<< EOL
<< "--help this message" << EOL
<< "--version dump the version" << EOL
<< EOL
<< "--recursive enable recursive mode" << EOL
<< "--force continue on error" << EOL
<< "--silent disable output" << EOL
<< ENDL;
}
void recursiveRemove(const String& folderPath) {
// String originalFolder = FileSystem::getCurrentFolder();
// FileSystem::setCurrentFolder(path);
FolderInfo folder(folderPath);
Array<String> entries = folder.getEntries();
while (enu.hasNext()) {
const String entry = enu.next();
if ((entry == ".") || (entry == "..")) {
continue;
}
try {
unsigned int type = FileSystem::getType(entry);
if (type & FileSystem::REGULAR) {
if (!silent) {
fout << "removing: " << entry << ENDL;
}
} else if (type & FileSystem::FOLDER) {
recursiveRemove(entry);
} else {
if (!silent) {
ferr << "Unable to remove entry: " << entry << ENDL;
}
if (!force) {
_throw FileSystemException("Unable to remove entry.", this);
}
}
} catch (FileSystemException& e) {
if (!force) {
ferr << entry << ": " << e << ENDL;
setExitCode(EXIT_CODE_ERROR);
_rethrow;
}
}
}
if (!silent) {
fout << "removing: " << folderPath << ENDL;
}
// FileSystem::setCurrentFolder(originalFolder);
}
void remove() {
while (enu.hasNext()) {
String path = enu.next();
ferr << "Error: " << "Entry does not exist: " << path << ENDL;
if (!force) {
setExitCode(EXIT_CODE_ERROR);
return;
}
continue;
}
try {
unsigned int type = FileSystem::getType(path);
if (type & FileSystem::REGULAR) {
if (!silent) {
fout << "removing: " << path << ENDL;
}
} else if (type & FileSystem::FOLDER) {
if (recursive) {
if (type & FileSystem::LINK) {
} else {
recursiveRemove(path);
}
} else {
if (!silent) {
fout << "removing: " << path << ENDL;
}
}
} else {
ferr << "Error: " << "Unable to remove entry: " << path << ENDL;
if (!force) {
setExitCode(EXIT_CODE_ERROR);
return;
}
}
} catch (FileSystemException& e) {
ferr << "Error: " << e.getMessage() << ENDL;
setExitCode(EXIT_CODE_ERROR);
}
}
}
void main() {
parseArguments();
switch (command) {
case COMMAND_HELP:
help();
break;
case COMMAND_VERSION:
version();
break;
case COMMAND_REMOVE:
remove();
break;
}
}
~RemoveApplication() {
}
};
APPLICATION_STUB(RemoveApplication);
FileSystem::LINK
@ LINK
Definition: FileSystem.h:74
FileSystem::entryExists
static bool entryExists(const String &path)
FileSystem::REGULAR
@ REGULAR
Definition: FileSystem.h:76
FolderInfo
Folder information.
Definition: FolderInfo.h:33
FileSystem::removeFolder
static void removeFolder(const String &path)
FileSystem::removeFile
static void removeFile(const String &path)
Array::append
void append(const Value &value)
Definition: Array.h:499
String
String.
Definition: String.h:102
Application
Application.
Definition: Application.h:53
FileSystemException
File system exception.
Definition: FileSystemException.h:28
FileSystem::getType
static unsigned int getType(const String &path)
FileSystem::FOLDER
@ FOLDER
Definition: FileSystem.h:73
Array::getReadEnumerator
ReadEnumerator getReadEnumerator() const noexcept
Definition: Array.h:489
Array< String >
Exception::getMessage
const char * getMessage() const noexcept
Definition: Exception.h:234
FolderInfo::getEntries
Array< String > getEntries() const