TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Classic View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Topic: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Greg Bek <[log in to unmask]>
Fri, 15 Oct 1999 09:40:10 -0700
text/plain (133 lines)
I've read the replies, about wanting to run 'cp'
but here is what I use in a program that copies and
entire directory to a staging area.

One assumption is that the files are less than a particular
size (easy to fix by adding a loop):

    package Pio renames Posix_Io;
    package Files renames Posix_Files;
    package Permissions renames Posix_Permissions;

    procedure Copy (D_Entry : Files.Directory_Entry;
                    Quit : in out Boolean) is
        Source_File : constant String :=
           Posix.To_String (Files.Filename_Of (D_Entry));

        Source_File_Handle : Pio.File_Descriptor;
        Keys_File_Handle : Pio.File_Descriptor;
        Results_File_Handle : Pio.File_Descriptor;

    begin
        if Source_File (1) /= '.' then
            if Debug then
                Text_Io.Put_Line ("cp " & Source_Directory &
                                  Source_File & " " & Keys_Directory);
                Text_Io.Put_Line ("chmod 755 " & Keys_Directory &
Source_File);
            else
                Text_Io.Put_Line ("cp " & Source_Directory &
                                  Source_File & " " & Keys_Directory);
                Text_Io.Put_Line ("chmod 755 " & Keys_Directory &
Source_File);

                Source_File_Handle :=
                   Pio.Open (Name => Posix.To_Posix_String
                                        (Source_Directory & Source_File),
                             Mode => Pio.Read_Only);

                Keys_File_Handle :=
                   Pio.Open_Or_Create
                      (Name => Posix.To_Posix_String
                                  (Keys_Directory & Source_File),
                       Mode => Pio.Write_Only,
                       Permissions => (Permissions.Group_Write |
                                       Permissions.Others_Write |
                                       Permissions.Set_Group_Id |
                                       Permissions.Set_User_Id => False,
                                       others => True),
                       Options => Pio.Append);

                --Text_Io.Put_Line ("created " & Keys_Directory &
Source_File);

                -- don't be fooled by the following.  They don't do a copy!
appe
ar to work so need to use read & write
                --Results_File_Handle := Pio.Duplicate
                --                        (File => Source_File_Handle,
                --                         Target => Keys_File_Handle);
                --  or
                -- Files.Link (Old_Pathname =>
                --             Posix.To_Posix_String (Source_Directory &
File),
                --          New_Pathname => Posix.To_Posix_String
                --                             (Keys_Directory & File));
                --  Link creates a hard link copy of the file.  Not what we
                --  want.

                declare
                    Buffer : Pio.Io_Buffer (1 .. 1024 * 100);
                    -- want one large read/write. 102400 bytes should be
                    -- sufficient for these shell scripts
                    Last : Posix.Io_Count;
                begin
                    Pio.Read (File => Source_File_Handle,
                              Buffer => Buffer,
                              Last => Last);
                    Pio.Write (File => Keys_File_Handle,
                               Buffer => Buffer
                                            (Buffer'First .. Integer
(Last)),
                               Last => Last);
                end;
                Pio.Close (Source_File_Handle);
                Pio.Close (Keys_File_Handle);

            end if;
            Quit := False;
        end if;
    end Copy;

-- instantiations of the generic POSIX directory iterator
    procedure Chmod_777_All is
       new Files.For_Every_Directory_Entry (Action => Chmod_777);
    procedure Remove_All is
       new Files.For_Every_Directory_Entry (Action => Remove);
    procedure Copy_All is
       new Files.For_Every_Directory_Entry (Action => Copy);

begin

-- set the permissions of the file for removal 777
    Chmod_777_All (Pathname => Posix.To_Posix_String (Keys_Directory));

-- remove all files in the appropriate keys directory
    Remove_All (Pathname => Posix.To_Posix_String (Keys_Directory));

-- copy the library scripts to keys directory and set proper permissions
    Copy_All (Pathname => Posix.To_Posix_String (Source_Directory));



> -----Original Message-----
> From: Team Ada: Ada Advocacy Issues (83 & 95)
> [mailto:[log in to unmask]]On Behalf Of Wretling Urban
> Sent: Friday, October 15, 1999 2:27 AM
> To: [log in to unmask]
> Subject: How do I copy a file
>
>
> Hello!
>
> I want to execute the cp command on unix to copy a file but cant seem to
> find a good way of solving that. I have tried to look in the
> online help for
> POSIX but haven't found anything (maybe I've looked in the wrong
> places??).
>
> If you know how to solve it please reply.
>
> /Urban
>

ATOM RSS1 RSS2