> For the complete documentation index, see [llms.txt](https://bayun.gitbook.io/bayuncoresdk-android/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bayun.gitbook.io/bayuncoresdk-android/5.-bayuncoresdk-methods/5.1-file-locking-unlocking.md).

# 6.1 Lock/Unlock File

### 6.1.1 Lock File

T he `lockFile` method of `BayunCore` class locks a file with default `encryption-policy` and `key-generation-policy` dictated by server settings.

* **Parameters**
  * **filePath** : Path of the file to be locked.
  * **success** : Success block to be executed after file is successfully locked.
  * **failure** : Failure block to be executed if locking fails, returns `BayunError`.

{% hint style="info" %}
The file at the given file path is overwritten with the locked file. If file locking fails original file is not changed.
{% endhint %}

{% tabs %}
{% tab title="Java" %}

```java
String fileURL = file.getAbsolutePath();

// Success Callback
Handler.Callback success = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        Log.d(TAG, "File locked successfully.");
        return false;
    }
}
// Failure Callback
Handler.Callback failure = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        String error = message.getData().getString("BayunError", "");
        Log.d(TAG, "Error locking file.");
        return false;
    }
};

bayunCore.lockFile(fileURL, success, failure);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val fileURL = file.getAbsolutePath()

// Success Callback
val success = Handler.Callback {
    Log.d(TAG, "File locked successfully.")
    false
}

// Failure Callback
val failure = Handler.Callback {
    val error = it.data.getString("BayunError", "")
    Log.d(TAG, "Error locking file.")
    false
}

bayunCore.lockFile(fileURL, success, failure)
```

{% endtab %}
{% endtabs %}

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

The `lockFile` method with encryption policy, key generation policy as parameters locks file with the encryption key dictated by the policy.

* **Parameters**
  * **filePath** : Path of the file to be locked.
  * **encryptionPolicy** : [BayunEncryptionPolicy](https://docs.bayunsystems.com/bayuncoresdk-android/5.-bayuncoresdk-methods#encryption-policy) determines the key for locking.
  * **keyGenerationPolicy** : [BayunKeyGenerationPolicy](https://docs.bayunsystems.com/bayuncoresdk-android/5.-bayuncoresdk-methods#key-generation-policy) determines the policy to generate the data encryption key.
  * **groupId** : GroupId is required if encryptionPolicy is `BayunEncryptionPolicyGroup`.
  * **success** : Success block to be executed after file is successfully locked.
  * **failure** : Failure block to be executed if locking fails, returns `BayunError`.

{% tabs %}
{% tab title="Java" %}

```java
String fileURL = file.getAbsolutePath();
int encryptionPolicy = BayunCore.ENCRYPTION_POLICY_COMPANY;
int keyGenerationPolicy = BayunCore.KEY_GENERATION_POLICY_ENVELOPE;
String groupId = "<groupId>";

// Success Callback
Handler.Callback success = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        Log.d(TAG, "File locked successfully.");
        return false;
    }
}
// Failure Callback
Handler.Callback failure = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        String error = message.getData().getString("BayunError", "");
        Log.d(TAG, "Error locking file.");
        return false;
    }
};

bayunCore.lockFile(fileURL, encryptionPolicy, keyGenerationPolicy, groupId, success, failure);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val fileURL = file.getAbsolutePath()
val encryptionPolicy = BayunCore.ENCRYPTION_POLICY_COMPANY
val keyGenerationPolicy = BayunCore.KEY_GENERATION_POLICY_ENVELOPE
val groupId = "<groupId>"

// Callbacks for locking a file
val success = Handler.Callback {
    Log.d(TAG, "File locked successfully.")
    false
}

val failure = Handler.Callback {
    val error = it.data.getString("BayunError", "")
    Log.d(TAG, "Error locking file.")
    }
    false
}

bayunCore.lockFile(fileURL, encryptionPolicy, keyGenerationPolicy, groupId, success, failure)
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
If encryption-policy is other than `BayunEncryptionPolicyGroup` then groupId should be **null**.
{% endhint %}

### 6.1.3 Unlock File

The `unlockFile` method of `BayunCore` class unlocks a locked file.

* **Parameters**
  * **filePath** : Path of the file to be unlocked.
  * **success** : Success block to be executed after file is successfully unlocked.
  * **failure** : Failure block to be executed if unlocking fails, returns `BayunError`.

{% tabs %}
{% tab title="Java" %}

```java
String fileURL = file.getAbsolutePath();

bayunCore.unlockFile(fileURL, success, failure);

// Success Callback
Handler.Callback success = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        Log.d(TAG, "File unlocked successfully.");
        return false;
    }
}
// Failure Callback
Handler.Callback failure = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        String error = message.getData().getString("BayunError", "");
        Log.d(TAG, "Error unlocking file.");
        }
        return false;
    }
};

bayunCore.unlockFile(fileURL, success, failure);

```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val fileURL = file.getAbsolutePath()

// Success Callback
val success = Handler.Callback {
    Log.d(TAG, "File unlocked successfully.")
    false
}

// Failure Callback
val failure = Handler.Callback {
    val error = it.data.getString("BayunError", "")
    Log.d(TAG, "Error unlocking file.")
    false
}

bayunCore.unlockFile(fileURL, success, failure)
```

{% endtab %}
{% endtabs %}
