Skip to content

Commit dfd4eae

Browse files
author
tortoisesvn
committed
Add option to use ".7z" as the file extension instead of ".cryptsync".
1 parent 119c690 commit dfd4eae

11 files changed

Lines changed: 177 additions & 100 deletions

src/CryptSync.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// CryptSync - A folder sync tool with encryption
22

3-
// Copyright (C) 2012 - Stefan Kueng
3+
// Copyright (C) 2012-2013 - Stefan Kueng
44

55
// This program is free software; you can redistribute it and/or
66
// modify it under the terms of the GNU General Public License
@@ -83,6 +83,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
8383
std::wstring pw = parser.HasVal(L"pw") ? parser.GetVal(L"pw") : L"";
8484
bool encnames = !!parser.HasKey(L"encnames");
8585
bool mirror = !!parser.HasKey(L"mirror");
86+
bool use7z = !!parser.HasKey(L"use7z");
8687
std::wstring ign = parser.HasVal(L"ignore") ? parser.GetVal(L"ignore") : L"";
8788

8889
CIgnores::Instance().Reload();
@@ -91,7 +92,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
9192

9293
CPairs pair;
9394
pair.clear();
94-
pair.AddPair(src, dst, pw, encnames, mirror);
95+
pair.AddPair(src, dst, pw, encnames, mirror, use7z);
9596
CFolderSync foldersync;
9697
foldersync.SyncFoldersWait(pair, parser.HasKey(L"progress") ? GetDesktopWindow() : NULL);
9798
return 1;

src/CryptSync.rc

370 Bytes
Binary file not shown.

src/FolderSync.cpp

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void CFolderSync::SyncFolderThread()
133133
SyncFolder(*it);
134134
{
135135
CAutoWriteLock locker(m_guard);
136-
m_currentPath = PairTuple();
136+
m_currentPath = PairData();
137137
}
138138
}
139139
if (m_pProgDlg)
@@ -153,14 +153,14 @@ void CFolderSync::SyncFile( const std::wstring& path )
153153
// check if the path notification comes from a folder that's
154154
// currently synced in the sync thread
155155
{
156-
std::wstring s = std::get<0>(m_currentPath);
156+
std::wstring s = m_currentPath.origpath;
157157
if (!s.empty())
158158
{
159159
if ((path.size() > s.size()) &&
160160
(_wcsicmp(s.c_str(), path.substr(0, s.size()).c_str())==0))
161161
return;
162162
}
163-
s = std::get<1>(m_currentPath);
163+
s = m_currentPath.cryptpath;
164164
if (!s.empty())
165165
{
166166
if ((path.size() > s.size()) &&
@@ -169,11 +169,11 @@ void CFolderSync::SyncFile( const std::wstring& path )
169169
}
170170
}
171171

172-
PairTuple pt;
172+
PairData pt;
173173
CAutoReadLock locker(m_guard);
174174
for (auto it = m_pairs.cbegin(); it != m_pairs.cend(); ++it)
175175
{
176-
std::wstring s = std::get<0>(*it);
176+
std::wstring s = it->origpath;
177177
if (path.size() > s.size())
178178
{
179179
if ((_wcsicmp(path.substr(0, s.size()).c_str(), s.c_str())==0)&&(path[s.size()] == '\\'))
@@ -183,7 +183,7 @@ void CFolderSync::SyncFile( const std::wstring& path )
183183
continue;
184184
}
185185
}
186-
s = std::get<1>(*it);
186+
s = it->cryptpath;
187187
if (path.size() > s.size())
188188
{
189189
if ((_wcsicmp(path.substr(0, s.size()).c_str(), s.c_str())==0)&&(path[s.size()] == '\\'))
@@ -196,21 +196,21 @@ void CFolderSync::SyncFile( const std::wstring& path )
196196
}
197197
}
198198

