Enterprise Data Guard Kit allows apps to obtain the tags of a file so HarmonyOS can effectively manage files based on control policies and file tags.
For details about these APIs, please refer to the API Reference.
| API | Description |
|---|---|
| queryFileTag(path: string, callback: AsyncCallback<FileTagInfo>): void | Obtains file tags using a callback. |
| queryFileTag(path: string): Promise<FileTagInfo> | Obtains file tags using a promise. |
Import required modules.
import { BusinessError } from '@kit.BasicServicesKit';
import { fileGuard } from '@kit.EnterpriseDataGuardKit';
import { hilog } from '@kit.PerformanceAnalysisKit'; Initialize the guard object of FileGuard and call the queryFileTag API to obtain file tags.
Obtain file tags using a callback.
const TAG: string = 'FileGuard_FileTag';
const DOMAIN: number = 0x0000;
// ...
/**
* Obtain file tags and return the result asynchronously using a callback.
*/
function queryFileTagCallback() {
let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
let path: string = '/data/service/el2/test/test1.txt';
guard.queryFileTag(path, (err: BusinessError, data: fileGuard.FileTagInfo) => {
if (err) {
hilog.error(DOMAIN, TAG, `Failed to query file tag. Code: ${err.code}, message: ${err.message}.`);
return;
}
hilog.info(DOMAIN, TAG,
`Succeeded in querying file tag. securityLevel: ${data.securityLevel}, tag: ${data.tag}.`);
});
} Obtain file tags using a promise.
const TAG: string = 'FileGuard_FileTag';
const DOMAIN: number = 0x0000;
// ...
/**
* Obtain file tags and return the result asynchronously using a promise.
*/
function queryFileTagPromise() {
let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
let path: string = '/data/service/el2/test/test2.txt';
guard.queryFileTag(path).then((data: fileGuard.FileTagInfo) => {
hilog.info(DOMAIN, TAG,
`Succeeded in querying file tag. securityLevel: ${data.securityLevel}, tag: ${data.tag}.`);
}).catch((err: BusinessError) => {
hilog.error(DOMAIN, TAG, `Failed to query file tag. Code: ${err.code}, message: ${err.message}.`);
});
}