Node: FileUtils, Next: GMP, Previous: DosUnix, Up: GPC Units

#### Higher level file and directory handling

The following listing contains the interface of the FileUtils unit.

This unit provides some routines for file and directory handling on a higher level than those provided by the RTS.

         { Some routines for file and directory handling on a higher level
           than those provided by the RTS.

           Copyright (C) 2000-2004 Free Software Foundation, Inc.

           Author: Frank Heckenbach <frank@pascal.gnu.de>

           This file is part of GNU Pascal.

           GNU Pascal is free software; you can redistribute it and/or modify
           it under the terms of the GNU General Public License as published
           by the Free Software Foundation; either version 2, or (at your
           option) any later version.

           GNU Pascal is distributed in the hope that it will be useful, but
           WITHOUT ANY WARRANTY; without even the implied warranty of
           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
           General Public License for more details.

           You should have received a copy of the GNU General Public License
           along with GNU Pascal; see the file COPYING. If not, write to the
           Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
           02111-1307, USA.

           As a special exception, if you link this file with files compiled
           with a GNU compiler to produce an executable, this does not cause
           the resulting executable to be covered by the GNU General Public
           License. This exception does not however invalidate any other
           reasons why the executable file might be covered by the GNU
           General Public License. }

         {$gnu-pascal,I-}
         {$if __GPC_RELEASE__ < 20030412}
         {$error This unit requires GPC release 20030412 or newer.}
         {$endif}

         unit FileUtils;

         interface

         uses GPC;

         type
           TStringProc = procedure (const s: String);

         { Finds all files matching the given Mask in the given Directory and
           all subdirectories of it. The matching is done using all wildcards
           and brace expansion, like MultiFileNameMatch does. For each file
           found, FileAction is executed. For each directory found (including
           . and .. if they match the Mask!), DirAction is executed. If
           MainDirFirst is True, this happens before processing the files in
           the directory and below, otherwise afterwards. (The former is
           useful, e.g., if this is used to copy a directory tree and
           DirAction does a MkDir, while the latter behaviour is required
           when removing a directory tree and DirAction does a RmDir.) Both
           FileAction and DirAction can be nil in which case nothing is done
           for files or directories found, respectively. (If DirAction is
           nil, the value of DirsFirst does not matter.) Of course,
           FileAction and DirAction may also be identical. The procedure
           leaves InOutRes set in case of any error. If FileAction or
           DirAction return with InOutRes set, FindFiles recognizes this and
           returns immediately. }
         procedure FindFiles (const Directory, Mask: String; MainDirFirst:
           Boolean;
                              FileAction, DirAction: TStringProc); attribute
           (iocritical, name = '_p_FindFiles');

         { Creates the directory given by Path and all directories in between
           that are necessary. Does not report an error if the directory
           already exists, but, of course, if it cannot be created because of
           missing permissions or because Path already exists as a file. }
         procedure MkDirs (const Path: String); attribute (iocritical, name
           = '_p_MkDirs');

         { Removes Path if empty as well as any empty parent directories.
           Does not report an error if the directory is not empty. }
         procedure RmDirs (const Path: String); attribute (iocritical, name
           = '_p_RmDirs');

         { Copies the file Source to Dest, overwriting Dest if it exists and
           can be written to. Returns any errors in IOResult. If Mode >= 0,
           it will change the permissions of Dest to Mode immediately after
           creating it and before writing any data to it. That's useful,
           e.g., if Dest is not meant to be world-readable, because if you'd
           do a ChMod after FileCopy, you might leave the data readable
           (depending on the umask) during the copying. If Mode < 0, Dest
           will be set to the same permissions Source has. In any case, Dest
           will be set to the modification time of Source after copying. On
           any error, the destination file is erased. This is to avoid
           leaving partial files in case of full file systems (one of the
           most common reasons for errors). }
         procedure FileCopy (const Source, Dest: String; Mode: Integer);
           attribute (iocritical, name = '_p_FileCopy');

         { Creates a backup of FileName in the directory BackupDirectory or,
           if BackupDirectory is empty, in the directory of FileName. Errors
           are returned in IOResult (and on any error, no partial backup file
           is left), but if FileName does not exist, this does *not* count as
           an error (i.e., BackupFile will just return without setting
           IOResult then). If OnlyUserReadable is True, the backup file will
           be given only user-read permissions, nothing else.

           The name chosen for the backup depends on the Simple and Short
           parameters. The short names will fit into 8+3 characters (whenever
           possible), while the long ones conform to the conventions used by
           most GNU tools. If Simple is True, a simple backup file name will
           be used, and previous backups under the same name will be
           overwritten (if possible). Otherwise, backups will be numbered,
           where the number is chosen to be larger than all existing backups,
           so it will be unique and increasing in chronological order. In
           particular:

           Simple  Short  Backup name
           True    True   Base name of FileName plus '.bak'
           False   True   Base name of FileName plus '.b' plus a number
           True    False  Base name plus extension of FileName plus '~'
           False   False  Base name plus extension of FileName plus '.~', a
                          number and '~' }
         procedure BackupFile (const FileName, BackupDirectory: String;
           Simple, Short, OnlyUserReadable: Boolean); attribute (iocritical,
           name = '_p_BackupFile');