199-
void CFolderSync::SyncFile( const std::wstring& path, const PairTuple& pt )
199+
void CFolderSync::SyncFile( const std::wstring& path, const PairData& pt )
200200
{
201-
std::wstring orig = std::get<0>(pt);
202-
std::wstring crypt = std::get<1>(pt);
201+
std::wstring orig = pt.origpath;
202+
std::wstring crypt = pt.cryptpath;
203203
if (orig.empty() || crypt.empty())
204204
return;
205205

206206
if ((orig.size() < path.size())&&(_wcsicmp(path.substr(0, orig.size()).c_str(), orig.c_str())==0))
207207
{
208-
crypt = crypt + L"\\" + GetEncryptedFilename(path.substr(orig.size()), std::get<2>(pt), std::get<3>(pt));
208+
crypt = crypt + L"\\" + GetEncryptedFilename(path.substr(orig.size()), pt.password, pt.encnames, pt.use7z);
209209
orig = path;
210210
}
211211
else
212212
{
213-
orig = orig + L"\\" + GetDecryptedFilename(path.substr(crypt.size()), std::get<2>(pt), std::get<3>(pt));
213+
orig = orig + L"\\" + GetDecryptedFilename(path.substr(crypt.size()), pt.password, pt.encnames, pt.use7z);
214214
crypt = path;
215215
}
216216

@@ -279,15 +279,15 @@ void CFolderSync::SyncFile( const std::wstring& path, const PairTuple& pt )
279279
// decrypt the file
280280
FileData fd;
281281
fd.ft = fdatacrypt.ftLastWriteTime;
282-
DecryptFile(orig, crypt, std::get<2>(pt), fd);
282+
DecryptFile(orig, crypt, pt.password, fd);
283283
}
284284
else if (cmp > 0)
285285
{
286286
// encrypted file is older than the original file
287287
// encrypt the file
288288
FileData fd;
289289
fd.ft = fdataorig.ftLastWriteTime;
290-
EncryptFile(orig, crypt, std::get<2>(pt), fd);
290+
EncryptFile(orig, crypt, pt.password, fd);
291291
}
292292
else if (cmp == 0)
293293
{
@@ -296,16 +296,16 @@ void CFolderSync::SyncFile( const std::wstring& path, const PairTuple& pt )
296296
}
297297
}
298298

299-
void CFolderSync::SyncFolder( const PairTuple& pt )
299+
void CFolderSync::SyncFolder( const PairData& pt )
300300
{
301301
if (m_pProgDlg)
302302
{
303303
m_pProgDlg->SetLine(0, L"scanning...");
304304
m_pProgDlg->SetLine(2, L"");
305305
m_pProgDlg->SetProgress(m_progress, m_progressTotal);
306306
}
307-
auto origFileList = GetFileList(std::get<0>(pt), std::get<2>(pt), std::get<3>(pt));
308-
auto cryptFileList = GetFileList(std::get<1>(pt), std::get<2>(pt), std::get<3>(pt));
307+
auto origFileList = GetFileList(pt.origpath, pt.password, pt.encnames, pt.use7z);
308+
auto cryptFileList = GetFileList(pt.cryptpath, pt.password, pt.encnames, pt.use7z);
309309

310310
m_progressTotal += DWORD(origFileList.size() + cryptFileList.size());
311311

@@ -320,17 +320,17 @@ void CFolderSync::SyncFolder( const PairTuple& pt )
320320
break;
321321
}
322322

