1# Persisting Temporary Permissions (C/C++) 2 3## When to Use 4 5If an application accesses a file by using Picker, the permission for accessing the file will be automatically invalidated after the application exits or the device restarts. To retain the permission for accessing the file, you need to [persist the permission](file-persistPermission.md#when-to-use). You can use the **FileShare** module to persist permissions on files or folders based on their URI, activate or deactivate persistent permissions on files or folders, and check the persistent permissions. 6 7## Available APIs 8 9For details about the APIs, see [FileShare](../reference/apis-core-file-kit/file_share.md). 10 11| API| Description| 12| -------- | -------- | 13| OH_FileShare_PersistPermission(const FileShare_PolicyInfo *policies, unsigned int policyNum, FileShare_PolicyErrorResult **result, unsigned int *resultNum) | Persists the permissions on files or folders.| 14| OH_FileShare_RevokePermission(const FileShare_PolicyInfo *policies, unsigned int policyNum, FileShare_PolicyErrorResult **result, unsigned int *resultNum) | Revokes the permissions from files or folders.| 15| OH_FileShare_ActivatePermission(const FileShare_PolicyInfo *policies, unsigned int policyNum, FileShare_PolicyErrorResult **result, unsigned int *resultNum) | Activates the persistent permissions on files or folders.| 16| OH_FileShare_DeactivatePermission(const FileShare_PolicyInfo *policies, unsigned int policyNum, FileShare_PolicyErrorResult **result, unsigned int *resultNum) | Deactivates the persistent permissions on files or folders.| 17| OH_FileShare_CheckPersistentPermission(const FileShare_PolicyInfo *policies, unsigned int policyNum, bool **result, unsigned int *resultNum) | Checks the persistent permissions on files or folders.| 18| OH_FileShare_ReleasePolicyErrorResult(FileShare_PolicyErrorResult *errorResult, unsigned int resultNum) | Releases the memory allocated for **FileShare_PolicyErrorResult**.| 19 20## Constraints 21 22- Before using the **FileShare** APIs, check that your device has SystemCapability.FileManagement.AppFileService.FolderAuthorization. 23 24- To call **FileShare** APIs, the application must have the ohos.permission.FILE_ACCESS_PERSIST permission. For details about how to request the permission, see [Workflow for Using Permissions](../security/AccessToken/determine-application-mode.md). 25 26## How to Develop 27 28The following example describes how to use the `FileShare` APIs. 29 30**Adding the Dynamic Link Library** 31 32Add the following library to **CMakeLists.txt**. 33 34```txt 35target_link_libraries(sample PUBLIC libohfileshare.so) 36``` 37 38**Including Header Files** 39 40```c++ 41#include <filemanagement/fileshare/oh_file_share.h> 42#include <iostream> 43``` 441. Create a **FileShare_PolicyInfo** instance, and use **OH_FileShare_PersistPermission** to persist the permissions on files based on their URI. The maximum value of **policyNum** is **500**. 45 ```c++ 46 static const uint32_t POLICY_NUM = 2; 47 char strTestPath1[] = "file://com.example.fileshare/data/storage/el2/base/files/test1.txt"; 48 char strTestPath2[] = "file://com.example.fileshare/data/storage/el2/base/files/test2.txt"; 49 FileShare_PolicyInfo policy[POLICY_NUM] = { 50 {strTestPath1, static_cast<unsigned int>(strlen(strTestPath1)), FileShare_OperationMode::READ_MODE}, 51 {strTestPath2, static_cast<unsigned int>(strlen(strTestPath2)), FileShare_OperationMode::WRITE_MODE}}; 52 FileShare_PolicyErrorResult* result = nullptr; 53 uint32_t resultNum = 0; 54 auto ret = OH_FileShare_PersistPermission(policy, POLICY_NUM, &result, &resultNum); 55 if (ret != ERR_OK) { 56 if (ret == ERR_EPERM && result != nullptr) { 57 for(uint32_t i = 0; i < resultNum; i++) { 58 std::cout << "error uri: " << result[i].uri << std::endl; 59 std::cout << "error code: " << result[i].code << std::endl; 60 std::cout << "error message: " << result[i].message << std::endl; 61 } 62 } 63 } 64 OH_FileShare_ReleasePolicyErrorResult(result, resultNum); 65 ``` 662. Call **OH_FileShare_ActivatePermission** to activate the persistent permissions on files. The maximum value of **policyNum** is **500**. 67 ```c++ 68 auto ret = OH_FileShare_ActivatePermission(policy, POLICY_NUM, &result, &resultNum); 69 if (ret != ERR_OK) { 70 if (ret == ERR_EPERM && result != nullptr) { 71 for(uint32_t i = 0; i < resultNum; i++) { 72 std::cout << "error uri: " << result[i].uri << std::endl; 73 std::cout << "error code: " << result[i].code << std::endl; 74 std::cout << "error message: " << result[i].message << std::endl; 75 } 76 } 77 } 78 OH_FileShare_ReleasePolicyErrorResult(result, resultNum); 79 ``` 803. Call **OH_FileShare_DeactivatePermission** to deactivate the persistent permissions on files. The maximum value of **policyNum** is **500**. 81 ```c++ 82 auto ret = OH_FileShare_DeactivatePermission(policy, POLICY_NUM, &result, &resultNum); 83 if (ret != ERR_OK) { 84 if (ret == ERR_EPERM && result != nullptr) { 85 for(uint32_t i = 0; i < resultNum; i++) { 86 std::cout << "error uri: " << result[i].uri << std::endl; 87 std::cout << "error code: " << result[i].code << std::endl; 88 std::cout << "error message: " << result[i].message << std::endl; 89 } 90 } 91 } 92 OH_FileShare_ReleasePolicyErrorResult(result, resultNum); 93 ``` 944. Call **OH_FileShare_RevokePermission** to revoke the persistent permissions from files. The maximum value of **policyNum** is **500**. 95 ```c++ 96 auto ret = OH_FileShare_RevokePermission(policy, POLICY_NUM, &result, &resultNum); 97 if (ret != ERR_OK) { 98 if (ret == ERR_EPERM && result != nullptr) { 99 for(uint32_t i = 0; i < resultNum; i++) { 100 std::cout << "error uri: " << result[i].uri << std::endl; 101 std::cout << "error code: " << result[i].code << std::endl; 102 std::cout << "error message: " << result[i].message << std::endl; 103 } 104 } 105 } 106 OH_FileShare_ReleasePolicyErrorResult(result, resultNum); 107 ``` 1085. Call **OH_FileShare_CheckPersistentPermission** to check the persistent permissions on files. The maximum value of **policyNum** is **500**. 109 ```c++ 110 bool *result = nullptr; 111 auto ret = OH_FileShare_CheckPersistentPermission(policy, POLICY_NUM, &result, &resultNum); 112 if (result != nullptr && resultNum > 0) { 113 for(uint32_t i = 0; i < resultNum && resultNum <= POLICY_NUM; i++) { 114 std::cout << "uri: " << policy[i].uri << std::endl; 115 std::cout << "result: " << result[i] << std::endl; 116 } 117 } 118 std::cout << "retCode: " << ret << std::endl; 119 free(result); 120 ``` 121