| 1 |
/* |
| 2 |
* Copyright (c) 2015-2016 Intel Corporation, Inc. All rights reserved. |
| 3 |
* |
| 4 |
* This software is available to you under a choice of one of two |
| 5 |
* licenses. You may choose to be licensed under the terms of the GNU |
| 6 |
* General Public License (GPL) Version 2, available from the file |
| 7 |
* COPYING in the main directory of this source tree, or the |
| 8 |
* BSD license below: |
| 9 |
* |
| 10 |
* Redistribution and use in source and binary forms, with or |
| 11 |
* without modification, are permitted provided that the following |
| 12 |
* conditions are met: |
| 13 |
* |
| 14 |
* - Redistributions of source code must retain the above |
| 15 |
* copyright notice, this list of conditions and the following |
| 16 |
* disclaimer. |
| 17 |
* |
| 18 |
* - Redistributions in binary form must reproduce the above |
| 19 |
* copyright notice, this list of conditions and the following |
| 20 |
* disclaimer in the documentation and/or other materials |
| 21 |
* provided with the distribution. |
| 22 |
* |
| 23 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 |
* SOFTWARE. |
| 31 |
*/ |
| 32 |
|
| 33 |
|
| 34 |
#include <windows.h> |
| 35 |
#include <assert.h> |
| 36 |
|
| 37 |
|
| 38 |
#define ND_SHARE_ATTR (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE) |
| 39 |
|
| 40 |
static inline DWORD nd_file_attributes(const wchar_t* path) |
| 41 |
{ |
| 42 |
if (path && path[0]) |
| 43 |
return GetFileAttributesW(path); |
| 44 |
|
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
int nd_file_exists(const wchar_t* path) |
| 49 |
{ |
| 50 |
return nd_file_attributes(path) != INVALID_FILE_ATTRIBUTES; |
| 51 |
} |
| 52 |
|
| 53 |
int nd_is_directory(const wchar_t* path) |
| 54 |
{ |
| 55 |
return nd_file_exists(path) && |
| 56 |
(nd_file_attributes(path) & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 57 |
} |
| 58 |
|
| 59 |
int nd_is_same_file(const wchar_t* path1, const wchar_t* path2) |
| 60 |
{ |
| 61 |
if (!path1 || !path1[0] || !path2 || !path2[0]) |
| 62 |
return 0; |
| 63 |
|
| 64 |
if (!lstrcmpiW(path1, path2)) |
| 65 |
return 1; |
| 66 |
|
| 67 |
int is_same = 1; |
| 68 |
|
| 69 |
if (nd_file_exists(path1) && nd_file_exists(path2)) { |
| 70 |
if (nd_is_directory(path1) ^ nd_is_directory(path2)) |
| 71 |
return 0; // if one file is directory & another - not - return false |
| 72 |
|
| 73 |
HANDLE f1 = CreateFileW(path1, FILE_READ_ATTRIBUTES, |
| 74 |
ND_SHARE_ATTR, |
| 75 |
0, OPEN_EXISTING, |
| 76 |
nd_is_directory(path1) ? FILE_FLAG_BACKUP_SEMANTICS : FILE_ATTRIBUTE_NORMAL, 0); |
| 77 |
if (f1 && f1 != INVALID_HANDLE_VALUE) { |
| 78 |
HANDLE f2 = CreateFileW(path2, FILE_READ_ATTRIBUTES, |
| 79 |
ND_SHARE_ATTR, |
| 80 |
0, OPEN_EXISTING, |
| 81 |
nd_is_directory(path2) ? FILE_FLAG_BACKUP_SEMANTICS : FILE_ATTRIBUTE_NORMAL, 0); |
| 82 |
if (f2 && f2 != INVALID_HANDLE_VALUE) { |
| 83 |
BY_HANDLE_FILE_INFORMATION info1; |
| 84 |
BY_HANDLE_FILE_INFORMATION info2; |
| 85 |
|
| 86 |
if (GetFileInformationByHandle(f1, &info1) && |
| 87 |
GetFileInformationByHandle(f2, &info2)) { |
| 88 |
if (info1.nFileIndexHigh == info2.nFileIndexHigh && |
| 89 |
info1.nFileIndexLow == info2.nFileIndexLow && |
| 90 |
info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber) |
| 91 |
is_same = 1; |
| 92 |
} |
| 93 |
CloseHandle(f2); |
| 94 |
} |
| 95 |
CloseHandle(f1); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return is_same; |
| 100 |
} |
| 101 |
|
| 102 |
const wchar_t *nd_filename(const wchar_t *path) |
| 103 |
{ |
| 104 |
assert(path); |
| 105 |
int i; |
| 106 |
|
| 107 |
for (i = lstrlenW(path) - 2; i >= 0; i--) |
| 108 |
if (path[i] == '/' || path[i] == '\\') |
| 109 |
return &path[i] + 1; |
| 110 |
return path; |
| 111 |
} |