323-
if (CIgnores::Instance().IsIgnored(std::get<0>(pt) + L"\\" + it->first))
323+
if (CIgnores::Instance().IsIgnored(pt.origpath + L"\\" + it->first))
324324
continue;
325325
auto cryptit = cryptFileList.find(it->first);
326326
if (cryptit == cryptFileList.end())
327327
{
328328
// file does not exist in the encrypted folder:
329329
// encrypt the file
330330
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": file %s does not exist in encrypted folder\n"), it->first.c_str());
331-
std::wstring cryptpath = std::get<1>(pt) + L"\\" + GetEncryptedFilename(it->first, std::get<2>(pt), std::get<3>(pt));
332-
std::wstring origpath = std::get<0>(pt) + L"\\" + it->first;
333-
EncryptFile(origpath, cryptpath, std::get<2>(pt), it->second);
331+
std::wstring cryptpath = pt.cryptpath + L"\\" + GetEncryptedFilename(it->first, pt.password, pt.encnames, pt.use7z);
332+
std::wstring origpath = pt.origpath + L"\\" + it->first;
333+
EncryptFile(origpath, cryptpath, pt.password, it->second);
334334
}
335335
else
336336
{
@@ -340,18 +340,18 @@ void CFolderSync::SyncFolder( const PairTuple& pt )
340340
// original file is older than the encrypted file
341341
// decrypt the file
342342
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": file %s is older than its encrypted partner\n"), it->first.c_str());
343-
std::wstring cryptpath = std::get<1>(pt) + L"\\" + GetEncryptedFilename(it->first, std::get<2>(pt), std::get<3>(pt));
344-
std::wstring origpath = std::get<0>(pt) + L"\\" + it->first;
345-
DecryptFile(origpath, cryptpath, std::get<2>(pt), it->second);
343+
std::wstring cryptpath = pt.cryptpath + L"\\" + GetEncryptedFilename(it->first, pt.password, pt.encnames, pt.use7z);
344+
std::wstring origpath = pt.origpath + L"\\" + it->first;
345+
DecryptFile(origpath, cryptpath, pt.password, it->second);
346346
}
347347
else if (cmp > 0)
348348
{
349349
// encrypted file is older than the original file
350350
// encrypt the file
351351
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": file %s is newer than its encrypted partner\n"), it->first.c_str());
352-
std::wstring cryptpath = std::get<1>(pt) + L"\\" + GetEncryptedFilename(it->first, std::get<2>(pt), std::get<3>(pt));
353-
std::wstring origpath = std::get<0>(pt) + L"\\" + it->first;
354-
EncryptFile(origpath, cryptpath, std::get<2>(pt), it->second);
352+
std::wstring cryptpath = pt.cryptpath + L"\\" + GetEncryptedFilename(it->first, pt.password, pt.encnames, pt.use7z);
353+
std::wstring origpath = pt.origpath + L"\\" + it->first;
354+
EncryptFile(origpath, cryptpath, pt.password, it->second);
355355
}
356356
else if (cmp == 0)
357357
{
@@ -362,7 +362,7 @@ void CFolderSync::SyncFolder( const PairTuple& pt )
362362
}
363363
// now go through the encrypted file list and if there's a file that's not in the original file list,
364364
// decrypt it
365-
if (!std::get<4>(pt) || origFileList.empty())
365+
if (!pt.oneway || origFileList.empty())
366366
{
367367
for (auto it = cryptFileList.cbegin(); (it != cryptFileList.cend()) && m_bRunning; ++it)
368368
{
@@ -375,21 +375,21 @@ void CFolderSync::SyncFolder( const PairTuple& pt )
375375
break;
376376
}
377377

378-
if (CIgnores::Instance().IsIgnored(std::get<0>(pt) + L"\\" + it->first))
378+
if (CIgnores::Instance().IsIgnored(pt.origpath + L"\\" + it->first))
379379
continue;
380380
auto origit = origFileList.find(it->first);
381381
if (origit == origFileList.end())
382382
{
383383
// file does not exist in the original folder:
384384
// decrypt the file
385-
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": decrypt file %s to %s\n"), it->first.c_str(), std::get<0>(pt).c_str());
385+
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": decrypt file %s to %s\n"), it->first.c_str(), pt.origpath.c_str());
386386
size_t slashpos = it->first.find_last_of('\\');
387387
std::wstring fname = it->first;
388388
if (slashpos != std::string::npos)
389389
fname = it->first.substr(slashpos + 1);
390-
std::wstring cryptpath = std::get<1>(pt) + L"\\" + it->second.filerelpath;
391-
std::wstring origpath = std::get<0>(pt) + L"\\" + it->first;
392-
if (!DecryptFile(origpath, cryptpath, std::get<2>(pt), it->second))
390+
std::wstring cryptpath = pt.cryptpath + L"\\" + it->second.filerelpath;
391+
std::wstring origpath = pt.origpath + L"\\" + it->first;
392+
if (!DecryptFile(origpath, cryptpath, pt.password, it->second))
393393
{
394394
if (!it->second.filenameEncrypted)
395395
{
@@ -401,7 +401,7 @@ void CFolderSync::SyncFolder( const PairTuple& pt )
401401
}
402402
}
403403

