1# @ohos.file.securityLabel (Data Label) 2 3The **securityLabel** module provides APIs for managing data security levels of files, including obtaining and setting file security levels. 4 5> **NOTE** 6> 7> 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. 8 9## Modules to Import 10 11```js 12import securityLabel from '@ohos.file.securityLabel'; 13``` 14 15## Guidelines 16 17Before 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: 18 19**Stage Model** 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 Model** 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 43For details about how to obtain the FA model context, see [Context](js-apis-inner-app-context.md#context). 44 45## securityLabel.setSecurityLabel 46 47setSecurityLabel(path:string, type:DataLevel):Promise<void> 48 49Sets a security label for a file in asynchronous mode. This API uses a promise to return the result. 50 51**System capability**: SystemCapability.FileManagement.File.FileIO 52 53**Parameters** 54 55| Name | Type | Mandatory| Description | 56| --------- | ------ | ---- | -------------------------------------------- | 57| path | string | Yes | Path of the target file. | 58| type | DataLevel | Yes | File security level to set, which can be **s0**, **s1**, **s2**, **s3**, or **s4**.| 59 60**Return value** 61 62 | Type | Description | 63 | ------------------- | ---------------- | 64 | Promise<void> | Promise that returns no value.| 65 66**Error codes** 67 68For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes). 69 70| ID| Error Message| 71| -------- | -------- | 72| 13900001 | Operation not permitted | 73| 13900007 | Arg list too long | 74| 13900015 | File exists | 75| 13900020 | Invalid argument | 76| 13900025 | No space left on device | 77| 13900037 | No data available | 78| 13900041 | Quota exceeded | 79| 13900042 | Unknown error | 80 81**Example** 82 83 ```js 84 let filePath = pathDir + '/test.txt'; 85 securityLabel.setSecurityLabel(filePath, "s0").then(() => { 86 console.info("setSecurityLabel successfully"); 87 }).catch((err) => { 88 console.info("setSecurityLabel failed with error message: " + err.message + ", error code: " + err.code); 89 }); 90 ``` 91 92## securityLabel.setSecurityLabel 93 94setSecurityLabel(path:string, type:DataLevel, callback: AsyncCallback<void>):void 95 96Sets a security label for a file in asynchronous mode. This API uses an asynchronous callback to return the result. 97 98**System capability**: SystemCapability.FileManagement.File.FileIO 99 100**Parameters** 101 102| Name | Type | Mandatory| Description | 103| --------- | ------------------------- | ---- | -------------------------------------------- | 104| path | string | Yes | Path of the target file. | 105| type | DataLevel | Yes | File security level to set, which can be **s0**, **s1**, **s2**, **s3**, or **s4**.| 106| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. | 107 108**Error codes** 109 110For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes). 111 112| ID| Error Message| 113| -------- | -------- | 114| 13900001 | Operation not permitted | 115| 13900007 | Arg list too long | 116| 13900015 | File exists | 117| 13900020 | Invalid argument | 118| 13900025 | No space left on device | 119| 13900037 | No data available | 120| 13900041 | Quota exceeded | 121| 13900042 | Unknown error | 122 123**Example** 124 125 ```js 126 let filePath = pathDir + '/test.txt'; 127 securityLabel.setSecurityLabel(filePath, "s0", (err) => { 128 if (err) { 129 console.info("setSecurityLabel failed with error message: " + err.message + ", error code: " + err.code); 130 } else { 131 console.info("setSecurityLabel successfully."); 132 } 133 }); 134 ``` 135 136## securityLabel.setSecurityLabelSync 137 138setSecurityLabelSync(path:string, type:DataLevel):void 139 140Sets a security label for a file in synchronous mode. 141 142**System capability**: SystemCapability.FileManagement.File.FileIO 143 144**Parameters** 145 146| Name | Type | Mandatory| Description | 147| --------- | ------ | ---- | -------------------------------------------- | 148| path | string | Yes | Path of the target file. | 149| type | DataLevel | Yes | File security level to set, which can be **s0**, **s1**, **s2**, **s3**, or **s4**.| 150 151**Error codes** 152 153For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes). 154 155| ID| Error Message| 156| -------- | -------- | 157| 13900001 | Operation not permitted | 158| 13900007 | Arg list too long | 159| 13900015 | File exists | 160| 13900020 | Invalid argument | 161| 13900025 | No space left on device | 162| 13900037 | No data available | 163| 13900041 | Quota exceeded | 164| 13900042 | Unknown error | 165 166**Example** 167 168```js 169let filePath = pathDir + '/test.txt'; 170securityLabel.setSecurityLabelSync(filePath, "s0"); 171``` 172 173## securityLabel.getSecurityLabel 174 175getSecurityLabel(path:string):Promise<string> 176 177Obtains the security label of a file in asynchronous mode. This API uses a promise to return the result. 178 179**System capability**: SystemCapability.FileManagement.File.FileIO 180 181**Parameters** 182 183 | Name| Type | Mandatory| Description | 184 | ------ | ------ | ---- | -------- | 185 | path | string | Yes | Path of the target file.| 186 187**Return value** 188 189 | Type | Description | 190 | --------------------- | ------------ | 191 | Promise<string> | Security label obtained.| 192 193**Error codes** 194 195For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes). 196 197| ID| Error Message| 198| -------- | -------- | 199| 13900001 | Operation not permitted | 200| 13900007 | Arg list too long | 201| 13900015 | File exists | 202| 13900020 | Invalid argument | 203| 13900025 | No space left on device | 204| 13900037 | No data available | 205| 13900041 | Quota exceeded | 206| 13900042 | Unknown error | 207 208**Example** 209 210 ```js 211 let filePath = pathDir + '/test.txt'; 212 securityLabel.getSecurityLabel(filePath).then((type) => { 213 console.log("getSecurityLabel successfully, Label: " + type); 214 }).catch((err) => { 215 console.log("getSecurityLabel failed with error message: " + err.message + ", error code: " + err.code); 216 }); 217 ``` 218 219## securityLabel.getSecurityLabel 220 221getSecurityLabel(path:string, callback:AsyncCallback<string>): void 222 223Obtains the security label of a file in asynchronous mode. This API uses a callback to return the result. 224 225**System capability**: SystemCapability.FileManagement.File.FileIO 226 227**Parameters** 228 229 | Name | Type | Mandatory| Description | 230 | -------- | --------------------------- | ---- | -------------------------- | 231 | path | string | Yes | Path of the target file. | 232 | callback | AsyncCallback<string> | Yes | Callback invoked to return the security label obtained.| 233 234**Error codes** 235 236For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes). 237 238| ID| Error Message| 239| -------- | -------- | 240| 13900001 | Operation not permitted | 241| 13900007 | Arg list too long | 242| 13900015 | File exists | 243| 13900020 | Invalid argument | 244| 13900025 | No space left on device | 245| 13900037 | No data available | 246| 13900041 | Quota exceeded | 247| 13900042 | Unknown error | 248 249**Example** 250 251 ```js 252 let filePath = pathDir + '/test.txt'; 253 securityLabel.getSecurityLabel(filePath, (err, type) => { 254 if (err) { 255 console.log("getSecurityLabel failed with error message: " + err.message + ", error code: " + err.code); 256 } else { 257 console.log("getSecurityLabel successfully, Label: " + type); 258 } 259 }); 260 ``` 261 262## securityLabel.getSecurityLabelSync 263 264getSecurityLabelSync(path:string):string 265 266Obtains the security label of a file in synchronous mode. 267 268**System capability**: SystemCapability.FileManagement.File.FileIO 269 270**Parameters** 271 272| Name| Type | Mandatory| Description | 273| ------ | ------ | ---- | -------- | 274| path | string | Yes | Path of the target file.| 275 276**Return value** 277 278| Type | Description | 279| ------ | ------------ | 280| string | Security label obtained.| 281 282**Error codes** 283 284For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes). 285 286| ID| Error Message| 287| -------- | -------- | 288| 13900001 | Operation not permitted | 289| 13900007 | Arg list too long | 290| 13900015 | File exists | 291| 13900020 | Invalid argument | 292| 13900025 | No space left on device | 293| 13900037 | No data available | 294| 13900041 | Quota exceeded | 295| 13900042 | Unknown error | 296 297**Example** 298 299```js 300let filePath = pathDir + '/test.txt'; 301let type = securityLabel.getSecurityLabelSync(filePath); 302console.log("getSecurityLabel successfully, Label: " + type); 303``` 304