Fix: FreeCAD Saves Over Read-Only Files On Windows/Linux

by Alex Johnson 57 views

Introduction

This article delves into a critical issue within FreeCAD, specifically how it handles files marked as read-only on Windows or write-protected on Linux/Windows systems. The core problem is that users can currently open a FreeCAD document file with these restrictions, make changes, and surprisingly, save the file without encountering an error. This behavior can lead to unintended modifications of files that should ideally remain unchanged. We will explore the problem, the background, proposed fixes, and alternative solutions to ensure FreeCAD behaves as expected when dealing with read-only files. Our main keyword here is FreeCAD read-only file issue, which is central to the problem we are addressing.

Understanding the Problem: Why FreeCAD Overwrites Read-Only Files

The Issue: The fundamental issue lies in FreeCAD's ability to save changes to files even when they are marked as read-only (on Windows) or lack write permissions (on Linux/Windows). Ideally, the expected behavior is that when a user attempts to save a modified read-only file, FreeCAD should display an error message, preventing the save operation from proceeding.

Expected Behavior: Users should be able to open files marked as read-only or write-protected, make modifications, but encounter an error message when trying to save their changes back to disk. This error message serves as a safeguard, alerting users that the file's permissions prevent saving, thus preserving the file's original state. This expected behavior is crucial for maintaining data integrity and preventing accidental overwrites.

Background Explanation: FreeCAD's unique backup file management system contributes to this issue. When a document is saved (unless backup policy is disabled), the original *.FcStd file remains untouched. Instead, FreeCAD creates temporary files and renames them to the original file's name. This process requires write permissions within the directory, not necessarily on the file itself. This design choice, while offering certain advantages, bypasses the typical read-only protection mechanisms. For workflows that rely on marking files as read-only at the OS level to prevent unintended changes, this behavior poses a significant challenge. Version control systems, such as SVN repositories utilizing the auto-prop svn:needs-lock feature, heavily depend on this read-only flag to manage file modifications.

Proposed Fixes for the Read-Only File Issue in FreeCAD

To address this issue, two primary fixes have been proposed. These solutions aim to ensure FreeCAD respects file permissions and prevents accidental overwrites of read-only files. Let's examine each proposed fix in detail.

1. Proactive Write Protection Check

This approach involves implementing a check at the beginning of the save process to determine if the file has write-blocking attributes. If the file is identified as write-protected, the user will receive a warning message, prompting them to use the "Save As" feature instead. This method offers a proactive way to prevent accidental overwrites by informing the user upfront about the file's read-only status.

Implementation Steps:

  1. Determine the Canonical Path: The first step is to determine the "canonical_path" of the file. This path represents the absolute and unique location of the file within the file system.
  2. Create a FileInfo Class: Next, a FileInfo class is instantiated using the canonical path. This class provides methods to access various file attributes, including its writability status.
  3. Check File Existence and Writability: The code then checks if the file exists and whether it is writable using the isWritable() method of the FileInfo class. If the file exists but is not writable, a Base::FileException() is thrown. This exception halts the save process and displays an error message to the user.
  4. Display Error Message: The error message should clearly indicate that the file is read-only and cannot be saved. It should also suggest using the "Save As" feature to save the modified file under a different name or location.

Code Snippet:

Base::FileInfo originalFileInfo(nativePath);
if (originalFileInfo.exists() && !originalFileInfo.isWritable()) {
    throw Base::FileException("Unable to save document because file is marked as read-only or write permission is not available.", originalFileInfo);
}

2. Enhanced FileInfo::isWritable() Method

This fix involves updating the FileInfo::isWritable() method to include an additional check for the Windows "read-only" file attribute. This ensures that FreeCAD accurately detects the read-only status of files on Windows systems. This enhancement provides a more comprehensive check for file writability, addressing the specific behavior of Windows file attributes.

Implementation Steps:

  1. Check Windows Read-Only Attribute: Within the FileInfo::isWritable() method, after the existing conditional checks, add a new check specifically for the Windows read-only file attribute.
  2. Use GetFileAttributes() Function: The GetFileAttributes() function (from <windows.h>) is used to retrieve the file attributes.
  3. Handle INVALID_FILE_ATTRIBUTES: If GetFileAttributes() returns INVALID_FILE_ATTRIBUTES, it indicates an error (e.g., network file issue). In such cases, the method should return false, indicating the file is not writable.
  4. Check FILE_ATTRIBUTE_READONLY Flag: If the FILE_ATTRIBUTE_READONLY flag is set in the attributes, the method should return false, indicating the file is read-only.

Code Snippet:

