awa_unit_2.4.0_59135a52/awa/plugins/awa-storages/src/awa-storages-stores-files.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
-----------------------------------------------------------------------
--  awa-storages-stores-files -- File system store
--  Copyright (C) 2012, 2015, 2016, 2018, 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Streams;
with Ada.Directories;
with Interfaces;

with Util.Files;
with Util.Log.Loggers;
with Util.Encoders;
with Util.Encoders.Base64;
package body AWA.Storages.Stores.Files is

   Log : constant Util.Log.Loggers.Logger := Util.Log.Loggers.Create ("AWA.Storages.Stores.Files");

   --  ------------------------------
   --  Create a file storage service and use the <tt>Root</tt> directory to store the files.
   --  ------------------------------
   function Create_File_Store (Root : in String) return Store_Access is
      Result : constant File_Store_Access := new File_Store '(Len => Root'Length,
                                                              Root => Root);
   begin
      return Result.all'Access;
   end Create_File_Store;

   --  ------------------------------
   --  Build a path where the file store represented by <tt>Store</tt> is saved.
   --  ------------------------------
   function Get_Path (Storage : in File_Store;
                      Store   : in AWA.Storages.Models.Storage_Ref'Class) return String is
   begin
      return Storage.Get_Path (Store.Get_Workspace.Get_Id, Store.Get_Id);
   end Get_Path;

   --  ------------------------------
   --  Build a path where the file store represented by <tt>Store</tt> is saved.
   --  ------------------------------
   function Get_Path (Storage      : in File_Store;
                      Workspace_Id : in ADO.Identifier;
                      File_Id      : in ADO.Identifier) return String is
      use Interfaces;
      use type Ada.Streams.Stream_Element_Offset;

      T      : Util.Encoders.Base64.Encoder;
      Buffer : Ada.Streams.Stream_Element_Array (1 .. 10);
      R      : Ada.Streams.Stream_Element_Array (1 .. 32);
      Last   : Ada.Streams.Stream_Element_Offset;
      Encoded : Ada.Streams.Stream_Element_Offset;
      Pos  : Positive := 1;
      Res : String (1 .. 16 + 5);
   begin
      Util.Encoders.Encode_LEB128 (Buffer, Buffer'First, Unsigned_64 (Workspace_Id), Last);
      Util.Encoders.Encode_LEB128 (Buffer, Last, Unsigned_64 (File_Id), Last);

      T.Transform (Data    => Buffer (1 .. Last),
                   Into    => R, Last => Last,
                   Encoded => Encoded);

      for I in 1 .. Last loop
         Res (Pos) := Character'Val (R (I));
         Pos := Pos + 1;
         if (I mod 2) = 0 and I /= Last then
            Res (Pos) := '/';
            Pos := Pos + 1;
         end if;
      end loop;
      return Util.Files.Compose (Storage.Root, Res (1 .. Pos - 1));
   end Get_Path;

   --  ------------------------------
   --  Save the file represented by the `Path` variable into a store and associate that
   --  content with the storage reference represented by `Into`.
   --  ------------------------------
   overriding
   procedure Save (Storage : in File_Store;
                   Session : in out ADO.Sessions.Master_Session;
                   Into    : in out AWA.Storages.Models.Storage_Ref'Class;
                   Path    : in String) is
      pragma Unreferenced (Session);

      Store : constant String := Storage.Get_Path (Into);
      Dir   : constant String := Ada.Directories.Containing_Directory (Store);
   begin
      Log.Info ("Storage save {0} to {1}", Path, Store);
      Ada.Directories.Create_Path (Dir);
      Ada.Directories.Copy_File (Source_Name => Path,
                                 Target_Name => Store,
                                 Form        => "all");
   end Save;

   overriding
   procedure Load (Storage : in File_Store;
                   Session : in out ADO.Sessions.Session'Class;
                   From    : in AWA.Storages.Models.Storage_Ref'Class;
                   Into    : in out Storage_File) is
      pragma Unreferenced (Session);

      Store : constant String := Storage.Get_Path (From);
   begin
      Into.Path := Ada.Strings.Unbounded.To_Unbounded_String (Store);
   end Load;

   --  ------------------------------
   --  Create a storage
   --  ------------------------------
   overriding
   procedure Create (Storage : in File_Store;
                     Session : in out ADO.Sessions.Master_Session;
                     From    : in AWA.Storages.Models.Storage_Ref'Class;
                     Into    : in out AWA.Storages.Storage_File) is
      pragma Unreferenced (Session);

      Store : constant String := Storage.Get_Path (From);
      Dir   : constant String := Ada.Directories.Containing_Directory (Store);
   begin
      Log.Info ("Storage create {0}", Store);
      Ada.Directories.Create_Path (Dir);
      Into.Path := Ada.Strings.Unbounded.To_Unbounded_String (Store);
   end Create;

   --  ------------------------------
   --  Delete the content associate with the storage represented by `From`.
   --  ------------------------------
   overriding
   procedure Delete (Storage : in File_Store;
                     Session : in out ADO.Sessions.Master_Session;
                     From    : in out AWA.Storages.Models.Storage_Ref'Class) is
      pragma Unreferenced (Session);

      Store : constant String := Storage.Get_Path (From);
   begin
      if Ada.Directories.Exists (Store) then
         Log.Info ("Storage delete {0}", Store);
         Ada.Directories.Delete_File (Store);
      end if;
   end Delete;

end AWA.Storages.Stores.Files;