harmony 鸿蒙@ohos.file.securityLabel (Data Label)

2023-02-17 浏览 (756)

@ohos.file.securityLabel (Data Label)

The securityLabel module provides APIs for managing data security levels of files, including obtaining and setting file 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.file.securityLabel';

Guidelines

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

Stage Model

import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';

export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: window.WindowStage) {
    let context = this.context;
    let pathDir = context.filesDir;
  }
}

FA Model

import featureAbility from '@ohos.ability.featureAbility';

let context = featureAbility.getContext();
context.getFilesDir().then((data) => {
  let pathDir = data;
})

For details about how to obtain the FA model context, see Context.

securityLabel.setSecurityLabel

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

Sets a 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
pathstringYesPath of the target file.
typeDataLevelYesFile security level to set, which can be s0, s1, s2, s3, or s4.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Basic File IO Error Codes.

IDError Message
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

Example

import { BusinessError } from '@ohos.base';
let filePath = pathDir + '/test.txt';
securityLabel.setSecurityLabel(filePath, "s0").then(() => {
  console.info("setSecurityLabel successfully");
}).catch((err: BusinessError) => {
  console.info("setSecurityLabel failed with error message: " + err.message + ", error code: " + err.code);
});

securityLabel.setSecurityLabel

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

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

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

NameTypeMandatoryDescription
pathstringYesPath of the target file.
typeDataLevelYesFile security level to set, which can be s0, s1, s2, s3, or s4.
callbackAsyncCallback<void>YesCallback invoked to return the result.

Error codes

For details about the error codes, see Basic File IO Error Codes.

IDError Message
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

Example

import { BusinessError } from '@ohos.base';
let filePath = pathDir + '/test.txt';
securityLabel.setSecurityLabel(filePath, "s0", (err: BusinessError) => {
  if (err) {
    console.info("setSecurityLabel failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("setSecurityLabel successfully.");
  }
});

securityLabel.setSecurityLabelSync

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

Sets a security label for a file in synchronous mode.

System capability: SystemCapability.FileManagement.File.FileIO

Parameters

NameTypeMandatoryDescription
pathstringYesPath of the target file.
typeDataLevelYesFile security level to set, which can be s0, s1, s2, s3, or s4.

Error codes

For details about the error codes, see Basic File IO Error Codes.

IDError Message
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

Example

let filePath = pathDir + '/test.txt';
securityLabel.setSecurityLabelSync(filePath, "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
pathstringYesPath of the target file.

Return value

TypeDescription
Promise<string>Security label obtained.

Error codes

For details about the error codes, see Basic File IO Error Codes.

IDError Message
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

Example

import { BusinessError } from '@ohos.base';
let filePath = pathDir + '/test.txt';
securityLabel.getSecurityLabel(filePath).then((type: string) => {
  console.log("getSecurityLabel successfully, Label: " + type);
}).catch((err: BusinessError) => {
  console.log("getSecurityLabel failed with error message: " + err.message + ", error code: " + err.code);
});

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
pathstringYesPath of the target file.
callbackAsyncCallback<string>YesCallback invoked to return the security label obtained.

Error codes

For details about the error codes, see Basic File IO Error Codes.

IDError Message
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

Example

import { BusinessError } from '@ohos.base';
let filePath = pathDir + '/test.txt';
securityLabel.getSecurityLabel(filePath, (err: BusinessError, type: string) => {
  if (err) {
    console.log("getSecurityLabel failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.log("getSecurityLabel successfully, Label: " + 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
pathstringYesPath of the target file.

Return value

TypeDescription
stringSecurity label obtained.

Error codes

For details about the error codes, see Basic File IO Error Codes.

IDError Message
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

Example

let filePath = pathDir + '/test.txt';
let type = securityLabel.getSecurityLabelSync(filePath);
console.log("getSecurityLabel successfully, Label: " + type);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

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

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

harmony 鸿蒙@ohos.bundle (Bundle)

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

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