Skip to content

Commit

Permalink
create win32::is_same_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
nu774 committed May 5, 2017
1 parent b7c8e56 commit c3a0a7e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
9 changes: 1 addition & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,7 @@ std::wstring get_output_filename(const wchar_t *ifilename, const Options &opts)
} catch (...) {
return ofilename;
}
BY_HANDLE_FILE_INFORMATION ibhi = { 0 }, obhi = { 0 };
HANDLE ih = reinterpret_cast<HANDLE>(_get_osfhandle(fileno(ifp.get())));
HANDLE oh = reinterpret_cast<HANDLE>(_get_osfhandle(fileno(ofp.get())));
GetFileInformationByHandle(ih, &ibhi);
GetFileInformationByHandle(oh, &obhi);
if (ibhi.dwVolumeSerialNumber != obhi.dwVolumeSerialNumber ||
ibhi.nFileIndexHigh != obhi.nFileIndexHigh ||
ibhi.nFileIndexLow != obhi.nFileIndexLow)
if (!win32::is_same_file(_fileno(ifp.get()), _fileno(ofp.get())))
return ofilename;

std::wstring tl = strutil::format(L"_%s", ext);
Expand Down
11 changes: 11 additions & 0 deletions win32util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,15 @@ namespace win32 {
}
return "";
}

bool is_same_file(HANDLE ha, HANDLE hb)
{
BY_HANDLE_FILE_INFORMATION bhfia, bhfib;

if (!GetFileInformationByHandle(ha, &bhfia)) return false;
if (!GetFileInformationByHandle(hb, &bhfib)) return false;
return bhfia.dwVolumeSerialNumber == bhfib.dwVolumeSerialNumber
&& bhfia.nFileIndexHigh == bhfib.nFileIndexHigh
&& bhfia.nFileIndexLow == bhfib.nFileIndexLow;
}
}
7 changes: 7 additions & 0 deletions win32util.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,12 @@ namespace win32 {
int create_named_pipe(const wchar_t *path);

std::string get_dll_version_for_locale(HMODULE hDll, WORD langid);

bool is_same_file(HANDLE ha, HANDLE hb);

inline bool is_same_file(int fda, int fdb)
{
return is_same_file(get_handle(fda), get_handle(fdb));
}
}
#endif

0 comments on commit c3a0a7e

Please sign in to comment.