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