From 887164d16d21c00848bf75e10f96d4b4803a866c Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Tue, 10 May 2022 16:52:41 +0100 Subject: [PATCH] Switch recmove to use cRecording::ChangeName() Fixes multi-select delete / Update() problem --- vdrclient.c | 149 ++++++---------------------------------------------- 1 file changed, 17 insertions(+), 132 deletions(-) diff --git a/vdrclient.c b/vdrclient.c index f0592e6..06f0fba 100644 --- a/vdrclient.c +++ b/vdrclient.c @@ -1027,154 +1027,39 @@ bool VDRClient::recmove(PFMap& postFields, Json::Value& js) // RETHROWS logger->debug("recmove"); std::string fileNameToAffect = getVarString(postFields, "filename"); - std::string requestedNewStr = getVarString(postFields, "newpath"); - - std::string dirNameSingleDate; - std::string dirNameSingleTitle; - std::string dirNameSingleFolder; - std::string dirNameFullPathTitle; - std::string dirNameFullPathDate; + std::string requestedNewDirName = getVarString(postFields, "newpath", true /* empty new path is ok */); try { LOCK_RECORDINGS_WRITE; + cRecording* recordingObj = Recordings->GetByName(fileNameToAffect.c_str()); + if (!recordingObj) throw 2; - pathsForRecordingName(Recordings, - fileNameToAffect, dirNameSingleDate, - dirNameSingleTitle, dirNameSingleFolder, - dirNameFullPathTitle, dirNameFullPathDate); - - char* requestedNewSinglePath = (char*)malloc(requestedNewStr.size() + 1); - strcpy(requestedNewSinglePath, requestedNewStr.c_str()); - logger->debug("recmoverename: to: {}", requestedNewSinglePath); - - requestedNewSinglePath = ExchangeChars(requestedNewSinglePath, true); - if (!strlen(requestedNewSinglePath)) throw 9; - logger->debug("recmoverename: EC: {}", requestedNewSinglePath); - - const char* vidDirStr = cVideoDirectory::Name(); - logger->debug("recmoverename: viddir: {}", vidDirStr); - - // Could be a new path - construct that first and test - - std::string newDirNameFullPathTitle = vidDirStr; - newDirNameFullPathTitle.append(requestedNewSinglePath); - free(requestedNewSinglePath); - - logger->debug("recmove: NPT: {}", newDirNameFullPathTitle); - - struct stat dstat; - int statret = stat(newDirNameFullPathTitle.c_str(), &dstat); - if ((statret == -1) && (errno == ENOENT)) // Dir does not exist - { - logger->debug("recmove: new path does not exist (1)"); - int mkdirret = mkdir(newDirNameFullPathTitle.c_str(), 0755); - if (mkdirret != 0) throw 4; - } - else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) - { - // Something exists but it's not a dir - throw 5; - } - - // New path now created or was there already - - newDirNameFullPathTitle.append(dirNameSingleTitle); - logger->debug("recmove: {}", newDirNameFullPathTitle); - - statret = stat(newDirNameFullPathTitle.c_str(), &dstat); - if ((statret == -1) && (errno == ENOENT)) // Dir does not exist - { - logger->debug("recmove: new dir does not exist (2)"); - int mkdirret = mkdir(newDirNameFullPathTitle.c_str(), 0755); - if (mkdirret != 0) throw 6; - } - else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) - { - // Something exists but it's not a dir - throw 7; - } - - // Ok, the directory container has been made, or it pre-existed. - - std::string newDirNameFullPathDate = newDirNameFullPathTitle + "/"; - newDirNameFullPathDate.append(dirNameSingleDate); - - logger->debug("recmove: doing rename '{}' '{}'", dirNameFullPathDate, newDirNameFullPathDate); - if (rename(dirNameFullPathDate.c_str(), newDirNameFullPathDate.c_str()) != 0) throw 8; + std::string newName; + if (requestedNewDirName.length()) newName = requestedNewDirName + FOLDERDELIMCHAR; + newName += recordingObj->BaseName(); - // Success. Test for remove old dir containter - rmdir(dirNameFullPathTitle.c_str()); // can't do anything about a fail result at this point. + logger->debug("RM2 NEWLOC: #{}#", newName); - // Test for remove old foldername - if (!dirNameSingleFolder.empty()) - { - std::string dirNameFullPathFolder = vidDirStr; - dirNameFullPathFolder.append("/"); - dirNameFullPathFolder.append(dirNameSingleFolder); - - logger->debug("recmove: oldfoldername: {}", dirNameFullPathFolder); - /* - DESCRIPTION - rmdir() deletes a directory, which must be empty. - ENOTEMPTY - pathname contains entries other than . and .. - So, should be safe to call rmdir on non-empty dir - */ - rmdir(dirNameFullPathFolder.c_str()); // can't do anything about a fail result at this point. - } + bool moveSuccess = recordingObj->ChangeName(newName.c_str()); - Recordings->Update(); - js["Result"] = true; - js["NewRecordingFileName"] = newDirNameFullPathDate; + js["Result"] = moveSuccess; + js["NewRecordingFileName"] = recordingObj->FileName(); + } + catch (BadParamException& e) + { + js["Result"] = false; + logger->error("recmove: Bad parameters"); + js["Error"] = "Bad request parameters"; } catch (int e) { js["Result"] = false; - if (e == 1) - { - logger->error("recmove: Bad parameters"); - js["Error"] = "Bad request parameters"; - } - else if (e == 2) + if (e == 2) { logger->error("recmove: Could not find recording to move"); js["Error"] = "Bad filename"; } - else if (e == 3) - { - logger->error("recmove: Could not move recording, it is still recording"); - js["Error"] = "Cannot move recording in progress"; - } - else if (e == 4) - { - logger->error("recmove: Failed to make new dir (1)"); - js["Error"] = "Failed to create new directory (1)"; - } - else if (e == 5) - { - logger->error("recmove: Something already exists? (1)"); - js["Error"] = "Something already exists at the new path (1)"; - } - else if (e == 6) - { - logger->error("recmove: Failed to make new dir (2)"); - js["Error"] = "Failed to create new directory (2)"; - } - else if (e == 7) - { - logger->error("recmove: Something already exists?"); - js["Error"] = "Something already exists at the new path"; - } - else if (e == 8) - { - logger->error("recmove: Rename failed"); - js["Error"] = "Move failed"; - } - else if (e == 9) - { - logger->error("recrename: ExchangeChars lost our string"); - js["Error"] = "Rename failed"; - } } return true; -- 2.39.2