Mercurial > vim
comparison src/os_win32.c @ 2793:ee48b3da9d53 v7.3.172
updated for version 7.3.172
Problem: MS-Windows: rename() might delete the file if the name differs but
it's actually the same file.
Solution: Use the file handle to check if it's the same file. (Yukihiro
Nakadaira)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Thu, 05 May 2011 16:41:24 +0200 |
parents | 3ea3dcbf2cd6 |
children | 66f2d62271fe |
comparison
equal
deleted
inserted
replaced
2792:f8a458d9f368 | 2793:ee48b3da9d53 |
---|---|
2643 * Return TRUE if file "fname" has more than one link. | 2643 * Return TRUE if file "fname" has more than one link. |
2644 */ | 2644 */ |
2645 int | 2645 int |
2646 mch_is_linked(char_u *fname) | 2646 mch_is_linked(char_u *fname) |
2647 { | 2647 { |
2648 BY_HANDLE_FILE_INFORMATION info; | |
2649 | |
2650 return win32_fileinfo(fname, &info) == FILEINFO_OK | |
2651 && info.nNumberOfLinks > 1; | |
2652 } | |
2653 | |
2654 /* | |
2655 * Get the by-handle-file-information for "fname". | |
2656 * Returns FILEINFO_OK when OK. | |
2657 * returns FILEINFO_ENC_FAIL when enc_to_utf16() failed. | |
2658 * Returns FILEINFO_READ_FAIL when CreateFile() failed. | |
2659 * Returns FILEINFO_INFO_FAIL when GetFileInformationByHandle() failed. | |
2660 */ | |
2661 int | |
2662 win32_fileinfo(char_u *fname, BY_HANDLE_FILE_INFORMATION *info) | |
2663 { | |
2648 HANDLE hFile; | 2664 HANDLE hFile; |
2649 int res = 0; | 2665 int res = FILEINFO_READ_FAIL; |
2650 BY_HANDLE_FILE_INFORMATION inf; | |
2651 #ifdef FEAT_MBYTE | 2666 #ifdef FEAT_MBYTE |
2652 WCHAR *wn = NULL; | 2667 WCHAR *wn = NULL; |
2653 | 2668 |
2654 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) | 2669 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
2670 { | |
2655 wn = enc_to_utf16(fname, NULL); | 2671 wn = enc_to_utf16(fname, NULL); |
2672 if (wn == NULL) | |
2673 res = FILEINFO_ENC_FAIL; | |
2674 } | |
2656 if (wn != NULL) | 2675 if (wn != NULL) |
2657 { | 2676 { |
2658 hFile = CreateFileW(wn, /* file name */ | 2677 hFile = CreateFileW(wn, /* file name */ |
2659 GENERIC_READ, /* access mode */ | 2678 GENERIC_READ, /* access mode */ |
2660 0, /* share mode */ | 2679 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share mode */ |
2661 NULL, /* security descriptor */ | 2680 NULL, /* security descriptor */ |
2662 OPEN_EXISTING, /* creation disposition */ | 2681 OPEN_EXISTING, /* creation disposition */ |
2663 0, /* file attributes */ | 2682 FILE_FLAG_BACKUP_SEMANTICS, /* file attributes */ |
2664 NULL); /* handle to template file */ | 2683 NULL); /* handle to template file */ |
2665 if (hFile == INVALID_HANDLE_VALUE | 2684 if (hFile == INVALID_HANDLE_VALUE |
2666 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) | 2685 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
2667 { | 2686 { |
2668 /* Retry with non-wide function (for Windows 98). */ | 2687 /* Retry with non-wide function (for Windows 98). */ |
2669 vim_free(wn); | 2688 vim_free(wn); |
2670 wn = NULL; | 2689 wn = NULL; |
2671 } | 2690 } |
2672 } | 2691 } |
2673 if (wn == NULL) | 2692 if (wn == NULL) |
2674 #endif | 2693 #endif |
2675 hFile = CreateFile(fname, /* file name */ | 2694 hFile = CreateFile(fname, /* file name */ |
2676 GENERIC_READ, /* access mode */ | 2695 GENERIC_READ, /* access mode */ |
2677 0, /* share mode */ | 2696 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share mode */ |
2678 NULL, /* security descriptor */ | 2697 NULL, /* security descriptor */ |
2679 OPEN_EXISTING, /* creation disposition */ | 2698 OPEN_EXISTING, /* creation disposition */ |
2680 0, /* file attributes */ | 2699 FILE_FLAG_BACKUP_SEMANTICS, /* file attributes */ |
2681 NULL); /* handle to template file */ | 2700 NULL); /* handle to template file */ |
2682 | 2701 |
2683 if (hFile != INVALID_HANDLE_VALUE) | 2702 if (hFile != INVALID_HANDLE_VALUE) |
2684 { | 2703 { |
2685 if (GetFileInformationByHandle(hFile, &inf) != 0 | 2704 if (GetFileInformationByHandle(hFile, info) != 0) |
2686 && inf.nNumberOfLinks > 1) | 2705 res = FILEINFO_OK; |
2687 res = 1; | 2706 else |
2707 res = FILEINFO_INFO_FAIL; | |
2688 CloseHandle(hFile); | 2708 CloseHandle(hFile); |
2689 } | 2709 } |
2690 | 2710 |
2691 #ifdef FEAT_MBYTE | 2711 #ifdef FEAT_MBYTE |
2692 vim_free(wn); | 2712 vim_free(wn); |