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