| 1 |
#include <windows.h> |
| 2 |
#include <commctrl.h> |
| 3 |
#include <tchar.h> |
| 4 |
|
| 5 |
#include "Tombo.h" |
| 6 |
#include "UniConv.h" |
| 7 |
#include "TString.h" |
| 8 |
#include "TomboURI.h" |
| 9 |
#include "DirectoryScanner.h" |
| 10 |
#include "MemoFolder.h" |
| 11 |
#include "PasswordManager.h" |
| 12 |
#include "MemoNote.h" |
| 13 |
#include "Message.h" |
| 14 |
#include "Repository.h" |
| 15 |
|
| 16 |
/////////////////////////////////////////////// |
| 17 |
// ctor & dtor |
| 18 |
/////////////////////////////////////////////// |
| 19 |
|
| 20 |
MemoFolder::MemoFolder() : pFullPath(NULL) |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
MemoFolder::~MemoFolder() |
| 25 |
{ |
| 26 |
if (pFullPath) delete [] pFullPath; |
| 27 |
} |
| 28 |
|
| 29 |
BOOL MemoFolder::Init(LPCTSTR pTop, LPCTSTR p) |
| 30 |
{ |
| 31 |
pTopDir = pTop; |
| 32 |
if ((pFullPath = StringDup(p)) == NULL) return FALSE; |
| 33 |
return TRUE; |
| 34 |
} |
| 35 |
|
| 36 |
/////////////////////////////////////////////// |
| 37 |
// ディレクトリの移動 |
| 38 |
/////////////////////////////////////////////// |
| 39 |
|
| 40 |
BOOL MemoFolder::Move(LPCTSTR pDst) |
| 41 |
{ |
| 42 |
// 末尾のディレクトリを取得 |
| 43 |
TCHAR aDst[MAX_PATH]; |
| 44 |
_tcscpy(aDst, pFullPath); |
| 45 |
ChopFileSeparator(aDst); |
| 46 |
TString sDir; |
| 47 |
sDir.GetDirectoryPath(aDst); |
| 48 |
LPTSTR p = aDst + _tcslen(sDir.Get()); |
| 49 |
|
| 50 |
TCHAR aSrc[MAX_PATH]; |
| 51 |
_tcscpy(aSrc, pFullPath); |
| 52 |
ChopFileSeparator(aSrc); |
| 53 |
|
| 54 |
// 移動先が移動元のフォルダ名と同じになるようにパラメータを調整 |
| 55 |
TString sDst; |
| 56 |
if (!sDst.Join(pDst, p)) return FALSE; |
| 57 |
|
| 58 |
// 移動処理 |
| 59 |
if (!MoveFile(aSrc, sDst.Get())) return FALSE; |
| 60 |
|
| 61 |
return TRUE; |
| 62 |
} |
| 63 |
|
| 64 |
/////////////////////////////////////////////// |
| 65 |
// Delete directory |
| 66 |
/////////////////////////////////////////////// |
| 67 |
|
| 68 |
DSFileDelete::DSFileDelete(){ /* NOP */} |
| 69 |
|
| 70 |
BOOL DSFileDelete::Init(LPCTSTR pPath) |
| 71 |
{ |
| 72 |
return DirectoryScanner::Init(pPath, 0); |
| 73 |
} |
| 74 |
|
| 75 |
void DSFileDelete::InitialScan() {/* NOP */} |
| 76 |
void DSFileDelete::PreDirectory(LPCTSTR) {/* NOP */} |
| 77 |
|
| 78 |
void DSFileDelete::PostDirectory(LPCTSTR) |
| 79 |
{ |
| 80 |
if (!IsStopScan()) { |
| 81 |
TCHAR buf[MAX_PATH*2]; |
| 82 |
_tcscpy(buf, CurrentPath()); |
| 83 |
ChopFileSeparator(buf); |
| 84 |
if (!RemoveDirectory(buf)) { |
| 85 |
_tcscpy(aFailPath, buf); |
| 86 |
// _tcscpy(aErrorMsg, MSG_RMDIR_FAILED); |
| 87 |
StopScan(); |
| 88 |
SetLastError(ERROR_TOMBO_E_RMDIR_FAILED); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void DSFileDelete::AfterScan() |
| 94 |
{ |
| 95 |
PostDirectory(NULL); |
| 96 |
} |
| 97 |
|
| 98 |
void DSFileDelete::File(LPCTSTR p) |
| 99 |
{ |
| 100 |
// if the file that TOMBO is not treat exists, stop deleting. |
| 101 |
if (MemoNote::IsNote(CurrentPath()) == NOTE_TYPE_NO) { |
| 102 |
_tcscpy(aFailPath, CurrentPath()); |
| 103 |
StopScan(); |
| 104 |
SetLastError(ERROR_TOMBO_W_OTHERFILE_EXISTS); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// Delete |
| 109 |
if (!DeleteFile(CurrentPath())) { |
| 110 |
_tcscpy(aFailPath, CurrentPath()); |
| 111 |
StopScan(); |
| 112 |
SetLastError(ERROR_TOMBO_E_RMFILE_FAILED); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/////////////////////////////////////////////// |
| 117 |
// ディレクトリコピー |
| 118 |
/////////////////////////////////////////////// |
| 119 |
|
| 120 |
class DSFileCopy: public DirectoryScanner { |
| 121 |
TCHAR aDstPath[MAX_PATH * 2]; |
| 122 |
public: |
| 123 |
TCHAR aFailPath[MAX_PATH * 2]; |
| 124 |
TCHAR aErrorMsg[1024]; |
| 125 |
|
| 126 |
BOOL Init(LPCTSTR pSrc, LPCTSTR pDst); |
| 127 |
void InitialScan(); |
| 128 |
void AfterScan(); |
| 129 |
void PreDirectory(LPCTSTR); |
| 130 |
void PostDirectory(LPCTSTR); |
| 131 |
void File(LPCTSTR); |
| 132 |
}; |
| 133 |
|
| 134 |
BOOL DSFileCopy::Init(LPCTSTR pSrc, LPCTSTR pDst) |
| 135 |
{ |
| 136 |
_tcscpy(aDstPath, pDst); |
| 137 |
return DirectoryScanner::Init(pSrc, 0); |
| 138 |
} |
| 139 |
|
| 140 |
void DSFileCopy::InitialScan() |
| 141 |
{ |
| 142 |
TCHAR buf[MAX_PATH * 2]; |
| 143 |
_tcscpy(buf, aDstPath); |
| 144 |
ChopFileSeparator(buf); |
| 145 |
if (!CreateDirectory(buf, NULL)) { |
| 146 |
_tcscpy(aFailPath, buf); |
| 147 |
_tcscpy(aErrorMsg, MSG_MKDIR_FAILED); |
| 148 |
StopScan(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void DSFileCopy::AfterScan() {/* NOP */} |
| 153 |
|
| 154 |
void DSFileCopy::File(LPCTSTR p) |
| 155 |
{ |
| 156 |
_tcscat(aDstPath, p); |
| 157 |
|
| 158 |
if (!CopyFile(CurrentPath(), aDstPath, FALSE)) { |
| 159 |
_tcscpy(aFailPath, aDstPath); |
| 160 |
_tcscpy(aErrorMsg, MSG_FILECOPY_FAILED); |
| 161 |
StopScan(); |
| 162 |
} |
| 163 |
|
| 164 |
*(aDstPath + (_tcslen(aDstPath) - _tcslen(p))) = TEXT('\0'); |
| 165 |
} |
| 166 |
|
| 167 |
void DSFileCopy::PreDirectory(LPCTSTR p) |
| 168 |
{ |
| 169 |
_tcscat(aDstPath, p); |
| 170 |
if (!CreateDirectory(aDstPath, NULL)) { |
| 171 |
_tcscpy(aFailPath, aDstPath); |
| 172 |
_tcscpy(aErrorMsg, MSG_MKDIR_FAILED); |
| 173 |
StopScan(); |
| 174 |
} |
| 175 |
_tcscat(aDstPath, TEXT("\\")); |
| 176 |
} |
| 177 |
|
| 178 |
void DSFileCopy::PostDirectory(LPCTSTR p) |
| 179 |
{ |
| 180 |
*(aDstPath + (_tcslen(aDstPath) - _tcslen(p) - 1)) = TEXT('\0'); |
| 181 |
} |
| 182 |
|
| 183 |
BOOL MemoFolder::Copy(LPCTSTR pDst) |
| 184 |
{ |
| 185 |
DSFileCopy fc; |
| 186 |
fc.Init(pFullPath, pDst); |
| 187 |
|
| 188 |
if (!fc.Scan()) { |
| 189 |
sErrorReason.Join(fc.aErrorMsg, TEXT(" : "), fc.aFailPath); |
| 190 |
return FALSE; |
| 191 |
} else { |
| 192 |
return TRUE; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/////////////////////////////////////////////// |
| 197 |
// ディレクトリ名の変更 |
| 198 |
/////////////////////////////////////////////// |
| 199 |
|
| 200 |
BOOL MemoFolder::Rename(LPCTSTR pNewName) |
| 201 |
{ |
| 202 |
if (_tcslen(pNewName) == 0) { |
| 203 |
SetLastError(ERROR_NO_DATA); |
| 204 |
return FALSE; |
| 205 |
} |
| 206 |
|
| 207 |
TString sCurrent; |
| 208 |
if (!sCurrent.Set(pFullPath)) return FALSE; |
| 209 |
sCurrent.ChopFileSeparator(); |
| 210 |
|
| 211 |
TString sNew; |
| 212 |
sNew.GetDirectoryPath(sCurrent.Get()); |
| 213 |
sNew.StrCat(pNewName); |
| 214 |
|
| 215 |
return MoveFile(sCurrent.Get(), sNew.Get()); |
| 216 |
} |
| 217 |
|
| 218 |
/////////////////////////////////////////////// |
| 219 |
// Encrypt/decrypt to folderr |
| 220 |
/////////////////////////////////////////////// |
| 221 |
|
| 222 |
DSEncrypt::~DSEncrypt() |
| 223 |
{ |
| 224 |
delete pURI; |
| 225 |
} |
| 226 |
|
| 227 |
BOOL DSEncrypt::Init(LPCTSTR pTopDir, LPCTSTR pPath, LPCTSTR pBaseURI, BOOL bEnc) { |
| 228 |
nBaseLen = _tcslen(pTopDir); |
| 229 |
nNotEncrypted = 0; |
| 230 |
bEncrypt = bEnc; |
| 231 |
pURI = new TString(); |
| 232 |
if (pURI == NULL || !pURI->Alloc(MAX_PATH)) return FALSE; |
| 233 |
nURIBufSize = MAX_PATH; |
| 234 |
|
| 235 |
_tcscpy(pURI->Get(), pBaseURI); |
| 236 |
nCurrentPos = _tcslen(pBaseURI); |
| 237 |
|
| 238 |
return DirectoryScanner::Init(pPath, 0); |
| 239 |
} |
| 240 |
|
| 241 |
BOOL DSEncrypt::CheckURIBuffer(LPCTSTR p) |
| 242 |
{ |
| 243 |
// check buffer size |
| 244 |
if (nCurrentPos + _tcslen(p) > nURIBufSize) { |
| 245 |
TString *pNewBuf = new TString(); |
| 246 |
if (pNewBuf == NULL || !pNewBuf->Alloc(nURIBufSize + MAX_PATH)) { |
| 247 |
StopScan(); |
| 248 |
return FALSE; |
| 249 |
} |
| 250 |
_tcscpy(pNewBuf->Get(), pURI->Get()); |
| 251 |
delete pURI; |
| 252 |
pURI = pNewBuf; |
| 253 |
} |
| 254 |
return TRUE; |
| 255 |
} |
| 256 |
|
| 257 |
void DSEncrypt::PreDirectory(LPCTSTR pDir) |
| 258 |
{ |
| 259 |
if (!CheckURIBuffer(pDir)) return; |
| 260 |
|
| 261 |
DWORD n = _tcslen(pDir); |
| 262 |
_tcscpy(pURI->Get() + nCurrentPos, pDir); |
| 263 |
nCurrentPos += n; |
| 264 |
_tcscat(pURI->Get() + nCurrentPos, TEXT("/")); |
| 265 |
nCurrentPos++; |
| 266 |
} |
| 267 |
|
| 268 |
void DSEncrypt::PostDirectory(LPCTSTR pDir) |
| 269 |
{ |
| 270 |
DWORD n = _tcslen(pDir); |
| 271 |
nCurrentPos -= n + 1; |
| 272 |
*(pURI->Get() + nCurrentPos) = TEXT('\0'); |
| 273 |
} |
| 274 |
|
| 275 |
void DSEncrypt::File(LPCTSTR pFile) |
| 276 |
{ |
| 277 |
if (!CheckURIBuffer(pFile)) return; |
| 278 |
|
| 279 |
_tcscpy(pURI->Get() + nCurrentPos, pFile); |
| 280 |
|
| 281 |
TomboURI sURI; |
| 282 |
if (!sURI.Init(pURI->Get())) { |
| 283 |
StopScan(); |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
URIOption gopt(NOTE_OPTIONMASK_VALID | NOTE_OPTIONMASK_ENCRYPTED); |
| 288 |
if (!g_Repository.GetOption(&sURI, &gopt)) { |
| 289 |
nNotEncrypted++; |
| 290 |
return; |
| 291 |
} |
| 292 |
if (gopt.bValid == FALSE || gopt.bFolder == TRUE) return; |
| 293 |
|
| 294 |
if (gopt.bEncrypt == bEncrypt) return; |
| 295 |
|
| 296 |
URIOption opt(NOTE_OPTIONMASK_ENCRYPTED); |
| 297 |
opt.bEncrypt = bEncrypt; |
| 298 |
if (!g_Repository.SetOption(&sURI, &opt)) { |
| 299 |
nNotEncrypted++; |
| 300 |
return; |
| 301 |
} |
| 302 |
} |