manage-external-storage.md 5.2 KB
Newer Older
G
Gloria 已提交
1
# Managing External Storage Devices (for System Applications Only)
A
Annie_wang 已提交
2

A
Annie_wang 已提交
3
Because external storage devices are pluggable, OpenHarmony provides functions for listening for the device insertion/removal events and mounting/unmounting an external storage device.
A
Annie_wang 已提交
4

A
Annie_wang 已提交
5
External storage devices are managed by the StorageManager and StorageDaemon services. StorageDaemon implements underlying listening and mount/unmount functions. StorageManager provides status change notifications and query and management of external storage devices for system applications.
A
Annie_wang 已提交
6 7 8 9 10 11 12

**Figure 1** External storage device management 

![External storage device management](figures/external-storage-device-management.png)

- When an external storage device is inserted, the StorageDaemon process obtains an insertion event over netlink and creates a disk device and volume. The created volume is in the **UNMOUNTED** state.

A
Annie_wang 已提交
13
- Then, the StorageDaemon process checks the volume. The volume transits to the **CHECKING** state.
A
Annie_wang 已提交
14 15 16 17
  - The StorageDaemon process mounts the volume if the check is successful. If the mount operation is successful, the volume state changes to **MOUNTED** and StorageManager is instructed to send the COMMON_EVENT_VOLUME_MOUNTED broadcast.
  - If the check fails, the volume state changes to **UNMOUNTED**.

- For a volume in the **MOUNTED** state:
A
Annie_wang 已提交
18
  - If the device is directly removed, the volume information is deleted and COMMON_EVENT_VOLUME_BAD_REMOVAL is broadcast.
A
Annie_wang 已提交
19 20 21 22 23 24 25 26 27 28 29 30
  - If the user chooses **Eject device**, the volume state changes to **EJECTING** and the COMMON_EVENT_VOLUME_EJECT is broadcast. After StorageDaemon unmounts the volume, the volume state changes to **UNMOUNTED** and COMMON_EVENT_VOLUME_UNMOUNTED is broadcast.

- For a volume in the **UNMOUNTED** state, removing the device will delete the volume information and broadcast COMMON_EVENT_VOLUME_REMOVED.

## Available APIs

For details about APIs, see [Volume Management](../reference/apis/js-apis-file-volumemanager.md).

The following table describes the broadcast related parameters.

**Table 1** Broadcast parameters

A
Annie_wang 已提交
31
| Broadcast| Parameter| 
A
Annie_wang 已提交
32
| -------- | -------- |
A
Annie_wang 已提交
33 34 35 36 37
| usual.event.data.VOLUME_REMOVED | **id**: ID of the volume.<br>**diskId**: ID of the disk to which the volume belongs.| 
| usual.event.data.VOLUME_UNMOUNTED | **id**: ID of the volume.<br>**diskId**: ID of the disk to which the volume belongs.<br>**volumeState**: state of the volume.| 
| usual.event.data.VOLUME_MOUNTED | **id**: ID of the volume.<br>**diskId**: ID of the disk to which the volume belongs.<br>**volumeState**: state of the volume.<br>**fsUuid**: universally unique identifier (UUID) of the volume.<br>**path**: path where the volume is mounted.| 
| usual.event.data.VOLUME_BAD_REMOVAL | **id**: ID of the volume.<br>**diskId**: ID of the disk to which the volume belongs.| 
| usual.event.data.VOLUME_EJECT | **id**: ID of the volume.<br>**diskId**: ID of the disk to which the volume belongs.<br>**volumeState**: state of the volume.| 
A
Annie_wang 已提交
38 39 40

## How to Develop

A
Annie_wang 已提交
41
You can subscribe to broadcast events to observe the insertion and removal of external storage devices, and query or manage volumes based on the volume information obtained from the broadcast.
A
Annie_wang 已提交
42

A
Annie_wang 已提交
43
1. Apply for permissions.<br>
A
Annie_wang 已提交
44 45
   Apply for the **ohos.permission.STORAGE_MANAGER** permission for subscribing to volume broadcast events. For details, see [Declaring Permissions in the Configuration File](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file).

A
Annie_wang 已提交
46 47
2. Subscribe to broadcast events.<br>
   You can subscribe to the following events:
A
Annie_wang 已提交
48 49 50 51 52 53 54 55 56 57

   - "usual.event.data.VOLUME_REMOVED": The device is removed.
   - "usual.event.data.VOLUME_UNMOUNTED": The volume is unmounted.
   - "usual.event.data.VOLUME_MOUNTED": The volume is mounted.
   - "usual.event.data.VOLUME_BAD_REMOVAL": The device is forcibly removed.
   - "usual.event.data.VOLUME_EJECT": The device is being ejected.

   ```ts
   import CommonEvent from '@ohos.commonEventManager';
   import volumeManager from '@ohos.file.volumeManager';
A
Annie_wang 已提交
58 59 60 61 62
   import { BusinessError } from '@ohos.base';

   let subscriber: CommonEvent.CommonEventSubscriber;
   async function example() {
     const subscribeInfo: CommonEvent.CommonEventSubscribeInfo = {
A
Annie_wang 已提交
63
       events: [
A
Annie_wang 已提交
64 65 66 67 68 69 70 71 72
         "usual.event.data.VOLUME_REMOVED",
         "usual.event.data.VOLUME_UNMOUNTED",
         "usual.event.data.VOLUME_MOUNTED",
         "usual.event.data.VOLUME_BAD_REMOVAL",
         "usual.event.data.VOLUME_EJECT"
       ]
     };
     subscriber = await CommonEvent.createSubscriber(subscribeInfo);
   }
A
Annie_wang 已提交
73 74
   ```

A
Annie_wang 已提交
75
3. Obtain volume information from the broadcast.
A
Annie_wang 已提交
76 77

   ```ts
A
Annie_wang 已提交
78
   CommonEvent.subscribe(subscriber, (err: BusinessError, data: CommonEvent.CommonEventData) => {
A
Annie_wang 已提交
79 80
     if (data.event === 'usual.event.data.VOLUME_MOUNTED') {
       // Manage the volume device based on the information obtained from the broadcast.
A
Annie_wang 已提交
81 82
       let volId: string = data.parameters.id;
       volumeManager.getVolumeById(volId, (error: BusinessError, vol: volumeManager.Volume) => {
A
Annie_wang 已提交
83
         if (error) {
A
Annie_wang 已提交
84
           console.error('volumeManager getVolumeById failed for ' + JSON.stringify(error));
A
Annie_wang 已提交
85 86 87 88 89 90 91
         } else {
           console.info('volumeManager getVolumeById successfully, the volume state is ' + vol.state);
         }
       })
     }
   })
   ```