This file is indexed.

/usr/share/ada/adainclude/gnatcoll/gnatcoll-io-remote.adb is in libgnatcoll1.6-dev 1.6gpl2014-9.

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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
------------------------------------------------------------------------------
--                             G N A T C O L L                              --
--                                                                          --
--                     Copyright (C) 2009-2014, AdaCore                     --
--                                                                          --
-- 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 GNAT.Directory_Operations;
with GNAT.Strings;                  use GNAT.Strings;

with GNATCOLL.IO.Native;
with GNATCOLL.IO.Remote.Unix;
with GNATCOLL.IO.Remote.Windows;
with GNATCOLL.Mmap;
with GNATCOLL.Path;                 use GNATCOLL.Path;
with GNATCOLL.Remote;               use GNATCOLL.Remote;
with GNATCOLL.Remote.Db;            use GNATCOLL.Remote.Db;

package body GNATCOLL.IO.Remote is

   procedure Internal_Initialize
     (File : not null access Remote_File_Record'Class;
      Host : String;
      Path : FS_String);
   --  Initialize internal fields according to the file's host

   -------------------------
   -- Internal_Initialize --
   -------------------------

   procedure Internal_Initialize
     (File : not null access Remote_File_Record'Class;
      Host : String;
      Path : FS_String)
   is
      Server : constant Server_Access := Get_Server (Host);
      Last   : Natural := Path'Last;
   begin
      --  Regexps might return file strings with a trailing CR or LF. Let's
      --  remove those before creating the File record.
      while Path (Last) = ASCII.CR or Path (Last) = ASCII.LF loop
         Last := Last - 1;
      end loop;

      File.Server     := Server;
      if File.Tmp_Norm then
         File.Full       := new FS_String'
           (GNATCOLL.Path.Normalize
              (Server.Shell_FS,
               From_Unix (Server.Shell_FS, Path (Path'First .. Last))));
      else
         File.Full       := new FS_String'
           (From_Unix (Server.Shell_FS, Path (Path'First .. Last)));
      end if;

      File.Resolved   := False;
   end Internal_Initialize;

   ------------------------
   -- Ensure_Initialized --
   ------------------------

   procedure Ensure_Initialized
     (File : not null access Remote_File_Record'Class)
   is
   begin
      if File.Server /= null then
         return;

      elsif not Is_Configured (File.Tmp_Host.all) then
         raise Remote_Config_Error with
           "File needs server " & File.Tmp_Host.all &
           " which is not configured";
      end if;

      Internal_Initialize (File, File.Tmp_Host.all, File.Tmp_Path.all);
      Free (File.Tmp_Host);
      Free (File.Tmp_Path);
   end Ensure_Initialized;

   ------------
   -- Create --
   ------------

   function Create
     (Host      : String;
      Path      : FS_String;
      Normalize : Boolean) return File_Access
   is
      Ret  : Remote_File_Access;

   begin
      Ret := new Remote_File_Record'
        (Ref_Count  => 1,
         Tmp_Host   => null,
         Tmp_Path   => null,
         Tmp_Norm   => Normalize,
         Tmp_Name   => (others => ' '),
         Server     => null,
         Full       => null,
         Normalized => null,
         Resolved   => False,
         Kind       => Unknown);

      if not Is_Configured (Host) then
         --  Delayed initialization
         Ret.Tmp_Host := new String'(Host);
         Ret.Tmp_Path := new FS_String'(Path);
      else
         Internal_Initialize (Ret, Host, Path);
      end if;

      return File_Access (Ret);
   end Create;

   -----------------
   -- Current_Dir --
   -----------------

   function Current_Dir (Host : String) return File_Access is
      Server : Server_Access;
   begin
      if not Is_Configured (Host) then
         raise Remote_Config_Error with
           "Invalid FS for host " & Host;
      else
         Server := Get_Server (Host);
      end if;

      case Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return Create
              (Host,
               GNATCOLL.IO.Remote.Unix.Current_Dir (Server),
               False);
         when FS_Windows =>
            return Create
              (Host,
               GNATCOLL.IO.Remote.Windows.Current_Dir (Server),
               False);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & Host;
      end case;
   end Current_Dir;

   --------------
   -- Home_Dir --
   --------------

   function Home_Dir (Host : String) return File_Access is
      Server : Server_Access;
   begin
      if not Is_Configured (Host) then
         raise Remote_Config_Error with
           "Invalid FS for host " & Host;
      else
         Server := Get_Server (Host);
      end if;

      case Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return Create
              (Host,
               GNATCOLL.IO.Remote.Unix.Home_Dir (Server),
               False);
         when FS_Windows =>
            return Create
              (Host,
               GNATCOLL.IO.Remote.Windows.Home_Dir (Server),
               False);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & Host;
      end case;
   end Home_Dir;

   -----------------------
   -- Get_Tmp_Directory --
   -----------------------

   function Get_Tmp_Directory (Host : String) return File_Access is
      Server : Server_Access;
   begin
      if not Is_Configured (Host) then
         raise Remote_Config_Error with
           "Invalid FS for host " & Host;
      else
         Server := Get_Server (Host);
      end if;

      case Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return Create
              (Host, GNATCOLL.IO.Remote.Unix.Tmp_Dir (Server), False);
         when FS_Windows =>
            return Create
              (Host, GNATCOLL.IO.Remote.Windows.Tmp_Dir (Server), False);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & Host;
      end case;
   end Get_Tmp_Directory;

   --------------------
   -- Locate_On_Path --
   --------------------

   function Locate_On_Path (Host : String; Base : FS_String) return File_Access
   is
      Server : Server_Access;
   begin
      if not Is_Configured (Host) then
         raise Remote_Config_Error with
           "Invalid FS for host " & Host;
      else
         Server := Get_Server (Host);
      end if;

      if GNATCOLL.Path.Is_Absolute_Path (Server.Shell_FS, Base) then
         return Create (Host, Base, False);
      end if;

      case Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            declare
               Ret : constant FS_String :=
                       GNATCOLL.IO.Remote.Unix.Locate_On_Path (Server, Base);
            begin
               if Ret = "" then
                  return null;
               else
                  return Create (Host, Ret, False);
               end if;
            end;

         when FS_Windows =>
            declare
               Ret : constant FS_String :=
                       GNATCOLL.IO.Remote.Windows.Locate_On_Path
                         (Server, Base);
            begin
               if Ret = "" then
                  return null;
               else
                  return Create (Host, Ret, False);
               end if;
            end;

         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & Host;
      end case;
   end Locate_On_Path;

   ------------------------
   -- Get_Logical_Drives --
   ------------------------

   function Get_Logical_Drives (Host : String) return File_Array is
      Server : Server_Access;
      List   : GNAT.Strings.String_List_Access;

   begin
      if not Is_Configured (Host) then
         raise Remote_Config_Error with
           "Invalid FS for host " & Host;
      else
         Server := Get_Server (Host);
      end if;

      case Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            List := GNATCOLL.IO.Remote.Unix.Get_Logical_Drives (Server);
         when FS_Windows =>
            List := GNATCOLL.IO.Remote.Windows.Get_Logical_Drives (Server);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & Host;
      end case;

      if List = null then
         return (1 .. 0 => <>);
      end if;

      declare
         Ret : File_Array (1 .. List'Length);
      begin
         for J in Ret'Range loop
            Ret (J) :=
              Create (Host, FS_String (List (List'First + J - Ret'First).all),
                      False);
         end loop;

         GNAT.Strings.Free (List);
         return Ret;
      end;
   end Get_Logical_Drives;

   --------------
   -- Get_Host --
   --------------

   function Get_Host
     (File : not null access Remote_File_Record) return String
   is
   begin
      if File.Server = null then
         if not Is_Configured (File.Tmp_Host.all) then
            return File.Tmp_Host.all;
         else
            Internal_Initialize (File, File.Tmp_Host.all, File.Tmp_Path.all);
            Free (File.Tmp_Host);
            Free (File.Tmp_Path);

            return File.Server.Nickname;
         end if;
      else
         return File.Server.Nickname;
      end if;
   end Get_Host;

   ------------------------
   -- Dispatching_Create --
   ------------------------

   function Dispatching_Create
     (Ref : not null access Remote_File_Record;
      Full_Path : FS_String) return File_Access
   is
   begin
      return Create (Ref.Get_Host, Full_Path, False);
   end Dispatching_Create;

   -------------
   -- To_UTF8 --
   -------------

   function To_UTF8
     (Ref : not null access Remote_File_Record;
      Path : FS_String) return String
   is
      pragma Unreferenced (Ref);
   begin
      return Codec.To_UTF8 (Path);
   end To_UTF8;

   ---------------
   -- From_UTF8 --
   ---------------

   function From_UTF8
     (Ref : not null access Remote_File_Record;
      Path : String) return FS_String
   is
      pragma Unreferenced (Ref);
   begin
      return Codec.From_UTF8 (Path);
   end From_UTF8;

   --------------
   -- Is_Local --
   --------------

   function Is_Local (File : Remote_File_Record) return Boolean is
      pragma Unreferenced (File);
   begin
      return False;
   end Is_Local;

   ------------
   -- Get_FS --
   ------------

   function Get_FS
     (File : not null access Remote_File_Record) return FS_Type
   is
   begin
      Ensure_Initialized (File);

      return File.Server.Shell_FS;
   end Get_FS;

   ----------------------
   -- Resolve_Symlinks --
   ----------------------

   procedure Resolve_Symlinks
     (File : not null access Remote_File_Record)
   is
   begin
      Ensure_Initialized (File);

      --  ??? Should we do something more here (e.g. try to actually resolve ?)
      if not File.Resolved then
         if File.Normalized = null then
            File.Normalized := new FS_String'
              (GNATCOLL.Path.Normalize (Get_FS (File), File.Full.all));
         end if;

         File.Resolved   := True;
      end if;
   end Resolve_Symlinks;

   ---------------------
   -- Is_Regular_File --
   ---------------------

   function Is_Regular_File
     (File : not null access Remote_File_Record) return Boolean
   is
   begin
      Ensure_Initialized (File);

      case File.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return GNATCOLL.IO.Remote.Unix.Is_Regular_File
              (File.Server, File.Full.all);
         when FS_Windows =>
            return GNATCOLL.IO.Remote.Windows.Is_Regular_File
              (File.Server, File.Full.all);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & File.Get_Host;
      end case;
   end Is_Regular_File;

   ----------
   -- Size --
   ----------

   overriding function Size
     (File : not null access Remote_File_Record) return Long_Integer
   is
   begin
      Ensure_Initialized (File);
      case File.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return GNATCOLL.IO.Remote.Unix.Size
              (File.Server, File.Full.all);
         when FS_Windows =>
            return GNATCOLL.IO.Remote.Windows.Size
              (File.Server, File.Full.all);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & File.Get_Host;
      end case;
   end Size;

   ------------------
   -- Is_Directory --
   ------------------

   function Is_Directory
     (File : not null access Remote_File_Record) return Boolean
   is
   begin
      Ensure_Initialized (File);

      case File.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return GNATCOLL.IO.Remote.Unix.Is_Directory
              (File.Server, File.Full.all);
         when FS_Windows =>
            return GNATCOLL.IO.Remote.Windows.Is_Directory
              (File.Server, File.Full.all);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & File.Get_Host;
      end case;
   end Is_Directory;

   ----------------------
   -- Is_Symbolic_Link --
   ----------------------

   function Is_Symbolic_Link
     (File : not null access Remote_File_Record) return Boolean
   is
   begin
      Ensure_Initialized (File);

      case File.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return GNATCOLL.IO.Remote.Unix.Is_Symbolic_Link
              (File.Server, File.Full.all);
         when FS_Windows =>
            return GNATCOLL.IO.Remote.Windows.Is_Symbolic_Link
              (File.Server, File.Full.all);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & File.Get_Host;
      end case;
   end Is_Symbolic_Link;

   ---------------------
   -- File_Time_Stamp --
   ---------------------

   function File_Time_Stamp
     (File : not null access Remote_File_Record) return Ada.Calendar.Time
   is
   begin
      Ensure_Initialized (File);

      case File.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return GNATCOLL.IO.Remote.Unix.File_Time_Stamp
              (File.Server, File.Full.all);
         when FS_Windows =>
            return GNATCOLL.IO.Remote.Windows.File_Time_Stamp
              (File.Server, File.Full.all);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & File.Get_Host;
      end case;
   end File_Time_Stamp;

   -----------------
   -- Is_Writable --
   -----------------

   function Is_Writable
     (File : not null access Remote_File_Record) return Boolean
   is
   begin
      Ensure_Initialized (File);

      case File.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return GNATCOLL.IO.Remote.Unix.Is_Writable
              (File.Server, File.Full.all);
         when FS_Windows =>
            return GNATCOLL.IO.Remote.Windows.Is_Writable
              (File.Server, File.Full.all);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & File.Get_Host;
      end case;
   end Is_Writable;

   ------------------
   -- Set_Writable --
   ------------------

   procedure Set_Writable
     (File  : not null access Remote_File_Record;
      State : Boolean)
   is
   begin
      Ensure_Initialized (File);

      case File.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            GNATCOLL.IO.Remote.Unix.Set_Writable
              (File.Server, File.Full.all,
               State);
         when FS_Windows =>
            GNATCOLL.IO.Remote.Windows.Set_Writable
              (File.Server, File.Full.all,
               State);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & File.Get_Host;
      end case;
   end Set_Writable;

   ------------------
   -- Set_Readable --
   ------------------

   procedure Set_Readable
     (File  : not null access Remote_File_Record;
      State : Boolean)
   is
   begin
      Ensure_Initialized (File);

      case File.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            GNATCOLL.IO.Remote.Unix.Set_Readable
              (File.Server, File.Full.all,
               State);
         when FS_Windows =>
            GNATCOLL.IO.Remote.Windows.Set_Readable
              (File.Server, File.Full.all,
               State);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & File.Get_Host;
      end case;
   end Set_Readable;

   ------------
   -- Rename --
   ------------

   procedure Rename
     (From    : not null access Remote_File_Record;
      Dest    : not null access Remote_File_Record;
      Success : out Boolean)
   is
   begin
      Ensure_Initialized (From);
      Ensure_Initialized (Dest);

      if From.Get_Host /= Dest.Get_Host then
         raise Remote_Config_Error with
           "cannot rename a file to another host";
      end if;

      case From.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            GNATCOLL.IO.Remote.Unix.Rename
              (From.Server, From.Full.all, Dest.Full.all, Success);
         when FS_Windows =>
            GNATCOLL.IO.Remote.Windows.Rename
              (From.Server, From.Full.all, Dest.Full.all, Success);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & From.Get_Host;
      end case;
   end Rename;

   ----------
   -- Copy --
   ----------

   procedure Copy
     (From    : not null access Remote_File_Record;
      Dest    : FS_String;
      Success : out Boolean)
   is
   begin
      Ensure_Initialized (From);

      case From.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            GNATCOLL.IO.Remote.Unix.Copy
              (From.Server, From.Full.all, Dest, Success);
         when FS_Windows =>
            GNATCOLL.IO.Remote.Windows.Copy
              (From.Server, From.Full.all, Dest, Success);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & From.Get_Host;
      end case;
   end Copy;

   ------------
   -- Delete --
   ------------

   procedure Delete
     (File    : not null access Remote_File_Record;
      Success : out Boolean)
   is
   begin
      Ensure_Initialized (File);

      case File.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            GNATCOLL.IO.Remote.Unix.Delete
              (File.Server, File.Full.all,
               Success);
         when FS_Windows =>
            GNATCOLL.IO.Remote.Windows.Delete
              (File.Server, File.Full.all,
               Success);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & File.Get_Host;
      end case;
   end Delete;

   ---------------------
   -- Read_Whole_File --
   ---------------------

   function Read_Whole_File
     (File : not null access Remote_File_Record)
      return GNAT.Strings.String_Access
   is
   begin
      Ensure_Initialized (File);

      case File.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return GNATCOLL.IO.Remote.Unix.Read_Whole_File
              (File.Server, File.Full.all);
         when FS_Windows =>
            return GNATCOLL.IO.Remote.Windows.Read_Whole_File
              (File.Server, File.Full.all);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & File.Get_Host;
      end case;
   end Read_Whole_File;

   ----------------
   -- Open_Write --
   ----------------

   procedure Open_Write
     (File    : not null access Remote_File_Record;
      Append  : Boolean := False;
      FD      : out GNAT.OS_Lib.File_Descriptor)
   is
      Tmp_Dir : File_Access :=
                  GNATCOLL.IO.Native.Get_Tmp_Directory;
      Cur_Dir : File_Access :=
                  GNATCOLL.IO.Native.Current_Dir;
      Tmp     : constant FS_String :=
                  GNATCOLL.Path.Ensure_Directory
                    (Tmp_Dir.Get_FS, Tmp_Dir.Full.all);
      Cur     : constant FS_String :=
                  GNATCOLL.Path.Ensure_Directory
                    (Cur_Dir.Get_FS, Cur_Dir.Full.all);
      Content : GNAT.Strings.String_Access;
      Dead    : Boolean;
      Written : Integer;
      pragma Unreferenced (Dead, Written);

   begin
      Ensure_Initialized (File);
      Unref (Tmp_Dir);
      Unref (Cur_Dir);

      GNAT.Directory_Operations.Change_Dir (String (Tmp));
      GNAT.OS_Lib.Create_Temp_File (FD, File.Tmp_Name);
      GNAT.Directory_Operations.Change_Dir (String (Cur));

      if Append then
         case File.Server.Shell_FS is
            when FS_Unix | FS_Unix_Case_Insensitive =>
               Content := GNATCOLL.IO.Remote.Unix.Read_Whole_File
                 (File.Server, File.Full.all);
            when FS_Windows =>
               Content := GNATCOLL.IO.Remote.Windows.Read_Whole_File
                 (File.Server, File.Full.all);
            when FS_Unknown =>
               raise Remote_Config_Error with
                 "Invalid FS for host " & File.Get_Host;
         end case;

         Written :=
           GNAT.OS_Lib.Write (FD, Content.all'Address, Content'Length);
         GNAT.Strings.Free (Content);
      end if;
   end Open_Write;

   -----------
   -- Close --
   -----------

   procedure Close
     (File    : not null access Remote_File_Record;
      FD      : GNAT.OS_Lib.File_Descriptor;
      Success : out Boolean)
   is
      Content : GNAT.Strings.String_Access;
      Tmp_Dir : File_Access :=
                  GNATCOLL.IO.Native.Get_Tmp_Directory;
      Tmp     : constant FS_String :=
                  GNATCOLL.Path.Ensure_Directory
                    (Tmp_Dir.Get_FS, Tmp_Dir.Full.all);
      Dead    : Boolean;
      pragma Unreferenced (Dead);

   begin
      Unref (Tmp_Dir);
      Ensure_Initialized (File);

      GNAT.OS_Lib.Close (FD);

      Content := GNATCOLL.Mmap.Read_Whole_File
        (String (Tmp) & File.Tmp_Name, Empty_If_Not_Found => True);

      case File.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            Success := GNATCOLL.IO.Remote.Unix.Write_File
              (File.Server, File.Full.all, Content.all);
         when FS_Windows =>
            Success := GNATCOLL.IO.Remote.Windows.Write_File
              (File.Server, File.Full.all, Content.all);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & File.Get_Host;
      end case;

      GNAT.OS_Lib.Delete_File (String (Tmp) & File.Tmp_Name, Dead);
   end Close;

   ----------------
   -- Change_Dir --
   ----------------

   function Change_Dir
     (Dir : not null access Remote_File_Record) return Boolean
   is
   begin
      Ensure_Initialized (Dir);

      case Dir.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return GNATCOLL.IO.Remote.Unix.Change_Dir
              (Dir.Server, Dir.Full.all);
         when FS_Windows =>
            return GNATCOLL.IO.Remote.Windows.Change_Dir
              (Dir.Server, Dir.Full.all);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & Dir.Get_Host;
      end case;
   end Change_Dir;

   --------------
   -- Read_Dir --
   --------------

   function Read_Dir
     (Dir            : not null access Remote_File_Record;
      Dirs_Only      : Boolean := False;
      Files_Only     : Boolean := False) return GNAT.Strings.String_List
   is
   begin
      Ensure_Initialized (Dir);

      case Dir.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return GNATCOLL.IO.Remote.Unix.Read_Dir
              (Dir.Server, Dir.Full.all,
               Dirs_Only, Files_Only);
         when FS_Windows =>
            return GNATCOLL.IO.Remote.Windows.Read_Dir
              (Dir.Server, Dir.Full.all,
               Dirs_Only, Files_Only);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & Dir.Get_Host;
      end case;
   end Read_Dir;

   --------------
   -- Make_Dir --
   --------------

   function Make_Dir
     (Dir       : not null access Remote_File_Record;
      Recursive : Boolean) return Boolean
   is
   begin
      Ensure_Initialized (Dir);

      case Dir.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            return GNATCOLL.IO.Remote.Unix.Make_Dir
              (Dir.Server, Dir.Full.all, Recursive);
         when FS_Windows =>
            return GNATCOLL.IO.Remote.Windows.Make_Dir
              (Dir.Server, Dir.Full.all, Recursive);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & Dir.Get_Host;
      end case;
   end Make_Dir;

   ----------------
   -- Remove_Dir --
   ----------------

   procedure Remove_Dir
     (Dir       : not null access Remote_File_Record;
      Recursive : Boolean;
      Success   : out Boolean)
   is
   begin
      Ensure_Initialized (Dir);

      case Dir.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            GNATCOLL.IO.Remote.Unix.Delete_Dir
              (Dir.Server, Dir.Full.all, Recursive, Success);
         when FS_Windows =>
            GNATCOLL.IO.Remote.Windows.Delete_Dir
              (Dir.Server, Dir.Full.all, Recursive, Success);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & Dir.Get_Host;
      end case;
   end Remove_Dir;

   --------------
   -- Copy_Dir --
   --------------

   procedure Copy_Dir
     (From    : not null access Remote_File_Record;
      Dest    : FS_String;
      Success : out Boolean)
   is
   begin
      Ensure_Initialized (From);

      case From.Server.Shell_FS is
         when FS_Unix | FS_Unix_Case_Insensitive =>
            GNATCOLL.IO.Remote.Unix.Copy_Dir
              (From.Server, From.Full.all, Dest, Success);
         when FS_Windows =>
            GNATCOLL.IO.Remote.Windows.Copy_Dir
              (From.Server, From.Full.all, Dest, Success);
         when FS_Unknown =>
            raise Remote_Config_Error with
              "Invalid FS for host " & From.Get_Host;
      end case;
   end Copy_Dir;

   -----------
   -- Codec --
   -----------

   package body Codec is

      -------------
      -- To_UTF8 --
      -------------

      function To_UTF8 (Path : FS_String) return String is
      begin
         --  ??? What if the Transport uses a specific charset ?
         return String (Path);
      end To_UTF8;

      ---------------
      -- From_UTF8 --
      ---------------

      function From_UTF8 (Path : String) return FS_String is
      begin
         --  ??? What if the Transport uses a specific charset ?
         return FS_String (Path);
      end From_UTF8;

   end Codec;

end GNATCOLL.IO.Remote;