# 5.1 Lock/Unlock File

### 5.1.1 Lock File

The `lockFile` function of `BayunCore` class locks a file with default [BayunEncryptionPolicy](https://bayun.gitbook.io/bayuncoresdk-cpp/bayuncoresdk-operations#encryption-policy) and  [BayunKeyGenerationPolicy](https://bayun.gitbook.io/bayuncoresdk-cpp/bayuncoresdk-operations#key-generation-policy) dictated by server settings.

The function takes the following parameters :

* **sessionId** : Unique SessionId which is received in the registration/login function response.
* **filePath** : Path of the file to be locked.
  * dataType : `std::string`

The file at the given file path is overwritten with the locked file. If file locking fails original file is not changed.

{% tabs %}
{% tab title="C++" %}

```cpp
#include "BayunCore.h"

std::string lockedFilePath = bayunCore->lockFile("<sessionId>", <"filePath">);
```

{% endtab %}
{% endtabs %}

### 5.1.2 Lock File with Encryption Policy, Key Generation Policy

The `lockFile` function with encryption policy, key generation policy as parameters locks file with the encryption key dictated by the policy. The function takes the following parameters :

* **sessionId** : Unique SessionId which is received in the registration/login function response.
* **filePath** : Path of the file to be locked.
* **encryptionPolicy** : [BayunEncryptionPolicy](https://bayun.gitbook.io/bayuncoresdk-cpp/bayuncoresdk-operations/..#encryption-policy) determines the key to be used to generate the lockingKey.
* **keyGenerationPolicy** : [BayunKeyGenerationPolicy](https://bayun.gitbook.io/bayuncoresdk-cpp/bayuncoresdk-operations/..#key-generation-policy) determines the policy to generate the data encryption key.
* **groupId** : GroupId is required if encryptionPolicy is `BayunEncryptionPolicyGroup`.

{% hint style="info" %}
If encryptionPolicy is other than `BayunEncryptionPolicyGroup` then groupId should be nil.
{% endhint %}

{% tabs %}
{% tab title="C++" %}

```cpp
#include "BayunCore.h"

Bayun::BayunEncryptionPolicy encryptionPolicy = Bayun::BayunEncryptionPolicy::Company;
Bayun::BayunKeyGenerationPolicy keyGenerationPolicy = Bayun::BayunKeyGenerationPolicy::Envelop;
std::string filePath = "<file path>";
std::string lockedFilePath = bayunCore->lockFile("<sessionId>", filePath, encryptionPolicy, keyGenerationPolicy, "<groupId>");

```

{% endtab %}
{% endtabs %}

### 5.1.3 Unlock File

The `unlockFile` function of `BayunCore` class unlocks a locked file. The function takes the following parameters :

* **sessionId** : Unique SessionId which is received in the registration/login function response.
* **filePath** : Path of the file to be unlocked.
  * dataType : `std::string`

The file at the given file path is overwritten with the unlocked file.

{% tabs %}
{% tab title="C++" %}

```cpp
#include "BayunCore.h"

std::string unlockedFilePath = bayunCore->unlockFile("<sessionId>","<filePath>");
```

{% endtab %}
{% endtabs %}
