diff --git a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp index 52ee635891b5765649a72e907fe31b8f97fa41d2..d93492ac71a06f730371028b6bbe0f76f5a01e55 100644 --- a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp @@ -21,7 +21,7 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/hash/hashImpl;"; + static const char *className = "L@ohos/file/hash/HashImpl;"; std::array methods = { ani_native_function { "hashSync", "Lstd/core/String;Lstd/core/String;:Lstd/core/String;", reinterpret_cast(HashAni::HashSync) }, diff --git a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets index 1e68269ace6dabdd99484162185d1f95525d2682..3c4fd40dcd7028c67d7ef84e3438cda5a921514c 100644 --- a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets +++ b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets @@ -14,33 +14,73 @@ */ import { BusinessError, AsyncCallback } from '@ohos.base'; +import stream from '@ohos.util.stream'; -function hashSync(path: string, algorithm: string): string { - return HashImpl.hashSync(path, algorithm); -} +export default namespace hash { + export function hash(path: string, algorithm: string): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { + let promise = taskpool.execute(HashImpl.hashSync, path, algorithm); + promise.then((ret: NullishType): void => { + let res = ret as string; + resolve(res); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } -function hash(path: string, algorithm: string): Promise { - return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { + export function hash(path: string, algorithm: string, callback: AsyncCallback): void { let promise = taskpool.execute(HashImpl.hashSync, path, algorithm); - promise.then((ret: NullishType): void => { + promise.then((ret: NullishType) => { + let e = new BusinessError(); + e.code = 0; let res = ret as string; - resolve(res); + callback(e, res); }).catch((e: BusinessError): void => { - reject(e); + callback(e, ""); }); - }); -} + } + + export function createHash(algorithm: string): HashStream { + return new HashStream(algorithm); + } -function hash(path: string, algorithm: string, callback: AsyncCallback): void { - let promise = taskpool.execute(HashImpl.hashSync, path, algorithm); - promise.then((ret: NullishType) => { - let e = new BusinessError(); - e.code = 0; - let res = ret as string; - callback(e, res); - }).catch((e: BusinessError): void => { - callback(e, ""); - }); + export class HashStream extends stream.Transform { + hs: hash.HashStream; + hashBuf?: ArrayBuffer; + + constructor(algorithm: string) { + super(); + this.hs = new hash.HashStream(algorithm); + } + + digest(): string { + return this.hs.digest(); + } + + update(data: ArrayBuffer): void { + this.hs.update(data); + } + + doTransform(chunk: string, encoding: string, callback: () => void): void { + let charCodes: number[] = []; + for (let i = 0; i < chunk.length; i++) { + charCodes = [...charCodes, chunk.charCodeAt(i)]; + } + const buf = new Uint8Array(charCodes).buffer; + this.hs.update((buf as ArrayBuffer)); + this.push(chunk); + callback(); + } + + doWrite(chunk: string | Uint8Array, encoding: string, callback: () => void): void { + callback(); + } + + doFlush(callback: () => void): void { + callback(); + } + } } class HashImpl { diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp index 9dfe0fb3ff27cb1896ae8304e28ef3176e6baae0..5bb0f2650cfb3291dd34e0fce1f212a4d9bc25e7 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp @@ -21,10 +21,12 @@ using namespace OHOS::FileManagement::ModuleFileIO::ANI; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/securityLabel/securitylabelImpl;"; + static const char *className = "L@ohos/file/securityLabel/SecurityLabelImpl;"; std::array methods = { ani_native_function { "setSecurityLabelSync", "Lstd/core/String;Lstd/core/String;:V", reinterpret_cast(SecurityLabelAni::SetSecurityLabelSync) }, + ani_native_function { "getSecurityLabelSync", "Lstd/core/String;:Lstd/core/String;", + reinterpret_cast(SecurityLabelAni::GetSecurityLabelSync) }, }; return BindClass(env, className, methods); } diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 28c002c7e3d732a7e139c6e7f909ec65a82d3192..b4268c6118846a602353c671ca9f86575dfea30e 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets @@ -15,39 +15,72 @@ import { BusinessError, AsyncCallback } from '@ohos.base'; -type DataLevel = 's0' | 's1' | 's2' | 's3' | 's4'; +namespace securityLabel { + export type DataLevel = 's0' | 's1' | 's2' | 's3' | 's4'; -function setSecurityLabel(path: string, type: DataLevel): Promise { - return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + export function setSecurityLabel(path: string, type: DataLevel): Promise { + return new Promise((resolve: (result: undefined) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string, type: DataLevel): void => SecurityLabelImpl.setSecurityLabelSync(path, type), path, type); + promise.then((ret: NullishType): void => { + resolve(undefined); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } + + export function setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { let promise = taskpool.execute((path: string, type: DataLevel): void => SecurityLabelImpl.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType): void => { - resolve(undefined); + let e = new BusinessError(); + e.code = 0; + callback(e, undefined); }).catch((e: BusinessError): void => { - reject(e); + callback(e, undefined); }); - }); -} + } -function setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { - let promise = taskpool.execute((path: string, type: DataLevel): void => SecurityLabelImpl.setSecurityLabelSync(path, type), path, type); - promise.then((ret: NullishType): void => { - let e = new BusinessError(); - e.code = 0; - callback(e, undefined); - }).catch((e: BusinessError): void => { - callback(e, undefined); - }); -} + export function setSecurityLabelSync(path: string, type: DataLevel): void { + return SecurityLabelImpl.setSecurityLabelSync(path, type); + } -function setSecurityLabelSync(path: string, type: DataLevel): void { - return SecurityLabelImpl.setSecurityLabelSync(path, type); + export function getSecurityLabel(path: string): Promise { + return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void): void => { + let promise = taskpool.execute((path: string): string => SecurityLabelImpl.getSecurityLabelSync(path), path); + promise.then((ret: NullishType): void => { + let r = ret as string; + resolve(r); + }).catch((e: BusinessError): void => { + reject(e); + }); + }); + } + + export function getSecurityLabel(path: string, callback: AsyncCallback): void { + let promise = taskpool.execute((path: string): string => SecurityLabelImpl.getSecurityLabelSync(path), path); + promise.then((ret: NullishType): void => { + let e = new BusinessError(); + e.code = 0; + let r = ret as string; + callback(e, r); + }).catch((e: BusinessError): void => { + callback(e, ""); + }); + } + + export function getSecurityLabelSync(path: string): string { + return SecurityLabelImpl.getSecurityLabelSync(path); + } } +export default securityLabel; + class SecurityLabelImpl { static { loadLibrary("ani_securitylabel_class"); } - static native setSecurityLabelSync(path: string, type: DataLevel): void; + static native setSecurityLabelSync(path: string, type: securityLabel.DataLevel): void; + static native getSecurityLabelSync(path: string): string; } diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp index 6c44deb69a515dcf5bf600643ea84b7886101809..4d6fd2b3211890d4e1092c8925da40bddbe0269f 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp @@ -55,6 +55,34 @@ void SecurityLabelAni::SetSecurityLabelSync( } } +ani_string SecurityLabelAni::GetSecurityLabelSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path) +{ + auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path); + if (!succPath) { + HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); + return nullptr; + } + + auto ret = DoGetSecurityLabel(srcPath); + if (!ret.IsSuccess()) { + HILOGE("Get securitylabel failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); + return nullptr; + } + + string res = ret.GetData().value(); + auto [succ, result] = TypeConverter::ToAniString(env, res); + if (!succ) { + HILOGE("Create ani_string error"); + ErrorHandler::Throw(env, UNKNOWN_ERR); + return nullptr; + } + + return result; +} + } // namespace ANI } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h index 025d65308f449d033b6d132ac9d0ac2845407103..8bb5741c9c347700ded871b900efc44297c823d9 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h +++ b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.h @@ -26,6 +26,7 @@ namespace ANI { class SecurityLabelAni final { public: static void SetSecurityLabelSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path, ani_string level); + static ani_string GetSecurityLabelSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path); }; } // namespace ANI diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp index fe1c8152a80eba92049b5fd32c9e080add726587..d68002e8aa8ddcab3863ebed88d44bcc22d1fa13 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.cpp @@ -37,6 +37,12 @@ FsResult DoSetSecurityLabel(const string &path, const string &dataLevel) return FsResult::Success(); } +FsResult DoGetSecurityLabel(const string &path) +{ + string ret = SecurityLabel::GetSecurityLabel(path); + return FsResult::Success(move(ret)); +} + } // namespace ModuleSecurityLabel } // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h index d56c069cb8751afaba8e8bac20edc587491ab00e..cb7766eaf5a6087c1e449e2077046ee4a63a9f33 100644 --- a/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h +++ b/interfaces/kits/js/src/mod_securitylabel/securitylabel_core.h @@ -24,6 +24,7 @@ namespace ModuleSecurityLabel { using namespace ModuleFileIO; FsResult DoSetSecurityLabel(const string &path, const string &dataLevel); +FsResult DoGetSecurityLabel(const string &path); } // namespace ModuleSecurityLabel } // namespace FileManagement