Christian Franke wrote:
the attached patch adds device_id() for Cygwin. Tested with 32- and 64-bit Cygwin.
Should be safe to include in 1.21 as no other platform is affected.

The attached new version adds serial number support.

Regards,
Christian

2015-12-17  Christian Franke  <[email protected]>

        * non_posix.cc:  Add device_id() for Cygwin.

diff --git a/non_posix.cc b/non_posix.cc
index 480c71d..fe33536 100644
--- a/non_posix.cc
+++ b/non_posix.cc
@@ -56,6 +56,51 @@ const char * device_id( const int fd )
   return id_str.c_str();
   }
 
+#elif defined(__CYGWIN__)
+
+#include <io.h>
+#include <windows.h>
+
+const char * device_id( const int fd )
+  {
+  HANDLE h = (HANDLE) _get_osfhandle( fd );
+  if( h == INVALID_HANDLE_VALUE )
+    return 0;
+
+  STORAGE_PROPERTY_QUERY query =
+    { StorageDeviceProperty, PropertyStandardQuery, { 0 } };
+  union {
+    char raw[256];
+    STORAGE_DEVICE_DESCRIPTOR desc;
+  } data = { 0,  };
+  DWORD nout = 0;
+
+  if( !DeviceIoControl( h, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(query),
+                        &data, sizeof(data), &nout, (LPOVERLAPPED)0 ) )
+    return 0;
+
+  if( !data.desc.VendorIdOffset || !data.desc.ProductIdOffset )
+    return 0;
+
+  static std::string id_str;
+  id_str  = &data.raw[data.desc.VendorIdOffset];
+  if (    data.desc.BusType != BusTypeAta
+       && data.desc.BusType != 0x0b ) // BusTypeSata
+    id_str += ' ';
+  id_str += &data.raw[data.desc.ProductIdOffset];
+  sanitize_string( id_str );
+
+  if ( data.desc.SerialNumberOffset )
+    {
+    std::string id_serial( &data.raw[data.desc.SerialNumberOffset] );
+    sanitize_string( id_serial );
+    if ( !id_serial.empty() )
+      { id_str += "::"; id_str += id_serial; }
+    }
+
+  return id_str.c_str();
+  }
+
 #else                          // use linux by default
 #include <linux/hdreg.h>
 
_______________________________________________
Bug-ddrescue mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/bug-ddrescue

Reply via email to