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),
46 eOpenOptionCloseOnExec = (1u << 8) // Close the file when executing a new process
50 ConvertOpenOptionsForPOSIXOpen (uint32_t open_options);
53 IOObject(eFDTypeFile, false),
54 m_descriptor (kInvalidDescriptor),
55 m_stream (kInvalidStream),
58 m_is_interactive (eLazyBoolCalculate),
59 m_is_real_terminal (eLazyBoolCalculate)
63 File (FILE *fh, bool transfer_ownership) :
64 IOObject(eFDTypeFile, false),
65 m_descriptor (kInvalidDescriptor),
68 m_own_stream (transfer_ownership),
69 m_is_interactive (eLazyBoolCalculate),
70 m_is_real_terminal (eLazyBoolCalculate)
74 File (const File &rhs);
77 operator= (const File &rhs);
78 //------------------------------------------------------------------
79 /// Constructor with path.
81 /// Takes a path to a file which can be just a filename, or a full
82 /// path. If \a path is not NULL or empty, this function will call
83 /// File::Open (const char *path, uint32_t options, uint32_t permissions).
86 /// The full or partial path to a file.
88 /// @param[in] options
89 /// Options to use when opening (see File::OpenOptions)
91 /// @param[in] permissions
92 /// Options to use when opening (see File::Permissions)
94 /// @see File::Open (const char *path, uint32_t options, uint32_t permissions)
95 //------------------------------------------------------------------
96 File (const char *path,
98 uint32_t permissions = lldb::eFilePermissionsFileDefault);
100 //------------------------------------------------------------------
101 /// Constructor with FileSpec.
103 /// Takes a FileSpec pointing to a file which can be just a filename, or a full
104 /// path. If \a path is not NULL or empty, this function will call
105 /// File::Open (const char *path, uint32_t options, uint32_t permissions).
108 /// The FileSpec for this file.
110 /// @param[in] options
111 /// Options to use when opening (see File::OpenOptions)
113 /// @param[in] permissions
114 /// Options to use when opening (see File::Permissions)
116 /// @see File::Open (const char *path, uint32_t options, uint32_t permissions)
117 //------------------------------------------------------------------
118 File (const FileSpec& filespec,
120 uint32_t permissions = lldb::eFilePermissionsFileDefault);
122 File (int fd, bool transfer_ownership) :
123 IOObject(eFDTypeFile, transfer_ownership),
125 m_stream (kInvalidStream),
131 //------------------------------------------------------------------
134 /// The destructor is virtual in case this class is subclassed.
135 //------------------------------------------------------------------
142 return DescriptorIsValid() || StreamIsValid();
145 //------------------------------------------------------------------
146 /// Convert to pointer operator.
148 /// This allows code to check a File object to see if it
149 /// contains anything valid using code such as:
158 /// A pointer to this object if either the directory or filename
159 /// is valid, NULL otherwise.
160 //------------------------------------------------------------------
164 return DescriptorIsValid() || StreamIsValid();
167 //------------------------------------------------------------------
168 /// Logical NOT operator.
170 /// This allows code to check a File object to see if it is
171 /// invalid using code such as:
180 /// Returns \b true if the object has an empty directory and
181 /// filename, \b false otherwise.
182 //------------------------------------------------------------------
186 return !DescriptorIsValid() && !StreamIsValid();
189 //------------------------------------------------------------------
190 /// Get the file spec for this file.
193 /// A reference to the file specification object.
194 //------------------------------------------------------------------
196 GetFileSpec (FileSpec &file_spec) const;
198 //------------------------------------------------------------------
199 /// Open a file for read/writing with the specified options.
201 /// Takes a path to a file which can be just a filename, or a full
205 /// The full or partial path to a file.
207 /// @param[in] options
208 /// Options to use when opening (see File::OpenOptions)
210 /// @param[in] permissions
211 /// Options to use when opening (see File::Permissions)
212 //------------------------------------------------------------------
214 Open (const char *path,
216 uint32_t permissions = lldb::eFilePermissionsFileDefault);
222 Duplicate (const File &rhs);
225 GetDescriptor() const;
232 SetDescriptor(int fd, bool transfer_ownership);
238 SetStream (FILE *fh, bool transfer_ownership);
240 //------------------------------------------------------------------
241 /// Read bytes from a file from the current file position.
243 /// NOTE: This function is NOT thread safe. Use the read function
244 /// that takes an "off_t &offset" to ensure correct operation in
245 /// multi-threaded environments.
248 /// A buffer where to put the bytes that are read.
250 /// @param[in/out] num_bytes
251 /// The number of bytes to read form the current file position
252 /// which gets modified with the number of bytes that were read.
255 /// An error object that indicates success or the reason for
257 //------------------------------------------------------------------
259 Read (void *buf, size_t &num_bytes);
261 //------------------------------------------------------------------
262 /// Write bytes to a file at the current file position.
264 /// NOTE: This function is NOT thread safe. Use the write function
265 /// that takes an "off_t &offset" to ensure correct operation in
266 /// multi-threaded environments.
269 /// A buffer where to put the bytes that are read.
271 /// @param[in/out] num_bytes
272 /// The number of bytes to write to the current file position
273 /// which gets modified with the number of bytes that were
277 /// An error object that indicates success or the reason for
279 //------------------------------------------------------------------
281 Write (const void *buf, size_t &num_bytes);
283 //------------------------------------------------------------------
284 /// Seek to an offset relative to the beginning of the file.
286 /// NOTE: This function is NOT thread safe, other threads that
287 /// access this object might also change the current file position.
288 /// For thread safe reads and writes see the following functions:
289 /// @see File::Read (void *, size_t, off_t &)
290 /// @see File::Write (const void *, size_t, off_t &)
292 /// @param[in] offset
293 /// The offset to seek to within the file relative to the
294 /// beginning of the file.
296 /// @param[in] error_ptr
297 /// A pointer to a lldb_private::Error object that will be
298 /// filled in if non-NULL.
301 /// The resulting seek offset, or -1 on error.
302 //------------------------------------------------------------------
304 SeekFromStart (off_t offset, Error *error_ptr = NULL);
306 //------------------------------------------------------------------
307 /// Seek to an offset relative to the current file position.
309 /// NOTE: This function is NOT thread safe, other threads that
310 /// access this object might also change the current file position.
311 /// For thread safe reads and writes see the following functions:
312 /// @see File::Read (void *, size_t, off_t &)
313 /// @see File::Write (const void *, size_t, off_t &)
315 /// @param[in] offset
316 /// The offset to seek to within the file relative to the
317 /// current file position.
319 /// @param[in] error_ptr
320 /// A pointer to a lldb_private::Error object that will be
321 /// filled in if non-NULL.
324 /// The resulting seek offset, or -1 on error.
325 //------------------------------------------------------------------
327 SeekFromCurrent (off_t offset, Error *error_ptr = NULL);
329 //------------------------------------------------------------------
330 /// Seek to an offset relative to the end of the file.
332 /// NOTE: This function is NOT thread safe, other threads that
333 /// access this object might also change the current file position.
334 /// For thread safe reads and writes see the following functions:
335 /// @see File::Read (void *, size_t, off_t &)
336 /// @see File::Write (const void *, size_t, off_t &)
338 /// @param[in/out] offset
339 /// The offset to seek to within the file relative to the
340 /// end of the file which gets filled in with the resulting
341 /// absolute file offset.
343 /// @param[in] error_ptr
344 /// A pointer to a lldb_private::Error object that will be
345 /// filled in if non-NULL.
348 /// The resulting seek offset, or -1 on error.
349 //------------------------------------------------------------------
351 SeekFromEnd (off_t offset, Error *error_ptr = NULL);
353 //------------------------------------------------------------------
354 /// Read bytes from a file from the specified file offset.
356 /// NOTE: This function is thread safe in that clients manager their
357 /// own file position markers and reads on other threads won't mess
358 /// up the current read.
361 /// A buffer where to put the bytes that are read.
363 /// @param[in/out] num_bytes
364 /// The number of bytes to read form the current file position
365 /// which gets modified with the number of bytes that were read.
367 /// @param[in/out] offset
368 /// The offset within the file from which to read \a num_bytes
369 /// bytes. This offset gets incremented by the number of bytes
373 /// An error object that indicates success or the reason for
375 //------------------------------------------------------------------
377 Read (void *dst, size_t &num_bytes, off_t &offset);
379 //------------------------------------------------------------------
380 /// Read bytes from a file from the specified file offset.
382 /// NOTE: This function is thread safe in that clients manager their
383 /// own file position markers and reads on other threads won't mess
384 /// up the current read.
386 /// @param[in/out] num_bytes
387 /// The number of bytes to read form the current file position
388 /// which gets modified with the number of bytes that were read.
390 /// @param[in/out] offset
391 /// The offset within the file from which to read \a num_bytes
392 /// bytes. This offset gets incremented by the number of bytes
395 /// @param[in] null_terminate
396 /// Ensure that the data that is read is terminated with a NULL
397 /// character so that the data can be used as a C string.
399 /// @param[out] data_buffer_sp
400 /// A data buffer to create and fill in that will contain any
401 /// data that is read from the file. This buffer will be reset
402 /// if an error occurs.
405 /// An error object that indicates success or the reason for
407 //------------------------------------------------------------------
409 Read (size_t &num_bytes,
412 lldb::DataBufferSP &data_buffer_sp);
414 //------------------------------------------------------------------
415 /// Write bytes to a file at the specified file offset.
417 /// NOTE: This function is thread safe in that clients manager their
418 /// own file position markers, though clients will need to implement
419 /// their own locking externally to avoid multiple people writing
420 /// to the file at the same time.
423 /// A buffer containing the bytes to write.
425 /// @param[in/out] num_bytes
426 /// The number of bytes to write to the file at offset \a offset.
427 /// \a num_bytes gets modified with the number of bytes that
430 /// @param[in/out] offset
431 /// The offset within the file at which to write \a num_bytes
432 /// bytes. This offset gets incremented by the number of bytes
433 /// that were written.
436 /// An error object that indicates success or the reason for
438 //------------------------------------------------------------------
440 Write (const void *src, size_t &num_bytes, off_t &offset);
442 //------------------------------------------------------------------
443 /// Flush the current stream
446 /// An error object that indicates success or the reason for
448 //------------------------------------------------------------------
452 //------------------------------------------------------------------
456 /// An error object that indicates success or the reason for
458 //------------------------------------------------------------------
462 //------------------------------------------------------------------
463 /// Get the permissions for a this file.
466 /// Bits logical OR'ed together from the permission bits defined
467 /// in lldb_private::File::Permissions.
468 //------------------------------------------------------------------
470 GetPermissions(Error &error) const;
473 GetPermissions(const FileSpec &file_spec, Error &error);
476 //------------------------------------------------------------------
477 /// Return true if this file is interactive.
480 /// True if this file is a terminal (tty or pty), false
482 //------------------------------------------------------------------
486 //------------------------------------------------------------------
487 /// Return true if this file from a real terminal.
489 /// Just knowing a file is a interactive isn't enough, we also need
490 /// to know if the terminal has a width and height so we can do
491 /// cursor movement and other terminal manipulations by sending
492 /// escape sequences.
495 /// True if this file is a terminal (tty, not a pty) that has
496 /// a non-zero width and height, false otherwise.
497 //------------------------------------------------------------------
499 GetIsRealTerminal ();
501 //------------------------------------------------------------------
502 /// Output printf formatted output to the stream.
504 /// Print some formatted output to the stream.
506 /// @param[in] format
507 /// A printf style format string.
510 /// Variable arguments that are needed for the printf style
511 /// format string \a format.
512 //------------------------------------------------------------------
514 Printf (const char *format, ...) __attribute__ ((format (printf, 2, 3)));
517 PrintfVarArg(const char *format, va_list args);
521 SetOptions (uint32_t options)
529 DescriptorIsValid () const
531 return m_descriptor >= 0;
535 StreamIsValid () const
537 return m_stream != kInvalidStream;
541 CalculateInteractiveAndTerminal ();
543 //------------------------------------------------------------------
545 //------------------------------------------------------------------
550 LazyBool m_is_interactive;
551 LazyBool m_is_real_terminal;
554 } // namespace lldb_private
556 #endif // #if defined(__cplusplus)
557 #endif // liblldb_File_h_