1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . D I R E C T O R I E S --
9 -- Copyright (C) 2004-2009, Free Software Foundation, Inc. --
11 -- This specification is derived for use with GNAT from AI-00248, which is --
12 -- expected to be a part of a future expected revised Ada Reference Manual. --
13 -- The copyright notice above, and the license provisions that follow apply --
14 -- solely to the contents of the part following the private keyword. --
16 -- GNAT is free software; you can redistribute it and/or modify it under --
17 -- terms of the GNU General Public License as published by the Free Soft- --
18 -- ware Foundation; either version 3, or (at your option) any later ver- --
19 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
20 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
21 -- or FITNESS FOR A PARTICULAR PURPOSE. --
23 -- As a special exception under Section 7 of GPL version 3, you are granted --
24 -- additional permissions described in the GCC Runtime Library Exception, --
25 -- version 3.1, as published by the Free Software Foundation. --
27 -- You should have received a copy of the GNU General Public License and --
28 -- a copy of the GCC Runtime Library Exception along with this program; --
29 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
30 -- <http://www.gnu.org/licenses/>. --
32 -- GNAT was originally developed by the GNAT team at New York University. --
33 -- Extensive contributions were provided by Ada Core Technologies Inc. --
35 ------------------------------------------------------------------------------
37 -- Ada 2005: Implementation of Ada.Directories (AI95-00248). Note that this
38 -- unit is available without -gnat05. That seems reasonable, since you only
39 -- get it if you explicitly ask for it.
41 -- External files may be classified as directories, special files, or ordinary
42 -- files. A directory is an external file that is a container for files on
43 -- the target system. A special file is an external file that cannot be
44 -- created or read by a predefined Ada Input-Output package. External files
45 -- that are not special files or directories are called ordinary files.
47 -- A file name is a string identifying an external file. Similarly, a
48 -- directory name is a string identifying a directory. The interpretation of
49 -- file names and directory names is implementation-defined.
51 -- The full name of an external file is a full specification of the name of
52 -- the file. If the external environment allows alternative specifications of
53 -- the name (for example, abbreviations), the full name should not use such
54 -- alternatives. A full name typically will include the names of all of
55 -- directories that contain the item. The simple name of an external file is
56 -- the name of the item, not including any containing directory names. Unless
57 -- otherwise specified, a file name or directory name parameter to a
58 -- predefined Ada input-output subprogram can be a full name, a simple name,
59 -- or any other form of name supported by the implementation.
61 -- The default directory is the directory that is used if a directory or
62 -- file name is not a full name (that is, when the name does not fully
63 -- identify all of the containing directories).
65 -- A directory entry is a single item in a directory, identifying a single
66 -- external file (including directories and special files).
68 -- For each function that returns a string, the lower bound of the returned
72 with Ada
.Finalization
;
73 with Ada
.IO_Exceptions
;
74 with Ada
.Strings
.Unbounded
;
76 package Ada
.Directories
is
78 -----------------------------------
79 -- Directory and File Operations --
80 -----------------------------------
82 function Current_Directory
return String;
83 -- Returns the full directory name for the current default directory. The
84 -- name returned shall be suitable for a future call to Set_Directory.
85 -- The exception Use_Error is propagated if a default directory is not
86 -- supported by the external environment.
88 procedure Set_Directory
(Directory
: String);
89 -- Sets the current default directory. The exception Name_Error is
90 -- propagated if the string given as Directory does not identify an
91 -- existing directory. The exception Use_Error is propagated if the
92 -- external environment does not support making Directory (in the absence
93 -- of Name_Error) a default directory.
95 procedure Create_Directory
96 (New_Directory
: String;
98 -- Creates a directory with name New_Directory. The Form parameter can be
99 -- used to give system-dependent characteristics of the directory; the
100 -- interpretation of the Form parameter is implementation-defined. A null
101 -- string for Form specifies the use of the default options of the
102 -- implementation of the new directory. The exception Name_Error is
103 -- propagated if the string given as New_Directory does not allow the
104 -- identification of a directory. The exception Use_Error is propagated if
105 -- the external environment does not support the creation of a directory
106 -- with the given name (in the absence of Name_Error) and form.
108 procedure Delete_Directory
(Directory
: String);
109 -- Deletes an existing empty directory with name Directory. The exception
110 -- Name_Error is propagated if the string given as Directory does not
111 -- identify an existing directory. The exception Use_Error is propagated
112 -- if the external environment does not support the deletion of the
113 -- directory (or some portion of its contents) with the given name (in the
114 -- absence of Name_Error).
116 procedure Create_Path
117 (New_Directory
: String;
118 Form
: String := "");
119 -- Creates zero or more directories with name New_Directory. Each
120 -- non-existent directory named by New_Directory is created. For example,
121 -- on a typical Unix system, Create_Path ("/usr/me/my"); would create
122 -- directory "me" in directory "usr", then create directory "my" in
123 -- directory "me". The Form can be used to give system-dependent
124 -- characteristics of the directory; the interpretation of the Form
125 -- parameter is implementation-defined. A null string for Form specifies
126 -- the use of the default options of the implementation of the new
127 -- directory. The exception Name_Error is propagated if the string given
128 -- as New_Directory does not allow the identification of any directory.
129 -- The exception Use_Error is propagated if the external environment does
130 -- not support the creation of any directories with the given name (in the
131 -- absence of Name_Error) and form.
133 procedure Delete_Tree
(Directory
: String);
134 -- Deletes an existing directory with name Directory. The directory and
135 -- all of its contents (possibly including other directories) are deleted.
136 -- The exception Name_Error is propagated if the string given as Directory
137 -- does not identify an existing directory. The exception Use_Error is
138 -- propagated if the external environment does not support the deletion of
139 -- the directory or some portion of its contents with the given name (in
140 -- the absence of Name_Error). If Use_Error is propagated, it is
141 -- unspecified if a portion of the contents of the directory are deleted.
143 procedure Delete_File
(Name
: String);
144 -- Deletes an existing ordinary or special file with Name. The exception
145 -- Name_Error is propagated if the string given as Name does not identify
146 -- an existing ordinary or special external file. The exception Use_Error
147 -- is propagated if the external environment does not support the deletion
148 -- of the file with the given name (in the absence of Name_Error).
150 procedure Rename
(Old_Name
, New_Name
: String);
151 -- Renames an existing external file (including directories) with Old_Name
152 -- to New_Name. The exception Name_Error is propagated if the string given
153 -- as Old_Name does not identify an existing external file. The exception
154 -- Use_Error is propagated if the external environment does not support the
155 -- renaming of the file with the given name (in the absence of Name_Error).
156 -- In particular, Use_Error is propagated if a file or directory already
157 -- exists with New_Name.
160 (Source_Name
: String;
161 Target_Name
: String;
162 Form
: String := "");
163 -- Copies the contents of the existing external file with Source_Name
164 -- to Target_Name. The resulting external file is a duplicate of the source
165 -- external file. The Form can be used to give system-dependent
166 -- characteristics of the resulting external file; the interpretation of
167 -- the Form parameter is implementation-defined. Exception Name_Error is
168 -- propagated if the string given as Source_Name does not identify an
169 -- existing external ordinary or special file or if the string given as
170 -- Target_Name does not allow the identification of an external file.
171 -- The exception Use_Error is propagated if the external environment does
172 -- not support the creating of the file with the name given by Target_Name
173 -- and form given by Form, or copying of the file with the name given by
174 -- Source_Name (in the absence of Name_Error).
176 ----------------------------------------
177 -- File and directory name operations --
178 ----------------------------------------
180 function Full_Name
(Name
: String) return String;
181 -- Returns the full name corresponding to the file name specified by Name.
182 -- The exception Name_Error is propagated if the string given as Name does
183 -- not allow the identification of an external file (including directories
184 -- and special files).
186 function Simple_Name
(Name
: String) return String;
187 -- Returns the simple name portion of the file name specified by Name. The
188 -- exception Name_Error is propagated if the string given as Name does not
189 -- allow the identification of an external file (including directories and
192 function Containing_Directory
(Name
: String) return String;
193 -- Returns the name of the containing directory of the external file
194 -- (including directories) identified by Name. If more than one directory
195 -- can contain Name, the directory name returned is implementation-defined.
196 -- The exception Name_Error is propagated if the string given as Name does
197 -- not allow the identification of an external file. The exception
198 -- Use_Error is propagated if the external file does not have a containing
201 function Extension
(Name
: String) return String;
202 -- Returns the extension name corresponding to Name. The extension name is
203 -- a portion of a simple name (not including any separator characters),
204 -- typically used to identify the file class. If the external environment
205 -- does not have extension names, then the null string is returned.
206 -- The exception Name_Error is propagated if the string given as Name does
207 -- not allow the identification of an external file.
209 function Base_Name
(Name
: String) return String;
210 -- Returns the base name corresponding to Name. The base name is the
211 -- remainder of a simple name after removing any extension and extension
212 -- separators. The exception Name_Error is propagated if the string given
213 -- as Name does not allow the identification of an external file
214 -- (including directories and special files).
217 (Containing_Directory
: String := "";
219 Extension
: String := "") return String;
220 -- Returns the name of the external file with the specified
221 -- Containing_Directory, Name, and Extension. If Extension is the null
222 -- string, then Name is interpreted as a simple name; otherwise Name is
223 -- interpreted as a base name. The exception Name_Error is propagated if
224 -- the string given as Containing_Directory is not null and does not allow
225 -- the identification of a directory, or if the string given as Extension
226 -- is not null and is not a possible extension, or if the string given as
227 -- Name is not a possible simple name (if Extension is null) or base name
228 -- (if Extension is non-null).
230 --------------------------------
231 -- File and directory queries --
232 --------------------------------
234 type File_Kind
is (Directory
, Ordinary_File
, Special_File
);
235 -- The type File_Kind represents the kind of file represented by an
236 -- external file or directory.
238 type File_Size
is range 0 .. Long_Long_Integer'Last;
239 -- The type File_Size represents the size of an external file
241 function Exists
(Name
: String) return Boolean;
242 -- Returns True if external file represented by Name exists, and False
243 -- otherwise. The exception Name_Error is propagated if the string given as
244 -- Name does not allow the identification of an external file (including
245 -- directories and special files).
247 function Kind
(Name
: String) return File_Kind
;
248 -- Returns the kind of external file represented by Name. The exception
249 -- Name_Error is propagated if the string given as Name does not allow the
250 -- identification of an existing external file.
252 function Size
(Name
: String) return File_Size
;
253 -- Returns the size of the external file represented by Name. The size of
254 -- an external file is the number of stream elements contained in the file.
255 -- If the external file is discontiguous (not all elements exist), the
256 -- result is implementation-defined. If the external file is not an
257 -- ordinary file, the result is implementation-defined. The exception
258 -- Name_Error is propagated if the string given as Name does not allow the
259 -- identification of an existing external file. The exception
260 -- Constraint_Error is propagated if the file size is not a value of type
263 function Modification_Time
(Name
: String) return Ada
.Calendar
.Time
;
264 -- Returns the time that the external file represented by Name was most
265 -- recently modified. If the external file is not an ordinary file, the
266 -- result is implementation-defined. The exception Name_Error is propagated
267 -- if the string given as Name does not allow the identification of an
268 -- existing external file. The exception Use_Error is propagated if the
269 -- external environment does not support the reading the modification time
270 -- of the file with the name given by Name (in the absence of Name_Error).
272 -------------------------
273 -- Directory Searching --
274 -------------------------
276 type Directory_Entry_Type
is limited private;
277 -- The type Directory_Entry_Type represents a single item in a directory.
278 -- These items can only be created by the Get_Next_Entry procedure in this
279 -- package. Information about the item can be obtained from the functions
280 -- declared in this package. A default initialized object of this type is
281 -- invalid; objects returned from Get_Next_Entry are valid.
283 type Filter_Type
is array (File_Kind
) of Boolean;
284 -- The type Filter_Type specifies which directory entries are provided from
285 -- a search operation. If the Directory component is True, directory
286 -- entries representing directories are provided. If the Ordinary_File
287 -- component is True, directory entries representing ordinary files are
288 -- provided. If the Special_File component is True, directory entries
289 -- representing special files are provided.
291 type Search_Type
is limited private;
292 -- The type Search_Type contains the state of a directory search. A
293 -- default-initialized Search_Type object has no entries available
294 -- (More_Entries returns False).
296 procedure Start_Search
297 (Search
: in out Search_Type
;
300 Filter
: Filter_Type
:= (others => True));
301 -- Starts a search in the directory entry in the directory named by
302 -- Directory for entries matching Pattern. Pattern represents a file name
303 -- matching pattern. If Pattern is null, all items in the directory are
304 -- matched; otherwise, the interpretation of Pattern is implementation-
305 -- defined. Only items which match Filter will be returned. After a
306 -- successful call on Start_Search, the object Search may have entries
307 -- available, but it may have no entries available if no files or
308 -- directories match Pattern and Filter. The exception Name_Error is
309 -- propagated if the string given by Directory does not identify an
310 -- existing directory, or if Pattern does not allow the identification of
311 -- any possible external file or directory. The exception Use_Error is
312 -- propagated if the external environment does not support the searching
313 -- of the directory with the given name (in the absence of Name_Error).
315 procedure End_Search
(Search
: in out Search_Type
);
316 -- Ends the search represented by Search. After a successful call on
317 -- End_Search, the object Search will have no entries available. Note
318 -- that it is not necessary to call End_Search if the call to Start_Search
319 -- was unsuccessful and raised an exception (but it is harmless to make
320 -- the call in this case).
322 function More_Entries
(Search
: Search_Type
) return Boolean;
323 -- Returns True if more entries are available to be returned by a call
324 -- to Get_Next_Entry for the specified search object, and False otherwise.
326 procedure Get_Next_Entry
327 (Search
: in out Search_Type
;
328 Directory_Entry
: out Directory_Entry_Type
);
329 -- Returns the next Directory_Entry for the search described by Search that
330 -- matches the pattern and filter. If no further matches are available,
331 -- Status_Error is raised. It is implementation-defined as to whether the
332 -- results returned by this routine are altered if the contents of the
333 -- directory are altered while the Search object is valid (for example, by
334 -- another program). The exception Use_Error is propagated if the external
335 -- environment does not support continued searching of the directory
336 -- represented by Search.
341 Filter
: Filter_Type
:= (others => True);
342 Process
: not null access procedure
343 (Directory_Entry
: Directory_Entry_Type
));
344 -- Searches in the directory named by Directory for entries matching
345 -- Pattern. The subprogram designated by Process is called with each
346 -- matching entry in turn. Pattern represents a pattern for matching file
347 -- names. If Pattern is null, all items in the directory are matched;
348 -- otherwise, the interpretation of Pattern is implementation-defined.
349 -- Only items that match Filter will be returned. The exception Name_Error
350 -- is propagated if the string given by Directory does not identify
351 -- an existing directory, or if Pattern does not allow the identification
352 -- of any possible external file or directory. The exception Use_Error is
353 -- propagated if the external environment does not support the searching
354 -- of the directory with the given name (in the absence of Name_Error).
356 -------------------------------------
357 -- Operations on Directory Entries --
358 -------------------------------------
360 function Simple_Name
(Directory_Entry
: Directory_Entry_Type
) return String;
361 -- Returns the simple external name of the external file (including
362 -- directories) represented by Directory_Entry. The format of the name
363 -- returned is implementation-defined. The exception Status_Error is
364 -- propagated if Directory_Entry is invalid.
366 function Full_Name
(Directory_Entry
: Directory_Entry_Type
) return String;
367 -- Returns the full external name of the external file (including
368 -- directories) represented by Directory_Entry. The format of the name
369 -- returned is implementation-defined. The exception Status_Error is
370 -- propagated if Directory_Entry is invalid.
372 function Kind
(Directory_Entry
: Directory_Entry_Type
) return File_Kind
;
373 -- Returns the kind of external file represented by Directory_Entry. The
374 -- exception Status_Error is propagated if Directory_Entry is invalid.
376 function Size
(Directory_Entry
: Directory_Entry_Type
) return File_Size
;
377 -- Returns the size of the external file represented by Directory_Entry.
378 -- The size of an external file is the number of stream elements contained
379 -- in the file. If the external file is discontiguous (not all elements
380 -- exist), the result is implementation-defined. If the external file
381 -- represented by Directory_Entry is not an ordinary file, the result is
382 -- implementation-defined. The exception Status_Error is propagated if
383 -- Directory_Entry is invalid. The exception Constraint_Error is propagated
384 -- if the file size is not a value of type File_Size.
386 function Modification_Time
387 (Directory_Entry
: Directory_Entry_Type
) return Ada
.Calendar
.Time
;
388 -- Returns the time that the external file represented by Directory_Entry
389 -- was most recently modified. If the external file represented by
390 -- Directory_Entry is not an ordinary file, the result is
391 -- implementation-defined. The exception Status_Error is propagated if
392 -- Directory_Entry is invalid. The exception Use_Error is propagated if
393 -- the external environment does not support the reading the modification
394 -- time of the file represented by Directory_Entry.
400 Status_Error
: exception renames Ada
.IO_Exceptions
.Status_Error
;
401 Name_Error
: exception renames Ada
.IO_Exceptions
.Name_Error
;
402 Use_Error
: exception renames Ada
.IO_Exceptions
.Use_Error
;
403 Device_Error
: exception renames Ada
.IO_Exceptions
.Device_Error
;
406 type Directory_Entry_Type
is record
407 Is_Valid
: Boolean := False;
408 Simple
: Ada
.Strings
.Unbounded
.Unbounded_String
;
409 Full
: Ada
.Strings
.Unbounded
.Unbounded_String
;
410 Kind
: File_Kind
:= Ordinary_File
;
413 -- The type Search_Data is defined in the body, so that the spec does not
414 -- depend on packages of the GNAT hierarchy.
417 type Search_Ptr
is access Search_Data
;
419 -- Search_Type need to be a controlled type, because it includes component
420 -- of type Dir_Type (in GNAT.Directory_Operations) that need to be closed
421 -- (if opened) during finalization. The component need to be an access
422 -- value, because Search_Data is not fully defined in the spec.
424 type Search_Type
is new Ada
.Finalization
.Controlled
with record
428 procedure Finalize
(Search
: in out Search_Type
);
429 -- Close the directory, if opened, and deallocate Value
431 procedure End_Search
(Search
: in out Search_Type
) renames Finalize
;