Ensure backup directory exists
Failed backups result in a user error message and asks to save the file anyways. Closes #2346, Closes #2441
This commit is contained in:
parent
b3f56a47cf
commit
a82d9f9981
|
@ -28,6 +28,7 @@
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <shlwapi.h>
|
#include <shlwapi.h>
|
||||||
|
#include <ShlObj.h>
|
||||||
#include "Notepad_plus_Window.h"
|
#include "Notepad_plus_Window.h"
|
||||||
#include "FileDialog.h"
|
#include "FileDialog.h"
|
||||||
#include "EncodingMapper.h"
|
#include "EncodingMapper.h"
|
||||||
|
@ -1157,69 +1158,56 @@ bool Notepad_plus::fileSave(BufferID id)
|
||||||
|
|
||||||
if (!buf->getFileReadOnly() && buf->isDirty()) //cannot save if readonly
|
if (!buf->getFileReadOnly() && buf->isDirty()) //cannot save if readonly
|
||||||
{
|
{
|
||||||
const TCHAR *fn = buf->getFullPathName();
|
|
||||||
if (buf->isUntitled())
|
if (buf->isUntitled())
|
||||||
{
|
{
|
||||||
return fileSaveAs(bufferID);
|
return fileSaveAs(bufferID);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
const NppGUI & nppgui = (NppParameters::getInstance())->getNppGUI();
|
const NppGUI & nppgui = (NppParameters::getInstance())->getNppGUI();
|
||||||
BackupFeature backup = nppgui._backup;
|
BackupFeature backup = nppgui._backup;
|
||||||
TCHAR *name = ::PathFindFileName(fn);
|
|
||||||
|
|
||||||
if (backup == bak_simple)
|
if (backup != bak_none)
|
||||||
{
|
{
|
||||||
//copy fn to fn.backup
|
const TCHAR *fn = buf->getFullPathName();
|
||||||
generic_string fn_bak(fn);
|
TCHAR *name = ::PathFindFileName(fn);
|
||||||
if ((nppgui._useDir) && (nppgui._backupDir != TEXT("")))
|
generic_string fn_bak;
|
||||||
|
|
||||||
|
if (nppgui._useDir && not nppgui._backupDir.empty())
|
||||||
{
|
{
|
||||||
|
// Get the custom directory, make sure it has a trailing slash
|
||||||
fn_bak = nppgui._backupDir;
|
fn_bak = nppgui._backupDir;
|
||||||
|
if (fn_bak.back() != TEXT('\\'))
|
||||||
fn_bak += TEXT("\\");
|
fn_bak += TEXT("\\");
|
||||||
fn_bak += name;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fn_bak = fn;
|
// Get the current file's directory
|
||||||
}
|
generic_string path = fn;
|
||||||
fn_bak += TEXT(".bak");
|
::PathRemoveFileSpec(path);
|
||||||
|
fn_bak = path.c_str();
|
||||||
|
fn_bak += TEXT("\\");
|
||||||
|
|
||||||
if (not ::CopyFile(fn, fn_bak.c_str(), FALSE))
|
// If verbose, save it in a sub folder
|
||||||
|
if (backup == bak_verbose)
|
||||||
{
|
{
|
||||||
return false;
|
fn_bak += TEXT("nppBackup\\");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make sure the directory exists
|
||||||
|
if (!::PathFileExists(fn_bak.c_str()))
|
||||||
|
{
|
||||||
|
SHCreateDirectory(NULL, fn_bak.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine what to name the backed-up file
|
||||||
|
if (backup == bak_simple)
|
||||||
|
{
|
||||||
|
fn_bak += name;
|
||||||
|
fn_bak += TEXT(".bak");
|
||||||
|
}
|
||||||
else if (backup == bak_verbose)
|
else if (backup == bak_verbose)
|
||||||
{
|
{
|
||||||
generic_string fn_dateTime_bak(TEXT(""));
|
|
||||||
|
|
||||||
if ((nppgui._useDir) && (nppgui._backupDir != TEXT("")))
|
|
||||||
{
|
|
||||||
fn_dateTime_bak = nppgui._backupDir;
|
|
||||||
fn_dateTime_bak += TEXT("\\");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
const TCHAR *bakDir = TEXT("nppBackup");
|
|
||||||
|
|
||||||
// std::string path should be a temp throwable variable
|
|
||||||
generic_string path = fn;
|
|
||||||
::PathRemoveFileSpec(path);
|
|
||||||
fn_dateTime_bak = path.c_str();
|
|
||||||
|
|
||||||
|
|
||||||
fn_dateTime_bak += TEXT("\\");
|
|
||||||
fn_dateTime_bak += bakDir;
|
|
||||||
fn_dateTime_bak += TEXT("\\");
|
|
||||||
|
|
||||||
if (!::PathFileExists(fn_dateTime_bak.c_str()))
|
|
||||||
{
|
|
||||||
::CreateDirectory(fn_dateTime_bak.c_str(), NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn_dateTime_bak += name;
|
|
||||||
|
|
||||||
const int temBufLen = 32;
|
const int temBufLen = 32;
|
||||||
TCHAR tmpbuf[temBufLen];
|
TCHAR tmpbuf[temBufLen];
|
||||||
time_t ltime = time(0);
|
time_t ltime = time(0);
|
||||||
|
@ -1228,17 +1216,26 @@ bool Notepad_plus::fileSave(BufferID id)
|
||||||
today = localtime(<ime);
|
today = localtime(<ime);
|
||||||
generic_strftime(tmpbuf, temBufLen, TEXT("%Y-%m-%d_%H%M%S"), today);
|
generic_strftime(tmpbuf, temBufLen, TEXT("%Y-%m-%d_%H%M%S"), today);
|
||||||
|
|
||||||
fn_dateTime_bak += TEXT(".");
|
fn_bak += name;
|
||||||
fn_dateTime_bak += tmpbuf;
|
fn_bak += TEXT(".");
|
||||||
fn_dateTime_bak += TEXT(".bak");
|
fn_bak += tmpbuf;
|
||||||
|
fn_bak += TEXT(".bak");
|
||||||
|
}
|
||||||
|
|
||||||
if (not ::CopyFile(fn, fn_dateTime_bak.c_str(), FALSE))
|
if (not ::CopyFile(fn, fn_bak.c_str(), FALSE))
|
||||||
|
{
|
||||||
|
generic_string msg = TEXT("The previous version of the file could not be saved into the backup directory at ");
|
||||||
|
msg += TEXT("\"");
|
||||||
|
msg += fn_bak;
|
||||||
|
msg += TEXT("\".\r\rDo you want to save the current file anyways?");
|
||||||
|
if (::MessageBox(_pPublicInterface->getHSelf(), msg.c_str(), TEXT("File Backup Failed"), MB_YESNO | MB_ICONERROR) == IDNO)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return doSave(bufferID, buf->getFullPathName(), false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return doSave(bufferID, buf->getFullPathName(), false);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue