Permalink
Browse files

Get the file/dir reversion completely working for the new life preser…

…ver UI.

It should now make sure that the user/group/permissions are set properly for the reverted file to exactly match the file in the snapshot.
  • Loading branch information...
Ken Moore
Ken Moore committed Oct 14, 2013
1 parent f50ac43 commit 87e0a970c5f0cf1d77d548553e736398a09c3f73
View
@@ -66,3 +66,87 @@ LPDataset LPGUtils::loadPoolData(QString zpool){
//Return the dataset
return DSC;
}
+
+QString LPGUtils::generateReversionFileName(QString fileName, QString destDir){
+ fileName = fileName.section("/",-1); //Make sure we only have the filename (no paths)
+ if( !destDir.endsWith("/") ){ destDir.append("/"); }
+ //Quick check if that file already exists in the destination directory
+ if( !QFile::exists(destDir+fileName) ){ return fileName; }
+ //Change the filename to prevent overwriting an existing file
+ QString ext = fileName.section(".",-1); //Get the extension for the filename
+ if( fileName != ext && fileName != ("."+ext) ){
+ //Extension found - need careful adjustment of filename
+ QString newFileName = fileName;
+ newFileName.replace("."+ext, "-reversion."+ext);
+ int i=2;
+ while( QFile::exists(destDir+newFileName) ){
+ //Also need to append a reversion number
+ newFileName = fileName;
+ newFileName.replace("."+ext, "-reversion"+QString::number(i)+"."+ext);
+ i++; //for the next time around
+ }
+ fileName = newFileName;
+ }else{
+ //no extension - just append the reversion
+ fileName.append("-reversion");
+ if(QFile::exists(destDir+fileName) ){
+ //Also need to append a reversion number
+ int i=2;
+ while( QFile::exists(destDir+fileName+QString::number(i)) ){ i++; }
+ fileName.append( QString::number(i) );
+ }
+ }
+ return fileName;
+}
+
+bool LPGUtils::revertFile(QString oldPath, QString newPath){
+ bool ok = QFile::copy(oldPath,newPath);
+ //return the path to the new file if the copy was successful
+ if(ok){
+ //reset the permissions on the reverted file to match the original
+ QFile::setPermissions(newPath, QFile::permissions(oldPath));
+ QFileInfo FI(oldPath);
+ system( QString("chown "+FI.owner()+":"+FI.group()+" "+newPath).toUtf8() );
+ }
+ return ok;
+}
+
+QStringList LPGUtils::revertDir(QString oldPath, QString newPath){
+ //Note: this is a recursive function and can take quite a while to perform lots of file copies
+
+ //Load the directories and create it if necessary
+ QDir oDir(oldPath);
+ QDir nDir(newPath);
+ bool ok=true;
+ if( !nDir.exists() ){
+ //Create the new Directory
+ nDir.cdUp();
+ ok = nDir.mkdir(newPath.section("/",-1));
+ if(ok){
+ nDir.cd(newPath.section("/",-1));
+ QFile::setPermissions(newPath, QFile::permissions(oldPath)); //make sure the new dir has the old permissions
+ QFileInfo FI(oldPath);
+ system( QString("chown "+FI.owner()+":"+FI.group()+" "+newPath).toUtf8() );
+ }
+ }
+ //Get a list of any files that error
+ QStringList errors;
+ if(!ok){
+ errors << newPath;
+ return errors;
+ }
+ //Get a list of all the files in the old dir and copy them over
+ QStringList fList = oDir.entryList(QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot, QDir::Name);
+ for(int i=0; i<fList.length(); i++){
+ if( !revertFile(oldPath+"/"+fList[i], newPath+"/"+fList[i]) ){
+ errors << newPath+"/"+fList[i];
+ }
+ }
+ //Now list all the directories in the old dir and recursively copy them over
+ fList = oDir.entryList(QDir::Dirs | QDir::Hidden | QDir::NoDotAndDotDot, QDir::Name);
+ for(int i=0; i<fList.length(); i++){
+ QStringList errs = revertDir(oldPath+"/"+fList[i], newPath+"/"+fList[i]);
+ if( !errs.isEmpty() ){ errors << errs; }
+ }
+ return errors;
+}
View
@@ -5,14 +5,18 @@
#include <QDialog>
#include <QStringList>
#include <QString>
+#include <QFile>
+#include <QDir>
#include "LPBackend.h"
#include "LPContainers.h"
class LPGUtils{
public:
static LPDataset loadPoolData(QString zpool); //Load backend data into container
-
+ static QString generateReversionFileName(QString filename, QString destDir);
+ static bool revertFile(QString oldPath, QString newPath); //copy a file out of a snapshot
+ static QStringList revertDir(QString oldPath, QString newPath); //copy a dir out of a snapshot
};
#endif
View
@@ -216,6 +216,28 @@ void LPMain::prevSnapshot(){
void LPMain::restoreFiles(){
qDebug() << "Restore file(s)";
+ QString filePath = fsModel->filePath( ui->treeView->currentIndex() );
+ qDebug() << " - Selected File:" << filePath;
+ QString destDir = filePath.remove("/.zfs/snapshot/"+ui->label_snapshot->text());
+ destDir.remove( "/"+filePath.section("/",-1) ); //get rid of the filename at the end
+ qDebug() << " - Destination Dir:" << destDir;
+ QString newFilePath = destDir+"/"+LPGUtils::generateReversionFileName(filePath, destDir);
+ qDebug() << " - Destination File:" << newFilePath;
+
+ //Perform the reversion(s)
+ if(QFileInfo(filePath).isDir()){
+ //Is a directory
+ QStringList errors = LPGUtils::revertDir(filePath, newFilePath);
+ if(!errors.isEmpty()){
+ qDebug() << "Failed Reversions:" << errors;
+ }
+ }else{
+ //Just a single file
+ bool ok = LPGUtils::revertFile(filePath, newFilePath);
+ if( !ok ){
+ qDebug() << "Failed Reversion:" << newFilePath;
+ }
+ }
}
View
@@ -546,7 +546,7 @@
<action name="actionCompress_Home_Dir">
<property name="icon">
<iconset resource="lPreserve.qrc">
- <normaloff>:/images/upload.png</normaloff>:/images/upload.png</iconset>
+ <normaloff>:/images/box_add.png</normaloff>:/images/box_add.png</iconset>
</property>
<property name="text">
<string>Compress Home Dir</string>
@@ -555,7 +555,7 @@
<action name="actionExtract_Home_Dir">
<property name="icon">
<iconset resource="lPreserve.qrc">
- <normaloff>:/images/folder.png</normaloff>:/images/folder.png</iconset>
+ <normaloff>:/images/box_extract.png</normaloff>:/images/box_extract.png</iconset>
</property>
<property name="text">
<string>Extract Home Dir</string>
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -1,5 +1,7 @@
<RCC>
<qresource>
+ <file>images/box_add.png</file>
+ <file>images/box_extract.png</file>
<file>images/tray-icon-error.png</file>
<file>images/tray-icon-working.png</file>
<file>images/next.png</file>

0 comments on commit 87e0a97

Please sign in to comment.