插入 USB 设备时获取挂载点 Mac OS X 和 Linux

发布于 2024-08-14 05:35:17 字数 164 浏览 11 评论 0原文

我正在尝试在 Mac OS 和 Linux 中开发一个程序,列出 USB 驱动器中的文件和文件夹。我需要获取连接和删除 USB 设备时的一些事件。我知道在 Mac OS XI 中可以使用 IOKit。但不知道如何获取设备挂载的挂载点。我可以使用 IOkit 获取它吗?有没有针对Linux和Mac的跨平台解决方案?

I am trying to develop a program in Mac OS and Linux which lists the files and folders in USB drive. I need to get the some events when USB device is connected and removed. I know that in Mac OS X I can use IOKit. But I do n't know how to get the mount point where the device is mounted. Can I get it using IOkit? Is there any cross platform solution for Linux and Mac?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖迟 2024-08-21 05:35:18

我用于获取可用安装点(使用 Java)的方法将“system_profiler SPUSBDataType -xml”命令的输出通过管道传输到 dd-plist 处理器。随后它在 USB 层次结构上递归,匹配具有“卷”键的那些。对于此数组中的每个项目,检索“mount_point”键以检索其安装位置。请参阅下面的代码示例:

/*
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);
  }

}

The approach I used for getting the available mount-points (with Java) pipes the output of the "system_profiler SPUSBDataType -xml" command to dd-plist processor. It subsequently recurses over the USB hierarchy, matching those having a "volumes" key. For each item in this array, retrieve the "mount_point" key to retrieve the location where it is mounted. See the code sample below:

/*
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);
  }

}
风柔一江水 2024-08-21 05:35:17

不,没有。在 Linux 下,您可以使用 HAL 或 DeviceKit 磁盘 D-Bus 接口。请注意,这些是可选组件,可能不存在。 HAL 较旧,DeviceKit-disks 是较新的实现,DK-d 取代了 HAL。

No, there isn't. Under Linux you may use HAL or DeviceKit-disks D-Bus interfaces. Note those are optional components and may be absent. HAL is older and DeviceKit-disks is newer implementations, with DK-d replacing HAL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文