Skip to content
Browse files

Updated the DeleteFile and RenameFile service API methods to use stor…

…age groups. Added more checks to the RenameFile method.

Signed-off-by: Stuart Morgan <smorgan@mythtv.org>
  • Loading branch information...
1 parent c7f317e commit 1ed2cf0f61fa9a0d074ee10717ffc75194cb630a @rsiebert rsiebert committed with stuartm Mar 15, 2013
View
6 mythtv/libs/libmythservicecontracts/services/contentServices.h
@@ -106,9 +106,11 @@ class SERVICE_PUBLIC ContentServices : public Service //, public QScriptable ??
virtual bool DownloadFile ( const QString &URL,
const QString &StorageGroup ) = 0;
- virtual bool DeleteFile ( const QString &FileName ) = 0;
+ virtual bool DeleteFile ( const QString &StorageGroup,
+ const QString &FileName ) = 0;
- virtual bool RenameFile ( const QString &FileName,
+ virtual bool RenameFile ( const QString &StorageGroup,
+ const QString &FileName,
const QString &NewName ) = 0;
virtual DTC::LiveStreamInfo *AddLiveStream ( const QString &StorageGroup,
View
78 mythtv/programs/mythbackend/services/content.cpp
@@ -822,62 +822,88 @@ bool Content::DownloadFile( const QString &sURL, const QString &sStorageGroup )
return false;
}
-/** \fn Content::DeleteImage(const QString &sFileName )
+/** \fn Content::DeleteImage( const QString &sStorageGroup,
+ * const QString &sFileName )
* \brief Permanently deletes the given file from the disk.
- * \param sFileName The full filename that shall be deleted
+ * \param sStorageGroup The storage group name where the image is located
+ * \param sFileName The filename including the path that shall be deleted
* \return bool True if deletion was successful, otherwise false
*/
-bool Content::DeleteFile( const QString &sFileName )
+bool Content::DeleteFile( const QString &sStorageGroup,
+ const QString &sFileName )
{
- if (sFileName.isEmpty())
- {
- LOG(VB_GENERAL, LOG_ERR, "DeleteFile - FileName is missing");
- return false;
- }
+ // Get the fileinfo object
+ QFileInfo fileInfo = GetFile(sStorageGroup, sFileName);
- if (!QFile::exists( sFileName ))
+ // Check if the file exists. Only then we can actually delete it.
+ if (!fileInfo.isFile() && !QFile::exists( fileInfo.absoluteFilePath()))
{
- LOG(VB_GENERAL, LOG_ERR, "DeleteFile - FileName does not exist.");
+ LOG(VB_GENERAL, LOG_ERR, "DeleteFile - File does not exist.");
return false;
}
- return QFile::remove( sFileName );
+ return QFile::remove( fileInfo.absoluteFilePath() );
}
-/** \fn Content::RenameFile(const QString &sFileName,
+/** \fn Content::RenameFile(const QString &sStorageGroup,
+ * const QString &sFileName,
* const QString &sNewFile)
* \brief Renames the file to the new name.
- * \param sFileName The full filename that shall be deleted
+ * \param sStorageGroup The storage group name where the image is located
+ * \param sFileName The filename including the path that shall be renamed
* \param sNewName The new name of the file (only the name, no path)
* \return bool True if renaming was successful, otherwise false
*/
-bool Content::RenameFile( const QString &sFileName,
+bool Content::RenameFile( const QString &sStorageGroup,
+ const QString &sFileName,
const QString &sNewName)
{
- if (sFileName.isEmpty())
+ QFileInfo fi = QFileInfo();
+ fi = GetFile(sStorageGroup, sFileName);
+
+ // Check if the file exists and is writable.
+ // Only then we can actually delete it.
+ if (!fi.isFile() && !QFile::exists(fi.absoluteFilePath()))
{
- LOG(VB_GENERAL, LOG_ERR, "RenameFile - FileName is missing");
+ LOG(VB_GENERAL, LOG_ERR, "RenameFile - File does not exist.");
return false;
}
- if (!QFile::exists( sFileName ))
+ // Check if the new filename has no path stuff specified
+ if (sNewName.contains("/") || sNewName.contains("\\"))
{
- LOG(VB_GENERAL, LOG_ERR, "RenameFile - FileName does not exist.");
+ LOG(VB_GENERAL, LOG_ERR, "RenameFile - New file must not contain a path.");
return false;
}
-
- QFileInfo fileInfo(sFileName);
- QString sNewFileName = fileInfo.path() + "/" + sNewName;
- if (QFile::exists( sNewFileName ))
+
+ // The newly renamed file must be in the same path as the original one.
+ // So we need to check if a file of the new name exists and would be
+ // overwritten by the new filename. To to this get the path from the
+ // original file and append the new file name, Then check
+ // if it exists in the given storage group
+
+ // Get the everthing until the last directory separator
+ QString path = sFileName.left(sFileName.lastIndexOf("/"));
+ // Append the new file name to the path
+ QString newFileName = path.append("/").append(sNewName);
+
+ QFileInfo nfi = QFileInfo();
+ nfi = GetFile(sStorageGroup, newFileName);
+
+ // Check if the target file is already present.
+ // If is there then abort, overwriting is not supported
+ if (nfi.isFile() || QFile::exists(nfi.absoluteFilePath()))
{
LOG(VB_GENERAL, LOG_ERR,
QString("RenameFile - New file %1 would overwrite "
- "existing one, not renaming.").arg(sNewFileName));
+ "existing one, not renaming.").arg(sFileName));
return false;
}
- // If the path of the new filename is not the same
- // as the original one, then the renaming will fail.
- return QFile::rename( sFileName, sNewFileName );
+ // All checks have been passed, rename the file
+ QFile file;
+ file.setFileName(fi.fileName());
+ QDir::setCurrent(fi.absolutePath());
+ return file.rename(sNewName);
}
/////////////////////////////////////////////////////////////////////////////
View
6 mythtv/programs/mythbackend/services/content.h
@@ -87,9 +87,11 @@ class Content : public ContentServices
bool DownloadFile ( const QString &URL,
const QString &StorageGroup );
- bool DeleteFile ( const QString &FileName );
+ bool DeleteFile ( const QString &StorageGroup,
+ const QString &FileName );
- bool RenameFile ( const QString &FileName,
+ bool RenameFile ( const QString &StorageGroup,
+ const QString &FileName,
const QString &NewName );
// HTTP Live Streaming

0 comments on commit 1ed2cf0

Please sign in to comment.
Something went wrong with that request. Please try again.