harmony 鸿蒙Application Sandbox Directory

  • 2023-06-24
  • 浏览 (740)

Application Sandbox Directory

The 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.

  • The system has a dedicated application sandbox directory in the internal storage to map the directory of each application. The application sandbox directory contains only the application directories and the minimum system files required for running of the application.

  • The application sandbox defines the minimum data range visible to each application. In the application sandbox directory, an 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.

  • 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, the application need to call specific APIs and have authorization from the user.

The following figure illustrates the file access mechanism in an application sandbox.

Figure 1 File access mechanism in an application sandbox

Application sandbox file access relationship

Application Sandbox Directory and Application Sandbox Path

With the application sandbox mechanism, an application is not aware of the existence and location of other applications’ directories and user file directories. Even the application directories visible to an application are isolated by permission and namespace to form an independent directory view and shield the real (physical) paths.

  • As shown in the following figure, the 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.

  • You can view the real application paths (the directory view visible to a system process) in the HDC shell environment. For details about the mappings between the application sandbox paths and real application paths, see Mappings Between Application Sandbox Paths and Physical Paths.

  • The application sandbox paths and physical paths are not in one-to-one mappings. The application sandbox paths are always less than the physical paths. You may not obtain the application sandbox path based on a physical path in certain cases, but you can obtain the physical path based on an application sandbox path.

Figure 2 Different directory views to processes and applications

Application sandbox path

Application File Directory and Application File Path

The application sandbox directory includes application file directories and system file directories.

The system file directories visible to an application are preset by OpenHarmony.

The following figure shows the application file directories. The path of a file or a folder in the application file directory is called the application file path. The sub-directories in the application file directory have different attributes.

Figure 3 Application file directory structure

Application file directory structure

  1. Level 1 directory data/: indicates an application file directory.

  2. Level 2 directory storage/: indicates a directory for persistent files of the application.

  3. Level 3 directories el1/ and el2/: indicate directories for files of different encryption levels (els).

    • el1: directory for the data that can be accessed once the device starts. This directory contains device-focused files.
    • 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.
      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) can be placed in the el1 directory. For details about the operations on el directories, see Obtaining and Modifying el Directories.
  4. Level 4 and level 5 directories: The application’s 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.

You can use UIAbilityContext, AbilityStageContext, and ExtensionContext to obtain application file paths related to an OpenHarmony Ability Package (HAP). When a HAP is uninstalled, the files stored in these directories are automatically deleted, without affecting the files in app-level directories. An application in the development state has one or more HAPs. For details, see Application Package Structure in Stage Mode.

For details about how to obtain the context and application file paths, see Context (Stage Model).

NOTE

  • 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.
  • Use Context to obtain the application file paths, including but are not limited to the directories highlighted in green in Figure 3.

The following table describes the application file paths and lifecycle.

Table 1 Application file paths

|Folder Name|Context Attribute Name|Type|Description| |——–|——–|——–|——–| |bundle|bundleCodeDir|Installation file directory|Directory for saving the HAPs after an application is installed.
This directory is cleared when the application is uninstalled.
Do not access resource files using concatenated paths. Use @ohos.resourceManager instead.
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.| |base|NA|Directory for device’s files|Directory for saving the application’s persistent data on the device. Subdirectories include files/, cache/, temp/, and haps/.
This directory is cleared when the application is uninstalled.| |database|databaseDir|Database directory|Directory in el1 for saving the files operated by the distributed database service.
This directory is cleared when the application is uninstalled.
This directory can be used to store the application’s private database data, such as database files, in distributed scenarios only.| |distributedfiles|distributedFilesDir|Distributed file directory|Directory in el2 for saving the application files that can be directly accessed across devices.
This directory is cleared when the application is uninstalled.
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 the application to run smoothly on multiple devices. The data stored in this directory enables an application to run smoothly on multiple devices that forms a Super Device.| |files|filesDir|Application file directory|Directory for saving the application’s persistent files on the device.
This directory is cleared when the application is uninstalled.
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. The data is stored in this directory to ensure privacy, security, and permanent validity.| |cache|cacheDir|Application cache file directory|Directory for caching the downloaded files of the application or saving the cache files regenerated on the device.
This directory is automatically cleared when the size of the cache directory reaches the quota or the system storage space reaches a certain threshold. The user can also clear this directory by using a system space management application.
The application needs to check whether the file still exists and determine whether to cache the file again.
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.| |preferences|preferencesDir|Preferences file directory|Directory for saving common application configuration and user preference data managed by using database APIs.
This directory is cleared when the application is uninstalled. For details about how to make preferences data persistent, see Persisting Preferences Data.
You can place application preferences data, including preference files and configuration files, in this directory. This directory applied to storing only a small amount of data.| |temp|tempDir|Temporary file directory|Directory for saving the files generated and required during the application’s runtime on the device.
This directory is cleared when the application exits.
You can place temporarily generated data of an application, including cached database data and images, temporary log files, downloaded application installation package, in this directory. The data stored in this directory can be deleted immediately after being used.|

你可能感兴趣的鸿蒙文章

harmony 鸿蒙File Management

harmony 鸿蒙Accessing Application Files

harmony 鸿蒙Backup and Restoration Accessed by Applications

harmony 鸿蒙Application Data Backup and Restoration Overview

harmony 鸿蒙Backup and Restoration Triggered by System Applications

harmony 鸿蒙Application File Overview

harmony 鸿蒙Uploading and Downloading an Application File

harmony 鸿蒙Obtaining Application and File System Space Statistics

harmony 鸿蒙Developing a File Manager Application (for System Applications Only)

harmony 鸿蒙Distributed File System Overview

0  赞