diff options
Diffstat (limited to 'lldb/source/Host/common/File.cpp')
-rw-r--r-- | lldb/source/Host/common/File.cpp | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/lldb/source/Host/common/File.cpp b/lldb/source/Host/common/File.cpp index 343312fb484..ec33bca4905 100644 --- a/lldb/source/Host/common/File.cpp +++ b/lldb/source/Host/common/File.cpp @@ -83,6 +83,20 @@ File::File(const char *path, uint32_t options, uint32_t permissions) : Open (path, options, permissions); } +File::File (const FileSpec& filespec, + uint32_t options, + uint32_t permissions) : + m_descriptor (kInvalidDescriptor), + m_stream (kInvalidStream), + m_options (0), + m_owned (false) +{ + if (filespec) + { + Open (filespec.GetPath().c_str(), options, permissions); + } +} + File::File (const File &rhs) : m_descriptor (kInvalidDescriptor), m_stream (kInvalidStream), @@ -261,6 +275,53 @@ File::Open (const char *path, uint32_t options, uint32_t permissions) return error; } +uint32_t +File::GetPermissions (const char *path, Error &error) +{ + if (path && path[0]) + { + struct stat file_stats; + if (::stat (path, &file_stats) == -1) + error.SetErrorToErrno(); + else + { + error.Clear(); + return file_stats.st_mode; // All bits from lldb_private::File::Permissions match those in POSIX mode bits + } + } + else + { + if (path) + error.SetErrorString ("invalid path"); + else + error.SetErrorString ("empty path"); + } + return 0; +} + +uint32_t +File::GetPermissions(Error &error) const +{ + int fd = GetDescriptor(); + if (fd != kInvalidDescriptor) + { + struct stat file_stats; + if (::fstat (fd, &file_stats) == -1) + error.SetErrorToErrno(); + else + { + error.Clear(); + return file_stats.st_mode; // All bits from lldb_private::File::Permissions match those in POSIX mode bits + } + } + else + { + error.SetErrorString ("invalid file descriptor"); + } + return 0; +} + + Error File::Close () { @@ -755,3 +816,51 @@ File::PrintfVarArg (const char *format, va_list args) } return result; } + +mode_t +File::ConvertOpenOptionsForPOSIXOpen (uint32_t open_options) +{ + mode_t mode = 0; + if (open_options & eOpenOptionRead && open_options & eOpenOptionWrite) + mode |= O_RDWR; + else if (open_options & eOpenOptionWrite) + mode |= O_WRONLY; + + if (open_options & eOpenOptionAppend) + mode |= O_APPEND; + + if (open_options & eOpenOptionTruncate) + mode |= O_TRUNC; + + if (open_options & eOpenOptionNonBlocking) + mode |= O_NONBLOCK; + + if (open_options & eOpenOptionCanCreateNewOnly) + mode |= O_CREAT | O_EXCL; + else if (open_options & eOpenOptionCanCreate) + mode |= O_CREAT; + + return mode; +} + +#define O_RDONLY 0x0000 /* open for reading only */ +#define O_WRONLY 0x0001 /* open for writing only */ +#define O_RDWR 0x0002 /* open for reading and writing */ +#define O_ACCMODE 0x0003 /* mask for above modes */ +#define O_NONBLOCK 0x0004 /* no delay */ +#define O_APPEND 0x0008 /* set append mode */ +#define O_SYNC 0x0080 /* synch I/O file integrity */ +#define O_SHLOCK 0x0010 /* open with shared file lock */ +#define O_EXLOCK 0x0020 /* open with exclusive file lock */ +#define O_ASYNC 0x0040 /* signal pgrp when data ready */ +#define O_FSYNC O_SYNC /* source compatibility: do not use */ +#define O_NOFOLLOW 0x0100 /* don't follow symlinks */ +#define O_CREAT 0x0200 /* create if nonexistant */ +#define O_TRUNC 0x0400 /* truncate to zero length */ +#define O_EXCL 0x0800 /* error if already exists */ +#define O_EVTONLY 0x8000 /* descriptor requested for event notifications only */ +#define O_NOCTTY 0x20000 /* don't assign controlling terminal */ +#define O_DIRECTORY 0x100000 +#define O_SYMLINK 0x200000 /* allow open of a symlink */ +#define O_DSYNC 0x400000 /* synch I/O data integrity */ +#define O_CLOEXEC 0x1000000 /* implicitly set FD_CLOEXEC */ |