Author: Shi Lei

With the continuous development of society, people gradually pay attention to more efficient, comfortable, convenient and interesting life and work experience. As a distributed operating system for the next generation, OpenAtom OpenHarmony (hereinafter referred to as “OpenHarmony”) has the technical characteristics of full scene, multi-device, natural interaction, convenient and accurate, providing a leading technical basis for the rapid development of digital transformation in the industry and a new idea for the innovation and satisfaction of user experience. In order to give you an in-depth understanding of OpenHarmony’s technical features, this issue analyzes the implementation of OpenHarmony HAP package installation.

I. Introduction of HAP package

HAP package is a module package generated by code, resources, third-party libraries, and application configuration files. It is mainly divided into two types: Entry and feature.

• Entry: The main module of the app, which acts as an entry point to the OpenHarmony app and provides the basic functionality of the app.

• Feature: Dynamic feature module of the application. As an extension of application capabilities, it can be installed selectively according to user requirements and device types.

The OpenHarmony user application package can contain only a basic Entry package, or it can contain a basic Entry package and one or more feature packages.

Have you got the type of HAP bag? Next, let’s carry out an in-depth analysis on the installation and implementation of HAP package.

Ii. Introduction to HAP Package installation process

First, let’s look at the HAP package installation process in a normal scenario. As shown in figure 1:

Figure 1 HAP package installation process \

  1. The HAP package to be installed is downloaded from the Content Delivery Network (CDN) cloud server on the device (such as tablet, large-screen, vehicle, PC, watch, and mobile phone).

  2. The packet management service on the device verifies HAP packets, including file validity, signature information, and configuration information of HAP packets.

  3. Installing THE HAP package includes creating an installation directory for the HAP package, decompressing and copying the HAP package to the installation directory, and storing the application information data in the database and cache.

Above is the HAP package from download to installation of the whole process, the next HAP package installation implementation details!

Three, HAP package installation details

Verify the validity of HAP packet files

When the backend device has finished downloading the HAP package file from various sources, the launcher (desktop application) obtains the path where the HAP file is stored, and the path is then passed to the package management service.

After obtaining all the HAP package file paths to install, the package management service starts to verify the VALIDITY of the HAP package file.

The verification mainly includes the following items:

• Whether the HAP package file exists

• The validity of the HAP packet file name, which must be no longer than 256 bytes

• The validity of the file type of HAP packet requires that the file must end with.hap as the suffix

• Type of HAP packet file path

• the size of an HAP packet file. The size of an HAP file must not exceed 4GB

• Whether the disk has enough space to install HAP package files

HAP Packet signature verification

To ensure that the application publisher is from the same organization or individual and prevent application information from being maliciously tampered, you need to verify the HAP packet signature information during installation or upgrade. During installation, ensure that the signatures of HAP packets to be installed are consistent with those of HAP packets that have been installed.

During signature verification, the packet management service invokes the interface of the security subsystem to obtain the signature information of HAP packet and verify the appId character string in the signature information (the character string is processed by hash algorithm to ensure that an application has a unique appId character string). The consistency of signature information in HAP packets is determined by checking whether the appId string in the signature information in different HAP packets is consistent.

HAP Packet configuration verification

During the installation, check whether the configuration information inside the APP object in the config.json file in all HAP packages is consistent.

The following is an example of the internal configuration information of the APP object in the config.json file:

"app": { "bundleName": "com.example.13jsdemo", "vendor": "huawei", "version": { "code": 1000000, "name": "1.0.0"}, "apiVersion" : {" compatible ": 4," releaseType ":" Release ", "target" : 5}},Copy the code

From the configuration example of config.json file, it can be seen that HAP packet configuration information includes the following items:

• Package name of the application

• suppliers

The version number,

• Release mode

• Applicable device type

• Version information of compatible apis

If the configuration information in the config.json file of multiple HAP packages for the same application is consistent, proceed with the subsequent installation. If they are inconsistent, the installation is terminated.