#ifdef FC_OS_WIN32
//requires import of <windows.h>
DWORD attributes = GetFileAttributes(FileName);
if (attributes == INVALID_FILE_ATTRIBUTES){
    //Log the error? 
    //usually indicates some kind of network file issue, so the file is probably not writable
    return false;
}
if ((attributes & FILE_ATTRIBUTE_READONLY) != 0)
{
    return false;
}
#endif

Alternative Solutions Considered for FreeCAD Read-Only Issue

While the proposed fixes offer direct solutions, alternative approaches were also considered. These alternatives provide different perspectives on addressing the issue, each with its own set of advantages and disadvantages. Let's explore these alternative solutions.

1. Saving an FcBak File

This solution involves saving an FcBak (backup) file when attempting to save over a read-only file. The intention is to create a backup of the file before the save operation fails, potentially allowing the user to recover their work. However, this approach has been deemed less desirable due to its implications.

Drawbacks:

  • Implies Recoverable Error: Saving an FcBak file suggests that the failure to save is an error that the user might want to recover from. However, in the context of a read-only file, the intended behavior is to prevent saving altogether, as the user should be aware of the file's protection.
  • Misleading User Expectation: Creating a backup file might mislead users into thinking that they can still save the changes, even though the file is intentionally protected. This can lead to confusion and potentially compromise the intended workflow.

2. Implementing Protections in the BackupPolicy Class

This alternative suggests placing the write protection checks directly within the BackupPolicy class, where the file overwriting actually occurs. This approach focuses on preventing the overwrite operation at the point where it is executed.

Considerations:

  • Targeted Protection: Implementing protections within the BackupPolicy class ensures that the checks are performed precisely when the file is about to be overwritten. This targeted approach can be more efficient and less prone to errors.
  • Centralized Logic: Centralizing the protection logic in the BackupPolicy class can simplify maintenance and ensure consistency in how read-only files are handled throughout FreeCAD.

Detailed Information about the User's System and FreeCAD Version

To provide a comprehensive understanding of the issue and the environment in which it occurs, it's essential to include detailed information about the user's system and the FreeCAD version being used. This information aids in replicating the issue, identifying potential conflicts, and ensuring the proposed solutions are compatible.

The following system and FreeCAD version details are provided:

  • Operating System: Windows 11 build 26100
  • Architecture: x86_64 (64-bit)
  • FreeCAD Version: 1.2.0dev.20251118 (Git shallow)
  • Build Date: 2025/11/18 21:54:51
  • Build Type: Release
  • Branch: main
  • Hash: be7d41dea75f7f2966d7b431d24f7060c6907339
  • Python Version: 3.11.14
  • Qt Version: 6.8.3
  • Coin Version: 4.0.3
  • Vtk Version: 9.3.1
  • Boost Version: 1_86
  • Eigen3 Version: 3.4.0
  • PySide Version: 6.8.3
  • Shiboken Version: 6.8.3
  • Xerces-c Version: 3.3.0
  • IfcOpenShell Version: 0.8.2
  • OCC Version: 7.8.1
  • Locale: English/United States (en_US)
  • Navigation Style/Orbit Style/Rotation Mode: CAD/Trackball/Drag at cursor
  • Stylesheet/Theme/QtStyle: FreeCAD Light.qss/FreeCAD Light/
  • Logical DPI/Physical DPI/Pixel Ratio: 96/101.521/1
  • Installed Mods:
    • AddonManager 2025.11.4
    • fasteners 0.5.43
    • Render 2024.12.15

This detailed information helps developers and other users understand the specific environment in which the issue was observed, facilitating effective troubleshooting and resolution.

Affected Subprojects and Additional Contextual Information

Subproject(s) Affected?

Currently, this issue does not appear to directly affect any specific subprojects within FreeCAD. The problem primarily concerns the core application's handling of file permissions and the save process.

Anything Else?

To provide further context and resources related to this issue, the following information is included:

These additional resources offer a deeper understanding of the issue and the ongoing efforts to improve file handling in FreeCAD.

Conclusion: Ensuring Data Integrity in FreeCAD

In conclusion, the issue of FreeCAD saving over read-only files is a significant concern that needs to be addressed to ensure data integrity and prevent unintended modifications. The proposed fixes, including the proactive write protection check and the enhanced FileInfo::isWritable() method, offer viable solutions to this problem. By implementing these fixes, FreeCAD can better respect file permissions and provide users with a more reliable and predictable experience.

While alternative solutions were considered, the direct fixes appear to be the most effective in addressing the core issue. The detailed system and version information, along with the additional context provided, will aid in the successful implementation of these fixes.

Further Resources: For more information on FreeCAD and its development, you can visit the official FreeCAD website and documentation at FreeCAD Official Website.

This article has explored the intricacies of FreeCAD's handling of read-only files, providing a comprehensive understanding of the issue and the steps being taken to resolve it. By addressing this issue, FreeCAD can continue to improve its reliability and usability for users worldwide.