Je suis en train de développer un programme sous Mac OS et Linux qui répertorie les fichiers et dossiers du lecteur USB. J'ai besoin d'obtenir certains événements lorsque le périphérique USB est connecté et retiré. Je sais que sous Mac OS X je peux utiliser IOKit. Mais je ne sais pas comment obtenir le point de montage où l'appareil est monté. Puis-je l'obtenir en utilisant IOkit? Existe-t-il une solution multiplateforme pour Linux et Mac?Obtention du point de montage lors de l'insertion d'un périphérique USB Mac OS X et Linux
1
A
Répondre
0
Non, il n'y en a pas. Sous Linux, vous pouvez utiliser les interfaces HAL ou DeviceKit-disks D-Bus. Notez que ce sont des composants facultatifs et peuvent être absents. HAL est plus ancien et les DeviceKit-disks sont des implémentations plus récentes, avec DK-d remplaçant HAL.
0
L'approche que j'ai utilisée pour obtenir les points de montage disponibles (avec Java) redirige la sortie de la commande "system_profiler SPUSBDataType -xml" vers le processeur dd-plist. Il récursive par la suite sur la hiérarchie USB, correspondant à ceux ayant une clé "volumes". Récupérez la clé "mount_point" pour chaque élément de ce tableau afin de récupérer l'emplacement où il est monté. Voir l'exemple de code ci-dessous:
/*
Copyright © 2014 Edwin de Jong. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY [LICENSOR] "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package nl.topicuszorg.laos.util.osx;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import nl.topicuszorg.laos.model.response.MountState;
import nl.topicuszorg.laos.model.response.MountedDevice;
import org.xml.sax.SAXException;
import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSObject;
import com.dd.plist.NSString;
import com.dd.plist.PropertyListFormatException;
import com.dd.plist.PropertyListParser;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
public class USBHelpers
{
private static final String SYSTEM_PROFILER_COMMAND = "/usr/sbin/system_profiler";
private static final String SPUSB_DATA_TYPE = "SPUSBDataType";
private interface SpUSBDataTypeIdentifiers
{
String ITEMS = "_items";
String VOLUMES = "volumes";
String VENDOR_ID = "vendor_id";
String MOUNT_POINT = "mount_point";
String NAME = "_name";
}
public static List<MountedDevice> findMountedDevicesOsX() throws IOException, PropertyListFormatException,
ParseException, ParserConfigurationException, SAXException
{
final Process process = new ProcessBuilder(SYSTEM_PROFILER_COMMAND, SPUSB_DATA_TYPE, "-xml")
.start();
return findMountedDevicesInConfiguration(process.getInputStream());
}
private static List<MountedDevice> findMountedDevicesInConfiguration(final InputStream processInputStream)
throws IOException, PropertyListFormatException, ParseException, ParserConfigurationException, SAXException
{
// Root is an array, the USB devices are hierarchical in _items (and eg. _items(0)._items)
final NSArray array = (NSArray) (PropertyListParser.parse(processInputStream));
final NSDictionary dict = (NSDictionary) array.objectAtIndex(0);
final NSArray itemsArray = (NSArray) dict.get(SpUSBDataTypeIdentifiers.ITEMS);
return recurseUSBDevices(itemsArray);
}
public static List<MountedDevice> recurseUSBDevices(NSArray items)
{
final Builder<MountedDevice> builder = ImmutableList.builder();
for (NSObject item : items.getArray())
{
builder.addAll(recurseUSBDevice((NSDictionary) item));
}
return builder.build();
}
private static List<MountedDevice> recurseUSBDevice(final NSDictionary dict)
{
final Builder<MountedDevice> builder = ImmutableList.builder();
for (final Map.Entry<String, NSObject> entry : dict.entrySet())
{
if (entry.getKey().equals(SpUSBDataTypeIdentifiers.ITEMS))
{
// The USB device is a hub
builder.addAll(recurseUSBDevices((NSArray) entry.getValue()));
}
if (entry.getKey().equals(SpUSBDataTypeIdentifiers.VOLUMES))
{
// This is a mountable device. We need to get the volumes, and for each volume, return it.
List<MountedDevice> mountedDeviceOpt = parseVolumes((NSArray) (entry.getValue()));
for (MountedDevice mountedDevice : mountedDeviceOpt)
{
mountedDevice.setVendorId(((NSString) dict.get(SpUSBDataTypeIdentifiers.VENDOR_ID)).toString());
builder.add(mountedDevice);
}
}
}
return builder.build();
}
private static List<MountedDevice> parseVolumes(final NSArray nsArray)
{
final Builder<MountedDevice> builder = ImmutableList.builder();
for (final NSObject item : nsArray.getArray())
{
builder.add(parseVolume((NSDictionary) item));
}
return builder.build();
}
private static MountedDevice parseVolume(final NSDictionary item)
{
final String mountPoint = ((NSString) item.get(SpUSBDataTypeIdentifiers.MOUNT_POINT)).toString();
final String name = ((NSString) item.get(SpUSBDataTypeIdentifiers.NAME)).toString();
return new MountedDevice(mountPoint, name, null, null, MountState.MOUNTED);
}
}
Questions connexes
- 1. objectif cacao-c pour os x: obtention du point de montage du volume à partir du chemin
- 2. problème d'impression point de montage de usb en python
- 3. Callstack sous Windows et Mac OS X/Linux sans symboles
- 4. Mac OS X et Mercurial
- 5. Formatage d'un lecteur flash USB sur Mac OS X
- 6. Emulation de binaires Linux sous Mac OS X
- 7. Recherche de chemins de lecteur USB sous Mac OS 10.4
- 8. Modification des autorisations du périphérique de stockage USB dans l'espace noyau OS X
- 9. Mac OS X python 'import vigra' erreur
- 10. API Java Sound sur OS X avec carte son USB
- 11. Qt4Dotnet sur Mac OS X
- 12. symbole introuvable lors de l'utilisation de ScriptingBridge.framework (Mac OS X)
- 13. Obtention d'informations sur le périphérique USB NON_HID
- 14. Mac OS X: obtention des noms des fichiers modifiés/écrits
- 15. Problème lors de l'exécution de Virtualenv sous Mac OS X
- 16. Génération d'événements de souris sous Mac OS X
- 17. Programmation d'un périphérique USB sur un Mac
- 18. Equivalents de XDG_CONFIG_HOME et XDG_DATA_HOME sur Mac OS X?
- 19. Est-il possible d'accéder à la table de montage Mac OS X lors de la connexion via SSH?
- 20. Compilation d'une application Qt pour Mac OS X sous Linux
- 21. Application/service Mac OS X et stdin?
- 22. La meilleure façon de modifier les fichiers de configuration du serveur Linux sous Mac OS X?
- 23. Mac OS X 10.5+ et POSIX
- 24. Périphérique de stockage USB SerialNumber du pilote minifiltre (du descripteur de périphérique USB)
- 25. Paperclip, Rails 2.3.3 et Mac OS X
- 26. Synchronisation entre Mac OS X et iPhone
- 27. Xcode 3.2.4 et Mac OS X 10.5
- 28. Comment désinstaller MySQL de Mac OS X?
- 29. Échange de vue Mac OS X
- 30. Equivalent GetTextExtentPoint32 sur Mac OS X
Merci beaucoup pour la réponse. En général, comment obtenir le chemin de montage d'un périphérique USB (j'utilise des iPods) sur Mac. J'utilise IOServiceMatching (kIOUSBDeviceClassName) et IOServiceAddMatchingNotification pour écouter les notifications d'ajout/suppression de périphérique USB. Une fois que je reçois la notification que je utilise IORegistryEntryCreateCFProperty (\t \t \t \t \t \t \t \t \t \t \t dispositif \t, \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t RRACC (kIOBSDNameKey), \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t kCFAllocatorDefault, 0); pour obtenir le nom de l'appareil. Mais cet appel renvoie NULL. – wantro
Suite du commentaire précédent ... Lorsque j'ai utilisé IORegistryEntrySearchCFProperty (e, kIOServicePlane, CFSTR (kIOBSDNameKey), NULL, bits); Je reçois le nom BSD en tant que disk2. Est-ce ainsi que je devrais obtenir le nom BSD? Parce que le profler du système montre le périphérique en tant que/dev/disk2s1 – wantro