• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Adaptation Guide for the Application Sandbox
2
3## Verification Process
4
5![](figures/verification-process.png)
6
71. Verify your application functions. If all the functions are normal, no adaptation is required.
82. If any function is found to be abnormal, analyze the code to identify the exception. Then adapt the file access model by following the instructions provided in [Adaptation Process](#adaptation-process).
93. After the adaptation is complete, verify the functions again. If all the functions are normal, the adaptation process is complete. If any function is found to be abnormal, repeat the operations described in Step 2.
10
11## Adaptation Process
12
13![](figures/adaptation-process.png)
14
151. Check whether the identified exception is caused by an incorrect access path of the application source code. You can check the correctness of an access path against [Sandbox File Access Specifications](#sandbox-file-access-specifications).
162. If the application source code accesses an incorrect path, change the path access policy. Specifically, use the **Context** API rather than the absolute path to access the **/data** directory. For details about the **Context** API, see the following documents:
17   https://gitee.com/openharmony/docs/blob/master/en/application-dev/application-models/application-context-stage.md
18   https://gitee.com/openharmony/docs/blob/master/en/application-dev/reference/apis/js-apis-ability-context.md
19   https://gitee.com/OpenHarmony/docs/blob/master/en/application-dev/reference/apis/js-apis-Context.md
203. If the exception is caused by a third-party module invoked by the application and the application source code does not use an absolute path to access files, proceed as follows:
21   - Request the third-party module to access the file path through the **Context** API instead of using the absolute path.
22   - If the third-party module is a common module used by both appspawn incubated processes and native processes, abstract the third-party module as a service and access the service in IPC mode.
23
24## Sandbox File Access Specifications
25
26**Access Paths in the Application Root Directory**
27
28| Path               | Description                                      |
29| --------------------- | ------------------------------------------ |
30| bin                   | Binary file directory.                      |
31| config                | Configuration directory.                                  |
32| data                  | Application data directory.                              |
33| dev                   | Device node directory.                                  |
34| etc                   | Soft link of **/system/etc**.                       |
35| init                  | Soft link of **/system/bin/init**.                  |
36| lib                   | Soft link of **/system/lib**.                       |
37| mnt                   | Mount directory.                                  |
38| proc                  | Directory of the proc file system.                          |
39| sys                   | Directory of the sys file system.                           |
40| sys_prod              | This directory varies according to devices. Some devices do not have this directory.|
41| system/app            | System directory resources.                              |
42| system/fonts          | System fonts.                              |
43| system/lib            | System libraries.                                    |
44| system/data           | System directory resources.                              |
45| system/usr            | System directory resources.                              |
46| system/profile        | System directory resources.                              |
47| system/bin            | System programs.                                  |
48| system/etc            | System configuration.                                  |
49| vendor/lib/chipsetsdk | Chip component directory. Currently, only chipsetsdk is mounted.        |
50
51**Access Paths in the Application Data Directory**
52
53| Path                            | Description                                                        |
54| ---------------------------------- | ------------------------------------------------------------ |
55| /data                              | Application data directory.                                                |
56| /data/storage                      | Application data.                                                    |
57| /data/bundles                      | Installation directory of all applications. Only applications with the BASIC or higher ability privilege level (APL) can access this directory.|
58| /data/storage/el1                  | Data directory of applications with encryption level 1.                                     |
59| /data/storage/el2                  | Data directory of applications with encryption level 2.                                     |
60| /data/storage/el1/base             | Data directory of applications with encryption level 1.                                     |
61| /data/storage/el1/bundles          | Installation package directory of applications with encryption level 1.                               |
62| /data/storage/el1/database         | Database directory of applications with encryption level 1.                               |
63| /data/storage/el2/base             | Data directory of applications with encryption level 2.                                     |
64| /data/storage/el2/database         | Database directory of applications with encryption level 2.                               |
65| /data/storage/el2/auth_groups      | Distributed data directory of applications with encryption level 2 but without an account.                     |
66| /data/storage/el2/distributedfiles | Distributed data directory of applications with encryption level 2 and an account.                     |
67
68When the application sandbox is enabled, your application cannot access the physical paths in the application data directory in the application namespace. It must use the **Context** API for the access.
69For a specific application, its physical data directory and sandbox data directory store the same set of data, which is associated using bind mounts. Any change to one directory affects the data in the other directory. Applications can access data in namespaces only through sandbox paths. The following table lists the mapping between physical paths and sandbox paths in the application data directory.
70
71**Mapping Between Physical Paths and Sandbox Paths in the Application Data Directory**
72
73| Physical Path                                                    | Sandbox Path                          | Description                                   |
74| ------------------------------------------------------------ | ---------------------------------- | --------------------------------------- |
75| /data/app/el1/bundle/public/\<PackageName>\                  | /data/storage/el1/bundle           | Application installation package directory.                         |
76| /data/app/el1/\<USERID\>/base/\<PACKAGENAME\>                | /data/storage/el1/base             | Encrypted data directory of applications with encryption level 1.                |
77| /data/app/el2/\<USERID\>/base/\<PACKAGENAME\>                | /data/storage/el2/base             | Encrypted data directory of applications with encryption level 2.                |
78| /data/app/el1/\<USERID\>/database/\<PACKAGENAME\>            | /data/storage/el1/database         | Encrypted database directory of applications with encryption level 1.              |
79| /data/app/el2/\<USERID\>/database/\<PACKAGENAME\>            | /data/storage/el2/database         | Encrypted database directory of applications with encryption level 2.              |
80| /mnt/hmdfs/\<USERID\>/account/merge_view/data/\<PACKAGENAME\> | /data/storage/el2/distributedfiles | Distributed data directory of applications with encryption level 2 and an account.|
81| /mnt/hmdfs/\<USERID\>/non_account/merge_view/data/           | /data/storage/el2/auth_groups      | Distributed data directory of applications with encryption level 2 but without an account.|
82| /mnt/hmdfs/                                                  | /mnt/hmdfs/                        | Distributed file system directory.                     |
83
84> **NOTE**
85>
86>  \<USERID\> indicates the current user ID.
87> \<PACKAGENAME\> indicates the name of the current application package.
88
89For more information about the mapping between physical paths and sandbox paths, see the following:
90https://gitee.com/OpenHarmony/startup_appspawn/blob/master/appdata-sandbox64.json
91
92## Case: No Audio in the Contacts.hap Dialing Scenario
93
94As shown in the figure below, the access path is a hardcoded absolute path, **/data/app/el1**. The access to such paths is not allowed in the application sandbox, causing the access failure.
95
96![](figures/example1.png)
97
98The preceding code snippet uses the **fileIO** interface to open a fixed path to obtain a **fdNumber**, which will be used for subsequent file access. Since the **fdNumber** can no longer be obtained using a fixed path, you can use other methods to obtain the **fdNumber**.
99In OpenHarmony, you can use **resourceManager** to obtain a **fdNumber**.
100The new code is as follows:
101
102![](figures/example2.png)
103
104## Workaround
105
106If an application exception persists after the preceding adaptation process, you can disable the application sandbox.
107
1081. Open the source code of the OpenHarmony startup_appspawn repository, and find the application sandbox configuration file based on the build type.
109   For a 32-bit system, modify the file **https://gitee.com/OpenHarmony/startup_appspawn/blob/master/appdata-sandbox.json**.
110   For a 64-bit system, modify the file **https://gitee.com/OpenHarmony/startup_appspawn/blob/master/appdata-sandbox.json**.
111
1122. Find the camera configuration in the .json file, and copy it to create your own application configuration record. In this new record, change the bundle name to your actual application name, and retain the other settings. The value **OFF** indicates that the application sandbox is disabled.
113
114   ![](figures/example3.png)
115