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