• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.securityLabel (数据标签)
2
3该模块提供文件数据安全等级的相关功能:向应用程序提供查询、设置文件数据安全等级的JS接口。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```ts
12import securityLabel from '@ohos.file.securityLabel';
13```
14
15## 使用说明
16
17使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考:
18
19
20  ```ts
21  import UIAbility from '@ohos.app.ability.UIAbility';
22  import window from '@ohos.window';
23
24  export default class EntryAbility extends UIAbility {
25    onWindowStageCreate(windowStage: window.WindowStage) {
26      let context = this.context;
27      let pathDir = context.filesDir;
28    }
29  }
30  ```
31
32使用该功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,获取方式及其接口用法请参考:[应用上下文Context-获取应用文件路径](../../application-models/application-context-stage.md#获取应用文件路径)
33
34## securityLabel.setSecurityLabel
35
36setSecurityLabel(path:string, type:DataLevel):Promise<void>
37
38以异步方法设置数据标签,以Promise形式返回结果。
39
40**系统能力**:SystemCapability.FileManagement.File.FileIO
41
42**参数:**
43
44| 参数名    | 类型       | 必填 | 说明                                         |
45| --------- | ------    | ---- | -------------------------------------------- |
46| path      | string    | 是   | 文件路径                                     |
47| type      | DataLevel | 是   | 文件等级属性,只支持"s0","s1","s2","s3","s4" |
48
49**返回值:**
50
51  | 类型                | 说明             |
52  | ------------------- | ---------------- |
53  | Promise<void> | Promise实例,用于异步获取结果。本调用将返回空值。|
54
55**错误码:**
56
57以下错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
58
59| 错误码ID | 错误信息 |
60| -------- | -------- |
61| 13900001 | Operation not permitted |
62| 13900007 | Arg list too long |
63| 13900015 | File exists |
64| 13900020 | Invalid argument |
65| 13900025 | No space left on device |
66| 13900037 | No data available |
67| 13900041 | Quota exceeded |
68| 13900042 | Unknown error |
69
70**示例:**
71
72  ```ts
73  import { BusinessError } from '@ohos.base';
74  let filePath = pathDir + '/test.txt';
75  securityLabel.setSecurityLabel(filePath, "s0").then(() => {
76    console.info("setSecurityLabel successfully");
77  }).catch((err: BusinessError) => {
78    console.error("setSecurityLabel failed with error message: " + err.message + ", error code: " + err.code);
79  });
80  ```
81
82## securityLabel.setSecurityLabel
83
84setSecurityLabel(path:string, type:DataLevel, callback: AsyncCallback<void>):void
85
86以异步方法设置数据标签,以callback形式返回结果。
87
88**系统能力**:SystemCapability.FileManagement.File.FileIO
89
90**参数:**
91
92| 参数名    | 类型                      | 必填 | 说明                                         |
93| --------- | ------------------------- | ---- | -------------------------------------------- |
94| path      | string                    | 是   | 文件路径                                     |
95| type      | DataLevel                 | 是   | 文件等级属性,只支持"s0","s1","s2","s3","s4" |
96| callback  | AsyncCallback<void> | 是   | 是否设置数据标签之后的回调                   |
97
98**错误码:**
99
100以下错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
101
102| 错误码ID | 错误信息 |
103| -------- | -------- |
104| 13900001 | Operation not permitted |
105| 13900007 | Arg list too long |
106| 13900015 | File exists |
107| 13900020 | Invalid argument |
108| 13900025 | No space left on device |
109| 13900037 | No data available |
110| 13900041 | Quota exceeded |
111| 13900042 | Unknown error |
112
113**示例:**
114
115  ```ts
116  import { BusinessError } from '@ohos.base';
117  let filePath = pathDir + '/test.txt';
118  securityLabel.setSecurityLabel(filePath, "s0", (err: BusinessError) => {
119    if (err) {
120      console.error("setSecurityLabel failed with error message: " + err.message + ", error code: " + err.code);
121    } else {
122      console.info("setSecurityLabel successfully.");
123    }
124  });
125  ```
126
127## securityLabel.setSecurityLabelSync
128
129setSecurityLabelSync(path:string, type:DataLevel):void
130
131以同步方法设置数据标签。
132
133**系统能力**:SystemCapability.FileManagement.File.FileIO
134
135**参数:**
136
137| 参数名    | 类型   | 必填 | 说明                                         |
138| --------- | ------ | ---- | -------------------------------------------- |
139| path      | string | 是   | 文件路径                                     |
140| type      | DataLevel | 是   | 文件等级属性,只支持"s0","s1","s2","s3","s4" |
141
142**错误码:**
143
144以下错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
145
146| 错误码ID | 错误信息 |
147| -------- | -------- |
148| 13900001 | Operation not permitted |
149| 13900007 | Arg list too long |
150| 13900015 | File exists |
151| 13900020 | Invalid argument |
152| 13900025 | No space left on device |
153| 13900037 | No data available |
154| 13900041 | Quota exceeded |
155| 13900042 | Unknown error |
156
157**示例:**
158
159```ts
160let filePath = pathDir + '/test.txt';
161securityLabel.setSecurityLabelSync(filePath, "s0");
162```
163
164## securityLabel.getSecurityLabel
165
166getSecurityLabel(path:string):Promise<string>
167
168异步方法获取数据标签,以Promise形式返回结果。
169
170**系统能力**:SystemCapability.FileManagement.File.FileIO
171
172**参数:**
173
174  | 参数名 | 类型   | 必填 | 说明     |
175  | ------ | ------ | ---- | -------- |
176  | path   | string | 是   | 文件路径 |
177
178**返回值:**
179
180  | 类型                  | 说明         |
181  | --------------------- | ------------ |
182  | Promise<string> | 返回数据标签 |
183
184**错误码:**
185
186以下错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
187
188| 错误码ID | 错误信息 |
189| -------- | -------- |
190| 13900001 | Operation not permitted |
191| 13900007 | Arg list too long |
192| 13900015 | File exists |
193| 13900020 | Invalid argument |
194| 13900025 | No space left on device |
195| 13900037 | No data available |
196| 13900041 | Quota exceeded |
197| 13900042 | Unknown error |
198
199**示例:**
200
201  ```ts
202  import { BusinessError } from '@ohos.base';
203  let filePath = pathDir + '/test.txt';
204  securityLabel.getSecurityLabel(filePath).then((type: string) => {
205    console.log("getSecurityLabel successfully, Label: " + type);
206  }).catch((err: BusinessError) => {
207    console.log("getSecurityLabel failed with error message: " + err.message + ", error code: " + err.code);
208  });
209  ```
210
211## securityLabel.getSecurityLabel
212
213getSecurityLabel(path:string, callback:AsyncCallback<string>): void
214
215异步方法获取数据标签,以callback形式返回结果。
216
217**系统能力**:SystemCapability.FileManagement.File.FileIO
218
219**参数:**
220
221  | 参数名   | 类型                        | 必填 | 说明                       |
222  | -------- | --------------------------- | ---- | -------------------------- |
223  | path     | string                      | 是   | 文件路径                   |
224  | callback | AsyncCallback<string> | 是   | 异步获取数据标签之后的回调 |
225
226**错误码:**
227
228以下错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
229
230| 错误码ID | 错误信息 |
231| -------- | -------- |
232| 13900001 | Operation not permitted |
233| 13900007 | Arg list too long |
234| 13900015 | File exists |
235| 13900020 | Invalid argument |
236| 13900025 | No space left on device |
237| 13900037 | No data available |
238| 13900041 | Quota exceeded |
239| 13900042 | Unknown error |
240
241**示例:**
242
243  ```ts
244  import { BusinessError } from '@ohos.base';
245  let filePath = pathDir + '/test.txt';
246  securityLabel.getSecurityLabel(filePath, (err: BusinessError, type: string) => {
247    if (err) {
248      console.log("getSecurityLabel failed with error message: " + err.message + ", error code: " + err.code);
249    } else {
250      console.log("getSecurityLabel successfully, Label: " + type);
251    }
252  });
253  ```
254
255## securityLabel.getSecurityLabelSync
256
257getSecurityLabelSync(path:string):string
258
259以同步方法获取数据标签。
260
261**系统能力**:SystemCapability.FileManagement.File.FileIO
262
263**参数:**
264
265| 参数名 | 类型   | 必填 | 说明     |
266| ------ | ------ | ---- | -------- |
267| path   | string | 是   | 文件路径 |
268
269**返回值:**
270
271| 类型   | 说明         |
272| ------ | ------------ |
273| string | 返回数据标签 |
274
275**错误码:**
276
277以下错误码的详细介绍请参见[基础文件IO错误码](errorcode-filemanagement.md#基础文件io错误码)。
278
279| 错误码ID | 错误信息 |
280| -------- | -------- |
281| 13900001 | Operation not permitted |
282| 13900007 | Arg list too long |
283| 13900015 | File exists |
284| 13900020 | Invalid argument |
285| 13900025 | No space left on device |
286| 13900037 | No data available |
287| 13900041 | Quota exceeded |
288| 13900042 | Unknown error |
289
290**示例:**
291
292```ts
293let filePath = pathDir + '/test.txt';
294let type = securityLabel.getSecurityLabelSync(filePath);
295console.log("getSecurityLabel successfully, Label: " + type);
296```
297