> For the complete documentation index, see [llms.txt](https://cristiandis.gitbook.io/ntix/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cristiandis.gitbook.io/ntix/ntix-documentation/docs-it/api/lock.md).

# LockFile

## LockFile

Impedisce esecuzioni `ntix apply` contemporanee tramite un lock esclusivo sul file.

```rust
use ntix_rs::lock::lock_file::LockFile;
```

### API

```rust
pub struct LockFile {
    lock_stream: Option<File>,
    lock_path: PathBuf,
}

impl LockFile {
    pub fn new(lock_path: Option<PathBuf>, should_lock: bool)
        -> Result<Self, Box<dyn std::error::Error>>;
    pub fn get_default_lock_path() -> Result<PathBuf, Box<dyn std::error::Error>>;
}

impl Drop for LockFile { /* rilascia il lock e rimuove il file */ }
```

| Membro                  | Descrizione                                                                                                                                                                                 |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `new`                   | Acquisisce un lock esclusivo in `lock_path` (o quello predefinito). Restituisce `Err` se è già bloccato da un altro processo. Passa `should_lock = false` per creare un'istanza senza lock. |
| `get_default_lock_path` | Restituisce `%LOCALAPPDATA%/ntix/apply.lock`                                                                                                                                                |
| `Drop`                  | Rilascia il flusso di lock ed elimina il file di lock                                                                                                                                       |

### Comportamento

* Su Windows, apre il file di lock con `CreateFileW` usando una modalità di condivisione zero, quindi una seconda apertura fallisce mentre è mantenuto
* Su host non Windows, usa un'apertura create-and-truncate (consultiva)
* Il contenuto del file di lock contiene `PID@UnixTimestamp`, che aiuta a identificare i lock obsoleti
* Quando il lock fallisce, il messaggio di errore suggerisce di eliminare il file di lock se è obsoleto
