@@ -1792,28 +1792,25 @@ disable_queue (const char *printer_uri, void *context)
17921792 httpClose (cups );
17931793}
17941794
1795- static int
1796- device_exists (char * device_path )
1795+ /*
1796+ * 'device_exists()' - Checks if the device really exists within filesystem.
1797+ */
1798+
1799+ static int /* 0 if not found, 1 if found*/
1800+ device_exists (
1801+ char * device_path ) /* I - string representing device path of USB device*/
17971802{
17981803 char full_path [100 ];
17991804 struct stat buffer ;
1800- int exist = -1 ;
1801- int path_length = strlen (device_path );
1802- // 5 is the length of "/sys" + null
1803- snprintf (full_path , path_length + 5 , "%s%s" , "/sys" , device_path );
18041805
1805- // check the exist of this device
1806- if (stat (full_path , & buffer ) == 0 ){
1807- exist = 1 ;
1808- }
1806+ // create a full path
1807+ snprintf (full_path , sizeof (full_path ), "%s%s" , "/sys" , device_path );
18091808
1810- return exist ;
1811- }
1809+ // check the exist of this device
1810+ if (stat (full_path , & buffer ) == 0 )
1811+ return 1 ;
18121812
1813- static char *
1814- compare_usb_uri (char * uri_store , char * uri_uevent )
1815- {
1816- return strstr (uri_uevent , uri_store );
1813+ return 0 ;
18171814}
18181815
18191816static int
@@ -1855,7 +1852,19 @@ do_remove (const char *devaddr)
18551852 prev = & map -> entries ;
18561853 for (entry = map -> entries ; entry ; entry = entry -> next )
18571854 {
1858- if (compare_usb_uri (entry -> devpath , devpath ) != NULL && device_exists (entry -> devpath ) == -1 )
1855+ /* devpath is a string obtained from USB 'remove' event
1856+ * and it is usually longer or the same as the string
1857+ * from the main USB 'add' event, when the device is added.
1858+ *
1859+ * Both strings represent device paths in filesystem without '/sys':
1860+ * f.e.:
1861+ * /devices/pci0000:00/0000:00:14.0/usb3/3-2
1862+ *
1863+ * When we are disabling the queue, we check if the string from 'add'
1864+ * event is a substring of string from 'remove' event and the actual
1865+ * device doesn't exist anymore.
1866+ */
1867+ if (strstr (devpath , entry -> devpath ) != NULL && !device_exists (entry -> devpath ))
18591868 {
18601869 uris = & entry -> uris ;
18611870 break ;
0 commit comments