1 //===-- File.h --------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_File_h_
11 #define liblldb_File_h_
12 #if defined(__cplusplus)
16 #include <sys/types.h>
18 #include "lldb/lldb-private.h"
20 namespace lldb_private {
22 //----------------------------------------------------------------------
23 /// @class File File.h "lldb/Host/File.h"
24 /// @brief A file class.
26 /// A file class that divides abstracts the LLDB core from host file
28 //----------------------------------------------------------------------
32 static int kInvalidDescriptor;
33 static FILE * kInvalidStream;
37 eOpenOptionRead = (1u << 0), // Open file for reading
38 eOpenOptionWrite = (1u << 1), // Open file for writing
39 eOpenOptionAppend = (1u << 2), // Don't truncate file when opening, append to end of file
40 eOpenOptionTruncate = (1u << 3), // Truncate file when opening
41 eOpenOptionNonBlocking = (1u << 4), // File reads
42 eOpenOptionCanCreate = (1u << 5), // Create file if doesn't already exist
43 eOpenOptionCanCreateNewOnly = (1u << 6) // Can create file only if it doesn't already exist
47 ConvertOpenOptionsForPOSIXOpen (uint32_t open_options);
51 ePermissionsUserRead = (1u << 8),
52 ePermissionsUserWrite = (1u << 7),
53 ePermissionsUserExecute = (1u << 6),
54 ePermissionsGroupRead = (1u << 5),
55 ePermissionsGroupWrite = (1u << 4),
56 ePermissionsGroupExecute = (1u << 3),
57 ePermissionsWorldRead = (1u << 2),
58 ePermissionsWorldWrite = (1u << 1),
59 ePermissionsWorldExecute = (1u << 0),
61 ePermissionsUserRW = (ePermissionsUserRead | ePermissionsUserWrite | 0 ),
62 ePermissionsUserRX = (ePermissionsUserRead | 0 | ePermissionsUserExecute ),
63 ePermissionsUserRWX = (ePermissionsUserRead | ePermissionsUserWrite | ePermissionsUserExecute ),
65 ePermissionsGroupRW = (ePermissionsGroupRead | ePermissionsGroupWrite | 0 ),
66 ePermissionsGroupRX = (ePermissionsGroupRead | 0 | ePermissionsGroupExecute ),
67 ePermissionsGroupRWX = (ePermissionsGroupRead | ePermissionsGroupWrite | ePermissionsGroupExecute ),
69 ePermissionsWorldRW = (ePermissionsWorldRead | ePermissionsWorldWrite | 0 ),
70 ePermissionsWorldRX = (ePermissionsWorldRead | 0 | ePermissionsWorldExecute ),
71 ePermissionsWorldRWX = (ePermissionsWorldRead | ePermissionsWorldWrite | ePermissionsWorldExecute ),
73 ePermissionsEveryoneR = (ePermissionsUserRead | ePermissionsGroupRead | ePermissionsWorldRead ),
74 ePermissionsEveryoneW = (ePermissionsUserWrite | ePermissionsGroupWrite | ePermissionsWorldWrite ),
75 ePermissionsEveryoneX = (ePermissionsUserExecute | ePermissionsGroupExecute | ePermissionsWorldExecute ),
77 ePermissionsEveryoneRW = (ePermissionsEveryoneR | ePermissionsEveryoneW | 0 ),
78 ePermissionsEveryoneRX = (ePermissionsEveryoneR | 0 | ePermissionsEveryoneX ),
79 ePermissionsEveryoneRWX = (ePermissionsEveryoneR | ePermissionsEveryoneW | ePermissionsEveryoneX ),
80 ePermissionsDefault = (ePermissionsUserRW | ePermissionsGroupRead)
84 m_descriptor (kInvalidDescriptor),
85 m_stream (kInvalidStream),
91 File (FILE *fh, bool transfer_ownership) :
92 m_descriptor (kInvalidDescriptor),
95 m_owned (transfer_ownership)
99 File (const File &rhs);
102 operator= (const File &rhs);
103 //------------------------------------------------------------------
104 /// Constructor with path.
106 /// Takes a path to a file which can be just a filename, or a full
107 /// path. If \a path is not NULL or empty, this function will call
108 /// File::Open (const char *path, uint32_t options, uint32_t permissions).
111 /// The full or partial path to a file.
113 /// @param[in] options
114 /// Options to use when opening (see File::OpenOptions)
116 /// @param[in] permissions
117 /// Options to use when opening (see File::Permissions)
119 /// @see File::Open (const char *path, uint32_t options, uint32_t permissions)
120 //------------------------------------------------------------------
121 File (const char *path,
123 uint32_t permissions = ePermissionsDefault);
125 //------------------------------------------------------------------
126 /// Constructor with FileSpec.
128 /// Takes a FileSpec pointing to a file which can be just a filename, or a full
129 /// path. If \a path is not NULL or empty, this function will call
130 /// File::Open (const char *path, uint32_t options, uint32_t permissions).
133 /// The FileSpec for this file.
135 /// @param[in] options
136 /// Options to use when opening (see File::OpenOptions)
138 /// @param[in] permissions
139 /// Options to use when opening (see File::Permissions)
141 /// @see File::Open (const char *path, uint32_t options, uint32_t permissions)
142 //------------------------------------------------------------------
143 File (const FileSpec& filespec,
145 uint32_t permissions = ePermissionsDefault);
147 File (int fd, bool tranfer_ownership) :
149 m_stream (kInvalidStream),
151 m_owned (tranfer_ownership)
154 //------------------------------------------------------------------
157 /// The destructor is virtual in case this class is subclassed.
158 //------------------------------------------------------------------
165 return DescriptorIsValid() || StreamIsValid();
168 //------------------------------------------------------------------
169 /// Convert to pointer operator.
171 /// This allows code to check a File object to see if it
172 /// contains anything valid using code such as:
181 /// A pointer to this object if either the directory or filename
182 /// is valid, NULL otherwise.
183 //------------------------------------------------------------------
187 return DescriptorIsValid() || StreamIsValid();
190 //------------------------------------------------------------------
191 /// Logical NOT operator.
193 /// This allows code to check a File object to see if it is
194 /// invalid using code such as:
203 /// Returns \b true if the object has an empty directory and
204 /// filename, \b false otherwise.
205 //------------------------------------------------------------------
209 return !DescriptorIsValid() && !StreamIsValid();
212 //------------------------------------------------------------------
213 /// Get the file spec for this file.
216 /// A reference to the file specification object.
217 //------------------------------------------------------------------
219 GetFileSpec (FileSpec &file_spec) const;
221 //------------------------------------------------------------------
222 /// Open a file for read/writing with the specified options.
224 /// Takes a path to a file which can be just a filename, or a full
228 /// The full or partial path to a file.
230 /// @param[in] options
231 /// Options to use when opening (see File::OpenOptions)
233 /// @param[in] permissions
234 /// Options to use when opening (see File::Permissions)
235 //------------------------------------------------------------------
237 Open (const char *path,
239 uint32_t permissions = ePermissionsDefault);
245 Duplicate (const File &rhs);
248 GetDescriptor() const;
251 SetDescriptor(int fd, bool transfer_ownership);
257 SetStream (FILE *fh, bool transfer_ownership);
259 //------------------------------------------------------------------
260 /// Read bytes from a file from the current file position.
262 /// NOTE: This function is NOT thread safe. Use the read function
263 /// that takes an "off_t &offset" to ensure correct operation in
264 /// multi-threaded environments.
267 /// A buffer where to put the bytes that are read.
269 /// @param[in/out] num_bytes
270 /// The number of bytes to read form the current file position
271 /// which gets modified with the number of bytes that were read.
274 /// An error object that indicates success or the reason for
276 //------------------------------------------------------------------
278 Read (void *buf, size_t &num_bytes);
280 //------------------------------------------------------------------
281 /// Write bytes to a file at the current file position.
283 /// NOTE: This function is NOT thread safe. Use the write function
284 /// that takes an "off_t &offset" to ensure correct operation in
285 /// multi-threaded environments.
288 /// A buffer where to put the bytes that are read.
290 /// @param[in/out] num_bytes
291 /// The number of bytes to write to the current file position
292 /// which gets modified with the number of bytes that were
296 /// An error object that indicates success or the reason for
298 //------------------------------------------------------------------
300 Write (const void *buf, size_t &num_bytes);
302 //------------------------------------------------------------------
303 /// Seek to an offset relative to the beginning of the file.
305 /// NOTE: This function is NOT thread safe, other threads that
306 /// access this object might also change the current file position.
307 /// For thread safe reads and writes see the following functions:
308 /// @see File::Read (void *, size_t, off_t &)
309 /// @see File::Write (const void *, size_t, off_t &)
311 /// @param[in] offset
312 /// The offset to seek to within the file relative to the
313 /// beginning of the file.
315 /// @param[in] error_ptr
316 /// A pointer to a lldb_private::Error object that will be
317 /// filled in if non-NULL.
320 /// The resulting seek offset, or -1 on error.
321 //------------------------------------------------------------------
323 SeekFromStart (off_t offset, Error *error_ptr = NULL);
325 //------------------------------------------------------------------
326 /// Seek to an offset relative to the current file position.
328 /// NOTE: This function is NOT thread safe, other threads that
329 /// access this object might also change the current file position.
330 /// For thread safe reads and writes see the following functions:
331 /// @see File::Read (void *, size_t, off_t &)
332 /// @see File::Write (const void *, size_t, off_t &)
334 /// @param[in] offset
335 /// The offset to seek to within the file relative to the
336 /// current file position.
338 /// @param[in] error_ptr
339 /// A pointer to a lldb_private::Error object that will be
340 /// filled in if non-NULL.
343 /// The resulting seek offset, or -1 on error.
344 //------------------------------------------------------------------
346 SeekFromCurrent (off_t offset, Error *error_ptr = NULL);
348 //------------------------------------------------------------------
349 /// Seek to an offset relative to the end of the file.
351 /// NOTE: This function is NOT thread safe, other threads that
352 /// access this object might also change the current file position.
353 /// For thread safe reads and writes see the following functions:
354 /// @see File::Read (void *, size_t, off_t &)
355 /// @see File::Write (const void *, size_t, off_t &)
357 /// @param[in/out] offset
358 /// The offset to seek to within the file relative to the
359 /// end of the file which gets filled in the the resulting
360 /// absolute file offset.
362 /// @param[in] error_ptr
363 /// A pointer to a lldb_private::Error object that will be
364 /// filled in if non-NULL.
367 /// The resulting seek offset, or -1 on error.
368 //------------------------------------------------------------------
370 SeekFromEnd (off_t offset, Error *error_ptr = NULL);
372 //------------------------------------------------------------------
373 /// Read bytes from a file from the specified file offset.
375 /// NOTE: This function is thread safe in that clients manager their
376 /// own file position markers and reads on other threads won't mess
377 /// up the current read.
380 /// A buffer where to put the bytes that are read.
382 /// @param[in/out] num_bytes
383 /// The number of bytes to read form the current file position
384 /// which gets modified with the number of bytes that were read.
386 /// @param[in/out] offset
387 /// The offset within the file from which to read \a num_bytes
388 /// bytes. This offset gets incremented by the number of bytes
392 /// An error object that indicates success or the reason for
394 //------------------------------------------------------------------
396 Read (void *dst, size_t &num_bytes, off_t &offset);
398 //------------------------------------------------------------------
399 /// Read bytes from a file from the specified file offset.
401 /// NOTE: This function is thread safe in that clients manager their
402 /// own file position markers and reads on other threads won't mess
403 /// up the current read.
405 /// @param[in/out] num_bytes
406 /// The number of bytes to read form the current file position
407 /// which gets modified with the number of bytes that were read.
409 /// @param[in/out] offset
410 /// The offset within the file from which to read \a num_bytes
411 /// bytes. This offset gets incremented by the number of bytes
414 /// @param[in] null_terminate
415 /// Ensure that the data that is read is terminated with a NULL
416 /// character so that the data can be used as a C string.
418 /// @param[out] data_buffer_sp
419 /// A data buffer to create and fill in that will contain any
420 /// data that is read from the file. This buffer will be reset
421 /// if an error occurs.
424 /// An error object that indicates success or the reason for
426 //------------------------------------------------------------------
428 Read (size_t &num_bytes,
431 lldb::DataBufferSP &data_buffer_sp);
433 //------------------------------------------------------------------
434 /// Write bytes to a file at the specified file offset.
436 /// NOTE: This function is thread safe in that clients manager their
437 /// own file position markers, though clients will need to implement
438 /// their own locking externally to avoid multiple people writing
439 /// to the file at the same time.
442 /// A buffer containing the bytes to write.
444 /// @param[in/out] num_bytes
445 /// The number of bytes to write to the file at offset \a offset.
446 /// \a num_bytes gets modified with the number of bytes that
449 /// @param[in/out] offset
450 /// The offset within the file at which to write \a num_bytes
451 /// bytes. This offset gets incremented by the number of bytes
452 /// that were written.
455 /// An error object that indicates success or the reason for
457 //------------------------------------------------------------------
459 Write (const void *src, size_t &num_bytes, off_t &offset);
461 //------------------------------------------------------------------
462 /// Flush the current stream
465 /// An error object that indicates success or the reason for
467 //------------------------------------------------------------------
471 //------------------------------------------------------------------
475 /// An error object that indicates success or the reason for
477 //------------------------------------------------------------------
481 //------------------------------------------------------------------
482 /// Get the permissions for a this file.
485 /// Bits logical OR'ed together from the permission bits defined
486 /// in lldb_private::File::Permissions.
487 //------------------------------------------------------------------
489 GetPermissions(Error &error) const;
492 GetPermissions (const char *path, Error &error);
494 //------------------------------------------------------------------
495 /// Output printf formatted output to the stream.
497 /// Print some formatted output to the stream.
499 /// @param[in] format
500 /// A printf style format string.
503 /// Variable arguments that are needed for the printf style
504 /// format string \a format.
505 //------------------------------------------------------------------
507 Printf (const char *format, ...) __attribute__ ((format (printf, 2, 3)));
510 PrintfVarArg(const char *format, va_list args);
516 DescriptorIsValid () const
518 return m_descriptor >= 0;
522 StreamIsValid () const
524 return m_stream != kInvalidStream;
527 //------------------------------------------------------------------
529 //------------------------------------------------------------------
536 } // namespace lldb_private
538 #endif // #if defined(__cplusplus)
539 #endif // liblldb_File_h_