1e41f4b71Sopenharmony_ci# Application Sandbox 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe application sandbox is an isolation mechanism used to prevent malicious data access through path traversal. With this mechanism, only the application sandbox directory is visible to an application. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci- The system provides a dedicated application sandbox directory in the internal storage to map the directory of each application. The application sandbox directory contains only the [application file directory](#application-file-directory-and-application-file-path) and the directory containing the minimum system files required for the running of the application. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci- The application sandbox limits the scope of data visible to an application. Each application can access only its own application files and the system files required for its running. The application cannot access files of other applications. This ensures application file security. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci- In each application sandbox directory, the application can save and process its application files in the application file directory, and can only read the system files and directories. To access [user files](user-file-overview.md), the application needs to call specific APIs and have authorization from the user. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciThe following figure illustrates the file access mechanism in the application sandbox. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci**Figure 1** File access mechanism in the application sandbox 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## Application Sandbox Directory and Application Sandbox Path 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciWith the application sandbox mechanism, an application is not aware of the existence and location of other applications' directories and user file directories. Even the directories visible to an application are isolated by permission and namespace to form an independent directory view and shield the physical (real) paths. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci- As shown in the following figure, the application sandbox mechanism minimizes the number of directories and files visible to a common application (third-party application). In addition, the directories and file paths visible to a common application are different from those visible to a system process. The path of a file or folder in the application sandbox directory visible to a common application is called the application sandbox path. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci<!--RP1--> 24e41f4b71Sopenharmony_ci- Generally, the hdc shell environment is equivalent to the system process view. Therefore, the application sandbox path is different from the physical path displayed on the hdc for debugging. For details about the mappings, see [Mapping Between Application Sandbox Paths and Physical Paths](#mapping-between-application-sandbox-paths-and-physical-paths). 25e41f4b71Sopenharmony_ci<!--RP1End--> 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci- The application sandbox paths and physical paths are not in one-to-one mappings. The application sandbox paths are always shorter than physical paths. Some physical paths do not have the corresponding application sandbox paths. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**Figure 2** Different directory views to processes and applications 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci## Application File Directory and Application File Path 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ciThe application sandbox directory consists of the application file directory and system file directory. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ciThe system file directory visible to an application is preset by OpenHarmony. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciThe following figure shows the application file directory structure. The path of a file or a folder in the application file directory is called the application file path. The application file paths have different attributes. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**Figure 3** Application file directory structure 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci> **NOTE** 46e41f4b71Sopenharmony_ci> 47e41f4b71Sopenharmony_ci> - Do not use file paths made up by level 1 to level 3 directory names. Incompatibility problems may occur if the directory names are changed in later versions. 48e41f4b71Sopenharmony_ci> - Use **Context** to obtain the application file paths, including but not limited to the directories highlighted in green in **Figure 3**. For details about how to obtain the context and application file paths, see [Context (Stage Model)](../application-models/application-context-stage.md). 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci1. Level 1 directory **data/**: application file directory. 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci2. Level 2 directory **storage/**: directory for persistent files of the application. 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci3. Level 3 directories **el1/** and **el2/**: directories for files of different encryption levels. 55e41f4b71Sopenharmony_ci - **el1**: directory for the data that can be accessed once the device starts. This directory contains device-focused files. 56e41f4b71Sopenharmony_ci - **el2**: directory for the data that can be accessed only after at least one successful unlock operation (by PIN, fingerprint, or facial authentication, or password-free sign-in) upon the start of the device. This directory contains user-focused files.<br> 57e41f4b71Sopenharmony_ci Unless otherwise required, application data is placed in the **el2** directory for security purposes. The data that needs to be accessed before the screen is unlocked (such as the clock, alarm, and wallpaper data) is placed in the **el1** directory. For details about the operations on **el1/** and **el2/**, see [Obtaining and Modifying el Directories](../application-models/application-context-stage.md#obtaining-and-modifying-encryption-levels). 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci4. Level 4 and level 5 directories: directories for the application global information and OpenHarmony Ability Packages (HAPs). An application in the development state has one or more HAPs. For details, see [Application Package Structure in Stage Model](../quick-start/application-package-structure-stage.md). 60e41f4b71Sopenharmony_ci The application global data is stored in the **distributedfiles** directory and **files**, **cache**, **preferences**, and **temp** in **base**. You can use **ApplicationContext** to obtain the application file paths of these directories. 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci You can use **UIAbilityContext**, **AbilityStageContext**, and **ExtensionContext** to obtain application file paths related to a HAP. When a HAP is uninstalled, the files in the **haps/** directory are automatically deleted, without affecting the files in application-level directories. 63e41f4b71Sopenharmony_ci For details about how to obtain the context and application file paths, see [Context (Stage Model)](../application-models/application-context-stage.md). 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci The following table describes the application file paths and their lifecycle. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci **Table 1** Application file paths 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci | Folder Name| Context Attribute Name| Type| Description| 70e41f4b71Sopenharmony_ci | -------- | -------- | -------- | -------- | 71e41f4b71Sopenharmony_ci | bundle | bundleCodeDir | Installation file directory| Directory for saving the HAPs after an application is installed.<br>This directory is cleared when the application is uninstalled.<br>Do not access resource files using concatenated paths. Use [@ohos.resourceManager](../reference/apis-localization-kit/js-apis-resource-manager.md) instead.<br>You can store the application's code resource data, including the HAPs of the application, reusable library files, and plug-ins, in this directory. The code in this directory can be dynamically loaded.| 72e41f4b71Sopenharmony_ci | base | NA | Directory for the device's files| Directory for saving the application's persistent data on the device. Subdirectories include **files/**, **cache/**, **temp/**, and **haps/**.<br>This directory is cleared when the application is uninstalled.| 73e41f4b71Sopenharmony_ci | database | databaseDir | Database directory| Directory in **el2** for saving the files operated by the distributed database service.<br>This directory is cleared when the application is uninstalled.<br>This directory can be used to store the application's private database data, such as database files, in distributed scenarios only.| 74e41f4b71Sopenharmony_ci | distributedfiles | distributedFilesDir | Distributed file directory| Directory in **el2** for saving the application files that can be directly accessed across devices.<br>This directory is cleared when the application is uninstalled.<br>You can place the application's data used for distributed scenarios, including file sharing, file backup, and file processing across devices, in this directory. The data stored in this directory enables an application to run smoothly on multiple devices that form a Super Device.| 75e41f4b71Sopenharmony_ci | files | filesDir | Application file directory| Directory for saving the application's persistent files on the device.<br>This directory is cleared when the application is uninstalled.<br>You can place the application's private data, including persistent files, images, media files, and log files, in this directory. The data is stored in this directory to ensure privacy, security, and permanent validity.| 76e41f4b71Sopenharmony_ci | cache | cacheDir | Application cache file directory| Directory for caching the downloaded files of the application or saving the cache files regenerated on the device.<br>This directory is automatically cleared when the size of the **cache** directory reaches the quota or the system storage space reaches a certain threshold. End users can also clear this directory by using a system space management application. <br>The application needs to check whether a file still exists and determine whether to cache a file again.<br>You can place the cached data of the application, including offline data, cached images, database backup, and temporary files, in this directory. Data stored in this directory may be automatically deleted by the system. Therefore, do not store important data in this directory.| 77e41f4b71Sopenharmony_ci | preferences | preferencesDir | Preferences file directory| Directory for saving common application configuration and user preferences data managed by using database APIs.<br>This directory is cleared when the application is uninstalled. For details about how to make preferences data persistent, see [Persisting Preferences Data](../database/data-persistence-by-preferences.md).<br>You can place application preferences data, including preference files and configuration files, in this directory. This directory applies to storing only a small amount of data.| 78e41f4b71Sopenharmony_ci | temp | tempDir | Temporary file directory| Directory for saving the files generated and required during the application's runtime on the device. <br>This directory is cleared when the application exits.<br>You can place temporarily generated data of the application, including cached database data and images, temporary log files, downloaded application installation packages, in this directory. The data stored in this directory can be deleted immediately after being used.| 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci## Mapping Between Application Sandbox Paths and Physical Paths 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ciThe read and write operations performed on an application sandbox directory are eventually performed on the files in the physical directory after address conversion. The following table lists their mappings. 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ciIn the physical paths, <USERID> has a fixed value of **100**, and <EXTENSIONPATH> is moduleName-extensionName. For details about the application running in an independent Extension sandbox, see [ExtensionAbility Component](../application-models/extensionability-overview.md). 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci| Application Sandbox Path| Physical Path| 87e41f4b71Sopenharmony_ci| -------- | -------- | 88e41f4b71Sopenharmony_ci| /data/storage/el1/bundle | Application installation package directory:<br> **/data/app/el1/bundle/public/<PACKAGENAME>**| 89e41f4b71Sopenharmony_ci| /data/storage/el1/base | Application directory of encryption level 1.<br> - Application running in a non-independent sandbox: **/data/app/el1/<USERID>/base/<PACKAGENAME>**<br> - Extension application running in an independent sandbox: **/data/app/el1/<USERID>/base/+extension-<EXTENSIONPATH>+<PACKAGENAME>**| 90e41f4b71Sopenharmony_ci| /data/storage/el2/base | Application directory of encryption level 2.<br> - Application running in a non-independent sandbox: **/data/app/el2/<USERID>/base/<PACKAGENAME>**<br> - Extension application running in an independent sandbox: **/data/app/el2/<USERID>/base/+extension-<EXTENSIONPATH>+<PACKAGENAME>**| 91e41f4b71Sopenharmony_ci| /data/storage/el1/database | Database directory of the application under **el1/**.<br> - Application running in a non-independent sandbox: **/data/app/el1/<USERID>/database/<PACKAGENAME>**<br> - Extension application running in an independent sandbox: **/data/app/el1/<USERID>/database/+extension-<EXTENSIONPATH>+<PACKAGENAME>**| 92e41f4b71Sopenharmony_ci| /data/storage/el2/database | Database directory of the application under **el2/**.<br> - Application running in a non-independent sandbox: **/data/app/el2/<USERID>/database/<PACKAGENAME>**<br> - Extension application running in an independent sandbox: **/data/app/el2/<USERID>/database/+extension-<EXTENSIONPATH>+<PACKAGENAME>**| 93e41f4b71Sopenharmony_ci| /data/storage/el2/distributedfiles | **/mnt/hmdfs/<USERID>/account/merge_view/data/<PACKAGENAME>** | 94