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"
19 #include "lldb/Host/IOObject.h"
21 namespace lldb_private {
23 //----------------------------------------------------------------------
24 /// @class File File.h "lldb/Host/File.h"
25 /// @brief A file class.
27 /// A file class that divides abstracts the LLDB core from host file
29 //----------------------------------------------------------------------
30 class File : public IOObject
33 static int kInvalidDescriptor;
34 static FILE * kInvalidStream;
38 eOpenOptionRead = (1u << 0), // Open file for reading
39 eOpenOptionWrite = (1u << 1), // Open file for writing
40 eOpenOptionAppend = (1u << 2), // Don't truncate file when opening, append to end of file
41 eOpenOptionTruncate = (1u << 3), // Truncate file when opening
42 eOpenOptionNonBlocking = (1u << 4), // File reads
43 eOpenOptionCanCreate = (1u << 5), // Create file if doesn't already exist
44 eOpenOptionCanCreateNewOnly = (1u << 6), // Can create file only if it doesn't already exist
45 eOpenoptionDontFollowSymlinks = (1u << 7)
49 ConvertOpenOptionsForPOSIXOpen (uint32_t open_options);
52 IOObject(eFDTypeFile, false),
53 m_descriptor (kInvalidDescriptor),
54 m_stream (kInvalidStream),
57 m_is_interactive (eLazyBoolCalculate),
58 m_is_real_terminal (eLazyBoolCalculate)
62 File (FILE *fh, bool transfer_ownership) :
63 IOObject(eFDTypeFile, false),
64 m_descriptor (kInvalidDescriptor),
67 m_own_stream (transfer_ownership),
68 m_is_interactive (eLazyBoolCalculate),
69 m_is_real_terminal (eLazyBoolCalculate)
73 File (const File &rhs);
76 operator= (const File &rhs);
77 //------------------------------------------------------------------
78 /// Constructor with path.
80 /// Takes a path to a file which can be just a filename, or a full
81 /// path. If \a path is not NULL or empty, this function will call
82 /// File::Open (const char *path, uint32_t options, uint32_t permissions).
85 /// The full or partial path to a file.
87 /// @param[in] options
88 /// Options to use when opening (see File::OpenOptions)
90 /// @param[in] permissions
91 /// Options to use when opening (see File::Permissions)
93 /// @see File::Open (const char *path, uint32_t options, uint32_t permissions)
94 //------------------------------------------------------------------
95 File (const char *path,
97 uint32_t permissions = lldb::eFilePermissionsFileDefault);
99 //------------------------------------------------------------------
100 /// Constructor with FileSpec.
102 /// Takes a FileSpec pointing to a file which can be just a filename, or a full
103 /// path. If \a path is not NULL or empty, this function will call
104 /// File::Open (const char *path, uint32_t options, uint32_t permissions).
107 /// The FileSpec for this file.
109 /// @param[in] options
110 /// Options to use when opening (see File::OpenOptions)
112 /// @param[in] permissions
113 /// Options to use when opening (see File::Permissions)
115 /// @see File::Open (const char *path, uint32_t options, uint32_t permissions)
116 //------------------------------------------------------------------
117 File (const FileSpec& filespec,
119 uint32_t permissions = lldb::eFilePermissionsFileDefault);
121 File (int fd, bool transfer_ownership) :
122 IOObject(eFDTypeFile, transfer_ownership),
124 m_stream (kInvalidStream),
130 //------------------------------------------------------------------
133 /// The destructor is virtual in case this class is subclassed.
134 //------------------------------------------------------------------
141 return DescriptorIsValid() || StreamIsValid();
144 //------------------------------------------------------------------
145 /// Convert to pointer operator.
147 /// This allows code to check a File object to see if it
148 /// contains anything valid using code such as:
157 /// A pointer to this object if either the directory or filename
158 /// is valid, NULL otherwise.
159 //------------------------------------------------------------------
163 return DescriptorIsValid() || StreamIsValid();
166 //------------------------------------------------------------------
167 /// Logical NOT operator.
169 /// This allows code to check a File object to see if it is
170 /// invalid using code such as:
179 /// Returns \b true if the object has an empty directory and
180 /// filename, \b false otherwise.
181 //------------------------------------------------------------------
185 return !DescriptorIsValid() && !StreamIsValid();
188 //------------------------------------------------------------------
189 /// Get the file spec for this file.
192 /// A reference to the file specification object.
193 //------------------------------------------------------------------
195 GetFileSpec (FileSpec &file_spec) const;
197 //------------------------------------------------------------------
198 /// Open a file for read/writing with the specified options.
200 /// Takes a path to a file which can be just a filename, or a full
204 /// The full or partial path to a file.
206 /// @param[in] options
207 /// Options to use when opening (see File::OpenOptions)
209 /// @param[in] permissions
210 /// Options to use when opening (see File::Permissions)
211 //------------------------------------------------------------------
213 Open (const char *path,
215 uint32_t permissions = lldb::eFilePermissionsFileDefault);
221 Duplicate (const File &rhs);
224 GetDescriptor() const;
231 SetDescriptor(int fd, bool transfer_ownership);
237 SetStream (FILE *fh, bool transfer_ownership);
239 //------------------------------------------------------------------
240 /// Read bytes from a file from the current file position.
242 /// NOTE: This function is NOT thread safe. Use the read function
243 /// that takes an "off_t &offset" to ensure correct operation in
244 /// multi-threaded environments.
247 /// A buffer where to put the bytes that are read.
249 /// @param[in/out] num_bytes
250 /// The number of bytes to read form the current file position
251 /// which gets modified with the number of bytes that were read.
254 /// An error object that indicates success or the reason for
256 //------------------------------------------------------------------
258 Read (void *buf, size_t &num_bytes);
260 //------------------------------------------------------------------
261 /// Write bytes to a file at the current file position.
263 /// NOTE: This function is NOT thread safe. Use the write function
264 /// that takes an "off_t &offset" to ensure correct operation in
265 /// multi-threaded environments.
268 /// A buffer where to put the bytes that are read.
270 /// @param[in/out] num_bytes
271 /// The number of bytes to write to the current file position
272 /// which gets modified with the number of bytes that were
276 /// An error object that indicates success or the reason for
278 //------------------------------------------------------------------
280 Write (const void *buf, size_t &num_bytes);
282 //------------------------------------------------------------------
283 /// Seek to an offset relative to the beginning of the file.
285 /// NOTE: This function is NOT thread safe, other threads that
286 /// access this object might also change the current file position.
287 /// For thread safe reads and writes see the following functions:
288 /// @see File::Read (void *, size_t, off_t &)
289 /// @see File::Write (const void *, size_t, off_t &)
291 /// @param[in] offset
292 /// The offset to seek to within the file relative to the
293 /// beginning of the file.
295 /// @param[in] error_ptr
296 /// A pointer to a lldb_private::Error object that will be
297 /// filled in if non-NULL.
300 /// The resulting seek offset, or -1 on error.
301 //------------------------------------------------------------------
303 SeekFromStart (off_t offset, Error *error_ptr = NULL);
305 //------------------------------------------------------------------
306 /// Seek to an offset relative to the current file position.
308 /// NOTE: This function is NOT thread safe, other threads that
309 /// access this object might also change the current file position.
310 /// For thread safe reads and writes see the following functions:
311 /// @see File::Read (void *, size_t, off_t &)
312 /// @see File::Write (const void *, size_t, off_t &)
314 /// @param[in] offset
315 /// The offset to seek to within the file relative to the
316 /// current file position.
318 /// @param[in] error_ptr
319 /// A pointer to a lldb_private::Error object that will be
320 /// filled in if non-NULL.
323 /// The resulting seek offset, or -1 on error.
324 //------------------------------------------------------------------
326 SeekFromCurrent (off_t offset, Error *error_ptr = NULL);
328 //------------------------------------------------------------------
329 /// Seek to an offset relative to the end of the file.
331 /// NOTE: This function is NOT thread safe, other threads that
332 /// access this object might also change the current file position.
333 /// For thread safe reads and writes see the following functions:
334 /// @see File::Read (void *, size_t, off_t &)
335 /// @see File::Write (const void *, size_t, off_t &)
337 /// @param[in/out] offset
338 /// The offset to seek to within the file relative to the
339 /// end of the file which gets filled in with the resulting
340 /// absolute file offset.
342 /// @param[in] error_ptr
343 /// A pointer to a lldb_private::Error object that will be
344 /// filled in if non-NULL.
347 /// The resulting seek offset, or -1 on error.
348 //------------------------------------------------------------------
350 SeekFromEnd (off_t offset, Error *error_ptr = NULL);
352 //------------------------------------------------------------------
353 /// Read bytes from a file from the specified file offset.
355 /// NOTE: This function is thread safe in that clients manager their
356 /// own file position markers and reads on other threads won't mess
357 /// up the current read.
360 /// A buffer where to put the bytes that are read.
362 /// @param[in/out] num_bytes
363 /// The number of bytes to read form the current file position
364 /// which gets modified with the number of bytes that were read.
366 /// @param[in/out] offset
367 /// The offset within the file from which to read \a num_bytes
368 /// bytes. This offset gets incremented by the number of bytes
372 /// An error object that indicates success or the reason for
374 //------------------------------------------------------------------
376 Read (void *dst, size_t &num_bytes, off_t &offset);
378 //------------------------------------------------------------------
379 /// Read bytes from a file from the specified file offset.
381 /// NOTE: This function is thread safe in that clients manager their
382 /// own file position markers and reads on other threads won't mess
383 /// up the current read.
385 /// @param[in/out] num_bytes
386 /// The number of bytes to read form the current file position
387 /// which gets modified with the number of bytes that were read.
389 /// @param[in/out] offset
390 /// The offset within the file from which to read \a num_bytes
391 /// bytes. This offset gets incremented by the number of bytes
394 /// @param[in] null_terminate
395 /// Ensure that the data that is read is terminated with a NULL
396 /// character so that the data can be used as a C string.
398 /// @param[out] data_buffer_sp
399 /// A data buffer to create and fill in that will contain any
400 /// data that is read from the file. This buffer will be reset
401 /// if an error occurs.
404 /// An error object that indicates success or the reason for
406 //------------------------------------------------------------------
408 Read (size_t &num_bytes,
411 lldb::DataBufferSP &data_buffer_sp);
413 //------------------------------------------------------------------
414 /// Write bytes to a file at the specified file offset.
416 /// NOTE: This function is thread safe in that clients manager their
417 /// own file position markers, though clients will need to implement
418 /// their own locking externally to avoid multiple people writing
419 /// to the file at the same time.
422 /// A buffer containing the bytes to write.
424 /// @param[in/out] num_bytes
425 /// The number of bytes to write to the file at offset \a offset.
426 /// \a num_bytes gets modified with the number of bytes that
429 /// @param[in/out] offset
430 /// The offset within the file at which to write \a num_bytes
431 /// bytes. This offset gets incremented by the number of bytes
432 /// that were written.
435 /// An error object that indicates success or the reason for
437 //------------------------------------------------------------------
439 Write (const void *src, size_t &num_bytes, off_t &offset);
441 //------------------------------------------------------------------
442 /// Flush the current stream
445 /// An error object that indicates success or the reason for
447 //------------------------------------------------------------------
451 //------------------------------------------------------------------
455 /// An error object that indicates success or the reason for
457 //------------------------------------------------------------------
461 //------------------------------------------------------------------
462 /// Get the permissions for a this file.
465 /// Bits logical OR'ed together from the permission bits defined
466 /// in lldb_private::File::Permissions.
467 //------------------------------------------------------------------
469 GetPermissions(Error &error) const;
472 GetPermissions (const char *path, Error &error);
475 //------------------------------------------------------------------
476 /// Return true if this file is interactive.
479 /// True if this file is a terminal (tty or pty), false
481 //------------------------------------------------------------------
485 //------------------------------------------------------------------
486 /// Return true if this file from a real terminal.
488 /// Just knowing a file is a interactive isn't enough, we also need
489 /// to know if the terminal has a width and height so we can do
490 /// cursor movement and other terminal manipulations by sending
491 /// escape sequences.
494 /// True if this file is a terminal (tty, not a pty) that has
495 /// a non-zero width and height, false otherwise.
496 //------------------------------------------------------------------
498 GetIsRealTerminal ();
500 //------------------------------------------------------------------
501 /// Output printf formatted output to the stream.
503 /// Print some formatted output to the stream.
505 /// @param[in] format
506 /// A printf style format string.
509 /// Variable arguments that are needed for the printf style
510 /// format string \a format.
511 //------------------------------------------------------------------
513 Printf (const char *format, ...) __attribute__ ((format (printf, 2, 3)));
516 PrintfVarArg(const char *format, va_list args);
520 SetOptions (uint32_t options)
528 DescriptorIsValid () const
530 return m_descriptor >= 0;
534 StreamIsValid () const
536 return m_stream != kInvalidStream;
540 CalculateInteractiveAndTerminal ();
542 //------------------------------------------------------------------
544 //------------------------------------------------------------------
549 LazyBool m_is_interactive;
550 LazyBool m_is_real_terminal;
553 } // namespace lldb_private
555 #endif // #if defined(__cplusplus)
556 #endif // liblldb_File_h_