blob: 3e93fc099c8a12a8466925bdf84643f8c6daf6dc [file] [log] [blame]
Greg Clayton60a63ae2011-02-08 17:49:02 +00001//===-- FileSpec.cpp --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10
11#include "lldb/Host/File.h"
12
13#include <fcntl.h>
14
15#include "lldb/Core/Error.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
20File::File(const char *path, uint32_t options, uint32_t permissions) :
21 m_file_spec (path, false),
22 m_file_desc (-1)
23{
24 Open (path, options, permissions);
25}
26
27File::~File()
28{
29 Close ();
30}
31
32Error
33File::Open (const char *path, uint32_t options, uint32_t permissions)
34{
35 Error error;
36 if (IsValid())
37 Close ();
38
39 int oflag = 0;
40 if (options & eOpenOptionRead &&
41 options & eOpenOptionWrite )
42 oflag |= O_RDWR;
43 else if (options & eOpenOptionRead)
44 oflag |= O_RDONLY;
45 else if (options & eOpenOptionWrite)
46 oflag |= O_WRONLY;
47
48 if (options & eOpenOptionNonBlocking)
49 oflag |= O_NONBLOCK;
50
51 if (options & eOpenOptionAppend)
52 oflag |= O_APPEND;
53
54 if (options & eOpenOptionCanCreate)
55 oflag |= O_CREAT;
56
57 if (options & eOpenOptionCanCreateNewOnly)
58 oflag |= O_CREAT | O_EXCL;
59
60 if (options & eOpenOptionTruncate)
61 oflag |= O_TRUNC;
62
63 if (options & eOpenOptionSharedLock)
64 oflag |= O_SHLOCK;
65
66 if (options & eOpenOptionExclusiveLock)
67 oflag |= O_EXLOCK;
68
69 mode_t mode = 0;
70 if (permissions & ePermissionsUserRead) mode |= S_IRUSR;
71 if (permissions & ePermissionsUserWrite) mode |= S_IWUSR;
72 if (permissions & ePermissionsUserExecute) mode |= S_IXUSR;
73 if (permissions & ePermissionsGroupRead) mode |= S_IRGRP;
74 if (permissions & ePermissionsGroupWrite) mode |= S_IWGRP;
75 if (permissions & ePermissionsGroupExecute) mode |= S_IXGRP;
76 if (permissions & ePermissionsWorldRead) mode |= S_IROTH;
77 if (permissions & ePermissionsWorldWrite) mode |= S_IWOTH;
78 if (permissions & ePermissionsWorldExecute) mode |= S_IXOTH;
79
80 m_file_desc = ::open(path, oflag, mode);
81 if (m_file_desc == -1)
82 error.SetErrorToErrno();
83 else
84 m_file_spec.SetFile (path, false);
85
86 return error;
87}
88
89Error
90File::Close ()
91{
92 Error error;
93 if (IsValid ())
94 {
95 if (::close (m_file_desc) != 0)
96 error.SetErrorToErrno();
97 }
98 return error;
99}
100
101Error
102File::Read (void *buf, size_t &num_bytes)
103{
104 Error error;
105 if (IsValid ())
106 {
107 ssize_t bytes_read = ::read (m_file_desc, buf, num_bytes);
108 if (bytes_read == -1)
109 {
110 error.SetErrorToErrno();
111 num_bytes = 0;
112 }
113 else
114 num_bytes = bytes_read;
115 }
116 else
117 {
118 num_bytes = 0;
119 error.SetErrorString("invalid file handle");
120 }
121 return error;
122}
123
124Error
125File::Write (const void *buf, size_t &num_bytes)
126{
127 Error error;
128 if (IsValid())
129 {
130 ssize_t bytes_written = ::write (m_file_desc, buf, num_bytes);
131 if (bytes_written == -1)
132 {
133 error.SetErrorToErrno();
134 num_bytes = 0;
135 }
136 else
137 num_bytes = bytes_written;
138 }
139 else
140 {
141 num_bytes = 0;
142 error.SetErrorString("invalid file handle");
143 }
144 return error;
145}
146