aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/doc/src/filelib.xml
blob: 5e631aac218c56a3a6950d9a99129a879642c0e2 (plain) (blame)
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
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>2003</year><year>2017</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    
    </legalnotice>

    <title>filelib</title>
    <prepared>Bjorn Gustavsson</prepared>
    <responsible>Bjorn Gustavsson</responsible>
    <docno>1</docno>
    <approved>Kenneth Lundin</approved>
    <checked></checked>
    <date>2003-01-21</date>
    <rev>A</rev>
    <file>filelib.xml</file>
  </header>
  <module>filelib</module>
  <modulesummary>File utilities, such as wildcard matching of filenames.
  </modulesummary>
  <description>
    <p>This module contains utilities on a higher level than the
      <seealso marker="kernel:file"><c>file</c></seealso> module.</p>

    <p>This module does not support "raw" filenames (that is, files whose
      names do not comply with the expected encoding). Such files are ignored
      by the functions in this module.</p>

    <p>For more information about raw filenames, see the
      <seealso marker="kernel:file"><c>file</c></seealso> module.</p>

    <note>
      <p>
	Functionality in this module generally assumes valid input and
	does not necessarily fail on input that does not use a valid
	encoding, but may instead very likely produce invalid output.
      </p>
      <p>
	File operations used to accept filenames containing
	null characters (integer value zero). This caused
	the name to be truncated and in some cases arguments
	to primitive operations to be mixed up. Filenames
	containing null characters inside the filename
	are now <em>rejected</em> and will cause primitive
	file operations to fail.
      </p>
    </note>
    <warning><p>
      Currently null characters at the end of the filename
      will be accepted by primitive file operations. Such
      filenames are however still documented as invalid. The
      implementation will also change in the future and
      reject such filenames.
    </p></warning>
  </description>

  <datatypes>
    <datatype>
      <name name="filename"/>
    </datatype>
    <datatype>
      <name name="dirname"/>
    </datatype>
    <datatype>
      <name name="dirname_all"/>
    </datatype>
    <datatype>
      <name name="filename_all"/>
    </datatype>
    <datatype>
      <name name="find_file_rule"/>
    </datatype>
    <datatype>
      <name name="find_source_rule"/>
    </datatype>
  </datatypes>

  <funcs>
    <func>
      <name name="ensure_dir" arity="1"/>
      <fsummary>Ensure that all parent directories for a file or directory
        exist.</fsummary>
      <desc>
        <p>Ensures that all parent directories for the specified file or
          directory name <c><anno>Name</anno></c>
          exist, trying to create them if necessary.</p>
        <p>Returns <c>ok</c> if all parent directories already exist
          or can be created. Returns <c>{error, <anno>Reason</anno>}</c> if
          some parent directory does not exist and cannot be created.</p>
      </desc>
    </func>

    <func>
      <name name="file_size" arity="1"/>
      <fsummary>Return the size in bytes of a file.</fsummary>
      <desc>
        <p>Returns the size of the specified file.</p>
      </desc>
    </func>

    <func>
      <name name="fold_files" arity="5"/>
      <fsummary>Fold over all files matching a regular expression.</fsummary>
      <desc>
        <p>Folds function <c><anno>Fun</anno></c> over all (regular) files
          <c><anno>F</anno></c> in directory <c><anno>Dir</anno></c> that match
          the regular expression <c><anno>RegExp</anno></c> (for a description
          of the allowed regular expressions,
          see the <seealso marker="re"><c>re</c></seealso> module).
          If <c><anno>Recursive</anno></c> is <c>true</c>, all subdirectories
          to <c>Dir</c>
          are processed. The regular expression matching is only done on
          the filename without the directory part.</p>
        <p>If Unicode filename translation is in effect and the file
          system is transparent, filenames that cannot be
          interpreted as Unicode can be encountered, in which case the
          <c>fun()</c> must be prepared to handle raw filenames
          (that is, binaries). If the regular expression contains
          codepoints &gt; 255, it does not match filenames that do
          not conform to the expected character encoding (that is, are not
          encoded in valid UTF-8).</p>
        <p>For more information about raw filenames, see the
          <seealso marker="kernel:file"><c>file</c></seealso> module.</p>
      </desc>
    </func>

    <func>
      <name name="is_dir" arity="1"/>
      <fsummary>Test whether <c>Name</c> refers to a directory.</fsummary>
      <desc>
        <p>Returns <c>true</c> if <c><anno>Name</anno></c>
          refers to a directory, otherwise <c>false</c>.</p>
      </desc>
    </func>

    <func>
      <name name="is_file" arity="1"/>
      <fsummary>Test whether <c>Name</c> refers to a file or directory.
      </fsummary>
      <desc>
        <p>Returns <c>true</c> if <c><anno>Name</anno></c>
          refers to a file or a directory, otherwise <c>false</c>.</p>
      </desc>
    </func>

    <func>
      <name name="is_regular" arity="1"/>
      <fsummary>Test whether <c>Name</c> refers to a (regular) file.</fsummary>
      <desc>
        <p>Returns <c>true</c> if <c><anno>Name</anno></c>
          refers to a (regular) file, otherwise <c>false</c>.</p>
      </desc>
    </func>

    <func>
      <name name="last_modified" arity="1"/>
      <fsummary>Return the local date and time when a file was last modified.
      </fsummary>
      <desc>
        <p>Returns the date and time the specified file or directory was last
          modified, or <c>0</c> if the file does not exist.</p>
      </desc>
    </func>

    <func>
      <name name="wildcard" arity="1"/>
      <fsummary>Match filenames using Unix-style wildcards.</fsummary>
      <desc>
        <p>Returns a list of all files that match Unix-style wildcard string
          <c><anno>Wildcard</anno></c>.</p>
        <p>The wildcard string looks like an ordinary filename, except
          that the following "wildcard characters" are interpreted in a special
          way:</p>
        <taglist>
          <tag>?</tag>
          <item>
            <p>Matches one character.</p>
          </item>
          <tag>*</tag>
          <item>
            <p>Matches any number of characters up to the end of
              the filename, the next dot, or the next slash.</p>
          </item>
          <tag>**</tag>
          <item>
            <p>Two adjacent <c>*</c> used as a single pattern match
              all files and zero or more directories and subdirectories.</p>
          </item>
          <tag>[Character1,Character2,...]</tag>
          <item>
            <p>Matches any of the characters listed. Two characters
              separated by a hyphen match a range of characters.
              Example: <c>[A-Z]</c> matches any uppercase letter.</p>
          </item>
          <tag>{Item,...}</tag>
          <item>
            <p>Alternation. Matches one of the alternatives.</p>
          </item>
        </taglist>
        <p>Other characters represent themselves. Only filenames that
          have exactly the same character in the same position match.
          Matching is case-sensitive, for example, "a" does not match "A".</p>
	<p>Directory separators must always be written as <c>/</c>, even on
	Windows.</p>
	<p>A character preceded by <c>\</c> loses its special meaning. Note
	that <c>\</c> must be written as <c>\\</c> in a string literal.
	For example, "\\?*" will match any filename starting with <c>?</c>.</p>
        <p>Notice that multiple "*" characters are allowed
          (as in Unix wildcards, but opposed to Windows/DOS wildcards).</p>
        <p><em>Examples:</em></p>
        <p>The following examples assume that the current directory is the
          top of an Erlang/OTP installation.</p>
        <p>To find all <c>.beam</c> files in all applications, use the
          following line:</p>
        <code type="none">
