This file is indexed.

/usr/share/ada/adainclude/gpr/gpr-compilation-sync.adb is in libgpr1-dev 2017-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
------------------------------------------------------------------------------
--                                                                          --
--                           GPR PROJECT MANAGER                            --
--                                                                          --
--          Copyright (C) 2014-2017, Free Software Foundation, Inc.         --
--                                                                          --
-- This library is free software;  you can redistribute it and/or modify it --
-- under terms of the  GNU General Public License  as published by the Free --
-- Software  Foundation;  either version 3,  or (at your  option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE.                            --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Calendar.Time_Zones; use Ada.Calendar;
with Ada.Containers.Vectors;
with Ada.Directories;         use Ada.Directories;
with Ada.Exceptions;          use Ada.Exceptions;
with Ada.Strings.Unbounded;   use Ada.Strings.Unbounded;
with Ada.Text_IO;             use Ada.Text_IO;

with GNAT.Regexp;  use GNAT.Regexp;
with GNAT.Sockets; use GNAT.Sockets;

with GPR.Util; use GPR.Util;

package body GPR.Compilation.Sync is

   use Ada;
   use type Containers.Count_Type;

   Common_Excluded_Patterns : Str_Vect.Vector;
   --  Default excluded patterns to use when in excluded mode as opposed to
   --  include mode where we describe the patterns to include specifically.

   Artifact_Excluded_Patterns : Str_Vect.Vector;
   --  Artifacts patterns to exclude

   Max_Gpr_Sync : constant := 10;
   --  The number of parallele synchronization done for the gpr protocol. This
   --  is currenlty fixed to 6 but could probable be a parameter. The number is
   --  high, as these tasks are mostly doing IO and so are not CPU demanding,
   --  the goal is to saturate the network bandwidth.

   --  Data for each synchronization job for the Gpr protocol

   type Gpr_Data is record
      Channel           : Protocol.Communication_Channel;
      Root_Dir          : Unbounded_String;
      Excluded_Patterns : Str_Vect.Vector;
      Included_Patterns : Str_Vect.Vector;
   end record;

   package Gpr_Data_Set is new Containers.Vectors (Positive, Gpr_Data);

   --  Queue of job to be done for the gpr protocol

   protected Gpr_Queue is

      procedure Add (Job : Gpr_Data);
      --  Add a new synchronization job

      entry Get
        (Job   : out Gpr_Data;
         Files : out File_Data_Set.Vector;
         Stop  : out Boolean);
      --  Get a synchronization job with the corresponding files, Stop is set
      --  to True if there is no more job to handle and False otherwise.

      procedure No_More_Job;

   private
      procedure Set_Project_Files (Job : Gpr_Data);
      --  Set the project files to be synchronized

      Jobs           : Gpr_Data_Set.Vector;
      Project_Files  : File_Data_Set.Vector;
      PF_Initialized : Boolean := False;
      No_More        : Boolean := False;
   end Gpr_Queue;

   --  Synchronization job are handled by the Gpr_Sync tasks

   task type Gpr_Sync is
      entry Stop;
   end Gpr_Sync;

   type Gpr_Sync_Tasks is array (1 .. Max_Gpr_Sync) of Gpr_Sync;
   type Sync_Tasks_Ref is access all Gpr_Sync_Tasks;

   Sync_Tasks : Sync_Tasks_Ref;
   --  Only allocated (and so started) if a some slaves are using the gpr
   --  protocol. Otherwise this variable will stay null.

   ---------------
   -- Gpr_Queue --
   ---------------

   protected body Gpr_Queue is

      ---------
      -- Add --
      ---------

      procedure Add (Job : Gpr_Data) is
      begin
         Jobs.Append (Job);
      end Add;

      ---------
      -- Get --
      ---------

      entry Get
        (Job   : out Gpr_Data;
         Files : out File_Data_Set.Vector;
         Stop  : out Boolean) when Jobs.Length > 0 or No_More is
      begin
         if Jobs.Length = 0 and then No_More then
            Stop := True;

         else
            Stop := False;
            Job := Jobs.First_Element;
            Jobs.Delete_First;

            if not PF_Initialized then
               Set_Project_Files (Job);
            end if;

            Files := Project_Files;
         end if;
      end Get;

      -----------------
      -- No_More_Job --
      -----------------

      procedure No_More_Job is
      begin
         No_More := True;
      end No_More_Job;

      -----------------------
      -- Set_Project_Files --
      -----------------------

      procedure Set_Project_Files (Job : Gpr_Data) is

         Root_Dir : constant String :=
                      (if Job.Root_Dir = Null_Unbounded_String
                       then "." else To_String (Job.Root_Dir));

         type Regexp_Set is array (Containers.Count_Type range <>) of Regexp;

         I_Regexp : Regexp_Set (1 .. Job.Included_Patterns.Length);
         E_Regexp : Regexp_Set (1 .. Job.Excluded_Patterns.Length);

         procedure Process (Prefix : String);

         -------------
         -- Process --
         -------------

         procedure Process (Prefix : String) is

            procedure Check (File : Directory_Entry_Type);
            --  Check and add this file if it passes the filters

            -----------
            -- Check --
            -----------

            procedure Check (File : Directory_Entry_Type) is
               use GNAT;

               function Match
                 (Name : String; R_Set : Regexp_Set)
                  return Boolean with Inline;
               --  Returns true if Name is matched by one of the regexp in
               --  R_Set.

               -----------
               -- Match --
               -----------

               function Match
                 (Name : String; R_Set : Regexp_Set) return Boolean is
               begin
                  for Regexp of R_Set loop
                     if Match (Name, Regexp) then
                        return True;
                     end if;
                  end loop;
                  return False;
               end Match;

               S_Name     : constant String := Simple_Name (File);
               Entry_Name : constant String := Prefix & S_Name;
               Is_File    : Boolean;

            begin
               if Kind (File) = Directory then
                  if S_Name not in "." | ".."
                    and then (I_Regexp'Length /= 0
                              or else not Match (S_Name, E_Regexp))
                    and then not Is_Symbolic_Link
                      (Root_Dir & Directory_Separator & Entry_Name)
                  then
                     Process (Entry_Name & Directory_Separator);
                  end if;

               else
                  if I_Regexp'Length = 0 then
                     if Match (S_Name, E_Regexp) then
                        Is_File := False;
                     else
                        Is_File := True;
                     end if;

                  else
                     if Match (S_Name, I_Regexp) then
                        Is_File := True;
                     else
                        Is_File := False;
                     end if;
                  end if;

                  if Is_File then
                     Project_Files.Append
                       (File_Data'
                          (To_Unbounded_String (Entry_Name),
                           To_Time_Stamp
                             (Modification_Time (File) -
                                Duration (Time_Zones.UTC_Time_Offset) * 60.0),
                           OS_Lib.Is_Executable_File
                             (Root_Dir & Directory_Separator & Entry_Name)));
                  end if;
               end if;
            end Check;

         begin
            Search
              (Directory =>
                 Root_Dir
                 & (if Prefix = "" then "" else Directory_Separator & Prefix),
               Pattern   => "*",
               Filter    => (Special_File => False, others => True),
               Process   => Check'Access);
         end Process;

      begin
         --  Compile the patterns

         declare
            K : Containers.Count_Type := 1;
         begin
            for P of Job.Included_Patterns loop
               I_Regexp (K) := Compile (P, Glob => True);
               K := K + 1;
            end loop;

            K := 1;

            for P of Job.Excluded_Patterns loop
               E_Regexp (K) := Compile (P, Glob => True);
               K := K + 1;
            end loop;
         end;

         --  Check the files under the project root

         Process (Prefix => "");

         PF_Initialized := True;
      end Set_Project_Files;

   end Gpr_Queue;

   --------------
   -- Gpr_Sync --
   --------------

   task body Gpr_Sync is
      Job         : Gpr_Data;
      Files       : File_Data_Set.Vector;
      No_More_Job : Boolean;
   begin
      For_Slave : loop
         --  Get a new job and the associated files if any

         Gpr_Queue.Get (Job, Files, No_More_Job);

         exit For_Slave when No_More_Job;

         declare
            Chunk_Size : constant := 250;
            --  This constant controls the number of files sent with the sync
            --  command. Doing one at a time is really time consumming as
            --  we have for every file and send and a receive command on
            --  the socket.

            F_List     : File_Data_Set.Vector;
            Count      : Natural := 0;

         begin
            --  Synchronize each file in the list we got

            for F of Files loop
               if Count = Chunk_Size then
                  Protocol.Sync_Files
                    (Job.Channel, To_String (Job.Root_Dir), F_List);

                  F_List.Clear;
                  Count := 0;
               end if;

               F_List.Append (F);
               Count := Count + 1;
            end loop;

            --  Then send the last chunk if any

            if Count /= 0 then
               Protocol.Sync_Files
                 (Job.Channel, To_String (Job.Root_Dir), F_List);
            end if;

            Protocol.Send_End_Of_File_List (Job.Channel);
         end;
      end loop For_Slave;

      accept Stop;

   exception
      when Socket_Error =>
         accept Stop;
      when E : others =>
         Put_Line (Exception_Information (E));
         OS_Exit (1);
   end Gpr_Sync;

   -------------------
   -- Receive_Files --
   -------------------

   function Receive_Files
     (Channel           : Protocol.Communication_Channel;
      Root_Dir          : String;
      Total_File        : out Natural;
      Total_Transferred : out Natural;
      Remote_Files      : out Files.Set;
      Is_Debug          : Boolean;
      Display           : access procedure (Message : String))
      return Protocol.Command_Kind
   is
      use GPR.Compilation.Protocol;
   begin
      Total_File := 0;
      Total_Transferred := 0;

      loop
         declare
            Cmd     : constant Command := Get_Command (Channel);
            To_Sync : File_Data_Set.Vector;
            K       : Positive := 1;
            Message : Unbounded_String;
         begin
            if Is_Debug then
               Message := To_Unbounded_String
                 ("command: " & Command_Kind'Image (Kind (Cmd)));

               if Args (Cmd) /= null then
                  for K in Args (Cmd)'Range loop
                     Append (Message, ", " & Args (Cmd) (K).all);
                  end loop;
               end if;

               Display (To_String (Message));
            end if;

            if Kind (Cmd) = TS then
               --  Check all files in the argument of the command. This is a
               --  list of couple (filename and time stamp).

               Check_All_Files : loop
                  Total_File := Total_File + 1;

                  declare
                     Path_Name     : constant String := Args (Cmd) (K).all;
                     Full_Path     : constant String :=
                                       Root_Dir & Directory_Separator
                                       & Path_Name;
                     TS            : constant Time_Stamp_Type :=
                                       Time_Stamp_Type
                                         (Args (Cmd) (K + 1).all);
                     Is_Executable : constant Boolean :=
                                       Boolean'Value (Args (Cmd) (K + 2).all);
                     File_Stamp    : Time_Stamp_Type;
                     Exists        : Boolean;
                  begin
                     if Ada.Directories.Exists (Full_Path) then
                        File_Stamp :=
                          To_Time_Stamp
                            (Modification_Time (Full_Path)
                             - Duration (Time_Zones.UTC_Time_Offset) * 60.0);
                        Exists := True;
                     else
                        Exists := False;
                     end if;

                     Remote_Files.Insert (Full_Path);

                     if not Exists or else File_Stamp /= TS then
                        To_Sync.Append
                          (File_Data'
                             (To_Unbounded_String (Path_Name),
                              TS, Is_Executable));
                     end if;
                  end;

                  K := K + 3;
                  exit Check_All_Files when K > Args (Cmd)'Length;
               end loop Check_All_Files;

               --  If all files are up-to-data

               if To_Sync.Length = 0 then
                  Send_Ok (Channel);

               else
                  --  Some files are to be synchronized, send the list of
                  --  names back to the master.

                  Send_Ko (Channel, To_Sync);

                  --  We then receive the files contents in the same order

                  Get_RAW_Data : declare
                     Max : constant String :=
                             Containers.Count_Type'Image (To_Sync.Length);
                     N   : Natural := 0;
                  begin
                     for W of To_Sync loop
                        declare
                           Full_Path : constant String :=
                                         Root_Dir & Directory_Separator
                                         & To_String (W.Path_Name);
                        begin
                           Create_Path (Containing_Directory (Full_Path));

                           Get_RAW_File_Content
                             (Channel, Full_Path, W.Timestamp);

                           --  And mark file executable if needed

                           if W.Is_Executable then
                              GNAT.OS_Lib.Set_Executable (Full_Path);
                           end if;

                        exception
                           when others =>
                              Display
                                ("failed to create file: " & Full_Path);
                              return Protocol.SI;
                        end;

                        N := N + 1;

                        if N mod 100 = 0 then
                           Display
                             ("File transfered"
                              & Natural'Image (N) & "/" & Max);
                        end if;
                     end loop;
                  end Get_RAW_Data;

                  Total_Transferred :=
                    Total_Transferred + Natural (To_Sync.Length);
               end if;

            else
               return Kind (Cmd);
            end if;
         end;
      end loop;
   end Receive_Files;

   ----------------
   -- Send_Files --
   ----------------

   procedure Send_Files
     (Channel           : Protocol.Communication_Channel;
      Root_Dir          : String;
      Excluded_Patterns : Str_Vect.Vector;
      Included_Patterns : Str_Vect.Vector;
      Mode              : Direction)
   is
      use type Str_Vect.Vector;
   begin
      --  Starts the tasks if not already done

      if Sync_Tasks = null then
         Sync_Tasks := new Gpr_Sync_Tasks;
      end if;

      Gpr_Queue.Add
        (Gpr_Data'
           (Channel,
            To_Unbounded_String (Root_Dir),
            Excluded_Patterns & Common_Excluded_Patterns
            & (if Mode = To_Slave
               then Artifact_Excluded_Patterns
               else Str_Vect.Empty_Vector),
            Included_Patterns));
   end Send_Files;

   ----------
   -- Wait --
   ----------

   procedure Wait is
   begin
      Gpr_Queue.No_More_Job;

      if Sync_Tasks /= null then
         for T of Sync_Tasks.all loop
            if not T'Terminated then
               T.Stop;
            end if;
         end loop;
      end if;
   end Wait;

begin
   Common_Excluded_Patterns.Append (".git");
   Common_Excluded_Patterns.Append (".svn");
   Common_Excluded_Patterns.Append (".hg");
   Common_Excluded_Patterns.Append ("CVS");
   Common_Excluded_Patterns.Append ("gnatinspect.db*");
   Common_Excluded_Patterns.Append ("GNAT-TEMP*.TMP");
   Common_Excluded_Patterns.Append ("*.lexch");

   Artifact_Excluded_Patterns.Append ("*.o");
   Artifact_Excluded_Patterns.Append ("*.obj");
   Artifact_Excluded_Patterns.Append ("*.ali");
   Artifact_Excluded_Patterns.Append ("*.dll");
   Artifact_Excluded_Patterns.Append ("*.so");
   Artifact_Excluded_Patterns.Append ("*.so.*");
   Artifact_Excluded_Patterns.Append ("*.exe");
end GPR.Compilation.Sync;