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