filelib:wildcard("lib/*/ebin/*.beam").</code>
        <p>To find <c>.erl</c> or <c>.hrl</c> in all applications <c>src</c>
          directories, use either of the following lines:</p>
        <code type="none">
filelib:wildcard("lib/*/src/*.?rl")</code>
        <code type="none">
filelib:wildcard("lib/*/src/*.{erl,hrl}")</code>
        <p>To find all <c>.hrl</c> files in <c>src</c> or <c>include</c>
          directories:</p>
        <code type="none">
filelib:wildcard("lib/*/{src,include}/*.hrl").</code>
        <p>To find all <c>.erl</c> or <c>.hrl</c> files in either
          <c>src</c> or <c>include</c> directories:</p>
        <code type="none">
filelib:wildcard("lib/*/{src,include}/*.{erl,hrl}")</code>
        <p>To find all <c>.erl</c> or <c>.hrl</c> files in any subdirectory:</p>
        <code type="none">
filelib:wildcard("lib/**/*.{erl,hrl}")</code>
      </desc>
    </func>

    <func>
      <name name="wildcard" arity="2"/>
      <fsummary>Match filenames using Unix-style wildcards starting at a
        specified directory.</fsummary>
      <desc>
        <p>Same as <seealso marker="#wildcard/1"><c>wildcard/1</c></seealso>,
          except that <c><anno>Cwd</anno></c> is used instead of the working
          directory.</p>
      </desc>
    </func>

    <func>
      <name name="find_file" arity="2"/>
      <name name="find_file" arity="3"/>
      <fsummary>Find a file relative to a given directory.</fsummary>
      <desc>
        <p>Looks for a file of the given name by applying suffix rules to
        the given directory path. For example, a rule <c>{"ebin", "src"}</c>
        means that if the directory path ends with <c>"ebin"</c>, the
        corresponding path ending in <c>"src"</c> should be searched.</p>
        <p>If <c><anno>Rules</anno></c> is left out or is an empty list, the
        default system rules are used. See also the Kernel application
        parameter <seealso
        marker="kernel:kernel_app#source_search_rules"><c>source_search_rules</c></seealso>.</p>
      </desc>
    </func>
    <func>
      <name name="find_source" arity="1"/>
      <fsummary>Find the source file for a given object file.</fsummary>
      <desc>
        <p>Equivalent to <c>find_source(Base, Dir)</c>, where <c>Dir</c> is
        <c>filename:dirname(<anno>FilePath</anno>)</c> and <c>Base</c> is
        <c>filename:basename(<anno>FilePath</anno>)</c>.</p>
      </desc>
    </func>
    <func>
      <name name="find_source" arity="2"/>
      <name name="find_source" arity="3"/>
      <fsummary>Find a source file relative to a given directory.</fsummary>
      <desc>
        <p>Applies file extension specific rules to find the source file for
        a given object file relative to the object directory. For example,
        for a file with the extension <c>.beam</c>, the default rule is to
        look for a file with a corresponding extension <c>.erl</c> by
        replacing the suffix <c>"ebin"</c> of the object directory path with
        <c>"src"</c>.
        The file search is done through <seealso
        marker="#find_file/3"><c>find_file/3</c></seealso>. The directory of
        the object file is always tried before any other directory specified
        by the rules.</p>
        <p>If <c><anno>Rules</anno></c> is left out or is an empty list, the
        default system rules are used. See also the Kernel application
        parameter <seealso
        marker="kernel:kernel_app#source_search_rules"><c>source_search_rules</c></seealso>.</p>
      </desc>
    </func>
  </funcs>
</erlref>