diff --git a/doc/rasctl.1 b/doc/rasctl.1 index f0a6d196..9cd67f6d 100644 --- a/doc/rasctl.1 +++ b/doc/rasctl.1 @@ -52,6 +52,9 @@ Display server-side settings like available images or supported device types. .BR \-v\fI " " \fI Display the rascsi version. .TP +.BR \-x\fI " "\fIFILENAME +Delete an image file in the default image folder. +.TP .BR \-i\fI " " \fIID ID is the SCSI ID that you want to control. (0-7) .TP diff --git a/doc/rasctl_man_page.txt b/doc/rasctl_man_page.txt index e5ebc652..294ccb75 100644 --- a/doc/rasctl_man_page.txt +++ b/doc/rasctl_man_page.txt @@ -46,6 +46,9 @@ OPTIONS -v Display the rascsi version. + -x FILENAME + Delete an image file in the default image folder. + -i ID ID is the SCSI ID that you want to control. (0-7) -c CMD Command is the operation being requested. Options are: diff --git a/src/raspberrypi/rascsi.cpp b/src/raspberrypi/rascsi.cpp index 24eec3eb..b3ef4262 100644 --- a/src/raspberrypi/rascsi.cpp +++ b/src/raspberrypi/rascsi.cpp @@ -756,6 +756,26 @@ bool CreateImage(int fd, const PbCommand& command) return ReturnStatus(fd); } +bool DeleteImage(int fd, const PbCommand& command) +{ + if (command.params().size() < 1 || command.params().Get(0).empty()) { + return ReturnStatus(fd, false, "Can't delete image file: Missing filename"); + } + + string filename = command.params().Get(0); + if (filename.find('/') != string::npos) { + return ReturnStatus(fd, false, "The image filename '" + filename + "' must not contain a path"); + } + + filename = default_image_folder + "/" + filename; + + if (unlink(filename.c_str())) { + return ReturnStatus(fd, false, "Can't delete image file '" + filename + "': " + string(strerror(errno))); + } + + return ReturnStatus(fd); +} + void DetachAll() { Device *map[devices.size()]; @@ -1189,21 +1209,30 @@ bool ProcessCmd(int fd, const PbDeviceDefinition& pb_device, const PbOperation o bool ProcessCmd(const int fd, const PbCommand& command) { - if (command.operation() == DETACH_ALL) { - DetachAll(); - return ReturnStatus(fd); - } - else if (command.operation() == RESERVE) { - const list ids = { command.params().begin(), command.params().end() }; - string invalid_id = SetReservedIds(ids); - if (!invalid_id.empty()) { - return ReturnStatus(fd, false, "Invalid ID " + invalid_id + " for " + PbOperation_Name(RESERVE)); + switch (command.operation()) { + case DETACH_ALL: + DetachAll(); + return ReturnStatus(fd); + + case RESERVE: { + const list ids = { command.params().begin(), command.params().end() }; + string invalid_id = SetReservedIds(ids); + if (!invalid_id.empty()) { + return ReturnStatus(fd, false, "Invalid ID " + invalid_id + " for " + PbOperation_Name(RESERVE)); + } + + return ReturnStatus(fd); } - return ReturnStatus(fd); - } - else if (command.operation() == CREATE_IMAGE) { - return CreateImage(fd, command); + case CREATE_IMAGE: + return CreateImage(fd, command); + + case DELETE_IMAGE: + return DeleteImage(fd, command); + + default: + // This is a device-specific command handled below + break; } const vector params = { command.params().begin(), command.params().end() }; diff --git a/src/raspberrypi/rasctl.cpp b/src/raspberrypi/rasctl.cpp index 7d317ff2..cfa9a393 100644 --- a/src/raspberrypi/rasctl.cpp +++ b/src/raspberrypi/rasctl.cpp @@ -221,6 +221,17 @@ void CommandCreateImage(const string&hostname, int port, const string& image_par SendCommand(hostname.c_str(), port, command, result); } +void CommandDeleteImage(const string&hostname, int port, const string& filename) +{ + PbCommand command; + command.set_operation(DELETE_IMAGE); + + command.add_params(filename); + + PbResult result; + SendCommand(hostname.c_str(), port, command, result); +} + void CommandDefaultImageFolder(const string& hostname, int port, const string& folder) { PbCommand command; @@ -522,7 +533,7 @@ int main(int argc, char* argv[]) opterr = 1; int opt; - while ((opt = getopt(argc, argv, "a:b:c:d:f:g:h:i:n:p:r:t:u:lsv")) != -1) { + while ((opt = getopt(argc, argv, "a:b:c:d:f:g:h:i:n:p:r:t:u:x:lsv")) != -1) { switch (opt) { case 'i': device->set_id(optarg[0] - '0'); @@ -633,6 +644,11 @@ int main(int argc, char* argv[]) cout << rascsi_get_version_string() << endl; exit(EXIT_SUCCESS); break; + + case 'x': + command.set_operation(DELETE_IMAGE); + image_params = optarg; + break; } } @@ -657,6 +673,10 @@ int main(int argc, char* argv[]) CommandCreateImage(hostname, port, image_params); exit(EXIT_SUCCESS); + case DELETE_IMAGE: + CommandDeleteImage(hostname, port, image_params); + exit(EXIT_SUCCESS); + case DEVICE_INFO: CommandDeviceInfo(hostname, port, command); exit(EXIT_SUCCESS);