|
99 | 99 | #ifndef NOB_H_ |
100 | 100 | #define NOB_H_ |
101 | 101 | #ifdef _WIN32 |
102 | | -#define _CRT_SECURE_NO_WARNINGS (1) |
| 102 | +# ifndef _CRT_SECURE_NO_WARNINGS |
| 103 | +# define _CRT_SECURE_NO_WARNINGS (1) |
| 104 | +# endif |
103 | 105 | #endif |
104 | 106 |
|
105 | 107 | #ifndef NOBDEF |
@@ -858,12 +860,36 @@ NOBDEF void nob__go_rebuild_urself(int argc, char **argv, const char *source_pat |
858 | 860 | // TODO: this is an experimental behavior behind a compilation flag. |
859 | 861 | // Once it is confirmed that it does not cause much problems on both POSIX and Windows |
860 | 862 | // we may turn it on by default. |
| 863 | +# ifdef _WIN32 |
| 864 | + nob_log(NOB_INFO, "Defer deleting of %s...", old_binary_path); |
| 865 | +# else |
861 | 866 | nob_delete_file(old_binary_path); |
| 867 | +# endif |
862 | 868 | #endif // NOB_EXPERIMENTAL_DELETE_OLD |
863 | 869 |
|
864 | 870 | nob_cmd_append(&cmd, binary_path); |
865 | 871 | nob_da_append_many(&cmd, argv, argc); |
866 | 872 | if (!nob_cmd_run_opt(&cmd, opt)) exit(1); |
| 873 | + |
| 874 | +#if defined(NOB_EXPERIMENTAL_DELETE_OLD) && defined(_WIN32) |
| 875 | + // Workaround to delete the old nob from a script instead, only after we exit this process. |
| 876 | + // If we don't do this, Windows will complain with "Access is denied" error, since |
| 877 | + // the current process is still being executed from the old nob executable. |
| 878 | + Nob_String_Builder sb = {0}; |
| 879 | + nob_sb_appendf(&sb, "$Attempts = 0; While ($Attempts++ -Lt 10) {\n"); |
| 880 | + nob_sb_appendf(&sb, " Remove-Item -Path \"%s\" -ErrorAction SilentlyContinue\n", old_binary_path); |
| 881 | + nob_sb_appendf(&sb, " If (Test-Path -Path \"%s\") { Start-Sleep -Milliseconds 100 }\n", old_binary_path); |
| 882 | + nob_sb_appendf(&sb, "}"); |
| 883 | + nob_sb_append_null(&sb); |
| 884 | + Nob_Procs procs = {0}; |
| 885 | + opt.async = &procs; |
| 886 | + // WARN: Running a PowerShell script like this might trigger anti-virus software |
| 887 | + // to think we are trying to run malicious code. |
| 888 | + nob_cmd_append(&cmd, "PowerShell", "-Command", sb.items); |
| 889 | + if (!nob_cmd_run_opt(&cmd, opt)) exit(1); |
| 890 | + nob_sb_free(sb); |
| 891 | +#endif // NOB_EXPERIMENTAL_DELETE_OLD |
| 892 | + |
867 | 893 | exit(0); |
868 | 894 | } |
869 | 895 |
|
@@ -1860,7 +1886,7 @@ NOBDEF bool nob_rename(const char *old_path, const char *new_path) |
1860 | 1886 | { |
1861 | 1887 | nob_log(NOB_INFO, "renaming %s -> %s", old_path, new_path); |
1862 | 1888 | #ifdef _WIN32 |
1863 | | - if (!MoveFileEx(old_path, new_path, MOVEFILE_REPLACE_EXISTING)) { |
| 1889 | + if (!MoveFileEx(old_path, new_path, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) { |
1864 | 1890 | nob_log(NOB_ERROR, "could not rename %s to %s: %s", old_path, new_path, nob_win32_error_message(GetLastError())); |
1865 | 1891 | return false; |
1866 | 1892 | } |
|
0 commit comments