harmony 鸿蒙@ohos.securityLabel (Data Label)

2022-08-09 浏览 (911)

@ohos.securityLabel (Data Label)

The secuityLabel module provides APIs to manage file data security levels, including obtaining and setting file data security levels.

NOTE The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import securityLabel from '@ohos.securityLabel';

Usage

Before using the APIs provided by this module to perform operations on a file or directory, obtain the path of the application sandbox as follows:

import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
let path = '';
context.getFilesDir().then((data) => {
    path = data;
})

securityLabel.setSecurityLabel

setSecurityLabel(path:string, type:dataLevel):Promise<void>

Sets the security label for a file in asynchronous mode. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

NameTypeMandatoryDescription
pathstringYesFile path.
typedataLevelYesFile security level, which can be s0, s1, s2, s3, or s4.

Return value

TypeDescription
Promise<void>Promise used to return the result. An empty value will be returned.

Example

securityLabel.setSecurityLabel(path, "s0").then(function(){
    console.info("setSecurityLabel successfully");
}).catch(function(error){
    console.info("setSecurityLabel failed with error:" + error);
});

securityLabel.setSecurityLabel

setSecurityLabel(path:string, type:dataLevel, callback: AsyncCallback<void>):void

Sets the security label for a file in asynchronous mode. This API uses a callback to return the result.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

NameTypeMandatoryDescription
pathstringYesFile path.
typedataLevelYesFile security level, which can be s0, s1, s2, s3, or s4.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

securityLabel.setSecurityLabel(path, "s0", function(error){
    console.info("setSecurityLabel:" + JSON.stringify(error));
});

securityLabel.setSecurityLabelSync

setSecurityLabelSync(path:string, type:dataLevel):void

Sets the security label for a file in synchronous mode.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

NameTypeMandatoryDescription
pathstringYesFile path.
typedataLevelYesFile security level, which can be s0, s1, s2, s3, or s4.

Example

securityLabel.setSecurityLabelSync(path, "s0");

securityLabel.getSecurityLabel

getSecurityLabel(path:string):Promise<string>

Obtains the security label of a file in asynchronous mode. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

NameTypeMandatoryDescription
pathstringYesFile path.

Return value

TypeDescription
Promise<string>Promise used to return the security label obtained.

Example

securityLabel.getSecurityLabel(path).then(function(type){
    console.log("getSecurityLabel successfully:" + type);
}).catch(function(err){
    console.log("getSecurityLabel failed with error:" + err);
});

securityLabel.getSecurityLabel

getSecurityLabel(path:string, callback:AsyncCallback<string>): void

Obtains the security label of a file in asynchronous mode. This API uses a callback to return the result.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

NameTypeMandatoryDescription
pathstringYesFile path.
callbackAsyncCallback<string>YesCallback used to return the security label obtained.

Example

securityLabel.getSecurityLabel(path, function(err, type){
    console.log("getSecurityLabel successfully:" + type);
});

securityLabel.getSecurityLabelSync

getSecurityLabelSync(path:string):string

Obtains the security label of a file in synchronous mode.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

NameTypeMandatoryDescription
pathstringYesFile path.

Return value

TypeDescription
stringSecurity label obtained.

Example

let result = securityLabel.getSecurityLabelSync(path);
console.log("getSecurityLabel successfully:" + result);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

harmony 鸿蒙@ohos.WorkSchedulerExtensionAbility (Work Scheduler Callbacks)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/a2c28f2578a34d8da4414bd2a9bccc0e