Added image file deletion

This commit is contained in:
Uwe Seimet 2021-09-15 11:08:23 +02:00
parent 9a8c35db6b
commit 812a322e8d
4 changed files with 69 additions and 14 deletions

View File

@ -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

View File

@ -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:

View File

@ -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,11 +1209,12 @@ bool ProcessCmd(int fd, const PbDeviceDefinition& pb_device, const PbOperation o
bool ProcessCmd(const int fd, const PbCommand& command)
{
if (command.operation() == DETACH_ALL) {
switch (command.operation()) {
case DETACH_ALL:
DetachAll();
return ReturnStatus(fd);
}
else if (command.operation() == RESERVE) {
case RESERVE: {
const list<string> ids = { command.params().begin(), command.params().end() };
string invalid_id = SetReservedIds(ids);
if (!invalid_id.empty()) {
@ -1202,8 +1223,16 @@ bool ProcessCmd(const int fd, const PbCommand& command)
return ReturnStatus(fd);
}
else if (command.operation() == CREATE_IMAGE) {
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<string> params = { command.params().begin(), command.params().end() };

View File

@ -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);