aboutsummaryrefslogtreecommitdiff
path: root/source/Host/common/File.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/Host/common/File.cpp')
-rw-r--r--source/Host/common/File.cpp126
1 files changed, 126 insertions, 0 deletions
diff --git a/source/Host/common/File.cpp b/source/Host/common/File.cpp
index c0d3c290f4c7..addd43515405 100644
--- a/source/Host/common/File.cpp
+++ b/source/Host/common/File.cpp
@@ -16,6 +16,10 @@
#include <stdarg.h>
#include <sys/stat.h>
+#ifdef _WIN32
+#include "lldb/Host/windows/windows.h"
+#endif
+
#include "lldb/Core/DataBufferHeap.h"
#include "lldb/Core/Error.h"
#include "lldb/Host/Config.h"
@@ -79,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),
@@ -167,7 +185,11 @@ File::Duplicate (const File &rhs)
if (rhs.DescriptorIsValid())
{
+#ifdef _WIN32
+ m_descriptor = ::_dup(rhs.GetDescriptor());
+#else
m_descriptor = ::fcntl(rhs.GetDescriptor(), F_DUPFD);
+#endif
if (!DescriptorIsValid())
error.SetErrorToErrno();
else
@@ -217,8 +239,12 @@ File::Open (const char *path, uint32_t options, uint32_t permissions)
oflag |= O_RDONLY;
}
+#ifndef _WIN32
if (options & eOpenOptionNonBlocking)
oflag |= O_NONBLOCK;
+#else
+ oflag |= O_BINARY;
+#endif
mode_t mode = 0;
if (oflag & O_CREAT)
@@ -247,6 +273,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 ()
{
@@ -452,6 +525,11 @@ File::Sync ()
Error error;
if (DescriptorIsValid())
{
+#ifdef _WIN32
+ int err = FlushFileBuffers((HANDLE)_get_osfhandle(m_descriptor));
+ if (err == 0)
+ error.SetErrorToGenericError();
+#else
int err = 0;
do
{
@@ -460,6 +538,7 @@ File::Sync ()
if (err == -1)
error.SetErrorToErrno();
+#endif
}
else
{
@@ -559,6 +638,7 @@ File::Write (const void *buf, size_t &num_bytes)
Error
File::Read (void *buf, size_t &num_bytes, off_t &offset)
{
+#ifndef _WIN32
Error error;
int fd = GetDescriptor();
if (fd != kInvalidDescriptor)
@@ -586,6 +666,14 @@ File::Read (void *buf, size_t &num_bytes, off_t &offset)
error.SetErrorString("invalid file handle");
}
return error;
+#else
+ long cur = ::lseek(m_descriptor, 0, SEEK_CUR);
+ SeekFromStart(offset);
+ Error error = Read(buf, num_bytes);
+ if (!error.Fail())
+ SeekFromStart(cur);
+ return error;
+#endif
}
Error
@@ -648,6 +736,7 @@ File::Write (const void *buf, size_t &num_bytes, off_t &offset)
int fd = GetDescriptor();
if (fd != kInvalidDescriptor)
{
+#ifndef _WIN32
ssize_t bytes_written = -1;
do
{
@@ -664,6 +753,17 @@ File::Write (const void *buf, size_t &num_bytes, off_t &offset)
offset += bytes_written;
num_bytes = bytes_written;
}
+#else
+ long cur = ::lseek(m_descriptor, 0, SEEK_CUR);
+ error = Write(buf, num_bytes);
+ long after = ::lseek(m_descriptor, 0, SEEK_CUR);
+
+ if (!error.Fail())
+ SeekFromStart(cur);
+
+ ssize_t bytes_written = after - cur;
+ offset = after;
+#endif
}
else
{
@@ -714,3 +814,29 @@ 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;
+}