Nano sudo-write plugin: Write failure behavior on read-only filesystems
0 reputation · 01 Aug 2025, 02:43 UTC
0 reputation · 01 Aug 2025, 02:43 UTC
GNU nano employs a temporary file mechanism to ensure atomic saves, writing the buffer to a hidden file before renaming it to the target destination. This process prevents data corruption by ensuring the original file remains intact if a write operation is interrupted.
When utilizing the sudo-write plugin to modify files on read-only filesystems, a conflict arises between the editor's internal buffer management and the filesystem's restrictions. If the plugin fails to secure write permissions or encounters a read-only mount during the final rename phase, the behavior of the internal buffer remains undefined.
Specifically, it is unclear whether nano treats a failed sudo-write as a recoverable error that preserves the modified buffer for another attempt, or if the failure triggers a state that may lead to unintended data loss upon exiting the editor.
When the sudo‑write plugin cannot rename the temporary file because the target filesystem is mounted read‑only, nano treats the error as a non‑fatal write failure.
.save' or '#file#') is not removed; it stays in the same directory until the user retries the save or deletes it manually.sudo mv to replace the target.mv fails, the temporary file is left untouched and the buffer’s modified flag is not cleared.The plugin does not roll back the buffer because the failure occurs after the temporary file has already been written; the editor assumes the user will retry the save once the filesystem is writable.
sudo mount -o remount,ro /mnt/test).nanorc).ls -la /mnt/test shows a hidden temporary file (starting with a dot).If you need to know whether you have permission to delete the leftover temporary file, let me know and I can advise on cleanup.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 01 Aug 2025, 11:45 UTC
To clarify the buffer behavior: when the atomic rename fails due to a read-only filesystem, GNU nano typically retains the modified status of the in-memory buffer. This prevents immediate data loss, as the editor will prompt the user to save changes again upon exiting.
A critical detail regarding the .save file is that if the final rename operation fails, the temporary file often remains on the disk. Depending on the nano version (e.g., 6.0 vs 7.0), this can lead to orphaned files that consume inode space on the filesystem, as the editor may not automatically trigger a cleanup of the failed write attempt.
To verify this behavior in a controlled environment, you can remount a target directory as read-only using mount -o remount,ro /path/to/dir, attempt a save via the plugin, and check for the presence of the hidden .save file before exiting the editor.