Added a file abtraction layer into the Host section of LLDB.



git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@125093 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Host/common/File.cpp b/source/Host/common/File.cpp
new file mode 100644
index 0000000..3e93fc0
--- /dev/null
+++ b/source/Host/common/File.cpp
@@ -0,0 +1,146 @@
+//===-- FileSpec.cpp --------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+
+#include "lldb/Host/File.h"
+
+#include <fcntl.h>
+
+#include "lldb/Core/Error.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+File::File(const char *path, uint32_t options, uint32_t permissions) :
+    m_file_spec (path, false),
+    m_file_desc (-1)
+{
+    Open (path, options, permissions);
+}
+
+File::~File()
+{
+    Close ();
+}
+
+Error
+File::Open (const char *path, uint32_t options, uint32_t permissions)
+{
+    Error error;
+    if (IsValid())
+        Close ();
+
+    int oflag = 0;
+    if (options & eOpenOptionRead && 
+        options & eOpenOptionWrite )
+        oflag |= O_RDWR;
+    else if (options & eOpenOptionRead)
+        oflag |= O_RDONLY;
+    else if (options & eOpenOptionWrite)
+        oflag |= O_WRONLY;
+    
+    if (options & eOpenOptionNonBlocking)
+        oflag |= O_NONBLOCK;
+
+    if (options & eOpenOptionAppend)
+        oflag |= O_APPEND;
+
+    if (options & eOpenOptionCanCreate)
+        oflag |= O_CREAT;
+    
+    if (options & eOpenOptionCanCreateNewOnly)
+        oflag |= O_CREAT | O_EXCL;
+    
+    if (options & eOpenOptionTruncate)
+        oflag |= O_TRUNC;
+    
+    if (options & eOpenOptionSharedLock)
+        oflag |= O_SHLOCK;
+
+    if (options & eOpenOptionExclusiveLock)
+        oflag |= O_EXLOCK;
+
+    mode_t mode = 0;
+    if (permissions & ePermissionsUserRead)     mode |= S_IRUSR;
+    if (permissions & ePermissionsUserWrite)    mode |= S_IWUSR;
+    if (permissions & ePermissionsUserExecute)  mode |= S_IXUSR;
+    if (permissions & ePermissionsGroupRead)    mode |= S_IRGRP;
+    if (permissions & ePermissionsGroupWrite)   mode |= S_IWGRP;
+    if (permissions & ePermissionsGroupExecute) mode |= S_IXGRP;
+    if (permissions & ePermissionsWorldRead)    mode |= S_IROTH;
+    if (permissions & ePermissionsWorldWrite)   mode |= S_IWOTH;
+    if (permissions & ePermissionsWorldExecute) mode |= S_IXOTH;
+
+    m_file_desc = ::open(path, oflag, mode);
+    if (m_file_desc == -1)
+        error.SetErrorToErrno();
+    else
+        m_file_spec.SetFile (path, false);
+    
+    return error;
+}
+
+Error
+File::Close ()
+{
+    Error error;
+    if (IsValid ())
+    {
+        if (::close (m_file_desc) != 0)
+            error.SetErrorToErrno();
+    }
+    return error;
+}
+    
+Error
+File::Read (void *buf, size_t &num_bytes)
+{
+    Error error;
+    if (IsValid ())
+    {
+        ssize_t bytes_read = ::read (m_file_desc, buf, num_bytes);
+        if (bytes_read == -1)
+        {
+            error.SetErrorToErrno();
+            num_bytes = 0;
+        }
+        else
+            num_bytes = bytes_read;
+    }
+    else 
+    {
+        num_bytes = 0;
+        error.SetErrorString("invalid file handle");
+    }
+    return error;
+}
+          
+Error
+File::Write (const void *buf, size_t &num_bytes)
+{
+    Error error;
+    if (IsValid())
+    {
+        ssize_t bytes_written = ::write (m_file_desc, buf, num_bytes);
+        if (bytes_written == -1)
+        {
+            error.SetErrorToErrno();
+            num_bytes = 0;
+        }
+        else
+            num_bytes = bytes_written;
+    }
+    else 
+    {
+        num_bytes = 0;
+        error.SetErrorString("invalid file handle");
+    }
+    return error;
+}
+