From d332f19e22bf5d6a31d5311d9228de631161e2a9 Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Tue, 2 Feb 2021 19:17:51 +0100 Subject: [PATCH] Fix problem with temporaries in Windows (#674) * Attempt fix at windows temporaries * Remove now superfluous exception handler * Review fix --- src/alire/alire-directories.adb | 30 ++++++++++++++------------- src/alire/alire-platform.ads | 21 ++++++++++++++++++- src/alire/os_linux/alire-platform.adb | 1 + 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/alire/alire-directories.adb b/src/alire/alire-directories.adb index ced67f2b..238afdfa 100644 --- a/src/alire/alire-directories.adb +++ b/src/alire/alire-directories.adb @@ -3,6 +3,8 @@ with Ada.Numerics.Discrete_Random; with Ada.Text_IO; with Ada.Unchecked_Deallocation; +with Alire.OS_Lib.Subprocess; +with Alire.Platform; with Alire.Properties; with Alire.Roots; @@ -349,6 +351,20 @@ package body Alire.Directories is return; end if; + -- Force writability of folder when in Windows, as some tools (e.g. git) + -- that create read-only files will cause a Use_Error + + if Kind (This.Filename) = Directory and then Platform.On_Windows then + Trace.Debug ("Forcing writability of temporary dir " & This.Filename); + OS_Lib.Subprocess.Checked_Spawn + ("attrib", + Utils.Empty_Vector + .Append ("-R") -- Remove read-only + .Append ("/D") -- On dirs + .Append ("/S") -- Recursively + .Append (This.Filename & "\*")); + end if; + if Exists (This.Filename) then if Kind (This.Filename) = Ordinary_File then Trace.Debug ("Deleting temporary file " & This.Filename & "..."); @@ -359,20 +375,6 @@ package body Alire.Directories is end if; end if; exception - when E : Use_Error => - -- For unclear reasons, Windows has trouble removing a folder that - -- contains a git repository. Warn and continue until we know more. - - -- The actual error, as reported by Python3: - -- PermissionError: [WinError 5] Access is denied: - -- 'alr-fwrt.tmp\\.git\\objects\\1d\\' - -- '696bed4ef917b1adbdef18723016987ed62e41' - - -- Since we are running tests as root, this might be that the file is - -- still open, but by whom? - - Log_Exception (E); - Trace.Warning ("Could not delete temporary: " & This.Filename); when E : others => Log_Exception (E); raise; diff --git a/src/alire/alire-platform.ads b/src/alire/alire-platform.ads index 3390ce4d..c476381b 100644 --- a/src/alire/alire-platform.ads +++ b/src/alire/alire-platform.ads @@ -1,6 +1,8 @@ with Alire.Platforms; with Alire.Environment; +private with GNATCOLL.OS.Constants; + package Alire.Platform is -- Alr.Platforms will be progressively migrated in here as needed. @@ -34,13 +36,30 @@ package Alire.Platform is -- via config. function Distribution_Is_Known return Boolean is - (Platforms."/=" (Distribution, Platforms.Distro_Unknown)); + (Platforms."/=" (Distribution, Platforms.Distro_Unknown)); + + function On_Windows return Boolean; + -- Say if we are on Windows, until the OS detection is moved here from + -- Alr.Platform. private + ------------------ + -- Distribution -- + ------------------ + function Distribution return Platforms.Distributions is (if Disable_Distribution_Detection then Platforms.Distro_Unknown else Detected_Distribution); + ---------------- + -- On_Windows -- + ---------------- + + pragma Warnings (Off, "condition is always"); -- Silence warning of OS check + function On_Windows return Boolean + is (GNATCOLL.OS.Constants.OS in GNATCOLL.OS.Windows); + pragma Warnings (On); + end Alire.Platform; diff --git a/src/alire/os_linux/alire-platform.adb b/src/alire/os_linux/alire-platform.adb index 360eb5e6..065565f3 100644 --- a/src/alire/os_linux/alire-platform.adb +++ b/src/alire/os_linux/alire-platform.adb @@ -1,5 +1,6 @@ with Alire.OS_Lib.Subprocess; with Alire.Utils; + with GNAT.Regpat; package body Alire.Platform is -- 2.39.5