Skip to content

Commit

Permalink
Store backed up files inside alire folder
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Sep 1, 2020
1 parent 4927a00 commit 1ecbaec
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 54 deletions.
37 changes: 19 additions & 18 deletions src/alire/alire-directories.adb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ package body Alire.Directories is
-- Backup_If_Existing --
------------------------

procedure Backup_If_Existing (File : Any_Path) is
procedure Backup_If_Existing (File : Any_Path;
Base_Dir : Any_Path := "")
is
use Ada.Directories;
Dst : constant String := (if Base_Dir /= ""
then Base_Dir / Simple_Name (File) & ".prev"
else File & ".prev");
begin
if Exists (File) then
Trace.Debug ("Backing up " & File);
Copy_File (File, File & ".prev", "mode=overwrite");
Trace.Debug ("Backing up " & File
& " with base dir: " & Base_Dir);
Copy_File (File, Dst, "mode=overwrite");
end if;
end Backup_If_Existing;

Expand Down Expand Up @@ -286,7 +292,6 @@ package body Alire.Directories is
overriding
procedure Finalize (This : in out Temp_File) is
use Ada.Directories;
use Ada.Exceptions;
begin
if This.Keep then
return;
Expand All @@ -301,12 +306,6 @@ package body Alire.Directories is
Delete_Tree (This.Filename);
end if;
end if;
exception
when E : others =>
Trace.Debug
("Temp_File.Finalize: unexpected exception: " &
Exception_Name (E) & ": " & Exception_Message (E) & " -- " &
Exception_Information (E));
end Finalize;

-------------------
Expand Down Expand Up @@ -369,14 +368,17 @@ package body Alire.Directories is
-- New_Replacement --
---------------------

function New_Replacement (File : Any_Path;
Backup : Boolean := True)
function New_Replacement (File : Any_Path;
Backup : Boolean := True;
Backup_Dir : Any_Path := "")
return Replacer is
begin
return This : constant Replacer := (Length => File'Length,
Original => File,
Backup => Backup,
Temp_Copy => <>)
return This : constant Replacer := (Length => File'Length,
Backup_Len => Backup_Dir'Length,
Original => File,
Backup => Backup,
Backup_Dir => Backup_Dir,
Temp_Copy => <>)
do
Ada.Directories.Copy_File (File, This.Temp_Copy.Filename);
end return;
Expand All @@ -387,12 +389,11 @@ package body Alire.Directories is
-------------

procedure Replace (This : in out Replacer) is
Backup : constant Any_Path := This.Original & ".prev";
begin
-- Copy around, so never ceases to be a valid manifest in place

if This.Backup then
Ada.Directories.Copy_File (This.Original, Backup);
Backup_If_Existing (This.Original, This.Backup_Dir);
end if;
Ada.Directories.Copy_File (This.Editable_Name, This.Original);

Expand Down
22 changes: 13 additions & 9 deletions src/alire/alire-directories.ads
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ package Alire.Directories is
function "/" (L, R : String) return String renames Directories."/";
end Operators;

procedure Backup_If_Existing (File : Any_Path);
-- If File exists, move to file.prev
procedure Backup_If_Existing (File : Any_Path;
Base_Dir : Any_Path := "");
-- If File exists, copy to file.prev. If Base_Dir /= "", it is instead
-- copied to Base_Dir / Simple_Name (file) & ".prev"

procedure Copy (Src_Folder,
Dst_Parent_Folder : String;
Expand Down Expand Up @@ -105,12 +107,13 @@ package Alire.Directories is
-- modified and can be tested as the client sees fit. 3) If the new file is
-- proper, the old one is renamed to .prev and the new one takes its place.

function New_Replacement (File : Any_Path;
Backup : Boolean := True)
function New_Replacement (File : Any_Path;
Backup : Boolean := True;
Backup_Dir : Any_Path := "")
return Replacer;
-- Receives a file to be modified, and prepares a copy in a temporary. If
-- Backup, once the replacement is performed, the original file is kept as
-- ".prev".
-- ".prev". Backup_Dir works as in Alire.Directories.Backup_If_Existing

function Editable_Name (This : Replacer) return Any_Path;
-- Obtain the editable copy
Expand Down Expand Up @@ -151,10 +154,11 @@ private
overriding
procedure Finalize (This : in out Temp_File);

