From 0c85c8b48cc1d5b81db2c3ce1ba3798c1e232d0a Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Tue, 10 May 2022 17:17:23 +0100 Subject: [PATCH] Switch recrename to use cRecording::ChangeName() --- vdrclient.c | 190 ++++++---------------------------------------------- vdrclient.h | 8 --- 2 files changed, 19 insertions(+), 179 deletions(-) diff --git a/vdrclient.c b/vdrclient.c index 06f0fba..8a1d8b1 100644 --- a/vdrclient.c +++ b/vdrclient.c @@ -826,197 +826,45 @@ bool VDRClient::recdel(PFMap& postFields, Json::Value& js) // RETHROWS return true; } -void VDRClient::pathsForRecordingName(const cRecordings* Recordings, const std::string& recordingName, - std::string& dirNameSingleDate, - std::string& dirNameSingleTitle, - std::string& dirNameSingleFolder, - std::string& dirNameFullPathTitle, - std::string& dirNameFullPathDate) // throws int -{ - const char* t; - { - const cRecording* recordingObj = Recordings->GetByName(recordingName.c_str()); - if (!recordingObj) throw 2; - t = recordingObj->FileName(); - - cRecordControl *rc = cRecordControls::GetRecordControl(recordingObj->FileName()); - if (rc) throw 3; - - logger->debug("paths: recording: {}", recordingObj->Name()); - } // unlock recordings - - logger->debug("paths: recording: {}", t); - - dirNameFullPathDate = t; - int k, j, m; - - // Find the datedirname - for(k = strlen(t) - 1; k >= 0; k--) - { - if (t[k] == '/') - { - logger->debug("recmoverename: l1: {}", strlen(&t[k+1]) + 1); - dirNameSingleDate.assign(&t[k+1], strlen(t) - k - 1); - logger->debug("paths: dirNameSingleDate: '{}'", dirNameSingleDate); - break; - } - } - - // Find the titledirname - - for(j = k-1; j >= 0; j--) - { - if (t[j] == '/') - { - logger->debug("recmoverename: l2: {}", k - j); - dirNameSingleTitle.assign(&t[j+1], k - j - 1); - logger->debug("paths: dirNameSingleTitle: '{}'", dirNameSingleTitle); - break; - } - } - - // Find the foldername - - const char* vidDirStr = cVideoDirectory::Name(); - int vidDirStrLen = strlen(vidDirStr); - - logger->debug("recmoverename: j = {}, strlenvd = {}", j, vidDirStrLen); - if (j > vidDirStrLen) // Rec is in a subfolder now - { - for(m = j-1; m >= 0; m--) - { - if (t[m] == '/') - { - logger->debug("recmoverename: l3: {}", j - m); - dirNameSingleFolder.assign(&t[m+1], j - m - 1); - logger->debug("paths: dirNameSingleFolder: '{}'", dirNameSingleFolder); - break; - } - } - } - - dirNameFullPathTitle.assign(t, k); - logger->debug("paths: dirNameFullPathTitle: '{}'", dirNameFullPathTitle); -} - bool VDRClient::recrename(PFMap& postFields, Json::Value& js) // RETHROWS { logger->debug("recrename"); std::string fileNameToAffect = getVarString(postFields, "filename"); - std::string requestedNewStr = getVarString(postFields, "newname"); - - std::string dirNameSingleDate; - std::string dirNameSingleTitle; - std::string dirNameSingleFolder; - std::string dirNameFullPathTitle; - std::string dirNameFullPathDate; - -#warning Switch this to Recording->ChangeName ? + std::string requestedNewName = getVarString(postFields, "newname"); try { LOCK_RECORDINGS_WRITE; + cRecording* recordingObj = Recordings->GetByName(fileNameToAffect.c_str()); + if (!recordingObj) throw 2; - pathsForRecordingName(Recordings, - fileNameToAffect, dirNameSingleDate, - dirNameSingleTitle, dirNameSingleFolder, - dirNameFullPathTitle, dirNameFullPathDate); - - char* requestedNewSingleTitle = (char*)malloc(requestedNewStr.size() + 1); - strcpy(requestedNewSingleTitle, requestedNewStr.c_str()); - logger->debug("recmoverename: to: {}", requestedNewSingleTitle); - - requestedNewSingleTitle = ExchangeChars(requestedNewSingleTitle, true); - if (!strlen(requestedNewSingleTitle)) throw 9; - logger->debug("recmoverename: EC: {}", requestedNewSingleTitle); - - 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("/"); - - if (!dirNameSingleFolder.empty()) - { - newDirNameFullPathTitle.append(dirNameSingleFolder); - newDirNameFullPathTitle.append("/"); - } - - newDirNameFullPathTitle.append(requestedNewSingleTitle); - free(requestedNewSingleTitle); - - logger->debug("recrename: NPT2: {}", newDirNameFullPathTitle); - - struct stat dstat; - int statret = stat(newDirNameFullPathTitle.c_str(), &dstat); - if ((statret == -1) && (errno == ENOENT)) // Dir does not exist - { - logger->debug("recrename: 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 - - std::string newDirNameFullPathDate = newDirNameFullPathTitle + "/"; - newDirNameFullPathDate.append(dirNameSingleDate); + std::string newName(recordingObj->Folder()); + logger->debug("newname after folder copy: #{}#"); + if (newName.length()) newName += FOLDERDELIMCHAR; + newName += requestedNewName; - logger->debug("recrename: doing rename '{}' '{}'", dirNameFullPathDate, newDirNameFullPathDate); - if (rename(dirNameFullPathDate.c_str(), newDirNameFullPathDate.c_str()) != 0) throw 8; + logger->debug("RM2 NEWLOC: #{}#", newName); - // Success. Test for remove old dir containter - rmdir(dirNameFullPathTitle.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("recrename: Bad parameters"); - js["Error"] = "Bad request parameters"; - } - else if (e == 2) + if (e == 2) { - logger->error("recrename: Could not find recording to move"); + logger->error("recmove: Could not find recording to move"); js["Error"] = "Bad filename"; } - else if (e == 3) - { - logger->error("recrename: Could not move recording, it is still recording"); - js["Error"] = "Cannot move recording in progress"; - } - else if (e == 4) - { - logger->error("recrename: Failed to make new dir (1)"); - js["Error"] = "Failed to create new directory (1)"; - } - else if (e == 5) - { - logger->error("recrename: Something already exists? (1)"); - js["Error"] = "Something already exists at the new path (1)"; - } - else if (e == 8) - { - logger->error("recrename: Rename failed"); - js["Error"] = "Rename failed"; - } - else if (e == 9) - { - logger->error("recrename: ExchangeChars lost our string"); - js["Error"] = "Rename failed"; - } } return true; diff --git a/vdrclient.h b/vdrclient.h index bf7e3fd..926f107 100644 --- a/vdrclient.h +++ b/vdrclient.h @@ -68,14 +68,6 @@ class VDRClient const cEvent* getEvent(const cChannels* Channels, const cSchedules* Schedules, Json::Value& js, int channelNumber, int eventID, int aroundTime); - void pathsForRecordingName(const cRecordings* Recordings, - const std::string& recordingName, - std::string& dirNameSingleDate, - std::string& dirNameSingleTitle, - std::string& dirNameSingleFolder, - std::string& dirNameFullPathTitle, - std::string& dirNameFullPathDate); // throws int - int getVarInt(PFMap& postFields, const char* paramName); // THROWS std::string getVarString(PFMap& postFields, const char* paramName, bool emptyOk = false); // THROWS -- 2.39.5