Commit 2bf53afb authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Move grabbag__file_are_same to the only file it's used in

This function doesn't compile when targetting WinRT/UWP (as it uses
forbidden function).
Since the function is only used in the flac program, it makes sense to
move the function there, as executable are not built for that platform.
parent 011b5e57
......@@ -47,10 +47,6 @@ const char *grabbag__file_get_basename(const char *srcpath);
*/
FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only);
/* returns true iff stat() succeeds for both files and they have the same device and inode. */
/* on windows, uses GetFileInformationByHandle() to compare */
FLAC__bool grabbag__file_are_same(const char *f1, const char *f2);
/* attempts to make writable before unlinking */
FLAC__bool grabbag__file_remove_file(const char *filename);
......
......@@ -29,6 +29,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#if !defined _MSC_VER && !defined __MINGW32__
/* unlink is in stdio.h in VC++ */
......@@ -71,6 +72,7 @@ static void show_help(void);
static void show_explain(void);
static void format_mistake(const char *infilename, FileFormat wrong, FileFormat right);
static FLAC__bool file_are_same(const char *f1, const char *f2);
static int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_last_file);
static int decode_file(const char *infilename);
......@@ -1682,6 +1684,44 @@ void format_mistake(const char *infilename, FileFormat wrong, FileFormat right)
flac__utils_printf(stderr, 1, "WARNING: %s is not a%s file; treating as a%s file\n", infilename, ff[wrong], ff[right]);
}
FLAC__bool file_are_same(const char *f1, const char *f2)
{
#if defined _WIN32 && !defined __CYGWIN__
/* see
* http://www.hydrogenaudio.org/forums/index.php?showtopic=49439&pid=444300&st=0
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileinformationbyhandle.asp
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/by_handle_file_information_str.asp
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/createfile.asp
* apparently both the files have to be open at the same time for the comparison to work
*/
FLAC__bool same = false;
BY_HANDLE_FILE_INFORMATION info1, info2;
HANDLE h1, h2;
BOOL ok = 1;
h1 = CreateFile_utf8(f1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
h2 = CreateFile_utf8(f2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(h1 == INVALID_HANDLE_VALUE || h2 == INVALID_HANDLE_VALUE)
ok = 0;
ok &= GetFileInformationByHandle(h1, &info1);
ok &= GetFileInformationByHandle(h2, &info2);
if(ok)
same =
info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber &&
info1.nFileIndexHigh == info2.nFileIndexHigh &&
info1.nFileIndexLow == info2.nFileIndexLow
;
if(h1 != INVALID_HANDLE_VALUE)
CloseHandle(h1);
if(h2 != INVALID_HANDLE_VALUE)
CloseHandle(h2);
return same;
#else
struct flac_stat_s s1, s2;
return f1 && f2 && flac_stat(f1, &s1) == 0 && flac_stat(f2, &s2) == 0 && s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
#endif
}
int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_last_file)
{
FILE *encode_infile;
......@@ -1924,7 +1964,7 @@ int encode_file(const char *infilename, FLAC__bool is_first_file, FLAC__bool is_
encode_options.error_on_compression_fail = option_values.error_on_compression_fail;
/* if infilename and outfilename point to the same file, we need to write to a temporary file */
if(encode_infile != stdin && grabbag__file_are_same(infilename, outfilename)) {
if(encode_infile != stdin && file_are_same(infilename, outfilename)) {
static const char *tmp_suffix = ".tmp,fl-ac+en'c";
size_t dest_len = strlen(outfilename) + strlen(tmp_suffix) + 1;
/*@@@@ still a remote possibility that a file with this filename exists */
......
......@@ -115,43 +115,6 @@ FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only
return true;
}
FLAC__bool grabbag__file_are_same(const char *f1, const char *f2)
{
#if defined _WIN32 && !defined __CYGWIN__
/* see
* http://www.hydrogenaudio.org/forums/index.php?showtopic=49439&pid=444300&st=0
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileinformationbyhandle.asp
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/by_handle_file_information_str.asp
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/createfile.asp
* apparently both the files have to be open at the same time for the comparison to work
*/
FLAC__bool same = false;
BY_HANDLE_FILE_INFORMATION info1, info2;
HANDLE h1, h2;
BOOL ok = 1;
h1 = CreateFile_utf8(f1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
h2 = CreateFile_utf8(f2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(h1 == INVALID_HANDLE_VALUE || h2 == INVALID_HANDLE_VALUE)
ok = 0;
ok &= GetFileInformationByHandle(h1, &info1);
ok &= GetFileInformationByHandle(h2, &info2);
if(ok)
same =
info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber &&
info1.nFileIndexHigh == info2.nFileIndexHigh &&
info1.nFileIndexLow == info2.nFileIndexLow
;
if(h1 != INVALID_HANDLE_VALUE)
CloseHandle(h1);
if(h2 != INVALID_HANDLE_VALUE)
CloseHandle(h2);
return same;
#else
struct flac_stat_s s1, s2;
return f1 && f2 && flac_stat(f1, &s1) == 0 && flac_stat(f2, &s2) == 0 && s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
#endif
}
FLAC__bool grabbag__file_remove_file(const char *filename)
{
return grabbag__file_change_stats(filename, /*read_only=*/false) && 0 == flac_unlink(filename);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment