1# @ohos.file.securityLabel (数据标签) 2 3该模块提供文件数据安全等级的相关功能:向应用程序提供查询、设置文件数据安全等级的JS接口。 4 5> **说明:** 6> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 7> 本模块支持对错误码进行处理,错误码及其适配方式[参考文档](../errorcodes/errorcode-filemanagement.md#错误码适配指导)。 8 9## 导入模块 10 11```js 12import securityLabel from '@ohos.file.securityLabel'; 13``` 14 15## 使用说明 16 17使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考: 18 19**Stage模型** 20 21 ```js 22import UIAbility from '@ohos.app.ability.UIAbility'; 23 24export default class EntryAbility extends UIAbility { 25 onWindowStageCreate(windowStage) { 26 let context = this.context; 27 let pathDir = context.filesDir; 28 } 29} 30 ``` 31 32**FA模型** 33 34 ```js 35 import featureAbility from '@ohos.ability.featureAbility'; 36 37 let context = featureAbility.getContext(); 38 context.getFilesDir().then((data) => { 39 let pathDir = data; 40 }) 41 ``` 42 43FA模型context的具体获取方法参见[FA模型](js-apis-inner-app-context.md#Context模块)。 44 45## securityLabel.setSecurityLabel 46 47setSecurityLabel(path:string, type:DataLevel):Promise<void> 48 49以异步方法设置数据标签,以promise形式返回结果。 50 51**系统能力**:SystemCapability.FileManagement.File.FileIO 52 53**参数:** 54 55| 参数名 | 类型 | 必填 | 说明 | 56| --------- | ------ | ---- | -------------------------------------------- | 57| path | string | 是 | 文件路径 | 58| type | DataLevel | 是 | 文件等级属性,只支持"s0","s1","s2","s3","s4" | 59 60**返回值:** 61 62 | 类型 | 说明 | 63 | ------------------- | ---------------- | 64 | Promise<void> | Promise实例,用于异步获取结果。本调用将返回空值。| 65 66**示例:** 67 68 ```js 69 let filePath = pathDir + '/test.txt'; 70 securityLabel.setSecurityLabel(filePath, "s0").then(() => { 71 console.info("setSecurityLabel successfully"); 72 }).catch((err) => { 73 console.info("setSecurityLabel failed with error message: " + err.message + ", error code: " + err.code); 74 }); 75 ``` 76 77## securityLabel.setSecurityLabel 78 79setSecurityLabel(path:string, type:DataLevel, callback: AsyncCallback<void>):void 80 81以异步方法设置数据标签,以callback形式返回结果。 82 83**系统能力**:SystemCapability.FileManagement.File.FileIO 84 85**参数:** 86 87| 参数名 | 类型 | 必填 | 说明 | 88| --------- | ------------------------- | ---- | -------------------------------------------- | 89| path | string | 是 | 文件路径 | 90| type | DataLevel | 是 | 文件等级属性,只支持"s0","s1","s2","s3","s4" | 91| callback | AsyncCallback<void> | 是 | 是否设置数据标签之后的回调 | 92 93**示例:** 94 95 ```js 96 let filePath = pathDir + '/test.txt'; 97 securityLabel.setSecurityLabel(filePath, "s0", (err) => { 98 if (err) { 99 console.info("setSecurityLabel failed with error message: " + err.message + ", error code: " + err.code); 100 } else { 101 console.info("setSecurityLabel successfully."); 102 } 103 }); 104 ``` 105 106## securityLabel.setSecurityLabelSync 107 108setSecurityLabelSync(path:string, type:DataLevel):void 109 110以同步方法设置数据标签。 111 112**系统能力**:SystemCapability.FileManagement.File.FileIO 113 114**参数:** 115 116| 参数名 | 类型 | 必填 | 说明 | 117| --------- | ------ | ---- | -------------------------------------------- | 118| path | string | 是 | 文件路径 | 119| type | DataLevel | 是 | 文件等级属性,只支持"s0","s1","s2","s3","s4" | 120 121**示例:** 122 123```js 124let filePath = pathDir + '/test.txt'; 125securityLabel.setSecurityLabelSync(filePath, "s0"); 126``` 127 128## securityLabel.getSecurityLabel 129 130getSecurityLabel(path:string):Promise<string> 131 132异步方法获取数据标签,以promise形式返回结果。 133 134**系统能力**:SystemCapability.FileManagement.File.FileIO 135 136**参数:** 137 138 | 参数名 | 类型 | 必填 | 说明 | 139 | ------ | ------ | ---- | -------- | 140 | path | string | 是 | 文件路径 | 141 142**返回值:** 143 144 | 类型 | 说明 | 145 | --------------------- | ------------ | 146 | Promise<string> | 返回数据标签 | 147 148**示例:** 149 150 ```js 151 let filePath = pathDir + '/test.txt'; 152 securityLabel.getSecurityLabel(filePath).then((type) => { 153 console.log("getSecurityLabel successfully, Label: " + type); 154 }).catch((err) => { 155 console.log("getSecurityLabel failed with error message: " + err.message + ", error code: " + err.code); 156 }); 157 ``` 158 159## securityLabel.getSecurityLabel 160 161getSecurityLabel(path:string, callback:AsyncCallback<string>): void 162 163异步方法获取数据标签,以callback形式返回结果。 164 165**系统能力**:SystemCapability.FileManagement.File.FileIO 166 167**参数:** 168 169 | 参数名 | 类型 | 必填 | 说明 | 170 | -------- | --------------------------- | ---- | -------------------------- | 171 | path | string | 是 | 文件路径 | 172 | callback | AsyncCallback<string> | 是 | 异步获取数据标签之后的回调 | 173 174**示例:** 175 176 ```js 177 let filePath = pathDir + '/test.txt'; 178 securityLabel.getSecurityLabel(filePath, (err, type) => { 179 if (err) { 180 console.log("getSecurityLabel failed with error message: " + err.message + ", error code: " + err.code); 181 } else { 182 console.log("getSecurityLabel successfully, Label: " + type); 183 } 184 }); 185 ``` 186## securityLabel.getSecurityLabelSync 187 188getSecurityLabelSync(path:string):string 189 190以同步方法获取数据标签。 191 192**系统能力**:SystemCapability.FileManagement.File.FileIO 193 194**参数:** 195 196| 参数名 | 类型 | 必填 | 说明 | 197| ------ | ------ | ---- | -------- | 198| path | string | 是 | 文件路径 | 199 200**返回值:** 201 202| 类型 | 说明 | 203| ------ | ------------ | 204| string | 返回数据标签 | 205 206**示例:** 207 208```js 209let filePath = pathDir + '/test.txt'; 210let type = securityLabel.getSecurityLabelSync(filePath); 211console.log("getSecurityLabel successfully, Label: " + type); 212``` 213