From 92bb94baa277acf3fe9c7efc202dd96947aeba9d Mon Sep 17 00:00:00 2001
From: Chris Tallon <chris@vomp.tv>
Date: Sat, 20 Apr 2013 15:37:06 +0100
Subject: [PATCH] Refactor rec-move

---
 handler.c | 335 +++++++++++++++++++++++++++---------------------------
 1 file changed, 166 insertions(+), 169 deletions(-)

diff --git a/handler.c b/handler.c
index 3a58e7a..25c678f 100644
--- a/handler.c
+++ b/handler.c
@@ -356,224 +356,221 @@ bool jsonserver_recmove(Json::Value& js, const char* postData)
   Log* log = Log::getInstance();
   log->log("JSONServer", Log::DEBUG, "recmove");
 
-  char sFileName[1000]; int mgv1 = mg_get_var(postData, strlen(postData), "filename", sFileName, 1000);  
-  char sNewPath[1000];  int mgv2 = mg_get_var(postData, strlen(postData), "newpath", sNewPath, 1000);  
-
-  if ( (mgv1 == -1) || (mgv2 == -1) || !strlen(sFileName) || !strlen(sNewPath) )
+  char* fileNameToMove = NULL;
+  char* requestedNewPath = NULL;
+  char* dateDirName = NULL;
+  char* titleDirName = NULL;
+  char* folderName = NULL;
+  char* newContainer = NULL;
+  char* newDir = NULL;
+  
+  try
   {
-    log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2);
-    js["Result"] = false;
-    js["Error"] = "Bad request parameters";
-    return true;
-  }
+    int postDataLen = strlen(postData)+1;
+    fileNameToMove = new char[postDataLen];
+    int mgv1 = mg_get_var(postData, postDataLen-1, "filename", fileNameToMove, postDataLen);
+    requestedNewPath = new char[postDataLen];
+    int mgv2 = mg_get_var(postData, postDataLen-1, "newpath", requestedNewPath, postDataLen);
 
-  cRecordings Recordings;
-  Recordings.Load(); // probably have to do this
-  cRecording* recording = Recordings.GetByName(sFileName);
+    if ((mgv1 == -1) || (mgv2 == -1) || !strlen(fileNameToMove) || !strlen(requestedNewPath))
+    {
+      log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2);
+      throw 1;
+    }
 
-  if (!recording)
-  {
-    log->log("JSONServer", Log::ERR, "Could not find recording to move");
-    js["Result"] = false;
-    js["Error"] = "Bad filename";
-    return true;
-  }
+    cRecordings Recordings;
+    Recordings.Load(); // probably have to do this
+    cRecording* recordingObj = Recordings.GetByName(fileNameToMove);
+    if (!recordingObj) throw 2;
+    
+    cRecordControl *rc = cRecordControls::GetRecordControl(recordingObj->FileName());
+    if (rc) throw 3;
 
-  cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
-  if (rc)
-  {
-    log->log("JSONServer", Log::ERR, "Could not move recording, it is still recording");
-    js["Result"] = false;
-    js["Error"] = "Cannot move recording in progress";
-    return true;
-  }
-  
-  log->log("JSONServer", Log::DEBUG, "moving recording: %s", recording->Name());
-  log->log("JSONServer", Log::DEBUG, "moving recording: %s", recording->FileName());
-  log->log("JSONServer", Log::DEBUG, "to: %s", sNewPath);
+    log->log("JSONServer", Log::DEBUG, "moving recording: %s", recordingObj->Name());
+    log->log("JSONServer", Log::DEBUG, "moving recording: %s", recordingObj->FileName());
+    log->log("JSONServer", Log::DEBUG, "to: %s", requestedNewPath);
 
-  const char* t = recording->FileName();
+    const char* t = recordingObj->FileName();
 
-  char* dateDirName = NULL;   int k;
-  char* titleDirName = NULL;  int j;
-  char* folderName = NULL;    int m;
-  
-  char* newContainer = NULL;
+    int k, j, m;
 
