Skip to content

Commit

Permalink
fix #1 failures to replace itself when TMPDIR and current file is not on
Browse files Browse the repository at this point in the history
the same volume
  • Loading branch information
pmq20 committed Jul 19, 2017
1 parent ea48cf7 commit da200cc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Auto-update shall only run once in every 24 hours with help of the file `~/.libautoupdate`
- add argument `force` to `autoupdate()` in order to force an auto-update check
- add CI to test `autoupdate()`
- fix failures to replace itself when TMPDIR and current file is not on the same volume
- https://github.com/pmq20/libautoupdate/issues/1

## v0.1.0

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ build_script:
cd build
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 ..
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 ..
cmake --build . --config Release
Expand Down
36 changes: 25 additions & 11 deletions src/autoupdate.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <conio.h>
#include <stdint.h>
#include <stdlib.h> /* exit */
#include <wchar.h>

int autoupdate(
int argc,
Expand Down Expand Up @@ -492,6 +493,30 @@ int autoupdate(
free(body_buffer);
return 2;
}
/* Windows paths can never be longer than this. */
const size_t exec_path_len = 32768;
wchar_t exec_path[32768];
DWORD utf16_len = GetModuleFileNameW(NULL, exec_path, exec_path_len);
if (0 == utf16_len) {
fprintf(stderr, "Auto-update Failed: GetModuleFileNameW failed with GetLastError=%d\n", GetLastError());
free((void*)(tmpdir));
free(uncomp);
free(body_buffer);
return 2;
}
if (tmpdir[0] != exec_path[0]) {
free((void*)(tmpdir));
tmpdir = wcsdup(exec_path);
wchar_t *backslash = wcsrchr(tmpdir, L'\\');
if (NULL == backslash) {
fprintf(stderr, "Auto-update Failed: Cannot find an approriate tmpdir with %S\n", tmpdir);
free((void*)(tmpdir));
free(uncomp);
free(body_buffer);
return 2;
}
*backslash = 0;
}
wchar_t *tmpf = autoupdate_tmpf(tmpdir, "exe");
if (NULL == tmpf) {
fprintf(stderr, "Auto-update Failed: no temporary file available\n");
Expand Down Expand Up @@ -524,17 +549,6 @@ int autoupdate(
fclose(fp);
free(uncomp);
free(body_buffer);
/* Windows paths can never be longer than this. */
const size_t exec_path_len = 32768;
wchar_t exec_path[32768];
DWORD utf16_len = GetModuleFileNameW(NULL, exec_path, exec_path_len);
if (0 == utf16_len) {
fprintf(stderr, "Auto-update Failed: GetModuleFileNameW failed with GetLastError=%d\n", GetLastError());
DeleteFileW(tmpf);
free((void*)(tmpdir));
free((void*)(tmpf));
return 2;
}
// Backup
wchar_t *selftmpf = autoupdate_tmpf(tmpdir, "exe");
if (NULL == selftmpf) {
Expand Down

0 comments on commit da200cc

Please sign in to comment.