1# Security Label 2 3The **secuityLabel** module provides APIs to manage file data security levels, including obtaining and setting file data security levels. 4 5> **NOTE**<br/> 6> 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. 7 8## Modules to Import 9 10```js 11import securityLabel from '@ohos.securityLabel'; 12``` 13 14## Usage 15 16Before using the APIs provided by this module to perform operations on a file or directory, obtain the path of the application sandbox as follows: 17 18```js 19import featureAbility from '@ohos.ability.featureAbility'; 20let context = featureAbility.getContext(); 21let path = ''; 22context.getFilesDir().then((data) => { 23 path = data; 24}) 25``` 26 27## securityLabel.setSecurityLabel 28 29setSecurityLabel(path:string, type:dataLevel):Promise<void> 30 31Sets the security label for a file in asynchronous mode. This API uses a promise to return the result. 32 33**System capability**: SystemCapability.FileManagement.File.FileIO 34 35**Parameters** 36 37| Name | Type | Mandatory| Description | 38| --------- | ------ | ---- | -------------------------------------------- | 39| path | string | Yes | File path. | 40| type | dataLevel | Yes | File security level, which can be **s0**, **s1**, **s2**, **s3**, or **s4**.| 41 42**Return value** 43 44 | Type | Description | 45 | ------------------- | ---------------- | 46 | Promise<void> | Promise used to return the result. An empty value will be returned.| 47 48**Example** 49 50 ```js 51 let type = "s4"; 52 securityLabel.setSecurityLabel(path, type).then(function(){ 53 console.info("setSecurityLabel successfully"); 54 }).catch(function(error){ 55 console.info("setSecurityLabel failed with error:" + error); 56 }); 57 ``` 58 59## securityLabel.setSecurityLabel 60 61setSecurityLabel(path:string, type:dataLevel, callback: AsyncCallback<void>):void 62 63Sets the security label for a file in asynchronous mode. This API uses a callback to return the result. 64 65**System capability**: SystemCapability.FileManagement.File.FileIO 66 67**Parameters** 68 69| Name | Type | Mandatory| Description | 70| --------- | ------------------------- | ---- | -------------------------------------------- | 71| path | string | Yes | File path. | 72| type | dataLevel | Yes | File security level, which can be **s0**, **s1**, **s2**, **s3**, or **s4**.| 73| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 74 75**Example** 76 77 ```js 78 let type = "s4"; 79 securityLabel.setSecurityLabel(path, type, function(error){ 80 console.info("setSecurityLabel:" + JSON.stringify(error)); 81 }); 82 ``` 83## securityLabel.setSecurityLabelSync 84 85setSecurityLabelSync(path:string, type:dataLevel):void 86 87Sets the security label for a file in synchronous mode. 88 89**System capability**: SystemCapability.FileManagement.File.FileIO 90 91**Parameters** 92 93| Name | Type | Mandatory| Description | 94| --------- | ------ | ---- | -------------------------------------------- | 95| path | string | Yes | File path. | 96| type | dataLevel | Yes | File security level, which can be **s0**, **s1**, **s2**, **s3**, or **s4**.| 97 98**Example** 99 100```js 101let type = "s4"; 102securityLabel.setSecurityLabelSync(path, type); 103``` 104 105## securityLabel.getSecurityLabel 106 107getSecurityLabel(path:string):Promise<string> 108 109Obtains the security label of a file in asynchronous mode. This API uses a promise to return the result. 110 111**System capability**: SystemCapability.FileManagement.File.FileIO 112 113**Parameters** 114 115 | Name| Type | Mandatory| Description | 116 | ------ | ------ | ---- | -------- | 117 | path | string | Yes | File path.| 118 119**Return value** 120 121 | Type | Description | 122 | --------------------- | ------------ | 123 | Promise<string> | Promise used to return the security label obtained.| 124 125**Example** 126 127 ```js 128 let type = "s4"; 129 securityLabel.getSecurityLabel(path).then(function(type){ 130 console.log("getSecurityLabel successfully:" + type); 131 }).catch(function(error){ 132 console.log("getSecurityLabel failed with error:" + error); 133 }); 134 ``` 135 136## securityLabel.getSecurityLabel 137 138getSecurityLabel(path:string, callback:AsyncCallback<string>): void 139 140Obtains the security label of a file in asynchronous mode. This API uses a callback to return the result. 141 142**System capability**: SystemCapability.FileManagement.File.FileIO 143 144**Parameters** 145 146 | Name | Type | Mandatory| Description | 147 | -------- | --------------------------- | ---- | -------------------------- | 148 | path | string | Yes | File path. | 149 | callback | AsyncCallback<string> | Yes | Callback used to return the security label obtained.| 150 151**Example** 152 153 ```js 154 let type = "s4"; 155 securityLabel.getSecurityLabel(path,function(error, type){ 156 console.log("getSecurityLabel successfully:" + type); 157 }); 158 ``` 159## securityLabel.getSecurityLabelSync 160 161getSecurityLabelSync(path:string):string 162 163Obtains the security label of a file in synchronous mode. 164 165**System capability**: SystemCapability.FileManagement.File.FileIO 166 167**Parameters** 168 169| Name| Type | Mandatory| Description | 170| ------ | ------ | ---- | -------- | 171| path | string | Yes | File path.| 172 173**Return value** 174 175| Type | Description | 176| ------ | ------------ | 177| string | Security label obtained.| 178 179**Example** 180 181```js 182let result = securityLabel.getSecurityLabelSync(path); 183console.log("getSecurityLabel successfully:" + result); 184``` 185