File Sync with Conflict Detection
Implement a manifest-based file synchronization system with conflict detection. Track file states via JSON, compare timestamps with tolerance, and detect when files were modified by others during offline work.
The Problem
Engineers cache files locally, edit them offline, then need to sync back to the server. The challenge: what if someone else modified the same file on the server while you were working offline?
Without conflict detection, you get silent overwrites — the last person to save wins, and the other's work is lost. This is especially dangerous with SolidWorks assemblies where multiple engineers edit referenced parts.
Architecture: The Manifest Approach
Instead of a database, store a JSON manifest alongside the cached files:
{
"version": 1,
"files": {
"C:/Server/Part1.SLDPRT": {
"originalLastModified": "2026-03-10T14:30:00",
"originalSize": 245760,
"isLibraryFile": false
}
}
}The manifest records the original state of each file at the time of download. This enables three-way comparison.
Change Detection Algorithm
For each cached file, compare three states:
| State | Source | Meaning |
|---|---|---|
| Manifest original | JSON file | What the file looked like when downloaded |
| Current local | Local disk | What the file looks like now (after user edits) |
| Current server | Server disk | What the file looks like on the server now |
Decision Matrix
| Local Changed? | Server Changed? | Action |
|---|---|---|
| No | No | Skip — file is unchanged |
| Yes | No | Safe to upload — only local edits |
| No | Yes | Safe to download — only server edits |
| Yes | Yes | **Conflict** — both sides modified |
Timestamp Comparison with Tolerance
Detecting change requires comparing timestamps — but file system precision varies:
bool HasChanged(FileInfo current, DateTime originalTime,
long originalSize)
{
// Size change is definitive
if (current.Length != originalSize) return true;
// Timestamp with 2-second tolerance
var diff = Math.Abs(
(current.LastWriteTime - originalTime).TotalSeconds);
return diff > 2;
}The 2-second tolerance handles FAT32 drives, USB sticks, and network shares where timestamps are rounded.
Resolution Strategies
When a conflict is detected, your tool should:
- Skip library files — read-only shared files (toolbox components) should never be modified locally
- Prompt the user — show both timestamps and sizes, let them choose which version to keep
- Keep both — rename the local copy with a
.localsuffix so neither version is lost
Manifest Best Practices
- Version field: Include a
versionnumber so future code can handle format changes - Relative paths: Store paths relative to the cache root so the manifest is portable
- Atomic writes: Write the manifest to a temp file, then rename — avoids corruption if the process crashes mid-write
- Exclude patterns: Skip temp files (
~$*,*.tmp,.swp) that SolidWorks creates during editing