Hi @pepe papo , and thanks for posting your question.
Thank you also for the additional details. Separating the lease and revision logic from crash durability, and stating a clear acceptance requirement, makes it much easier to answer from the documentation rather than from observed behavior.
Summary
Based on the public Microsoft documentation I could find, there is no documented non-administrative API sequence that guarantees the complete file-content and namespace durability you describe, particularly for the newly created workspaces directory. File flushing and MoveFileExW are documented building blocks, but I could not verify that their combination provides that complete guarantee.
1. Supported user-mode directory flush
- The native routine is callable from user mode: the
ZwFlushBuffersFileExpage says user-mode callers should use the nameNtFlushBuffersFileEx. It returnsSTATUS_ACCESS_DENIEDwhen the handle has neither write nor append access, andFLUSH_FLAGS_FILE_DATA_SYNC_ONLYis documented as not valid with directory handles. -
NtCreateFilesays not to specifyFILE_WRITE_DATAorFILE_APPEND_DATA(or generic rights) for a directory. File Access Rights Constants shows that, for a directory, the same values meanFILE_ADD_FILE(0x2) andFILE_ADD_SUBDIRECTORY(0x4). This explains the shared bit values, but does not establish a documented access contract for flushing a directory. - I could not find public Microsoft documentation that specifies the access mask, create options, sharing mode and supported versions for this purpose.
2. Replacement: atomicity versus durability
- Atomicity: Microsoft's guidance on alternatives to Transactional NTFS describes writing the data to a new file and then replacing the original as the common approach when changes must be either completely applied or not applied at all, and names
ReplaceFileas one method. That guidance addresses atomicity, not durability.ReplaceFileWalso documentsREPLACEFILE_WRITE_THROUGHas not supported. - Durability:
MoveFileExWdocumentsMOVEFILE_WRITE_THROUGHas "The function does not return until the file is actually moved on the disk." This is the strongest documented statement for this step. The documentation does not separately define crash or power-loss semantics for the destination name binding, and I could not find an additional supported operation that closes that gap. - Python's
os.replacedoes not expose a write-through option. If you want the documentedMOVEFILE_WRITE_THROUGHbehavior, callMoveFileExWdirectly, for example (sketch):
import ctypes
from ctypes import wintypes
MOVEFILE_REPLACE_EXISTING = 0x1
MOVEFILE_WRITE_THROUGH = 0x8
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
kernel32.MoveFileExW.argtypes = (wintypes.LPCWSTR, wintypes.LPCWSTR, wintypes.DWORD)
kernel32.MoveFileExW.restype = wintypes.BOOL
# tmp_path was written, flushed (os.fsync) and closed before this call
if not kernel32.MoveFileExW(tmp_path, final_path,
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH):
raise ctypes.WinError(ctypes.get_last_error())
3. First-time Profile\workspaces creation
CreateDirectoryW has no write-through option. I could not find a documented non-administrative operation that guarantees persistence of a new directory entry, or documentation stating whether the parent or any ancestors must be flushed. MS-FSA 2.1.5.7 says a flush "ensures that the directory structure is persisted to stable storage", but it does not define that scope for a child entry held by its parent.
4. Storage and failure boundaries
- Windows 10/11, local NTFS: MS-FSA Appendix A (note 80) says that behavior is only implemented by NTFS: "Other file systems return STATUS_SUCCESS and perform no other action."
- Standard user: a volume-wide
FlushFileBuffersrequires administrative privileges, so it is not available to your process. - Filter drivers: I could not find public Microsoft documentation that defines durability guarantees in the presence of third-party filesystem filter drivers.
- Storage cache:
NtFlushBuffersFileExwithFlags = 0asks the underlying storage to flush its cache. The result still depends on the controller and device honoring that request. - OS crash versus power loss: I could not find public Microsoft documentation that defines these two cases separately for namespace changes.
5. Limitation and recovery-oriented design
Within the public documentation, I could not find a non-administrative sequence that provides the requested guarantee. A recovery-oriented design avoids depending on that gap:
- At startup, treat a missing
workspacesdirectory or bookmark file as "not yet initialized" rather than as corruption, and recreate it. - After a restart, validate the bookmark (parse, revision, referenced workspaces) and fall back to the last valid state if it does not validate.
- Because the bookmark is operational state separate from the evidence store, keep it rebuildable from the evidence data where possible.
- For structured data that needs transactional updates, the same Microsoft guidance points to the Extensible Storage Engine (ESE), which is included in Windows.
These are design suggestions, not a Microsoft-confirmed durability guarantee. If you need a formal statement beyond the public documentation for a specific Windows build and storage stack, a Microsoft support request is the right channel for that.
References
- MoveFileExW
- ReplaceFileW
- Alternatives to using Transactional NTFS
- File Access Rights Constants
- MS-FSA Appendix A: Product Behavior
If this instruction is applicable to your situation, I would greatly appreciate it if you could follow the instruction here so others experiencing similar behavior can benefit from it as well. If you have any follow-up questions, feel free to add a comment.