type Replacer (Length : Positive) is tagged limited record
Original : Any_Path (1 .. Length);
Temp_Copy : Temp_File;
Backup : Boolean := True;
type Replacer (Length, Backup_Len : Natural) is tagged limited record
Original : Any_Path (1 .. Length);
Temp_Copy : Temp_File;
Backup : Boolean := True;
Backup_Dir : Any_Path (1 .. Backup_Len);
end record;

end Alire.Directories;
11 changes: 9 additions & 2 deletions src/alire/alire-manifest.adb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ with Ada.Text_IO; use Ada.Text_IO;

with Alire.Directories;
with Alire.Errors;
with Alire.Paths;
with Alire.Releases;
with Alire.TOML_Keys;
with Alire.Utils.Text_Files;
Expand All @@ -17,7 +18,10 @@ package body Alire.Manifest is
procedure Append (Name : Any_Path;
Deps : Dependencies.Containers.List) is
Replacer : constant Directories.Replacer :=
Directories.New_Replacement (Name);
Directories.New_Replacement
(Name,
Backup => True,
Backup_Dir => Paths.Working_Folder_Inside_Root);
File : File_Type;
begin
if Deps.Is_Empty then
Expand Down Expand Up @@ -235,7 +239,10 @@ package body Alire.Manifest is
end Remove;

Replacer : constant Directories.Replacer :=
Directories.New_Replacement (Name);
Directories.New_Replacement
(Name,
Backup => True,
Backup_Dir => Paths.Working_Folder_Inside_Root);
begin
if Deps.Is_Empty then
return;
Expand Down
20 changes: 13 additions & 7 deletions src/alire/alire-utils-text_files.adb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package body Alire.Utils.Text_Files is

declare
Replacer : Directories.Replacer :=
Directories.New_Replacement (This.Name, This.Backup);
Directories.New_Replacement (This.Name,
This.Backup,
This.Backup_Dir);
begin
Open (File, Out_File, Replacer.Editable_Name);
for Line of This.Lines loop
Expand All @@ -41,17 +43,21 @@ package body Alire.Utils.Text_Files is
-- Load --
----------

function Load (From : Any_Path;
Backup : Boolean := True)
function Load (From : Any_Path;
Backup : Boolean := True;
Backup_Dir : Any_Path := "")
return File
is
F : File_Type;
begin
return This : File := (Ada.Finalization.Limited_Controlled with
Length => From'Length,
Name => From,
Backup => Backup,
others => <>)
Length => From'Length,
Backup_Len => Backup_Dir'Length,
Name => From,
Backup => Backup,
Backup_Dir => Backup_Dir,
Lines => <>,
Orig => <>)
do
Open (F, In_File, From);
while not End_Of_File (F) loop
Expand Down
20 changes: 12 additions & 8 deletions src/alire/alire-utils-text_files.ads
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ package Alire.Utils.Text_Files is

type File (<>) is tagged limited private;

function Load (From : Any_Path;
Backup : Boolean := True)
function Load (From : Any_Path;
Backup : Boolean := True;
Backup_Dir : Any_Path := "")
return File;
-- Load a text file into memory. If Backup, when saving takes place the
-- original is renamed to ".prev".
-- original is renamed to ".prev". Backup_Dir optionally designates where
-- the backup file will be moved.

function Lines (This : aliased in out File) return access String_Vector;

private

type File (Length : Natural) is new Ada.Finalization.Limited_Controlled
type File (Length, Backup_Len : Natural) is
new Ada.Finalization.Limited_Controlled
with record
Name : Any_Path (1 .. Length);
Lines : aliased String_Vector; -- The final contents
Orig : String_Vector; -- The original contents
Backup : Boolean := True;
Name : Any_Path (1 .. Length);
Lines : aliased String_Vector; -- The final contents
Orig : String_Vector; -- The original contents
Backup : Boolean := True;
Backup_Dir : Any_Path (1 .. Backup_Len);
end record;

