]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Include/Library/FileHandleLib.h
MdePkg: Clean up source files
[mirror_edk2.git] / MdePkg / Include / Library / FileHandleLib.h
1 /** @file
2 Provides interface to EFI_FILE_HANDLE functionality.
3
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #ifndef _FILE_HANDLE_LIBRARY_HEADER_
16 #define _FILE_HANDLE_LIBRARY_HEADER_
17
18 #include <Protocol/SimpleFileSystem.h>
19 #include <Guid/FileInfo.h>
20
21 /// The tag for use in identifying UNICODE files.
22 /// If the file is UNICODE, the first 16 bits of the file will equal this value.
23 extern CONST UINT16 gUnicodeFileTag;
24
25 /**
26 This function retrieves information about the file for the handle
27 specified and stores it in the allocated pool memory.
28
29 This function allocates a buffer to store the file's information. It is the
30 caller's responsibility to free the buffer.
31
32 @param[in] FileHandle The file handle of the file for which information is
33 being requested.
34
35 @retval NULL Information could not be retrieved.
36 @retval !NULL The information about the file.
37 **/
38 EFI_FILE_INFO*
39 EFIAPI
40 FileHandleGetInfo (
41 IN EFI_FILE_HANDLE FileHandle
42 );
43
44 /**
45 This function sets the information about the file for the opened handle
46 specified.
47
48 @param[in] FileHandle The file handle of the file for which information
49 is being set.
50
51 @param[in] FileInfo The information to set.
52
53 @retval EFI_SUCCESS The information was set.
54 @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
55 @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
56 @retval EFI_NO_MEDIA The device has no medium.
57 @retval EFI_DEVICE_ERROR The device reported an error.
58 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
59 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
60 @retval EFI_ACCESS_DENIED The file was opened read only.
61 @retval EFI_VOLUME_FULL The volume is full.
62 **/
63 EFI_STATUS
64 EFIAPI
65 FileHandleSetInfo (
66 IN EFI_FILE_HANDLE FileHandle,
67 IN CONST EFI_FILE_INFO *FileInfo
68 );
69
70 /**
71 This function reads information from an opened file.
72
73 If FileHandle is not a directory, the function reads the requested number of
74 bytes from the file at the file's current position and returns them in Buffer.
75 If the read goes beyond the end of the file, the read length is truncated to the
76 end of the file. The file's current position is increased by the number of bytes
77 returned. If FileHandle is a directory, the function reads the directory entry
78 at the file's current position and returns the entry in Buffer. If the Buffer
79 is not large enough to hold the current directory entry, then
80 EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
81 BufferSize is set to be the size of the buffer needed to read the entry. On
82 success, the current position is updated to the next directory entry. If there
83 are no more directory entries, the read returns a zero-length buffer.
84 EFI_FILE_INFO is the structure returned as the directory entry.
85
86 @param[in] FileHandle The opened file handle.
87 @param[in, out] BufferSize On input, the size of buffer in bytes. On return,
88 the number of bytes written.
89 @param[out] Buffer The buffer to put read data into.
90
91 @retval EFI_SUCCESS Data was read.
92 @retval EFI_NO_MEDIA The device has no media.
93 @retval EFI_DEVICE_ERROR The device reported an error.
94 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
95 @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
96 size.
97
98 **/
99 EFI_STATUS
100 EFIAPI
101 FileHandleRead(
102 IN EFI_FILE_HANDLE FileHandle,
103 IN OUT UINTN *BufferSize,
104 OUT VOID *Buffer
105 );
106
107 /**
108 Write data to a file.
109
110 This function writes the specified number of bytes to the file at the current
111 file position. The current file position is advanced the actual number of bytes
112 written, which is returned in BufferSize. Partial writes only occur when there
113 has been a data error during the write attempt (such as "volume space full").
114 The file is automatically grown to hold the data if required. Direct writes to
115 opened directories are not supported.
116
117 @param[in] FileHandle The opened file for writing.
118 @param[in, out] BufferSize On input, the number of bytes in Buffer. On output,
119 the number of bytes written.
120 @param[in] Buffer The buffer containing data to write is stored.
121
122 @retval EFI_SUCCESS Data was written.
123 @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
124 @retval EFI_NO_MEDIA The device has no media.
125 @retval EFI_DEVICE_ERROR The device reported an error.
126 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
127 @retval EFI_WRITE_PROTECTED The device is write-protected.
128 @retval EFI_ACCESS_DENIED The file was opened for read only.
129 @retval EFI_VOLUME_FULL The volume is full.
130 **/
131 EFI_STATUS
132 EFIAPI
133 FileHandleWrite(
134 IN EFI_FILE_HANDLE FileHandle,
135 IN OUT UINTN *BufferSize,
136 IN VOID *Buffer
137 );
138
139 /**
140 Close an open file handle.
141
142 This function closes a specified file handle. All "dirty" cached file data is
143 flushed to the device, and the file is closed. In all cases the handle is
144 closed.
145
146 @param[in] FileHandle The file handle to close.
147
148 @retval EFI_SUCCESS The file handle was closed successfully.
149 **/
150 EFI_STATUS
151 EFIAPI
152 FileHandleClose (
153 IN EFI_FILE_HANDLE FileHandle
154 );
155
156 /**
157 Delete a file and close the handle.
158
159 This function closes and deletes a file. In all cases the file handle is closed.
160 If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
161 returned, but the handle is still closed.
162
163 @param[in] FileHandle The file handle to delete.
164
165 @retval EFI_SUCCESS The file was closed successfully.
166 @retval EFI_WARN_DELETE_FAILURE The handle was closed, but the file was not
167 deleted.
168 @retval INVALID_PARAMETER One of the parameters has an invalid value.
169 **/
170 EFI_STATUS
171 EFIAPI
172 FileHandleDelete (
173 IN EFI_FILE_HANDLE FileHandle
174 );
175
176 /**
177 Set the current position in a file.
178
179 This function sets the current file position for the handle to the position
180 supplied. With the exception of moving to position 0xFFFFFFFFFFFFFFFF, only
181 absolute positioning is supported, and moving past the end of the file is
182 allowed (a subsequent write would grow the file). Moving to position
183 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
184 If FileHandle is a directory, the only position that may be set is zero. This
185 has the effect of starting the read process of the directory entries over again.
186
187 @param[in] FileHandle The file handle on which the position is being set.
188 @param[in] Position The byte position from the beginning of the file.
189
190 @retval EFI_SUCCESS The operation completed successfully.
191 @retval EFI_UNSUPPORTED The request for non-zero is not valid on
192 directories.
193 @retval INVALID_PARAMETER One of the parameters has an invalid value.
194 **/
195 EFI_STATUS
196 EFIAPI
197 FileHandleSetPosition (
198 IN EFI_FILE_HANDLE FileHandle,
199 IN UINT64 Position
200 );
201
202 /**
203 Gets a file's current position.
204
205 This function retrieves the current file position for the file handle. For
206 directories, the current file position has no meaning outside of the file
207 system driver. As such, the operation is not supported. An error is returned
208 if FileHandle is a directory.
209
210 @param[in] FileHandle The open file handle on which to get the position.
211 @param[out] Position The byte position from beginning of file.
212
213 @retval EFI_SUCCESS The operation completed successfully.
214 @retval INVALID_PARAMETER One of the parameters has an invalid value.
215 @retval EFI_UNSUPPORTED The request is not valid on directories.
216 **/
217 EFI_STATUS
218 EFIAPI
219 FileHandleGetPosition (
220 IN EFI_FILE_HANDLE FileHandle,
221 OUT UINT64 *Position
222 );
223 /**
224 Flushes data on a file.
225
226 This function flushes all modified data associated with a file to a device.
227
228 @param[in] FileHandle The file handle on which to flush data.
229
230 @retval EFI_SUCCESS The data was flushed.
231 @retval EFI_NO_MEDIA The device has no media.
232 @retval EFI_DEVICE_ERROR The device reported an error.
233 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
234 @retval EFI_WRITE_PROTECTED The file or medium is write protected.
235 @retval EFI_ACCESS_DENIED The file was opened for read only.
236 **/
237 EFI_STATUS
238 EFIAPI
239 FileHandleFlush (
240 IN EFI_FILE_HANDLE FileHandle
241 );
242
243 /**
244 Function to determine if a given handle is a directory handle.
245
246 Open the file information on the DirHandle and verify that the Attribute
247 includes EFI_FILE_DIRECTORY bit set.
248
249 @param[in] DirHandle Handle to open file.
250
251 @retval EFI_SUCCESS DirHandle is a directory.
252 @retval EFI_INVALID_PARAMETER DirHandle is NULL.
253 The file information returns from FileHandleGetInfo is NULL.
254 @retval EFI_NOT_FOUND DirHandle is not a directory.
255 **/
256 EFI_STATUS
257 EFIAPI
258 FileHandleIsDirectory (
259 IN EFI_FILE_HANDLE DirHandle
260 );
261
262 /** Retrieve first entry from a directory.
263
264 This function takes an open directory handle and gets information from the
265 first entry in the directory. A buffer is allocated to contain
266 the information and a pointer to the buffer is returned in *Buffer. The
267 caller can use FileHandleFindNextFile() to get subsequent directory entries.
268
269 The buffer will be freed by FileHandleFindNextFile() when the last directory
270 entry is read. Otherwise, the caller must free the buffer, using FreePool,
271 when finished with it.
272
273 @param[in] DirHandle The file handle of the directory to search.
274 @param[out] Buffer The pointer to pointer to buffer for file's information.
275
276 @retval EFI_SUCCESS Found the first file.
277 @retval EFI_NOT_FOUND Cannot find the directory.
278 @retval EFI_NO_MEDIA The device has no media.
279 @retval EFI_DEVICE_ERROR The device reported an error.
280 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
281 @return Others The status of FileHandleGetInfo, FileHandleSetPosition,
282 or FileHandleRead.
283 **/
284 EFI_STATUS
285 EFIAPI
286 FileHandleFindFirstFile (
287 IN EFI_FILE_HANDLE DirHandle,
288 OUT EFI_FILE_INFO **Buffer
289 );
290
291 /** Retrieve next entries from a directory.
292
293 To use this function, the caller must first call the FileHandleFindFirstFile()
294 function to get the first directory entry. Subsequent directory entries are
295 retrieved by using the FileHandleFindNextFile() function. This function can
296 be called several times to get each entry from the directory. If the call of
297 FileHandleFindNextFile() retrieved the last directory entry, the next call of
298 this function will set *NoFile to TRUE and free the buffer.
299
300 @param[in] DirHandle The file handle of the directory.
301 @param[out] Buffer The pointer to buffer for file's information.
302 @param[out] NoFile The pointer to boolean when last file is found.
303
304 @retval EFI_SUCCESS Found the next file, or reached last file.
305 @retval EFI_NO_MEDIA The device has no media.
306 @retval EFI_DEVICE_ERROR The device reported an error.
307 @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
308 **/
309 EFI_STATUS
310 EFIAPI
311 FileHandleFindNextFile(
312 IN EFI_FILE_HANDLE DirHandle,
313 OUT EFI_FILE_INFO *Buffer,
314 OUT BOOLEAN *NoFile
315 );
316
317 /**
318 Retrieve the size of a file.
319
320 This function extracts the file size info from the FileHandle's EFI_FILE_INFO
321 data.
322
323 @param[in] FileHandle The file handle from which size is retrieved.
324 @param[out] Size The pointer to size.
325
326 @retval EFI_SUCCESS Operation was completed successfully.
327 @retval EFI_DEVICE_ERROR Cannot access the file.
328 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
329 Size is NULL.
330 **/
331 EFI_STATUS
332 EFIAPI
333 FileHandleGetSize (
334 IN EFI_FILE_HANDLE FileHandle,
335 OUT UINT64 *Size
336 );
337
338 /**
339 Set the size of a file.
340
341 This function changes the file size info from the FileHandle's EFI_FILE_INFO
342 data.
343
344 @param[in] FileHandle The file handle whose size is to be changed.
345 @param[in] Size The new size.
346
347 @retval EFI_SUCCESS The operation completed successfully.
348 @retval EFI_DEVICE_ERROR Cannot access the file.
349 @retval EFI_INVALID_PARAMETER FileHandle is NULL.
350 **/
351 EFI_STATUS
352 EFIAPI
353 FileHandleSetSize (
354 IN EFI_FILE_HANDLE FileHandle,
355 IN UINT64 Size
356 );
357
358 /**
359 Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
360 directory 'stack'. If the file is a directory, then append the '\' char at the
361 end of name string. If it's not a directory, then the last '\' should not be
362 added.
363
364 @param[in] Handle Handle to the Directory or File to create path to.
365 @param[out] FullFileName Pointer to pointer to generated full file name. It
366 is the responsibility of the caller to free this memory
367 with a call to FreePool().
368 @retval EFI_SUCCESS The operation was successful and FullFileName is valid.
369 @retval EFI_INVALID_PARAMETER Handle was NULL.
370 @retval EFI_INVALID_PARAMETER FullFileName was NULL.
371 @retval EFI_OUT_OF_MEMORY A memory allocation failed.
372 **/
373 EFI_STATUS
374 EFIAPI
375 FileHandleGetFileName (
376 IN CONST EFI_FILE_HANDLE Handle,
377 OUT CHAR16 **FullFileName
378 );
379
380 /**
381 Function to read a single line (up to but not including the \n) from a file.
382
383 If the position upon start is 0, then the Ascii Boolean will be set. This should be
384 maintained and not changed for all operations with the same file.
385 The function will not return the \r and \n character in buffer. When an empty line is
386 read a CHAR_NULL character will be returned in buffer.
387
388 @param[in] Handle FileHandle to read from.
389 @param[in, out] Buffer The pointer to buffer to read into.
390 @param[in, out] Size The pointer to number of bytes in Buffer.
391 @param[in] Truncate If the buffer is large enough, this has no effect.
392 If the buffer is is too small and Truncate is TRUE,
393 the line will be truncated.
394 If the buffer is is too small and Truncate is FALSE,
395 then no read will occur.
396
397 @param[in, out] Ascii Boolean value for indicating whether the file is
398 Ascii (TRUE) or UCS2 (FALSE).
399
400 @retval EFI_SUCCESS The operation was successful. The line is stored in
401 Buffer.
402 @retval EFI_INVALID_PARAMETER Handle was NULL.
403 @retval EFI_INVALID_PARAMETER Size was NULL.
404 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
405 Size was updated to the minimum space required.
406 @sa FileHandleRead
407 **/
408 EFI_STATUS
409 EFIAPI
410 FileHandleReadLine(
411 IN EFI_FILE_HANDLE Handle,
412 IN OUT CHAR16 *Buffer,
413 IN OUT UINTN *Size,
414 IN BOOLEAN Truncate,
415 IN OUT BOOLEAN *Ascii
416 );
417
418 /**
419 Function to read a single line from a file. The \n is not included in the returned
420 buffer. The returned buffer must be callee freed.
421
422 If the position upon start is 0, then the Ascii Boolean will be set. This should be
423 maintained and not changed for all operations with the same file.
424
425 @param[in] Handle FileHandle to read from.
426 @param[in, out] Ascii Boolean value for indicating whether the file is
427 Ascii (TRUE) or UCS2 (FALSE).
428
429 @return The line of text from the file.
430
431 @sa FileHandleReadLine
432 **/
433 CHAR16*
434 EFIAPI
435 FileHandleReturnLine(
436 IN EFI_FILE_HANDLE Handle,
437 IN OUT BOOLEAN *Ascii
438 );
439
440 /**
441 Function to write a line of text to a file.
442
443 If the file is a Unicode file (with UNICODE file tag) then write the unicode
444 text.
445 If the file is an ASCII file then write the ASCII text.
446 If the size of file is zero (without file tag at the beginning) then write
447 ASCII text as default.
448
449 @param[in] Handle FileHandle to write to.
450 @param[in] Buffer Buffer to write, if NULL the function will
451 take no action and return EFI_SUCCESS.
452
453 @retval EFI_SUCCESS The data was written.
454 Buffer is NULL.
455 @retval EFI_INVALID_PARAMETER Handle is NULL.
456 @retval EFI_OUT_OF_RESOURCES Unable to allocate temporary space for ASCII
457 string due to out of resources.
458
459 @sa FileHandleWrite
460 **/
461 EFI_STATUS
462 EFIAPI
463 FileHandleWriteLine(
464 IN EFI_FILE_HANDLE Handle,
465 IN CHAR16 *Buffer
466 );
467
468 /**
469 Function to take a formatted argument and print it to a file.
470
471 @param[in] Handle The file handle for the file to write to.
472 @param[in] Format The format argument (see printlib for the format specifier).
473 @param[in] ... The variable arguments for the format.
474
475 @retval EFI_SUCCESS The operation was successful.
476 @retval other A return value from FileHandleWriteLine.
477
478 @sa FileHandleWriteLine
479 **/
480 EFI_STATUS
481 EFIAPI
482 FileHandlePrintLine(
483 IN EFI_FILE_HANDLE Handle,
484 IN CONST CHAR16 *Format,
485 ...
486 );
487
488 /**
489 Function to determine if a FILE_HANDLE is at the end of the file.
490
491 This will NOT work on directories.
492
493 If Handle is NULL, then ASSERT().
494
495 @param[in] Handle The file handle.
496
497 @retval TRUE The position is at the end of the file.
498 @retval FALSE The position is not at the end of the file.
499 **/
500 BOOLEAN
501 EFIAPI
502 FileHandleEof(
503 IN EFI_FILE_HANDLE Handle
504 );
505
506 #endif //_FILE_HANDLE_LIBRARY_HEADER_
507