Skip to content

Commit da200cc

Browse files
committed
fix #1 failures to replace itself when TMPDIR and current file is not on
the same volume
1 parent ea48cf7 commit da200cc

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- Auto-update shall only run once in every 24 hours with help of the file `~/.libautoupdate`
66
- add argument `force` to `autoupdate()` in order to force an auto-update check
77
- add CI to test `autoupdate()`
8+
- fix failures to replace itself when TMPDIR and current file is not on the same volume
9+
- https://github.com/pmq20/libautoupdate/issues/1
810

911
## v0.1.0
1012

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ build_script:
2121
2222
cd build
2323
24-
cmake -DBUILD_TESTS=ON -G"$env:GENERATOR" -DZLIB_INCLUDE_DIR:PATH=C:\projects\libautoupdate\zlib.$env:PlatformToolset.windesktop.msvcstl.dyn.rt-dyn.1.2.8.8\build\native\include -DZLIB_LIBRARY_RELEASE:FILEPATH=C:\projects\libautoupdate\zlib.$env:PlatformToolset.windesktop.msvcstl.dyn.rt-dyn.1.2.8.8\lib\native\$env:PlatformToolset\windesktop\msvcstl\dyn\rt-dyn\$env:Platform\RelWithDebInfo\zlib.lib ..
24+
cmake -DBUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Release -G"$env:GENERATOR" -DZLIB_INCLUDE_DIR:PATH=C:\projects\libautoupdate\zlib.$env:PlatformToolset.windesktop.msvcstl.dyn.rt-dyn.1.2.8.8\build\native\include -DZLIB_LIBRARY_RELEASE:FILEPATH=C:\projects\libautoupdate\zlib.$env:PlatformToolset.windesktop.msvcstl.dyn.rt-dyn.1.2.8.8\lib\native\$env:PlatformToolset\windesktop\msvcstl\dyn\rt-dyn\$env:Platform\RelWithDebInfo\zlib.lib ..
2525
2626
cmake --build . --config Release
2727

src/autoupdate.c

+25-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <conio.h>
2020
#include <stdint.h>
2121
#include <stdlib.h> /* exit */
22+
#include <wchar.h>
2223

2324
int autoupdate(
2425
int argc,
@@ -492,6 +493,30 @@ int autoupdate(
492493
free(body_buffer);
493494
return 2;
494495
}
496+
/* Windows paths can never be longer than this. */
497+
const size_t exec_path_len = 32768;
498+
wchar_t exec_path[32768];
499+
DWORD utf16_len = GetModuleFileNameW(NULL, exec_path, exec_path_len);
500+
if (0 == utf16_len) {
501+
fprintf(stderr, "Auto-update Failed: GetModuleFileNameW failed with GetLastError=%d\n", GetLastError());
502+
free((void*)(tmpdir));
503+
free(uncomp);
504+
free(body_buffer);
505+
return 2;
506+
}
507+
if (tmpdir[0] != exec_path[0]) {
508+
free((void*)(tmpdir));
509+
tmpdir = wcsdup(exec_path);
510+
wchar_t *backslash = wcsrchr(tmpdir, L'\\');
511+
if (NULL == backslash) {
512+
fprintf(stderr, "Auto-update Failed: Cannot find an approriate tmpdir with %S\n", tmpdir);
513+
free((void*)(tmpdir));
514+
free(uncomp);
515+
free(body_buffer);
516+
return 2;
517+
}
518+
*backslash = 0;
519+
}
495520
wchar_t *tmpf = autoupdate_tmpf(tmpdir, "exe");
496521
if (NULL == tmpf) {
497522
fprintf(stderr, "Auto-update Failed: no temporary file available\n");
@@ -524,17 +549,6 @@ int autoupdate(
524549
fclose(fp);
525550
free(uncomp);
526551
free(body_buffer);
527-
/* Windows paths can never be longer than this. */
528-
const size_t exec_path_len = 32768;
529-
wchar_t exec_path[32768];
530-
DWORD utf16_len = GetModuleFileNameW(NULL, exec_path, exec_path_len);
531-
if (0 == utf16_len) {
532-
fprintf(stderr, "Auto-update Failed: GetModuleFileNameW failed with GetLastError=%d\n", GetLastError());
533-
DeleteFileW(tmpf);
534-
free((void*)(tmpdir));
535-
free((void*)(tmpf));
536-
return 2;
537-
}
538552
// Backup
539553
wchar_t *selftmpf = autoupdate_tmpf(tmpdir, "exe");
540554
if (NULL == selftmpf) {

0 commit comments

Comments
 (0)