-  // Find the datedirname
-  for(k = strlen(t) - 1; k >= 0; k--)
-  {
-    if (t[k] == '/')
+    // Find the datedirname
+    for(k = strlen(t) - 1; k >= 0; k--)
     {
-      log->log("JSONServer", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
-      dateDirName = new char[strlen(&t[k+1]) + 1];
-      strcpy(dateDirName, &t[k+1]);
-      break;
+      if (t[k] == '/')
+      {
+        log->log("JSONServer", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
+        dateDirName = new char[strlen(&t[k+1]) + 1];
+        strcpy(dateDirName, &t[k+1]);
+        break;
+      }
     }
-  }
 
-  // Find the titledirname
+    // Find the titledirname
 
-  for(j = k-1; j >= 0; j--)
-  {
-    if (t[j] == '/')
+    for(j = k-1; j >= 0; j--)
     {
-      log->log("JSONServer", Log::DEBUG, "l2: %i", k - j);
-      titleDirName = new char[k - j];
-      memcpy(titleDirName, &t[j+1], k - j - 1);
-      titleDirName[k - j - 1] = '\0';
-      break;
+      if (t[j] == '/')
+      {
+        log->log("JSONServer", Log::DEBUG, "l2: %i", k - j);
+        titleDirName = new char[k - j];
+        memcpy(titleDirName, &t[j+1], k - j - 1);
+        titleDirName[k - j - 1] = '\0';
+        break;
+      }
     }
-  }
 
-  // Find the foldername
+    // Find the foldername
 
-  log->log("JSONServer", Log::DEBUG, "j = %u, strlenvd = %u", j, strlen(VideoDirectory));
-  if (j > strlen(VideoDirectory)) // Rec is in a subfolder now
-  {
-    for(m = j-1; m >= 0; m--)
+    log->log("JSONServer", Log::DEBUG, "j = %u, strlenvd = %u", j, strlen(VideoDirectory));
+    if (j > strlen(VideoDirectory)) // Rec is in a subfolder now
     {
-      if (t[m] == '/')
+      for(m = j-1; m >= 0; m--)
       {
-        log->log("JSONServer", Log::DEBUG, "l3: %i", j - m);
-        folderName = new char[j - m];
-        memcpy(folderName, &t[m+1], j - m - 1);
-        folderName[j - m - 1] = '\0';
-        break;
+        if (t[m] == '/')
+        {
+          log->log("JSONServer", Log::DEBUG, "l3: %i", j - m);
+          folderName = new char[j - m];
+          memcpy(folderName, &t[m+1], j - m - 1);
+          folderName[j - m - 1] = '\0';
+          break;
+        }
       }
     }
-  }
 
-  ExchangeChars(sNewPath, true);
-  
-  log->log("JSONServer", Log::DEBUG, "datedirname: %s", dateDirName);
-  log->log("JSONServer", Log::DEBUG, "titledirname: %s", titleDirName);
-  log->log("JSONServer", Log::DEBUG, "viddir: %s", VideoDirectory);
-  if (folderName) log->log("JSONServer", Log::DEBUG, "folderName: %s", folderName);
-  log->log("JSONServer", Log::DEBUG, "EC: %s", sNewPath);
-
-  // Could be a new path - construct that first and test
-  newContainer = new char[strlen(VideoDirectory) + strlen(sNewPath) + strlen(titleDirName) + 1];
-  sprintf(newContainer, "%s%s", VideoDirectory, sNewPath);
-  log->log("JSONServer", Log::DEBUG, "NPT: %s", newContainer);
-  struct stat dstat;
-  int statret = stat(newContainer, &dstat);
-  if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
-  {
-    log->log("JSONServer", Log::DEBUG, "new path does not exist (1)");
-    int mkdirret = mkdir(newContainer, 0755);
-    if (mkdirret != 0)
+    ExchangeChars(requestedNewPath, true);
+
+    log->log("JSONServer", Log::DEBUG, "datedirname: %s", dateDirName);
+    log->log("JSONServer", Log::DEBUG, "titledirname: %s", titleDirName);
+    log->log("JSONServer", Log::DEBUG, "viddir: %s", VideoDirectory);
+    if (folderName) log->log("JSONServer", Log::DEBUG, "folderName: %s", folderName);
+    log->log("JSONServer", Log::DEBUG, "EC: %s", requestedNewPath);
+
+    // Could be a new path - construct that first and test
+    newContainer = new char[strlen(VideoDirectory) + strlen(requestedNewPath) + strlen(titleDirName) + 1];
+    sprintf(newContainer, "%s%s", VideoDirectory, requestedNewPath);
+    log->log("JSONServer", Log::DEBUG, "NPT: %s", newContainer);
+    struct stat dstat;
+    int statret = stat(newContainer, &dstat);
+    if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
     {
-      delete[] dateDirName;
-      delete[] titleDirName;
-      if (folderName) delete[] folderName;
-      delete[] newContainer;
-      
-      log->log("JSONServer", Log::ERR, "Failed to make new dir (1)");
-      js["Result"] = false;
-      js["Error"] = "Failed to create new directory (1)";
-      return true;
+      log->log("JSONServer", Log::DEBUG, "new path does not exist (1)");
+      int mkdirret = mkdir(newContainer, 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;
     }
-  }
-  else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
-  {
-    delete[] dateDirName;
-    delete[] titleDirName;
-    if (folderName) delete[] folderName;
-    delete[] newContainer;
-    
-    log->log("JSONServer", Log::ERR, "Something already exists? (1)");
-    js["Result"] = false;
-    js["Error"] = "Something already exists at the new path (1)";
-    return true;
-  }
 
-  // New path now created or was there already
-  
-  sprintf(newContainer, "%s%s%s", VideoDirectory, sNewPath, titleDirName);
-  log->log("JSONServer", Log::DEBUG, "%s", newContainer);
+    // New path now created or was there already
 
-  statret = stat(newContainer, &dstat);
-  if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
-  {
-    log->log("JSONServer", Log::DEBUG, "new dir does not exist (2)");
-    int mkdirret = mkdir(newContainer, 0755);
-    if (mkdirret != 0)
-    {
-      delete[] dateDirName;
-      delete[] titleDirName;
-      if (folderName) delete[] folderName;
-      delete[] newContainer;
+    sprintf(newContainer, "%s%s%s", VideoDirectory, requestedNewPath, titleDirName);
+    log->log("JSONServer", Log::DEBUG, "%s", newContainer);
 
-      log->log("JSONServer", Log::ERR, "Failed to make new dir (2)");
-      js["Result"] = false;
-      js["Error"] = "Failed to create new directory (2)";
-      return true;
+    statret = stat(newContainer, &dstat);
+    if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
+    {
+      log->log("JSONServer", Log::DEBUG, "new dir does not exist (2)");
+      int mkdirret = mkdir(newContainer, 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;
     }
-  }
-  else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
-  {
-    delete[] dateDirName;
-    delete[] titleDirName;
-    if (folderName) delete[] folderName;
-    delete[] newContainer;
-
-    log->log("JSONServer", Log::ERR, "Something already exists?");
-    js["Result"] = false;
-    js["Error"] = "Something already exists at the new path";
-    return true;
-  }
 
-  // Ok, the directory container has been made, or it pre-existed.
+    // Ok, the directory container has been made, or it pre-existed.
 
-  char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
-  sprintf(newDir, "%s/%s", newContainer, dateDirName);
+    newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
+    sprintf(newDir, "%s/%s", newContainer, dateDirName);
 
-  log->log("JSONServer", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
-  int renameret = rename(t, newDir);
-  if (renameret == 0)
-  {
+    log->log("JSONServer", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
+    if (rename(t, newDir) != 0) throw 8;
+    
     // Success. Test for remove old dir containter
-    char* oldTitleDir = new char[k+1];
-    memcpy(oldTitleDir, t, k);
-    oldTitleDir[k] = '\0';
-    log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(oldTitleDir), oldTitleDir);
-    rmdir(oldTitleDir); // can't do anything about a fail result at this point.
-    delete[] oldTitleDir;
+    char* tempOldTitleDir = new char[k+1];
+    memcpy(tempOldTitleDir, t, k);
+    tempOldTitleDir[k] = '\0';
+    log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(tempOldTitleDir), tempOldTitleDir);
+    rmdir(tempOldTitleDir); // can't do anything about a fail result at this point.
+    delete[] tempOldTitleDir;
 
     // Test for remove old foldername
     if (folderName)
     {
-      char* oldFolderName = new char[j+1];
-      memcpy(oldFolderName, t, j);
-      oldFolderName[j] = '\0';
-      log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldfoldername: %s", j+1, j, strlen(oldFolderName), oldFolderName);
+      char* tempOldFolderName = new char[j+1];
+      memcpy(tempOldFolderName, t, j);
+      tempOldFolderName[j] = '\0';
+      log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldfoldername: %s", j+1, j, strlen(tempOldFolderName), tempOldFolderName);
       /*
       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(oldFolderName); // can't do anything about a fail result at this point.
-      delete[] oldFolderName;
+      rmdir(tempOldFolderName); // can't do anything about a fail result at this point.
+      delete[] tempOldFolderName;
     }
-    
+
     ::Recordings.Update();
     js["Result"] = true;
     js["NewRecordingFileName"] = newDir;
   }
-  else
+  catch (int e)
   {
-    log->log("JSONServer", Log::ERR, "Rename failed");
     js["Result"] = false;
-    js["Error"] = "Move failed";
+    if (e == 1)
+    {
+      log->log("JSONServer", Log::ERR, "Bad parameters");
+      js["Error"] = "Bad request parameters";
+    }
+    else if (e == 2)
+    {
+      log->log("JSONServer", Log::ERR, "Could not find recording to move");
+      js["Error"] = "Bad filename";
+    }
+    else if (e == 3)
+    {
+      log->log("JSONServer", Log::ERR, "Could not move recording, it is still recording");
+      js["Error"] = "Cannot move recording in progress";
+    }
+    else if (e == 4)
+    {
+      log->log("JSONServer", Log::ERR, "Failed to make new dir (1)");
+      js["Error"] = "Failed to create new directory (1)";
+    }
+    else if (e == 5)
+    {
+      log->log("JSONServer", Log::ERR, "Something already exists? (1)");
+      js["Error"] = "Something already exists at the new path (1)";
+    }
+    else if (e == 6)
+    {
+      log->log("JSONServer", Log::ERR, "Failed to make new dir (2)");
+      js["Error"] = "Failed to create new directory (2)";
+    }
+    else if (e == 7)
+    {
+      log->log("JSONServer", Log::ERR, "Something already exists?");
+      js["Error"] = "Something already exists at the new path";
+    }
+    else if (e == 8)
+    {
+      log->log("JSONServer", Log::ERR, "Rename failed");
+      js["Error"] = "Move failed";
+    }
   }
 
-  delete[] dateDirName;
-  delete[] titleDirName;
-  delete[] newContainer;
-  if (folderName) delete[] folderName;
-  delete[] newDir;
+  if (fileNameToMove)   delete[] fileNameToMove;
+  if (requestedNewPath) delete[] requestedNewPath;
+  if (dateDirName)      delete[] dateDirName;
+  if (titleDirName)     delete[] titleDirName;
+  if (folderName)       delete[] folderName;
+  if (newContainer)     delete[] newContainer;
+  if (newDir)           delete[] newDir;
 
   return true;
 }
-- 
2.39.5