该模块提供文件数据安全等级的相关功能:向应用程序提供查询、设置文件数据安全等级的JS接口。

说明:

本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import { securityLabel } from '@kit.CoreFileKit';
ts

使用说明

使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考:

import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';

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

使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径

DataLevel

type DataLevel = ‘s0’ | ‘s1’ | ‘s2’ | ‘s3’ | ‘s4’

数据安全级别。

系统能力:SystemCapability.FileManagement.File.FileIO

securityLabel.setSecurityLabel

setSecurityLabel(path:string, type:DataLevel):Promise

以异步方法设置数据标签。数据标签安全等级仅可由低向高或平级设置,以Promise形式返回结果。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名类型必填说明
pathstring文件路径。
type[DataLevel]文件等级属性,只支持"s0",“s1”,“s2”,“s3”,“s4”。

返回值:

类型说明
PromisePromise实例,用于异步获取结果。本调用将返回空值。
错误码ID错误信息
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

示例:

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

securityLabel.setSecurityLabel

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

以异步方法设置数据标签。数据标签安全等级仅可由低向高或平级设置,以callback形式返回结果。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名类型必填说明
pathstring文件路径。
type[DataLevel]文件等级属性,只支持"s0",“s1”,“s2”,“s3”,“s4”。
callbackAsyncCallback设置数据标签之后的回调。
错误码ID错误信息
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

示例:

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

securityLabel.setSecurityLabelSync

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

以同步方法设置数据标签。数据标签安全等级仅可由低向高或平级设置。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名类型必填说明
pathstring文件路径。
type[DataLevel]文件等级属性,只支持"s0",“s1”,“s2”,“s3”,“s4”。
错误码ID错误信息
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

示例:

let filePath = pathDir + '/test.txt';
securityLabel.setSecurityLabelSync(filePath, "s0");
ts

securityLabel.getSecurityLabel

getSecurityLabel(path:string):Promise

异步方法获取数据标签。若未设置过数据标签安全等级则默认返回“s3”,以Promise形式返回结果。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名类型必填说明
pathstring文件路径。

返回值:

类型说明
Promise返回数据标签。
错误码ID错误信息
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

示例:

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

securityLabel.getSecurityLabel

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

异步方法获取数据标签。若未设置过数据标签安全等级则默认返回“s3”,以callback形式返回结果。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名类型必填说明
pathstring文件路径。
callbackAsyncCallback异步获取数据标签之后的回调。
错误码ID错误信息
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

示例:

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

securityLabel.getSecurityLabelSync

getSecurityLabelSync(path:string):string

以同步方法获取数据标签。若未设置过数据标签安全等级则默认返回“s3”。

系统能力:SystemCapability.FileManagement.File.FileIO

参数:

参数名类型必填说明
pathstring文件路径。

返回值:

类型说明
string返回数据标签。
错误码ID错误信息
13900001Operation not permitted
13900007Arg list too long
13900015File exists
13900020Invalid argument
13900025No space left on device
13900037No data available
13900041Quota exceeded
13900042Unknown error

示例:

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

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