Skip to content

Commit

Permalink
Ensure backup directory exists
Browse files Browse the repository at this point in the history
Failed backups result in a user error message and asks to save the file anyways. Closes notepad-plus-plus#2346, Closes notepad-plus-plus#2441
  • Loading branch information
dail8859 committed Oct 24, 2016
1 parent b3f56a4 commit a82d9f9
Showing 1 changed file with 50 additions and 53 deletions.
103 changes: 50 additions & 53 deletions PowerEditor/src/NppIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <time.h>
#include <shlwapi.h>
#include <ShlObj.h>
#include "Notepad_plus_Window.h"
#include "FileDialog.h"
#include "EncodingMapper.h"
Expand Down Expand Up @@ -1157,69 +1158,56 @@ bool Notepad_plus::fileSave(BufferID id)

if (!buf->getFileReadOnly() && buf->isDirty()) //cannot save if readonly
{
const TCHAR *fn = buf->getFullPathName();
if (buf->isUntitled())
{
return fileSaveAs(bufferID);
}
else

const NppGUI & nppgui = (NppParameters::getInstance())->getNppGUI();
BackupFeature backup = nppgui._backup;

if (backup != bak_none)
{
const NppGUI & nppgui = (NppParameters::getInstance())->getNppGUI();
BackupFeature backup = nppgui._backup;
const TCHAR *fn = buf->getFullPathName();
TCHAR *name = ::PathFindFileName(fn);
generic_string fn_bak;

if (backup == bak_simple)
if (nppgui._useDir && not nppgui._backupDir.empty())
{
//copy fn to fn.backup
generic_string fn_bak(fn);
if ((nppgui._useDir) && (nppgui._backupDir != TEXT("")))
{
fn_bak = nppgui._backupDir;
// Get the custom directory, make sure it has a trailing slash
fn_bak = nppgui._backupDir;
if (fn_bak.back() != TEXT('\\'))
fn_bak += TEXT("\\");
fn_bak += name;
}
else
{
fn_bak = fn;
}
fn_bak += TEXT(".bak");

if (not ::CopyFile(fn, fn_bak.c_str(), FALSE))
{
return false;
}
}
else if (backup == bak_verbose)
else
{
generic_string fn_dateTime_bak(TEXT(""));

if ((nppgui._useDir) && (nppgui._backupDir != TEXT("")))
{
fn_dateTime_bak = nppgui._backupDir;
fn_dateTime_bak += TEXT("\\");
}
else
// Get the current file's directory
generic_string path = fn;
::PathRemoveFileSpec(path);
fn_bak = path.c_str();
fn_bak += TEXT("\\");

// If verbose, save it in a sub folder
if (backup == bak_verbose)
{
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_bak += TEXT("nppBackup\\");
}
}

fn_dateTime_bak += name;
// 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)
{
const int temBufLen = 32;
TCHAR tmpbuf[temBufLen];
time_t ltime = time(0);
Expand All @@ -1228,17 +1216,26 @@ bool Notepad_plus::fileSave(BufferID id)
today = localtime(&ltime);
generic_strftime(tmpbuf, temBufLen, TEXT("%Y-%m-%d_%H%M%S"), today);

fn_dateTime_bak += TEXT(".");
fn_dateTime_bak += tmpbuf;
fn_dateTime_bak += TEXT(".bak");
fn_bak += name;
fn_bak += TEXT(".");
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 doSave(bufferID, buf->getFullPathName(), false);
}

return doSave(bufferID, buf->getFullPathName(), false);
}
return false;
}
Expand Down

0 comments on commit a82d9f9

Please sign in to comment.