404-
std::map<std::wstring,FileData, ci_less> CFolderSync::GetFileList( const std::wstring& path, const std::wstring& password, bool encnames ) const
404+
std::map<std::wstring,FileData, ci_less> CFolderSync::GetFileList( const std::wstring& path, const std::wstring& password, bool encnames, bool use7z ) const
405405
{
406406
std::wstring enumpath = path;
407407
if ((enumpath.size() == 2)&&(enumpath[1]==':'))
@@ -414,13 +414,13 @@ std::map<std::wstring,FileData, ci_less> CFolderSync::GetFileList( const std::ws
414414
bool bRecurse = true;
415415
while (enumerator.NextFile(filepath, &isDir, bRecurse))
416416
{
417+
bRecurse = true;
417418
if (isDir)
418419
{
419420
if (CIgnores::Instance().IsIgnored(filepath))
420421
bRecurse = false; // don't recurse into ignored folders
421422
continue;
422423
}
423-
bRecurse = true;
424424
if (!m_bRunning)
425425
break;
426426

@@ -433,7 +433,7 @@ std::map<std::wstring,FileData, ci_less> CFolderSync::GetFileList( const std::ws
433433
std::wstring relpath = filepath.substr(path.size()+1);
434434
fd.filerelpath = relpath;
435435

436-
std::wstring decryptedRelPath = GetDecryptedFilename(relpath, password, encnames);
436+
std::wstring decryptedRelPath = GetDecryptedFilename(relpath, password, encnames, use7z);
437437
fd.filenameEncrypted = (_wcsicmp(decryptedRelPath.c_str(), fd.filerelpath.c_str())!=0);
438438
if (fd.filenameEncrypted)
439439
{
@@ -563,7 +563,7 @@ bool CFolderSync::DecryptFile( const std::wstring& orig, const std::wstring& cry
563563
return bRet;
564564
}
565565

566-
std::wstring CFolderSync::GetDecryptedFilename( const std::wstring& filename, const std::wstring& password, bool encryptname ) const
566+
std::wstring CFolderSync::GetDecryptedFilename( const std::wstring& filename, const std::wstring& password, bool encryptname, bool use7z ) const
567567
{
568568
std::wstring decryptName = filename;
569569
size_t dotpos = filename.find_last_of('.');
@@ -577,10 +577,21 @@ std::wstring CFolderSync::GetDecryptedFilename( const std::wstring& filename, co
577577
{
578578
std::wstring f = filename;
579579
std::transform(f.begin(), f.end(), f.begin(), std::tolower);
580-
size_t pos = f.find(L".cryptsync");
581-
if ((pos != std::string::npos) && (pos == (filename.size() - 10)))
580+
if (use7z)
582581
{
583-
return filename.substr(0, pos);
582+
size_t pos = f.find(L".7z");
583+
if ((pos != std::string::npos) && (pos == (filename.size() - 3)))
584+
{
585+
return filename.substr(0, pos);
586+
}
587+
}
588+
else
589+
{
590+
size_t pos = f.find(L".cryptsync");
591+
if ((pos != std::string::npos) && (pos == (filename.size() - 10)))
592+
{
593+
return filename.substr(0, pos);
594+
}
584595
}
585596
return filename;
586597
}
@@ -671,17 +682,27 @@ std::wstring CFolderSync::GetDecryptedFilename( const std::wstring& filename, co
671682
return decryptName;
672683
}
673684

674-
std::wstring CFolderSync::GetEncryptedFilename( const std::wstring& filename, const std::wstring& password, bool encryptname ) const
685+
std::wstring CFolderSync::GetEncryptedFilename( const std::wstring& filename, const std::wstring& password, bool encryptname, bool use7z ) const
675686
{
676687
std::wstring encryptFilename = filename;
677688
if (!encryptname)
678689
{
679690
std::wstring f = filename;
680691
std::transform(f.begin(), f.end(), f.begin(), std::tolower);
681-
size_t pos = f.find(L".cryptsync");
682-
if ((pos == std::string::npos) || (pos != (filename.size() - 10)))
683-
encryptFilename += L".cryptsync";
684-
return encryptFilename;
692+
if (use7z)
693+
{
694+
size_t pos = f.find(L".7z");
695+
if ((pos == std::string::npos) || (pos != (filename.size() - 3)))
696+
encryptFilename += L".7z";
697+
return encryptFilename;
698+
}
699+
else
700+
{
701+
size_t pos = f.find(L".cryptsync");
702+
if ((pos == std::string::npos) || (pos != (filename.size() - 10)))
703+
encryptFilename += L".cryptsync";
704+
return encryptFilename;
705+
}
685706
}
686707

687708
bool bResult = true;
@@ -733,7 +754,10 @@ std::wstring CFolderSync::GetEncryptedFilename( const std::wstring& filename, co
733754
encryptFilename += L"\\";
734755
encryptFilename += *it;
735756
}
736-
encryptFilename += L".cryptsync";
757+
if (use7z)
758+
encryptFilename += L".7z";
759+
else
760+
encryptFilename += L".cryptsync";
737761

738762
CryptDestroyKey(hKey); // Release provider handle.
739763
}

src/FolderSync.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ class CFolderSync
7777

7878
private:
7979
static unsigned int __stdcall SyncFolderThreadEntry(void* pContext);
80-
void SyncFile(const std::wstring& path, const PairTuple& pt);
80+
void SyncFile(const std::wstring& path, const PairData& pt);
8181
void SyncFolderThread();
82-
void SyncFolder(const PairTuple& pt);
83-
std::map<std::wstring,FileData, ci_less> GetFileList(const std::wstring& path, const std::wstring& password, bool encnames) const;
82+
void SyncFolder(const PairData& pt);
83+
std::map<std::wstring,FileData, ci_less> GetFileList(const std::wstring& path, const std::wstring& password, bool encnames, bool use7z) const;
8484
bool EncryptFile(const std::wstring& orig, const std::wstring& crypt, const std::wstring& password, const FileData& fd);
8585
bool DecryptFile(const std::wstring& orig, const std::wstring& crypt, const std::wstring& password, const FileData& fd);
86-
std::wstring GetDecryptedFilename(const std::wstring& filename, const std::wstring& password, bool encryptname) const;
87-
std::wstring GetEncryptedFilename(const std::wstring& filename, const std::wstring& password, bool encryptname) const;
86+
std::wstring GetDecryptedFilename(const std::wstring& filename, const std::wstring& password, bool encryptname, bool use7z) const;
87+
std::wstring GetEncryptedFilename(const std::wstring& filename, const std::wstring& password, bool encryptname, bool use7z) const;
8888

8989
bool Run7Zip(LPWSTR cmdline, const std::wstring& cwd) const;
9090

@@ -99,7 +99,7 @@ class CFolderSync
9999
DWORD m_progressTotal;
100100
volatile LONG m_bRunning;
101101
CAutoGeneralHandle m_hThread;
102-
PairTuple m_currentPath;
102+
PairData m_currentPath;
103103
std::map<std::wstring, SyncOp> m_failures;
104104
std::set<std::wstring> m_notifyignores;
105105
};

0 commit comments

Comments
 (0)