From 0a54990183296d3899f88ea0a1619fc708b072bf Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sun, 21 May 2006 22:19:23 +0000 Subject: [PATCH] Completion of move recording code --- mvpclient.c | 164 ++++++++++++++++++++++++++++------------------------ 1 file changed, 87 insertions(+), 77 deletions(-) diff --git a/mvpclient.c b/mvpclient.c index f8f5a2b..69973af 100644 --- a/mvpclient.c +++ b/mvpclient.c @@ -403,72 +403,62 @@ int MVPClient::processMoveRecording(UCHAR* data, int length) cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName()); if (!rc) { -// if () - log->log("Client", Log::DEBUG, "moving recording: %s", recording->Name()); - log->log("Client", Log::DEBUG, "moving recording: %s", recording->FileName()); - log->log("Client", Log::DEBUG, "to: %s", newPath); + log->log("Client", Log::DEBUG, "moving recording: %s", recording->Name()); + log->log("Client", Log::DEBUG, "moving recording: %s", recording->FileName()); + log->log("Client", Log::DEBUG, "to: %s", newPath); - const char* t = recording->FileName(); + const char* t = recording->FileName(); - char* dateDirName = NULL; int k; - char* titleDirName = NULL; int j; + char* dateDirName = NULL; int k; + char* titleDirName = NULL; int j; - // Find the datedirname - for(k = strlen(t) - 1; k >= 0; k--) + // Find the datedirname + for(k = strlen(t) - 1; k >= 0; k--) + { + if (t[k] == '/') { - if (t[k] == '/') - { - log->log("Client", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1); - dateDirName = new char[strlen(&t[k+1]) + 1]; - strcpy(dateDirName, &t[k+1]); - break; - } + log->log("Client", 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--) + for(j = k-1; j >= 0; j--) + { + if (t[j] == '/') { - if (t[j] == '/') - { - log->log("Client", Log::DEBUG, "l2: %i", (k - j - 1) + 1); - titleDirName = new char[(k - j - 1) + 1]; - memcpy(titleDirName, &t[j+1], k - j - 1); - titleDirName[k - j - 1] = '\0'; - break; - } + log->log("Client", Log::DEBUG, "l2: %i", (k - j - 1) + 1); + titleDirName = new char[(k - j - 1) + 1]; + memcpy(titleDirName, &t[j+1], k - j - 1); + titleDirName[k - j - 1] = '\0'; + break; } + } - log->log("Client", Log::DEBUG, "datedirname: %s", dateDirName); - log->log("Client", Log::DEBUG, "titledirname: %s", titleDirName); + log->log("Client", Log::DEBUG, "datedirname: %s", dateDirName); + log->log("Client", Log::DEBUG, "titledirname: %s", titleDirName); - log->log("Client", Log::DEBUG, "viddir: %s", VideoDirectory); + log->log("Client", Log::DEBUG, "viddir: %s", VideoDirectory); - char* newContainer = new char[strlen(VideoDirectory) + strlen(newPath) + 1 + strlen(titleDirName) + 1]; - log->log("Client", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(newPath) + 1 + strlen(titleDirName) + 1); - sprintf(newContainer, "%s%s/%s", VideoDirectory, newPath, titleDirName); + char* newContainer = new char[strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1]; + log->log("Client", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1); + sprintf(newContainer, "%s%s%s", VideoDirectory, newPath, titleDirName); - // FIXME Check whether this already exists before mkdiring it + // FIXME Check whether this already exists before mkdiring it - log->log("Client", Log::DEBUG, "%s", newContainer); + log->log("Client", Log::DEBUG, "%s", newContainer); - struct stat dstat; - int statret = stat(newContainer, &dstat); - if ((statret == -1) && (errno == ENOENT)) // Dir does not exist - { - log->log("Client", Log::DEBUG, "new dir does not exist"); - int mkdirret = mkdir(newContainer, 0755); - if (mkdirret != 0) - { - delete[] dateDirName; - delete[] titleDirName; - delete[] newContainer; - sendULONG(5); - return 1; - } - } - else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir + struct stat dstat; + int statret = stat(newContainer, &dstat); + if ((statret == -1) && (errno == ENOENT)) // Dir does not exist + { + log->log("Client", Log::DEBUG, "new dir does not exist"); + int mkdirret = mkdir(newContainer, 0755); + if (mkdirret != 0) { delete[] dateDirName; delete[] titleDirName; @@ -476,37 +466,57 @@ int MVPClient::processMoveRecording(UCHAR* data, int length) sendULONG(5); return 1; } + } + else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir + { + delete[] dateDirName; + delete[] titleDirName; + delete[] newContainer; + sendULONG(5); + return 1; + } - // 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); + char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1]; + sprintf(newDir, "%s/%s", newContainer, dateDirName); - log->log("Client", Log::DEBUG, "doing rename '%s' '%s'", t, newDir); - int renameret = rename(t, newDir); - if (renameret == 0) - { - // Success. Test for remove old dir containter - char* oldTitleDir = new char[k+1]; - memcpy(oldTitleDir, t, k); - oldTitleDir[k] = '\0'; - log->log("Client", 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; - } + log->log("Client", Log::DEBUG, "doing rename '%s' '%s'", t, newDir); + int renameret = rename(t, newDir); + if (renameret == 0) + { + // Success. Test for remove old dir containter + char* oldTitleDir = new char[k+1]; + memcpy(oldTitleDir, t, k); + oldTitleDir[k] = '\0'; + log->log("Client", 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; + } - delete[] dateDirName; - delete[] titleDirName; - delete[] newContainer; - delete[] newDir; - - if (renameret == 0) sendULONG(1); - else sendULONG(5); -// } -// else -// { -// sendULONG(2); -// } + if (renameret == 0) + { + // Tell VDR + ::Recordings.Update(); + + // Success. Send a different packet from just a ulong + int totalLength = 4 + 4 + strlen(newDir) + 1; + UCHAR* sendBuffer = new UCHAR[totalLength]; + *(ULONG*)&sendBuffer[0] = htonl(totalLength - 4); + *(ULONG*)&sendBuffer[4] = htonl(1); // success + strcpy((char*)&sendBuffer[8], newDir); + tcp.sendPacket(sendBuffer, totalLength); + delete[] sendBuffer; + } + else + { + sendULONG(5); + } + + delete[] dateDirName; + delete[] titleDirName; + delete[] newContainer; + delete[] newDir; } else { -- 2.39.5