Project

General

Profile

Patch #984 » ReplaceFile_2.patch

Thomas Beutlich, 04 Sep 2014 05:01

View differences:

src/basicio.cpp (working copy)
716 716
                // that file has been opened with FILE_SHARE_DELETE by another process,
717 717
                // like a virus scanner or disk indexer
718 718
                // (see also http://stackoverflow.com/a/11023068)
719
                if (ReplaceFileW(wpf, fileIo->wpath().c_str(), NULL, REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL) == 0) {
720
                    throw WError(17, fileIo->wpath(), wpf, strError().c_str());
719
                typedef BOOL (WINAPI * ReplaceFileW_t)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
720
                HMODULE hKernel = LoadLibraryA("kernel32.dll");
721
                if (hKernel) {
722
                    ReplaceFileW_t pfcn_ReplaceFileW = (ReplaceFileW_t)GetProcAddress(hKernel, "ReplaceFileW");
723
                    if (pfcn_ReplaceFileW) {
724
                        BOOL ret = pfcn_ReplaceFileW(wpf, fileIo->wpath().c_str(), NULL, REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL);
725
                        FreeLibrary(hKernel);
726
                        if (ret == 0) {
727
                            if (GetLastError() == ERROR_FILE_NOT_FOUND) {
728
                                if (::_wrename(fileIo->wpath().c_str(), wpf) == -1) {
729
                                    throw WError(17, fileIo->wpath(), wpf, strError().c_str());
730
                                }
731
                                ::_wremove(fileIo->wpath().c_str());
732
                            }
733
                            else {
734
                                throw WError(17, fileIo->wpath(), wpf, strError().c_str());
735
                            }
736
                        }
737
                    }
738
                    else {
739
                        FreeLibrary(hKernel);
740
                        if (fileExists(wpf) && ::_wremove(wpf) != 0) {
741
                            throw WError(2, wpf, strError().c_str(), "::_wremove");
742
                        }
743
                        if (::_wrename(fileIo->wpath().c_str(), wpf) == -1) {
744
                            throw WError(17, fileIo->wpath(), wpf, strError().c_str());
745
                        }
746
                        ::_wremove(fileIo->wpath().c_str());
747
                    }
721 748
                }
722 749
#else
723 750
                if (fileExists(wpf) && ::_wremove(wpf) != 0) {
......
754 781
                // that file has been opened with FILE_SHARE_DELETE by another process,
755 782
                // like a virus scanner or disk indexer
756 783
                // (see also http://stackoverflow.com/a/11023068)
757
                if (ReplaceFile(pf, fileIo->path().c_str(), NULL, REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL) == 0) {
758
                    throw Error(17, fileIo->path(), pf, strError());
784
                typedef BOOL (WINAPI * ReplaceFileA_t)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
785
                HMODULE hKernel = LoadLibraryA("kernel32.dll");
786
                if (hKernel) {
787
                    ReplaceFileA_t pfcn_ReplaceFileA = (ReplaceFileA_t)GetProcAddress(hKernel, "ReplaceFileA");
788
                    if (pfcn_ReplaceFileA) {
789
                        BOOL ret = pfcn_ReplaceFileA(pf, fileIo->path().c_str(), NULL, REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL);
790
                        FreeLibrary(hKernel);
791
                        if (ret == 0) {
792
                            if (GetLastError() == ERROR_FILE_NOT_FOUND) {
793
                                if (::rename(fileIo->path().c_str(), pf) == -1) {
794
                                    throw Error(17, fileIo->path(), pf, strError());
795
                                }
796
                                ::remove(fileIo->path().c_str());
797
                            }
798
                            else {
799
                                throw Error(17, fileIo->path(), pf, strError());
800
                            }
801
                        }
802
                    }
803
                    else {
804
                        FreeLibrary(hKernel);
805
                        if (fileExists(pf) && ::remove(pf) != 0) {
806
                            throw Error(2, pf, strError(), "::remove");
807
                        }
808
                        if (::rename(fileIo->path().c_str(), pf) == -1) {
809
                            throw Error(17, fileIo->path(), pf, strError());
810
                        }
811
                        ::remove(fileIo->path().c_str());
812
                    }
759 813
                }
760 814
#else
761 815
                if (fileExists(pf) && ::remove(pf) != 0) {
(3-3/4)