From 27046fd1a74504d3080881fa3d4f0fe204ffdd90 Mon Sep 17 00:00:00 2001 From: jules Date: Sat, 3 May 2014 16:46:44 +0100 Subject: [PATCH] Added new method File::getFileIdentifier() for retrieving a file's unique ID. --- modules/juce_core/files/juce_File.h | 8 +++++++ .../juce_core/native/juce_linux_Network.cpp | 5 ++++- modules/juce_core/native/juce_mac_Network.mm | 13 +++++++---- .../juce_core/native/juce_posix_SharedCode.h | 6 +++++ modules/juce_core/native/juce_win32_Files.cpp | 22 +++++++++++++++++++ .../juce_core/native/juce_win32_Network.cpp | 16 +++++++++----- 6 files changed, 60 insertions(+), 10 deletions(-) diff --git a/modules/juce_core/files/juce_File.h b/modules/juce_core/files/juce_File.h index cfcba5e6b0..28c2e706e1 100644 --- a/modules/juce_core/files/juce_File.h +++ b/modules/juce_core/files/juce_File.h @@ -361,6 +361,14 @@ public: */ File getLinkedTarget() const; + /** Returns a unique identifier for the file, if one is available. + + Depending on the OS and file-system, this may be a unix inode number or + a win32 file identifier, or 0 if it fails to find one. The number will + be unique on the filesystem, but not globally. + */ + uint64 getFileIdentifier() const; + //============================================================================== /** Returns the last modification time of this file. diff --git a/modules/juce_core/native/juce_linux_Network.cpp b/modules/juce_core/native/juce_linux_Network.cpp index 36aeeb8079..7543a77d67 100644 --- a/modules/juce_core/native/juce_linux_Network.cpp +++ b/modules/juce_core/native/juce_linux_Network.cpp @@ -46,7 +46,10 @@ void MACAddress::findAllAddresses (Array& result) && (ifr.ifr_flags & IFF_LOOPBACK) == 0 && ioctl (s, SIOCGIFHWADDR, &ifr) == 0) { - result.addIfNotAlreadyThere (MACAddress ((const uint8*) ifr.ifr_hwaddr.sa_data)); + MACAddress ma ((const uint8*) ifr.ifr_hwaddr.sa_data); + + if (! ma.isNull()) + result.addIfNotAlreadyThere (ma); } } diff --git a/modules/juce_core/native/juce_mac_Network.mm b/modules/juce_core/native/juce_mac_Network.mm index 5cce04180b..130759e6d6 100644 --- a/modules/juce_core/native/juce_mac_Network.mm +++ b/modules/juce_core/native/juce_mac_Network.mm @@ -39,12 +39,17 @@ void MACAddress::findAllAddresses (Array& result) { const sockaddr_dl* const sadd = (const sockaddr_dl*) cursor->ifa_addr; - #ifndef IFT_ETHER - #define IFT_ETHER 6 - #endif + #ifndef IFT_ETHER + enum { IFT_ETHER = 6 }; + #endif if (sadd->sdl_type == IFT_ETHER) - result.addIfNotAlreadyThere (MACAddress (((const uint8*) sadd->sdl_data) + sadd->sdl_nlen)); + { + MACAddress ma (MACAddress (((const uint8*) sadd->sdl_data) + sadd->sdl_nlen)); + + if (! ma.isNull()) + result.addIfNotAlreadyThere (ma); + } } } diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index ca5768dd13..9bcaa7ebde 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -280,6 +280,12 @@ int64 File::getSize() const return juce_stat (fullPath, info) ? info.st_size : 0; } +uint64 File::getFileIdentifier() const +{ + juce_statStruct info; + return juce_stat (fullPath, info) ? (uint64) info.st_ino : 0; +} + //============================================================================== bool File::hasWriteAccess() const { diff --git a/modules/juce_core/native/juce_win32_Files.cpp b/modules/juce_core/native/juce_win32_Files.cpp index 6f0b3f4ffc..2ef738331e 100644 --- a/modules/juce_core/native/juce_win32_Files.cpp +++ b/modules/juce_core/native/juce_win32_Files.cpp @@ -474,6 +474,28 @@ int64 File::getVolumeTotalSize() const return WindowsFileHelpers::getDiskSpaceInfo (getFullPathName(), true); } +uint64 File::getFileIdentifier() const +{ + uint64 result = 0; + + HANDLE h = CreateFile (getFullPathName().toWideCharPointer(), + GENERIC_READ, FILE_SHARE_READ, nullptr, + OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); + + if (h != INVALID_HANDLE_VALUE) + { + BY_HANDLE_FILE_INFORMATION info; + zerostruct (info); + + if (GetFileInformationByHandle (h, &info)) + result = (((uint64) info.nFileIndexHigh) << 32) | info.nFileIndexLow; + + CloseHandle (h); + } + + return result; +} + //============================================================================== bool File::isOnCDRomDrive() const { diff --git a/modules/juce_core/native/juce_win32_Network.cpp b/modules/juce_core/native/juce_win32_Network.cpp index 22e502795f..e7af2c1b34 100644 --- a/modules/juce_core/native/juce_win32_Network.cpp +++ b/modules/juce_core/native/juce_win32_Network.cpp @@ -287,7 +287,7 @@ private: if (bytesToDo > 0 && ! InternetWriteFile (request, - static_cast (postData.getData()) + bytesSent, + static_cast (postData.getData()) + bytesSent, (DWORD) bytesToDo, &bytesDone)) { break; @@ -342,7 +342,13 @@ struct GetAdaptersInfoHelper namespace MACAddressHelpers { - void getViaGetAdaptersInfo (Array& result) + static void addAddress (Array& result, const MACAddress& ma) + { + if (! ma.isNull()) + result.addIfNotAlreadyThere (ma); + } + + static void getViaGetAdaptersInfo (Array& result) { GetAdaptersInfoHelper gah; @@ -350,11 +356,11 @@ namespace MACAddressHelpers { for (PIP_ADAPTER_INFO adapter = gah.adapterInfo; adapter != nullptr; adapter = adapter->Next) if (adapter->AddressLength >= 6) - result.addIfNotAlreadyThere (MACAddress (adapter->Address)); + addAddress (result, MACAddress (adapter->Address)); } } - void getViaNetBios (Array& result) + static void getViaNetBios (Array& result) { DynamicLibrary dll ("netapi32.dll"); JUCE_LOAD_WINAPI_FUNCTION (dll, Netbios, NetbiosCall, UCHAR, (PNCB)) @@ -396,7 +402,7 @@ namespace MACAddressHelpers ncb.ncb_length = sizeof (ASTAT); if (NetbiosCall (&ncb) == 0 && astat.adapt.adapter_type == 0xfe) - result.addIfNotAlreadyThere (MACAddress (astat.adapt.adapter_address)); + addAddress (result, MACAddress (astat.adapt.adapter_address)); } } }