# mdasar2927/linux-file-io-systems-locking architecture

> Architecture diagram of the GitHub repository mdasar2927/linux-file-io-systems-locking, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mdasar2927/linux-file-io-systems-locking
- Repository: https://github.com/mdasar2927/linux-file-io-systems-locking

## Overview

#### Purpose and Entry Points
This is a small Linux/POSIX C systems-programming exercise with two standalone command-line programs:
- `filecopy.c`: copies one file to another.
- `lock.c`: demonstrates advisory file locking and lock inspection.

Each source file defines its own `main`; there is no shared library, build system, service, or application server.

#### File Copy CLI
`filecopy.c` accepts exactly `<source_file> <destination_file>`. It validates arguments, opens the source read-only, and creates/truncates the destination with owner read/write permissions.

Core boundary: filesystem paths supplied by the caller become POSIX file descriptors via `open`.

Paths:
- `filecopy.c`

#### Copy Data Path
`filecopy.c` performs buffered sequential copying:
1. `read` obtains up to 1024 bytes from the source descriptor.
2. `write` sends that byte count to the destination descriptor.
3. The loop ends at EOF; descriptors are closed afterward.

Failures opening, reading, or writing are reported with `perror` and terminate the process. Destination truncation occurs before the copy loop, so a later read/write failure can leave a partial destination file.

Paths:
- `filecopy.c`

#### Locking CLI
`lock.c` accepts `<filename>`, opens it write-only, and operates on its descriptor using Linux `flock`.

It first acquires a shared advisory lock (`LOCK_SH`), waits, then attempts a non-blocking upgrade to an exclusive lock (`LOCK_EX | LOCK_NB`). It later unlocks with `LOCK_UN` and closes the descriptor.

Paths:
- `lock.c`

#### Lock State and Contention Boundary
The locking flow is intentionally process-coordination oriented rather than data mutation oriented. Lock acquisition depends on other processes holding compatible `flock` locks on the same underlying file. Failed exclusive upgrade releases the shared lock before exiting.

The program opens the target without `O_CREAT`; the file must already exist and be writable by the executing user.

Paths:
- `lock.c`

#### External Runtime and Observability
Both programs target a Linux/POSIX userspace and rely on libc plus kernel file-descriptor APIs (`open`, `read`, `write`, `close`, `flock`). `lock.c` additionally invokes the external Linux `lslocks` command through `system("lslocks")` after each lock-state transition, exposing kernel-visible lock state to stdout.

Paths:
- `lock.c`
- `filecopy.c`

Main entry points: [`lock.c`](https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/lock.c) (Advisory lock flow), [`filecopy.c`](https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/filecopy.c) (filecopy main)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_copy["File Copy CLI"]
  node_filecopy["filecopy main<br/>POSIX CLI<br/>[filecopy.c]"]
  node_copy_open["Open source and destination<br/>FD boundary<br/>[filecopy.c]"]
  node_copy_loop["Buffered copy loop<br/>read/write loop<br/>[filecopy.c]"]
  node_copy_close["Close descriptors<br/>cleanup<br/>[filecopy.c]"]
end

subgraph group_locking["Locking CLI"]
  node_lock["lock main<br/>POSIX CLI<br/>[lock.c]"]
  node_lock_open["Open target<br/>FD boundary<br/>[lock.c]"]
  node_lock_flow["Advisory lock flow<br/>flock state machine<br/>[lock.c]"]
  node_lock_inspect["lslocks inspection<br/>external command<br/>[lock.c]"]
end

node_caller(("Command-line caller<br/>user input"))
node_filesystem[("Linux filesystem<br/>persistent storage")]
node_posix{{"libc and Linux kernel APIs<br/>runtime boundary"}}
node_contenders(("Other locking processes<br/>external processes"))

node_caller -->|"source and destination arguments"| node_filecopy
node_caller -->|"filename argument"| node_lock
node_filecopy -->|"validated paths"| node_copy_open
node_copy_open -->|"open source; create/truncate destination"| node_filesystem
node_copy_open -->|"file descriptors"| node_copy_loop
node_copy_loop -->|"sequential file data"| node_filesystem
node_copy_loop -->|"EOF or failure"| node_copy_close
node_lock -->|"target path"| node_lock_open
node_lock_open -->|"open existing writable file"| node_filesystem
node_lock_open -->|"target descriptor"| node_lock_flow
node_lock_flow -->|"advisory flock state"| node_filesystem
node_contenders -.->|"lock contention"| node_lock_flow
node_lock_flow -->|"after each state transition"| node_lock_inspect
node_lock_inspect -->|"system&#40;&quot;lslocks&quot;&#41;"| node_posix
node_filecopy -->|"POSIX descriptor calls"| node_posix
node_lock -->|"flock and descriptor calls"| node_posix

click node_filecopy "https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/filecopy.c"
click node_copy_open "https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/filecopy.c"
click node_copy_loop "https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/filecopy.c"
click node_copy_close "https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/filecopy.c"
click node_lock "https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/lock.c"
click node_lock_open "https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/lock.c"
click node_lock_flow "https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/lock.c"
click node_lock_inspect "https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/lock.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_filecopy,node_copy_open,node_copy_loop,node_copy_close toneBlue
class node_lock,node_lock_open,node_lock_flow,node_lock_inspect toneAmber
class node_caller,node_filesystem,node_posix,node_contenders toneNeutral
```

## Components

### File Copy CLI

- [filecopy main](https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/filecopy.c): POSIX CLI · Validates source and destination paths. · `filecopy.c`
- [Open source and destination](https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/filecopy.c): FD boundary · Creates or truncates the destination before copying. · `filecopy.c`
- [Buffered copy loop](https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/filecopy.c): read/write loop · Reads up to 1024 bytes and writes each chunk. · `filecopy.c`
- [Close descriptors](https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/filecopy.c): cleanup · `filecopy.c`

### Locking CLI

- [lock main](https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/lock.c): POSIX CLI · Accepts one existing writable target file. · `lock.c`
- [Open target](https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/lock.c): FD boundary · Opens write-only without creating the file. · `lock.c`
- [Advisory lock flow](https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/lock.c): flock state machine · Shared lock, nonblocking exclusive upgrade, then unlock. · `lock.c`
- [lslocks inspection](https://github.com/mdasar2927/linux-file-io-systems-locking/blob/main/lock.c): external command · Prints kernel-visible lock state after transitions. · `lock.c`

### Other components

- **Command-line caller**: user input
- **Linux filesystem**: persistent storage · Maps paths to underlying files and descriptors.
- **libc and Linux kernel APIs**: runtime boundary · Provides open, read, write, close, and flock.
- **Other locking processes**: external processes · Compatible or conflicting flock holders determine acquisition.

## Connections

- Command-line caller → filecopy main: source and destination arguments
- Command-line caller → lock main: filename argument
- filecopy main → Open source and destination: validated paths
- Open source and destination → Linux filesystem: open source; create/truncate destination
- Open source and destination → Buffered copy loop: file descriptors
- Buffered copy loop → Linux filesystem: sequential file data
- Buffered copy loop → Close descriptors: EOF or failure
- lock main → Open target: target path
- Open target → Linux filesystem: open existing writable file
- Open target → Advisory lock flow: target descriptor
- Advisory lock flow → Linux filesystem: advisory flock state
- Other locking processes → Advisory lock flow: lock contention
- Advisory lock flow → lslocks inspection: after each state transition
- lslocks inspection → libc and Linux kernel APIs: system("lslocks")
- filecopy main → libc and Linux kernel APIs: POSIX descriptor calls
- lock main → libc and Linux kernel APIs: flock and descriptor calls
