From ca6c216f75b178ca4f215d628cacaf285656aa25 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sat, 20 Apr 2013 14:57:50 +0100 Subject: [PATCH] Move recording to new folder --- handler.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 86 insertions(+), 10 deletions(-) diff --git a/handler.c b/handler.c index c3c1a20..3a58e7a 100644 --- a/handler.c +++ b/handler.c @@ -396,6 +396,8 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) char* dateDirName = NULL; int k; char* titleDirName = NULL; int j; + char* folderName = NULL; int m; + char* newContainer = NULL; // Find the datedirname @@ -416,41 +418,96 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) { if (t[j] == '/') { - log->log("JSONServer", Log::DEBUG, "l2: %i", (k - j - 1) + 1); - titleDirName = new char[(k - j - 1) + 1]; + 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 + + 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--) + { + 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); - - ExchangeChars(sNewPath, true); + 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]; - log->log("JSONServer", Log::DEBUG, "l10: %i", 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) + { + 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; + } + } + 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); - struct stat dstat; - int statret = stat(newContainer, &dstat); + statret = stat(newContainer, &dstat); if ((statret == -1) && (errno == ENOENT)) // Dir does not exist { - log->log("JSONServer", Log::DEBUG, "new 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; - log->log("JSONServer", Log::ERR, "Failed to make new dir"); + log->log("JSONServer", Log::ERR, "Failed to make new dir (2)"); js["Result"] = false; - js["Error"] = "Failed to create new directory"; + js["Error"] = "Failed to create new directory (2)"; return true; } } @@ -458,6 +515,7 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) { delete[] dateDirName; delete[] titleDirName; + if (folderName) delete[] folderName; delete[] newContainer; log->log("JSONServer", Log::ERR, "Something already exists?"); @@ -483,6 +541,23 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) rmdir(oldTitleDir); // can't do anything about a fail result at this point. delete[] oldTitleDir; + // 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); + /* + 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; + } + ::Recordings.Update(); js["Result"] = true; js["NewRecordingFileName"] = newDir; @@ -497,6 +572,7 @@ bool jsonserver_recmove(Json::Value& js, const char* postData) delete[] dateDirName; delete[] titleDirName; delete[] newContainer; + if (folderName) delete[] folderName; delete[] newDir; return true; -- 2.39.5