File Preservation
env-twin preserves important file attributes during backup and restore operations to maintain file integrity and system compatibility. This includes permissions, timestamps, and content formatting.Overview
The file preservation system ensures that restored files maintain:- File permissions - Unix-style permissions (chmod values)
- File timestamps - Access and modification times
- Content formatting - Line endings, encoding, and structure
- Atomic operations - Safe file updates without corruption
File Permissions
How Permissions Are Preserved
On Unix-like systems (Linux, macOS), file permissions are preserved during restore operations (file-restoration.ts:94-96):Using —preserve-permissions Flag
Enable permission preservation explicitly:- Current permissions are detected - Before restore, existing file permissions are read
- Permissions are applied - After writing content, original permissions are restored
- Fallback for new files - Sensitive files get
0o600(read/write for owner only)
Permission preservation is enabled by default on Unix systems and automatically disabled on Windows.
Permission Handling by File Type
Sensitive Files (.env files)
For security, environment files receive restrictive permissions (file-restoration.ts:46-51):.env.env.local.env.production.env.development.env.staging- Any file matching
.env*(except.env.example)
Regular Files
Non-sensitive files use default system permissions:Platform Differences
Windows Permission Handling
Windows Permission Handling
Windows does not support Unix-style file permissions (
chmod values). On Windows:- Permission preservation is automatically skipped (file-restoration.ts:137-139)
- Files use default Windows ACLs
- The
--preserve-permissionsflag is safely ignored
Unix/Linux Permission Handling
Unix/Linux Permission Handling
On Unix-like systems:
- Full
chmodpermission bits are preserved - Includes owner, group, and other permissions
- Special bits (setuid, setgid, sticky) are maintained
0o600=-rw-------(owner read/write)0o644=-rw-r--r--(owner read/write, others read)0o755=-rwxr-xr-x(owner all, others read/execute)
File Timestamps
Timestamp Types
Two timestamps are tracked for each file:- Access time (atime) - Last time file was read
- Modification time (mtime) - Last time file content changed
Using —preserve-timestamps Flag
Maintain original file timestamps during restore:Timestamp preservation is enabled by default to maintain file history and prevent unnecessary rebuilds in build systems.
Why Preserve Timestamps?
- Build system optimization - Tools like webpack, make, and others use modification times to determine if files need rebuilding
- Version control compatibility - Maintain file history consistency
- Audit trails - Preserve when files were last modified for compliance
- Sorting and organization - File managers can show correct modification dates
Timestamp Precision
Timestamps are stored with millisecond precision (rollback-manager.ts:186):Atomic File Operations
env-twin uses atomic file operations to prevent corruption during writes.How Atomic Writes Work
ThewriteAtomic function (atomic-fs.ts:15-63) ensures safe file updates:
- Create temporary file - Write to
.{filename}.{timestamp}.tmp - Write content - Fully write new content to temp file
- Atomic rename - Replace original file in single operation
- Cleanup on error - Remove temp file if any step fails
Benefits of Atomic Operations
✅ No partial writes - File is never in half-written state ✅ Crash safety - Power loss during write won’t corrupt original file ✅ Concurrent read safety - Other processes always see complete file ✅ Automatic cleanup - Temp files removed on errorWindows-Specific Handling
On Windows,rename() can fail if the target exists (atomic-fs.ts:37-50):
Content Formatting Preservation
Line Endings
Files are read and written using UTF-8 encoding with preserved line endings:- Unix/Linux:
\n(LF) - Windows:
\r\n(CRLF) - Legacy Mac:
\r(CR)
Character Encoding
All files are handled as UTF-8:Whitespace and Formatting
All whitespace is preserved exactly:- Leading and trailing whitespace
- Empty lines
- Indentation (spaces and tabs)
- Comments and their formatting
Security Considerations
Symlink Protection
Before writing, env-twin checks for symlinks to prevent overwriting files outside the working directory (file-restoration.ts:269-282):Path Traversal Prevention
All paths are validated to prevent directory traversal (file-restoration.ts:66-77):Secure Permission Defaults
Sensitive files automatically receive restrictive permissions (file-restoration.ts:305-307):--preserve-permissions is not used.
Advanced Options
Combining Preservation Flags
For maximum fidelity during restore:- ✅ Original permissions restored
- ✅ Original timestamps maintained
- ✅ Rollback snapshot created before changes
- ✅ Detailed logging of all operations
Selective Preservation
You can choose which attributes to preserve:Dry Run with Preservation Check
Preview what would be preserved:Best Practices
Use
--preserve-permissions and --preserve-timestamps together for exact file reproduction.-
Production environments:
-
Development environments:
-
CI/CD pipelines:
-
Security-critical environments:
Troubleshooting
Permissions Not Preserved
Issue: File permissions change after restore Solutions:-
Verify you’re on a Unix-like system:
-
Check if
--preserve-permissionsis enabled: -
Verify you have permission to set file modes:
Timestamps Not Preserved
Issue: File modification times change after restore Solutions:-
Enable timestamp preservation:
-
Check filesystem support:
File Corruption During Write
Issue: File becomes corrupted or empty during restore Cause: Atomic write failure or disk full Solution:-
Check disk space:
-
Use
--dry-runfirst: -
Enable rollback for safety:
Related Topics
- Rollback System - Snapshot creation and rollback triggers
- Restore Command - Using restore with preservation flags
- Security Best Practices - Securing environment files