overriding
Expand Down
19 changes: 14 additions & 5 deletions src/alire/alire-workspace.adb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ with Alire.Lockfiles;
with Alire.Manifest;
with Alire.Origins.Deployers;
with Alire.OS_Lib;
with Alire.Paths;
with Alire.Properties.Actions.Executor;
with Alire.Roots;
with Alire.Solutions.Diffs;
Expand Down Expand Up @@ -206,15 +207,21 @@ package body Alire.Workspace is
Working_Dir : Guard (Enter (Release.Unique_Folder))
with Unreferenced;
begin
Ada.Directories.Create_Path (Paths.Working_Folder_Inside_Root);

if GNAT.OS_Lib.Is_Regular_File (Roots.Crate_File_Name) then
Trace.Debug ("Backing up bundled manifest file as *.upstream");
declare
Upstream_File : constant String :=
Roots.Crate_File_Name & ".upstream";
Paths.Working_Folder_Inside_Root /
(Roots.Crate_File_Name & ".upstream");
begin
Alire.Directories.Backup_If_Existing (Upstream_File);
Ada.Directories.Rename (Old_Name => Roots.Crate_File_Name,
New_Name => Upstream_File);
Alire.Directories.Backup_If_Existing
(Upstream_File,
Base_Dir => Paths.Working_Folder_Inside_Root);
Ada.Directories.Rename
(Old_Name => Roots.Crate_File_Name,
New_Name => Upstream_File);
end;
end if;
end;
Expand Down Expand Up @@ -266,7 +273,9 @@ package body Alire.Workspace is
& Release.Milestone.Image & " with"
& Release.Dependencies.Leaf_Count'Img & " dependencies");

Directories.Backup_If_Existing (Root.Crate_File);
Directories.Backup_If_Existing
(Root.Crate_File,
Base_Dir => Paths.Working_Folder_Inside_Root);

Release.To_File (Root.Crate_File, Manifest.Local);
end Generate_Manifest;
Expand Down
5 changes: 4 additions & 1 deletion src/alr/alr-commands.adb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ with Alire.Config;
with Alire.Errors;
with Alire.Features.Index;
with Alire.Lockfiles;
with Alire.Paths;
with Alire.Platforms;
with Alire.Roots.Optional;
with Alire.Solutions;
Expand Down Expand Up @@ -447,7 +448,9 @@ package body Alr.Commands is
& " Internal data is going to be updated and, as a result,"
& " any existing pins will be unpinned and will need to be"
& " manually recreated.");
Alire.Directories.Backup_If_Existing (Checked.Lock_File);
Alire.Directories.Backup_If_Existing
(Checked.Lock_File,
Base_Dir => Alire.Paths.Working_Folder_Inside_Root);
Ada.Directories.Delete_File (Checked.Lock_File);

when Lockfiles.Missing =>
Expand Down
6 changes: 4 additions & 2 deletions src/alr/alr-utils-auto_gpr_with.adb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ with Ada.Directories;

with Alire.Directories;
with Alire.Utils.User_Input;
with Alire.Config;
with Alire.Config.Edit;
with Alire.Paths;

package body Alr.Utils.Auto_GPR_With is

Expand Down Expand Up @@ -118,7 +118,9 @@ package body Alr.Utils.Auto_GPR_With is
Close (In_File);
Close (Out_File);

Alire.Directories.Backup_If_Existing (GPR_File);
Alire.Directories.Backup_If_Existing
(GPR_File,
Base_Dir => Alire.Paths.Working_Folder_Inside_Root);
Ada.Directories.Copy_File (Tmp.Filename, GPR_File);
end Update;

Expand Down
6 changes: 4 additions & 2 deletions testsuite/tests/get/backup-user-manifest/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
run_alr('get', 'crate')
chdir('crate_1.0.0_filesystem')

upstream = path.join('alire', 'alire.toml.upstream')

# Verify that the manifest has been properly renamed
assert path.isfile('alire.toml.upstream'), "Expected backup file missing"
assert path.isfile(upstream), "Expected backup file missing"

# Verify that contents are as expected in the generated and backed up manifests
assert "badproperty" not in content_of("alire.toml"), \
"Unexpected contents present in manifest file"
assert "badproperty" in content_of("alire.toml.upstream"), \
assert "badproperty" in content_of(upstream), \
"Unexpected contents missing in upstream manifest file"

print('SUCCESS')

0 comments on commit 1ecbaec

Please sign in to comment.