File Type
This document describes the design of the file resource type for managing files and directories.
Overview
The file resource manages files and directories with three aspects:
- Existence: Whether the file/directory exists or is absent
- Content: The contents of a file (from inline content or source file)
- Attributes: Owner, group, and permissions
Provider Interface
File providers must implement the FileProvider interface:
Method Responsibilities
| Method | Responsibility |
|---|---|
Status | Query current file state (existence, type, content hash, attributes) |
Store | Create or update a file with content and attributes |
CreateDirectory | Create a directory with attributes |
Status Response
The Status method returns a FileState containing:
The Ensure field in CommonResourceState is set to:
presentif a regular file existsdirectoryif a directory existsabsentif the path does not exist
Available Providers
| Provider | Platform | Documentation |
|---|---|---|
posix | Unix/Linux | Posix |
Ensure States
| Value | Description |
|---|---|
present | Path must be a regular file with specified content |
absent | Path must not exist |
directory | Path must be a directory |
Content Sources
Files can receive content from two mutually exclusive sources:
| Property | Description |
|---|---|
contents | Inline string content (template-resolved) |
source | Path to local file to copy from |
When using source, the path is relative to the manifest’s working directory if one is set.
Required Properties
Unlike some resources, file resources require explicit attributes:
| Property | Required | Description |
|---|---|---|
owner | Yes | Username that owns the file |
group | Yes | Group that owns the file |
mode | Yes | Permissions in octal notation |
This prevents accidental creation of files with default or inherited permissions.
Apply Logic
Idempotency
The file resource checks multiple attributes for idempotency:
State Checks (in order)
- Ensure match: Current type matches desired (
present/absent/directory) - Content match: SHA256 checksum of contents matches (for
ensure: present) - Owner match: Current owner matches desired
- Group match: Current group matches desired
- Mode match: Current permissions match desired
Decision Table
| Desired | Current State | Action |
|---|---|---|
absent | absent | None |
absent | present/directory | Remove |
directory | directory + matching attrs | None |
directory | absent/present | CreateDirectory |
directory | directory + wrong attrs | CreateDirectory (updates attrs) |
present | present + matching all | None |
present | absent | Store |
present | present + wrong content | Store |
present | present + wrong attrs | Store |
Content Comparison
Content is compared using SHA256 checksums:
| Source | Checksum Method |
|---|---|
contents property | Sha256HashBytes([]byte(contents)) |
source property | Sha256HashFile(adjustedPath) |
| Existing file | Sha256HashFile(filePath) |
Mode Validation
File modes are validated during resource creation:
Valid Formats:
"0644"- Standard octal"644"- Without leading zero"0o755"- With0oprefix"0O700"- With0Oprefix
Validation Rules:
- Must be valid octal number (digits 0-7)
- Must be β€
0777(no setuid/setgid/sticky via mode)
Path Validation
File paths must be:
- Absolute (start with
/) - Clean (no
.or..components,filepath.Clean(path) == path)
Working Directory
When a manifest has a working directory (e.g., extracted from an archive), the source property is resolved relative to it:
This allows manifests bundled with their source files to use relative paths.
Noop Mode
In noop mode, the file type:
- Queries current state normally
- Computes content checksums
- Logs what actions would be taken
- Sets appropriate
NoopMessage:- “Would have created the file”
- “Would have created directory”
- “Would have removed the file”
- Reports
Changed: trueif changes would occur - Does not call provider Store/CreateDirectory methods
- Does not remove files
Desired State Validation
After applying changes (in non-noop mode), the type verifies the file reached the desired state by calling Status() again and checking all attributes match. If validation fails, ErrDesiredStateFailed is returned.