5#include <QtCore/private/qcore_ohos_p.h>
6#include <QtCore/private/qohoscommon_p.h>
7#include <QtCore/private/qohoslogger_p.h>
8#include <QtCore/private/qohospathutils_p.h>
9#include <QtCore/qscopeguard.h>
10#include <QtOhosAppKit/private/qohosoperationstatus_p.h>
20#include <filemanagement/fileshare/oh_file_share.h>
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
54std::shared_ptr<
char> makeSharedNullTerminatedString(
std::string str)
56 auto sharedStrData = QtOhos::moveToSharedPtr(std::move(str) +
'\0');
57 return std::shared_ptr<
char>(sharedStrData, &sharedStrData->front());
60std::shared_ptr<
char> makeSharedNullTerminatedString(
const char *str)
62 return makeSharedNullTerminatedString(std::string(str !=
nullptr ? str :
""));
65std::shared_ptr<::FileShare_PolicyInfo> makeFileSharePolicyInfo(
66 std::string uri,
unsigned operationMode)
68 auto sharedUri = makeSharedNullTerminatedString(std::move(uri));
70 auto policyInfo = QtOhos::moveToSharedPtr(
71 ::FileShare_PolicyInfo{
72 .uri = sharedUri.get(),
73 .length =
static_cast<
unsigned>(std::strlen(sharedUri.get())),
74 .operationMode = operationMode,
77 return QtOhos::makeSharedPtrWithAttachedExtraData(
78 policyInfo, sharedUri);
81std::vector<std::shared_ptr<::FileShare_PolicyInfo>> convertToFileSharePolicyInfos(
82 const QList<PathPolicy> &policies)
84 std::vector<std::shared_ptr<::FileShare_PolicyInfo>> fileSharePolicies;
86 for (
const auto &policy : policies) {
87 fileSharePolicies.push_back(
88 makeFileSharePolicyInfo(
89 tryMapPathToOhosFileUri(policy.path.toStdString()).value_or(
""),
90 static_cast<
unsigned>(policy.operationModes.toInt())));
93 return fileSharePolicies;
96std::shared_ptr<::FileShare_PolicyErrorResult> makeFileSharePolicyErrorResultFromRawStruct(
97 const ::FileShare_PolicyErrorResult &inputStruct)
99 auto sharedUri = makeSharedNullTerminatedString(inputStruct.uri);
100 auto sharedMessage = makeSharedNullTerminatedString(inputStruct.message);
102 auto policyErrorResult = QtOhos::moveToSharedPtr(
103 ::FileShare_PolicyErrorResult{
104 .uri = sharedUri.get(),
105 .code = inputStruct.code,
106 .message = sharedMessage.get(),
109 return QtOhos::makeSharedPtrWithAttachedExtraData(
111 QtOhos::moveToSharedPtr(std::make_tuple(sharedUri, sharedMessage)));
114std::vector<::FileShare_PolicyInfo> makePoliciesRawVectorView(
115 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies)
117 std::vector<::FileShare_PolicyInfo> rawVectorView;
118 for (
const auto &policyPtr : policies)
119 rawVectorView.push_back(*policyPtr);
121 return rawVectorView;
124template<
typename PermissionActionFunc>
125::FileManagement_ErrCode callFileSharePermissionActionFunc(
126 PermissionActionFunc permissionActionFunc,
127 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
128 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &outResult)
130 auto policiesRawVectorView = makePoliciesRawVectorView(policies);
131 ::FileShare_PolicyErrorResult *resultParam =
nullptr;
132 unsigned resultNumParam = 0;
133 auto resultParamReleaseGuard = qScopeGuard(
135 if (resultParam !=
nullptr && resultNumParam != 0)
136 ::OH_FileShare_ReleasePolicyErrorResult(resultParam, resultNumParam);
139 auto errCode = permissionActionFunc(
140 policiesRawVectorView.data(), policiesRawVectorView.size(),
141 &resultParam, &resultNumParam);
144 if (resultParam !=
nullptr) {
145 for (
unsigned i = 0; i < resultNumParam; ++i) {
147 makeFileSharePolicyErrorResultFromRawStruct(resultParam[i]));
154::FileManagement_ErrCode fileShareCheckPersistentPermission(
155 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
156 std::vector<
bool> &outResult)
158 auto policiesRawVectorView = makePoliciesRawVectorView(policies);
159 bool *resultParam =
nullptr;
160 auto resultParamReleaseGuard = qScopeGuard(
164 unsigned resultNumParam = 0;
166 auto errCode = ::OH_FileShare_CheckPersistentPermission(
167 policiesRawVectorView.data(), policiesRawVectorView.size(),
168 &resultParam, &resultNumParam);
171 if (resultParam !=
nullptr) {
172 for (
unsigned i = 0; i < resultNumParam; ++i)
173 outResult.push_back(resultParam[i]);
179::FileManagement_ErrCode fileSharePersistPermission(
180 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
181 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &outResult)
183 return callFileSharePermissionActionFunc(
184 Q_OHOS_NAMED_FUNC(::OH_FileShare_PersistPermission),
185 policies, outResult);
188::FileManagement_ErrCode fileShareRevokePermission(
189 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
190 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &outResult)
192 return callFileSharePermissionActionFunc(
193 Q_OHOS_NAMED_FUNC(::OH_FileShare_RevokePermission),
194 policies, outResult);
197::FileManagement_ErrCode fileShareActivatePermission(
198 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
199 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &outResult)
201 return callFileSharePermissionActionFunc(
202 Q_OHOS_NAMED_FUNC(::OH_FileShare_ActivatePermission),
203 policies, outResult);
206::FileManagement_ErrCode fileShareDeactivatePermission(
207 const std::vector<std::shared_ptr<::FileShare_PolicyInfo>> &policies,
208 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &outResult)
210 return callFileSharePermissionActionFunc(
211 Q_OHOS_NAMED_FUNC(::OH_FileShare_DeactivatePermission),
212 policies, outResult);
215std::optional<PathPolicyError> tryMapFileSharePolicyErrorCode(::FileShare_PolicyErrorCode errorCode)
218 case ::FileShare_PolicyErrorCode::PERSISTENCE_FORBIDDEN:
219 return std::make_optional(PathPolicyError::PersistenceForbidden);
220 case ::FileShare_PolicyErrorCode::INVALID_MODE:
221 return std::make_optional(PathPolicyError::InvalidMode);
222 case ::FileShare_PolicyErrorCode::INVALID_PATH:
223 return std::make_optional(PathPolicyError::InvalidPath);
224 case ::FileShare_PolicyErrorCode::PERMISSION_NOT_PERSISTED:
225 return std::make_optional(PathPolicyError::PermissionNotPersisted);
230QList<PathPolicyErrorInfo> convertToPathPolicyErrorInfos(
231 const std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> &policyErrorResults)
233 QList<PathPolicyErrorInfo> result;
234 for (
const auto &policyErrorResult : policyErrorResults) {
236 .path = policyErrorResult->uri !=
nullptr
237 ? QString::fromStdString(tryMapOhosFileUriToPath(policyErrorResult->uri).value_or(
""))
240 tryMapFileSharePolicyErrorCode(policyErrorResult->code)
241 .value_or(PathPolicyError::Unknown),
242 .errorMessage = QLatin1String(
243 policyErrorResult->message !=
nullptr ? policyErrorResult->message :
""),
250QList<PathPolicyCheckResult> convertToPathPolicyCheckResults(
251 const std::vector<
bool> &checkResults,
252 const QList<PathPolicy> &policies)
254 QList<PathPolicyCheckResult> result;
255 for (
std::size_t i = 0; i < checkResults.size() && policies.size(); ++i)
256 result.push_back({policies[i], checkResults[i]});
261bool validateCheckResultsAgainstPolicies(
262 const std::vector<
bool> &checkResults,
263 const QList<PathPolicy> &policies)
265 return checkResults.size() ==
static_cast<
std::size_t>(policies.size());
268bool isSuccessErrorCode(::FileManagement_ErrCode errorCode)
270 return errorCode == ::FileManagement_ErrCode::ERR_OK;
275
276
277
278
279
280
281
282
283class ActionResultImpl :
public ActionResult
286 ActionResultImpl(
bool successFlag, QList<PathPolicyErrorInfo> errorInfoList);
287 std::shared_ptr<OperationStatus> operationStatus()
const override;
288 QList<PathPolicyErrorInfo> errorInfoList()
const override;
291 std::shared_ptr<OperationStatus> m_operationStatus;
292 QList<PathPolicyErrorInfo> m_errorInfoList;
295ActionResultImpl::ActionResultImpl(
bool successFlag, QList<PathPolicyErrorInfo> errorInfoList)
296 : m_operationStatus(createOperationStatus(successFlag))
297 , m_errorInfoList(std::move(errorInfoList))
302
303
304
305
306
307
308std::shared_ptr<OperationStatus> ActionResultImpl::operationStatus()
const
310 return m_operationStatus;
314
315
316
317
318
319
320
321
322QList<PathPolicyErrorInfo> ActionResultImpl::errorInfoList()
const
324 return m_errorInfoList;
329
330
331
332
333
334
335class CheckResultImpl :
public CheckResult
339 bool successFlag,
const std::vector<
bool> &checkResults,
340 const QList<PathPolicy> &policies);
341 std::shared_ptr<OperationStatus> operationStatus()
const override;
342 QList<PathPolicyCheckResult> checkResultList()
const override;
345 std::shared_ptr<OperationStatus> m_operationStatus;
346 QList<PathPolicyCheckResult> m_checkResultList;
349CheckResultImpl::CheckResultImpl(
350 bool successFlag,
const std::vector<
bool> &checkResults,
351 const QList<PathPolicy> &policies)
353 if (validateCheckResultsAgainstPolicies(checkResults, policies)) {
354 m_operationStatus = createOperationStatus(successFlag);
355 m_checkResultList = convertToPathPolicyCheckResults(checkResults, policies);
358 "%s: didn't get check results for all requested paths. "
359 "Got %lld check results, but requested checks for %lld paths.",
360 Q_FUNC_INFO,
static_cast<
long long>(checkResults.size()),
361 static_cast<
long long>(policies.size()));
362 m_operationStatus = createOperationStatus(
false);
367
368
369
370
371
372
373std::shared_ptr<OperationStatus> CheckResultImpl::operationStatus()
const
375 return m_operationStatus;
379
380
381
382
383
384
385
386
387QList<PathPolicyCheckResult> CheckResultImpl::checkResultList()
const
389 return m_checkResultList;
395
396
397
398
399
400
401
402
403
404
405
408
409
410
411
412
413
414
415
416
417
418
419
420
421
424
425
426
427
428
431
432
433
436
437
438
439
440
443
444
445
446
447
448
449
452
453
454
457
458
459
460
461
464
465
466
469
470
471
472
473
476
477
478
479
480
483
484
485
496
497
498
499
500
501
502
503
504
505
506
507
508
511 auto actionResult = QOhosJsThreadGateway::eval(
512 [&](QOhosJsState &) {
513 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> outResults;
514 auto retCode = fileSharePersistPermission(
515 convertToFileSharePolicyInfos(policies), outResults);
517 return std::make_pair(isSuccessErrorCode(retCode), convertToPathPolicyErrorInfos(outResults));
520 return std::make_shared<ActionResultImpl>(actionResult.first, actionResult.second);
524
525
526
527
528
529
530
531
532
533
534
537 auto actionResult = QOhosJsThreadGateway::eval(
538 [&](QOhosJsState &) {
539 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> outResults;
540 auto retCode = fileShareRevokePermission(
541 convertToFileSharePolicyInfos(policies), outResults);
543 return std::make_pair(isSuccessErrorCode(retCode), convertToPathPolicyErrorInfos(outResults));
546 return std::make_shared<ActionResultImpl>(actionResult.first, actionResult.second);
550
551
552
553
554
555
556
557
558
559
560
561
562
565 auto actionResult = QOhosJsThreadGateway::eval(
566 [&](QOhosJsState &) {
567 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> outResults;
568 auto retCode = fileShareActivatePermission(
569 convertToFileSharePolicyInfos(policies), outResults);
571 return std::make_pair(isSuccessErrorCode(retCode), convertToPathPolicyErrorInfos(outResults));
574 return std::make_shared<ActionResultImpl>(actionResult.first, actionResult.second);
578
579
580
581
582
583
584
585
586
587
588
591 auto actionResult = QOhosJsThreadGateway::eval(
592 [&](QOhosJsState &) {
593 std::vector<std::shared_ptr<::FileShare_PolicyErrorResult>> outResults;
594 auto retCode = fileShareDeactivatePermission(
595 convertToFileSharePolicyInfos(policies), outResults);
597 return std::make_pair(isSuccessErrorCode(retCode), convertToPathPolicyErrorInfos(outResults));
600 return std::make_shared<ActionResultImpl>(actionResult.first, actionResult.second);
604
605
606
607
608
609
610
611
612
613
614
615
618 auto checkResult = QOhosJsThreadGateway::eval(
619 [&](QOhosJsState &) {
620 std::vector<
bool> outResults;
621 auto retCode = fileShareCheckPersistentPermission(
622 convertToFileSharePolicyInfos(policies), outResults);
624 return std::make_pair(isSuccessErrorCode(retCode), outResults);
627 return std::make_shared<CheckResultImpl>(checkResult.first, checkResult.second, policies);
std::shared_ptr< ActionResult > activatePermission(const QList< PathPolicy > &policies)
Activates file or folder permissions from policies.
std::shared_ptr< ActionResult > deactivatePermission(const QList< PathPolicy > &policies)
Deactivates file or folder permissions from policies.
std::shared_ptr< ActionResult > persistPermission(const QList< PathPolicy > &policies)
Persists file or folder permissions from policies.
std::shared_ptr< CheckResult > checkPersistent(const QList< PathPolicy > &policies)
Checks if file or folder permissions from policies have persistent permissions.
std::shared_ptr< ActionResult > revokePermission(const QList< PathPolicy > &policies)
Revokes file or folder permissions from policies.