HAP installation package

The package management service installs HAP packages by creating the installation directory of HAP packages, decompressing THE HAP package files, and storing the application information data in the database and cache.

Due to the control of system files, the package management service does not have the permission to create a directory, nor can it copy the files extracted from HAP package to the specified directory. Therefore, the resident process of the system — Installd process is used to create a file directory, and complete IO operations of files in the directory (input/output abbreviated, Refers to read and write operations.

The specific operation is shown in Figure 2:

Figure 2 Installing HAP package \

  1. The packet management service communicates with the InstalLD Process through inter-process Communication (IPC).

  2. The Installd process first creates the directory by calling the kernel’s interface mkdir and then writes the file to the directory through the file operation, which converts the file into a byte stream.

  3. The Installd process returns the result of the successful creation as an error code to the package management service. Error codes returned include:

• ERR_APPEXECFWK_INSTALLD_CREATE_DIR_FAILED: The directory fails to be created

• ERR_APPEXECFWK_INSTALLD_REMOVE_DIR_FAILED: The directory fails to be removed

• ERR_OK: the directory is successfully created or deleted

  1. The packet management service stores HAP packet information in the service cache and writes it to the database to prevent packet information loss.

Description:

Although the Installd process has the permission to create and delete directories, the Installd process has limited permission to create and delete directories in the parent directory of the application directory.

After verifying the validity of HAP package file, verifying the signature information of HAP package, verifying the configuration information of HAP package and analyzing the installation of HAP package, is it clear about the whole installation process of HAP package? Let’s explore the upgrade installation strategy of HAP package.

4. Upgrade the installation policy

Upgrade installation must consider the version compatibility between installed HAP packages and HAP packages to be installed. If a developer iterates over new features and features in a new version of THE HAP package, those changes may not be compatible with a lower version of the HAP package. If some HAP packets of an application are upgraded to a higher version and still retain some HAP packets of the lower version, the lower version will not work properly. Therefore, a proper upgrade policy is the basis to ensure the normal running of applications.

The HAP package upgrade strategy is summarized as follows:

• Installation of HAP packs with a lower version number than the installed app is not allowed.

• If an ENTRY TYPE HAP packet is installed, the installed feature HAP packet must be the same as the version number of the entry type HAP packet. If an entry type HAP package of a later version is installed, uninstall the installed lower-version feature HAP package after the upgrade, and then reinstall the higher-version feature HAP package.

• If the Entry TYPE HAP package is not installed, you can upgrade and install a higher version of the feature type HAP package, but you need to ensure that the lower version of the HAP package is uninstalled after the upgrade.

5. Installation exception handling

What if the installation still fails after the above steps are completed? The following frequently asked questions are provided for developers to query.

Problem Phenomenon 1

When some HAP packages have been installed, the installation of an HAP package fails because the directory of one HAP package fails to be created.

Solutions:

Before the installation process ends, the system uninstalls the installed HAP package, deletes the installation and data directories that have been created, and removes package information from the package management service and database, and then reinstall.

Problem Phenomenon 2

During the INSTALLATION of HAP package, the device suddenly restarts, causing the installation of HAP package to be interrupted.

Solutions:

After the device is restarted, the package management service checks the application installation status. If the status is normal, the system restores the database information to the cache of the package management service. If the status is abnormal, the system deletes the package information, residual package files, installation directory, and data directory, and then installs the software again.

Problem Phenomenon 3

The device was restarted during the HAP package upgrade installation.

Solutions:

After the device restarts, the package management service deletes the temporary installation directory that has been created.

Problem Phenomenon 4

During installation, the package management service suddenly restarted.

Solutions:

The package management service suddenly restarts and does not return any results. At this point, the death listening mechanism provided by IPC will inform the installed device that the package management service has died. After a few seconds, the package management service automatically restarts. You only need to reinstall the package.