-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptSync.cpp
More file actions
221 lines (193 loc) · 8.46 KB
/
Copy pathCryptSync.cpp
File metadata and controls
221 lines (193 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// CryptSync - A folder sync tool with encryption
// Copyright (C) 2012-2014 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "CmdLineParser.h"
#include "TrayWindow.h"
#include "Ignores.h"
#include "PathUtils.h"
#include "CircularLog.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
CPairs g_pairs;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
std::wstring GetMutexID()
{
std::wstring t;
CAutoGeneralHandle token;
BOOL result = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, token.GetPointer());
if(result)
{
DWORD len = 0;
GetTokenInformation(token, TokenStatistics, NULL, 0, &len);
if (len >= sizeof (TOKEN_STATISTICS))
{
std::unique_ptr<BYTE[]> data (new BYTE[len]);
GetTokenInformation(token, TokenStatistics, data.get(), len, &len);
LUID uid = ((PTOKEN_STATISTICS)data.get())->AuthenticationId;
wchar_t buf[100] = {0};
swprintf_s(buf, L"{81C34844-03AC-4DAA-865B-BC51F07F7F9E}-%08x%08x", uid.HighPart, uid.LowPart);
t = buf;
}
}
return t;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(nCmdShow);
SetDllDirectory(L"");
OleInitialize(NULL);
CoInitializeEx(0, COINIT_APARTMENTTHREADED);
LoadLibrary(L"riched32.dll");
CCmdLineParser parser(lpCmdLine);
if (parser.HasKey(L"?") || parser.HasKey(L"help"))
{
std::wstring sInfo = L"/src : path to source folder with original content\n"
L"/dst : path to encrpyted folder\n"
L"/pw : password for encryption\n"
L"/cpy : copy only pattern\n"
L"/nsy : don't sync pattern\n"
L"/encnames : encrypt file and folder names\n"
L"/mirror : mirror only from src to dst\n"
L"/use7z : use .7z instead of .cryptsync extension\n"
L"/useGPG : use GnuPG encryption\n"
L"/fat : use FAT write time accuracy (2s)\n"
L"/ignore : ignore patterns\n"
L"\n"
L"/syncall : syncs all set up pairs and then exists\n"
L"/progress : shows a progress dialog while syncing\n"
L"/logpath : path to a logfile\n"
L"/maxlog : maximum number of lines the logfile can have\n"
L"/tray : start in background without showing a dialog first";
MessageBox(NULL, sInfo.c_str(), L"CryptSync Command Line Options", MB_ICONINFORMATION);
return 1;
}
std::wstring lp = parser.HasVal(L"logpath") ? parser.GetVal(L"logpath") : L"";
int maxlog = parser.HasVal(L"maxlog") ? parser.GetLongVal(L"maxlog") : 10000;
if (lp.empty())
{
// in case CryptSync was installed with the installer, there's a registry
// entry made by the installer.
HKEY subKey = nullptr;
LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\CryptSync", 0, KEY_READ, &subKey);
if (result != ERROR_SUCCESS)
{
// CryptSync was not installed, which means it's run as a portable app:
// use the same directory as the exe is in to store the application data
DWORD len = 0;
DWORD bufferlen = MAX_PATH; // MAX_PATH is not the limit here!
std::unique_ptr<wchar_t[]> path(new wchar_t[bufferlen]);
do
{
bufferlen += MAX_PATH; // MAX_PATH is not the limit here!
path = std::unique_ptr<wchar_t[]>(new wchar_t[bufferlen]);
len = GetModuleFileName(hInst, path.get(), bufferlen);
} while(len == bufferlen);
std::wstring sPath = path.get();
sPath = sPath.substr(0, sPath.find_last_of('\\'));
lp = CPathUtils::GetLongPathname(sPath);
lp += L"\\CryptSync.log";
}
else
{
RegCloseKey(subKey);
// CryptSync is installed: we must not store the application data
// in the same directory as the exe is but in %APPDATA%\CryptSync instead
WCHAR outpath[MAX_PATH] = {0};
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, outpath)))
{
lp = outpath;
lp += L"\\CryptSync";
lp = CPathUtils::GetLongPathname(lp);
lp += L"\\CryptSync.log";
}
}
}
CCircularLog::Instance().Init(lp, maxlog);
CCircularLog::Instance()(L"Starting CryptSync");
if (parser.HasVal(L"src") && parser.HasVal(L"dst"))
{
std::wstring src = parser.GetVal(L"src");
std::wstring dst = parser.GetVal(L"dst");
std::wstring pw = parser.HasVal(L"pw") ? parser.GetVal(L"pw") : L"";
std::wstring cpy = parser.HasVal(L"cpy") ? parser.GetVal(L"cpy") : L"";
std::wstring nsy = parser.HasVal(L"nsy") ? parser.GetVal(L"nsy") : L"";
bool encnames = !!parser.HasKey(L"encnames");
bool mirror = !!parser.HasKey(L"mirror");
bool use7z = !!parser.HasKey(L"use7z");
bool useGPG = !!parser.HasKey(L"useGPG");
bool fat = !!parser.HasKey(L"fat");
std::wstring ign = parser.HasVal(L"ignore") ? parser.GetVal(L"ignore") : L"";
CIgnores::Instance().Reload();
if (!ign.empty())
CIgnores::Instance().Reload(ign);
CPairs pair;
pair.clear();
pair.AddPair(src, dst, pw, cpy, nsy, encnames, mirror, use7z, useGPG, fat);
CFolderSync foldersync;
foldersync.SyncFoldersWait(pair, parser.HasKey(L"progress") ? GetDesktopWindow() : NULL);
return 1;
}
if (parser.HasKey(L"syncall"))
{
CIgnores::Instance().Reload();
CPairs pair;
CFolderSync foldersync;
foldersync.SyncFoldersWait(pair, parser.HasKey(L"progress") ? GetDesktopWindow() : NULL);
return 1;
}
MSG msg;
HANDLE hReloadProtection = ::CreateMutex(NULL, FALSE, GetMutexID().c_str());
if ((!hReloadProtection) || (GetLastError() == ERROR_ALREADY_EXISTS))
{
// An instance of CryptSync is already running
CoUninitialize();
OleUninitialize();
if (hReloadProtection)
CloseHandle(hReloadProtection);
return 0;
}
CTrayWindow trayWindow(hInstance);
trayWindow.ShowDialogImmediately(!parser.HasKey(L"tray"));
if (trayWindow.RegisterAndCreateWindow())
{
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CryptSync));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
CoUninitialize();
OleUninitialize();
CloseHandle(hReloadProtection);
return 1;
}