Skip to content

Commit 89629c6

Browse files
committed
Issue Squirrel#1853, commit for a PR
1 parent 51f5e2c commit 89629c6

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

src/Setup/UpdateRunner.cpp

+75-4
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ int CUpdateRunner::ExtractUpdaterAndRun(wchar_t* lpCommandLine, bool useFallback
165165
PROCESS_INFORMATION pi = { 0 };
166166
STARTUPINFO si = { 0 };
167167
CResource zipResource;
168-
wchar_t targetDir[MAX_PATH] = { 0 };
168+
wchar_t targetDir[MAX_PATH + 1] = { 0 };
169169
wchar_t logFile[MAX_PATH];
170170

171171
std::vector<CString> to_delete;
@@ -175,7 +175,7 @@ int CUpdateRunner::ExtractUpdaterAndRun(wchar_t* lpCommandLine, bool useFallback
175175
DirectoryExists(envSquirrelTemp) &&
176176
DirectoryIsWritable(envSquirrelTemp) &&
177177
!PathIsUNCW(envSquirrelTemp)) {
178-
_swprintf_c(targetDir, _countof(targetDir), L"%s", envSquirrelTemp);
178+
_swprintf_c(targetDir, _countof(targetDir) - 1, L"%s", envSquirrelTemp);
179179
goto gotADir;
180180
}
181181

@@ -191,7 +191,7 @@ int CUpdateRunner::ExtractUpdaterAndRun(wchar_t* lpCommandLine, bool useFallback
191191
SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, appDataDir);
192192
GetUserName(username, &unameSize);
193193

194-
_swprintf_c(targetDir, _countof(targetDir), L"%s\\%s", appDataDir, username);
194+
_swprintf_c(targetDir, _countof(targetDir) - 1, L"%s\\%s", appDataDir, username);
195195

196196
if (!CreateDirectory(targetDir, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) {
197197
wchar_t err[4096];
@@ -203,7 +203,7 @@ int CUpdateRunner::ExtractUpdaterAndRun(wchar_t* lpCommandLine, bool useFallback
203203

204204
gotADir:
205205

206-
wcscat_s(targetDir, _countof(targetDir), L"\\SquirrelTemp");
206+
wcscat_s(targetDir, _countof(targetDir) - 1, L"\\SquirrelTemp");
207207

208208
if (!CreateDirectory(targetDir, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) {
209209
wchar_t err[4096];
@@ -216,6 +216,35 @@ int CUpdateRunner::ExtractUpdaterAndRun(wchar_t* lpCommandLine, bool useFallback
216216
goto failedExtract;
217217
}
218218

219+
const char tempDirValues[] = "\0abcdefghijklmnopqrstuvwxyz";
220+
int tempDirValuesCount = sizeof(tempDirValues) - 1;
221+
bool tempDirCreated = false;
222+
for (int i = 1; i < (tempDirValuesCount * tempDirValuesCount * tempDirValuesCount * tempDirValuesCount); i++) {
223+
int c0 = i % tempDirValuesCount;
224+
int c1 = (i / tempDirValuesCount) % tempDirValuesCount;
225+
int c2 = (i / (tempDirValuesCount * tempDirValuesCount)) % tempDirValuesCount;
226+
int c3 = (i / (tempDirValuesCount * tempDirValuesCount * tempDirValuesCount)) % tempDirValuesCount;
227+
228+
wchar_t tempDir[MAX_PATH] = { 0 };
229+
_swprintf_c(tempDir, _countof(tempDir), L"%s\\temp%c%c%c%c", targetDir, tempDirValues[c3], tempDirValues[c2], tempDirValues[c1], tempDirValues[c0]);
230+
231+
if (CreateDirectory(tempDir, NULL)) {
232+
_swprintf_c(targetDir, _countof(targetDir) - 1, L"%s", tempDir);
233+
tempDirCreated = true;
234+
break;
235+
}
236+
}
237+
if (!tempDirCreated) {
238+
wchar_t err[4096];
239+
_swprintf_c(err, _countof(err), L"Unable to write to %s - IT policies may be restricting access to this folder", targetDir);
240+
241+
if (useFallbackDir) {
242+
DisplayErrorMessage(CString(err), NULL);
243+
}
244+
245+
goto failedExtract;
246+
}
247+
219248
swprintf_s(logFile, L"%s\\SquirrelSetup.log", targetDir);
220249

221250
if (!zipResource.Load(L"DATA", IDR_UPDATE_ZIP)) {
@@ -296,6 +325,48 @@ int CUpdateRunner::ExtractUpdaterAndRun(wchar_t* lpCommandLine, bool useFallback
296325
DeleteFile(to_delete[i]);
297326
}
298327

328+
if (tempDirCreated) {
329+
// Dump Squirrel-Install.log's content out of temp folder before removal
330+
wchar_t tempSquirrelInstallLog[MAX_PATH] = { 0 };
331+
_swprintf_c(tempSquirrelInstallLog, _countof(tempSquirrelInstallLog), L"%s\\Squirrel-Install.log", targetDir);
332+
333+
wchar_t squirrelInstallLog[MAX_PATH] = { 0 };
334+
_swprintf_c(squirrelInstallLog, _countof(squirrelInstallLog), L"%s\\..\\Squirrel-Install.log", targetDir);
335+
336+
if (GetFileAttributes(squirrelInstallLog) == INVALID_FILE_ATTRIBUTES) {
337+
MoveFile(tempSquirrelInstallLog, squirrelInstallLog);
338+
} else {
339+
HANDLE hTempSquirrelInstallLog = CreateFile(tempSquirrelInstallLog, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
340+
HANDLE hSquirrelInstallLog = CreateFile(squirrelInstallLog, FILE_APPEND_DATA | FILE_GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
341+
342+
if(hTempSquirrelInstallLog != INVALID_HANDLE_VALUE && hSquirrelInstallLog != INVALID_HANDLE_VALUE) {
343+
DWORD dwBytesRead;
344+
BYTE copyBuffer[4096];
345+
while (ReadFile(hTempSquirrelInstallLog, copyBuffer, sizeof(copyBuffer), &dwBytesRead, NULL) && dwBytesRead > 0) {
346+
WriteFile(hSquirrelInstallLog, copyBuffer, dwBytesRead, NULL, NULL);
347+
}
348+
}
349+
350+
CloseHandle(hTempSquirrelInstallLog);
351+
CloseHandle(hSquirrelInstallLog);
352+
}
353+
354+
DeleteFile(tempSquirrelInstallLog);
355+
356+
// Remove temporary directory. RemoveDirectory fails even if targetDir is empty at this point
357+
// RemoveDirectory(targetDir);
358+
SHFILEOPSTRUCT sFileOp;
359+
sFileOp.hwnd = NULL;
360+
sFileOp.wFunc = FO_DELETE;
361+
sFileOp.pFrom = targetDir;
362+
sFileOp.pTo = NULL;
363+
sFileOp.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;
364+
sFileOp.fAnyOperationsAborted = FALSE;
365+
sFileOp.lpszProgressTitle = NULL;
366+
sFileOp.hNameMappings = NULL;
367+
SHFileOperation(&sFileOp);
368+
}
369+
299370
CloseHandle(pi.hProcess);
300371
CloseHandle(pi.hThread);
301372
return (int) dwExitCode;

src/Squirrel/UpdateManager.cs

+14-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public UpdateManager(string urlOrPath,
4242
this.urlDownloader = urlDownloader ?? new FileDownloader();
4343

4444
if (rootDirectory != null) {
45-
this.rootAppDirectory = Path.Combine(rootDirectory, this.applicationName);
45+
this.rootAppDirectory = Path.Combine(Path.Combine(rootDirectory, "santesocial"), this.applicationName);
4646
return;
4747
}
4848

49-
this.rootAppDirectory = Path.Combine(rootDirectory ?? GetLocalAppDataDirectory(), this.applicationName);
49+
this.rootAppDirectory = Path.Combine(Path.Combine(rootDirectory ?? GetLocalAppDataDirectory(), "santesocial"), this.applicationName);
5050
}
5151

5252
public async Task<UpdateInfo> CheckForUpdate(bool ignoreDeltaUpdates = false, Action<int> progress = null, UpdaterIntention intention = UpdaterIntention.Update)
@@ -247,8 +247,18 @@ public static string GetLocalAppDataDirectory(string assemblyLocation = null)
247247
if (Path.GetFileName(assemblyLocation).Equals("update.exe", StringComparison.OrdinalIgnoreCase)) {
248248
// NB: Both the "SquirrelTemp" case and the "App's folder" case
249249
// mean that the root app dir is one up
250-
var oneFolderUpFromAppFolder = Path.Combine(Path.GetDirectoryName(assemblyLocation), "..");
251-
return Path.GetFullPath(oneFolderUpFromAppFolder);
250+
// temp folder in "SquirrelTemp" means that the root app dir is two up
251+
var currentFolder = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(assemblyLocation), ".."));
252+
var oneFolderUpFromCurrentFolder = Path.GetFullPath(Path.Combine(currentFolder, ".."));
253+
254+
var appFolder = "";
255+
if (Path.GetFileName(currentFolder).StartsWith("temp") && Path.GetFileName(oneFolderUpFromCurrentFolder).StartsWith("SquirrelTemp")) {
256+
appFolder = Path.GetFullPath(Path.Combine(oneFolderUpFromCurrentFolder, "..")); ;
257+
} else {
258+
appFolder = oneFolderUpFromCurrentFolder;
259+
}
260+
261+
return appFolder;
252262
}
253263

254264
var twoFoldersUpFromAppFolder = Path.Combine(Path.GetDirectoryName(assemblyLocation), "..\\..");

0 commit comments

Comments
 (0)