Class StorageDevice

java.lang.Object
com.tccc.kos.core.service.udev.storage.StorageDevice

public class StorageDevice extends Object
Wraps a UdevDevice and provides easy access to storage specific data. A storage device represents a partition on a storage device that can be mounted. Devices aren't automatically mounted when detected. This allows devices to be inserted and removed without impact and only be mounted for the duration of a particular operation. This minimizes the chances of a user pulling a removable device while the device is mounted and in operation.

Devices can be mounted and unmounted using the mount() and unmount() methods. Mounts are use counted so mounting an already mounted device will simply bump the use count. Similarly, the device is only unmounted when the use count reaches zero. This allows multiple systems to use the mount at the same time without worrying that it will be unmounted during use. This also means that every mount call must have a matching unmount call.

The actual mount location for this device when mounted can is available via getMountDir(). This directory will only exists while the device is mounted and will be removed when the device is unmounted. This path is relative to the node the device is attached to so will not be accessible via the local filesystem if the device is on a different node.

If the device is removed or the node the device is attached to disconnects, the device object will be orphaned and garbage collected once no longer referenced. Any mount / unmount calls will appear to work until the use count reaches zero after which mounting will no longer work.

Since:
1.0
Version:
2024-04-30
  • Constructor Details

    • StorageDevice

      public StorageDevice()
  • Method Details

    • getId

      public String getId()
      Return the id of the device.
    • getSyspath

      public String getSyspath()
      Return the syspath of the device. This uniquely defines the device and can be used as a device key.
    • getRunTimeMs

      public long getRunTimeMs()
      Return how long the device has been running in ms.
    • getVendor

      public String getVendor()
      Return vendor name of the device.
    • getModel

      public String getModel()
      Return model of the device.
    • getLabel

      public String getLabel()
      Return label of the partition.
    • getMountDir

      public File getMountDir()
      Return the directory the device is mounted at. This returns null unless the storage is mounted via mount() . Once unmounted, this will return to null.
    • isRemoved

      public boolean isRemoved()
      Return true if the device is removed. This will be true if the device is removed or if the node the device is attached to disconnects. The mount and unmount calls will continue to function as expected until the use count on the mount reaches zero. This can be used to detect if operations on the mounted filesystem will fail even though it appears to be mounted.
    • getNodeId

      public NodeId getNodeId()
      Return the nodeId the device is attached to.
    • isLocal

      public boolean isLocal()
      Return true if the device is attached to the local node.
    • getStorageInfo

      public StorageInfo getStorageInfo() throws IOException
      Return information about the mounted storage. Returns null if the device isn't mounted. If mounted, this will fetch the latest information from the device.
      Throws:
      IOException
    • mount

      public StorageInfo mount() throws IOException
      Mount the storage with MountOption.USB which is a collection of options geared towards security and minimizing file corruption on device removal.

      If the device is already mounted, this simply bumps the use count and the options will be ignored.

      Throws:
      IOException
    • mount

      public StorageInfo mount(int mountOptions) throws IOException
      Mount the storage with the specified mount options.

      If the device is already mounted, this simply bumps the use count and the options will be ignored.

      Parameters:
      mountOptions - logical OR of various MountOption constants
      Throws:
      IOException
    • mount

      public StorageInfo mount(int mountOptions, String filesystemOptions) throws IOException
      Mount the storage with the specified mount and filesystem options.

      If the device is already mounted, this simply bumps the use count and the options will be ignored.

      Parameters:
      mountOptions - logical OR of various MountOption constants
      filesystemOptions - comma separated list of filesystem specific options
      Throws:
      IOException - of the mount fails
    • unmount

      public void unmount()
      Unmount the storage
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getStorageService

      public StorageService getStorageService()
    • getUdevDevice

      public UdevDevice getUdevDevice()
    • getListeners

      public ListenerList<StorageMountListener> getListeners()