blob: c95e08935d14cef3d39119556010afbf3b83e202 [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) :
Greg Clayton60a63ae2011-02-08 17:49:02 +000021 m_file_desc (-1)
22{
23 Open (path, options, permissions);
24}
25
26File::~File()
27{
28 Close ();
29}
30
31Error
32File::Open (const char *path, uint32_t options, uint32_t permissions)
33{
34 Error error;
35 if (IsValid())
36 Close ();
37
38 int oflag = 0;
39 if (options & eOpenOptionRead &&
40 options & eOpenOptionWrite )
41 oflag |= O_RDWR;
42 else if (options & eOpenOptionRead)
43 oflag |= O_RDONLY;
44 else if (options & eOpenOptionWrite)
45 oflag |= O_WRONLY;
46
47 if (options & eOpenOptionNonBlocking)
48 oflag |= O_NONBLOCK;
49
50 if (options & eOpenOptionAppend)
51 oflag |= O_APPEND;
52
53 if (options & eOpenOptionCanCreate)
54 oflag |= O_CREAT;
55
56 if (options & eOpenOptionCanCreateNewOnly)
57 oflag |= O_CREAT | O_EXCL;
58
59 if (options & eOpenOptionTruncate)
60 oflag |= O_TRUNC;
61
62 if (options & eOpenOptionSharedLock)
63 oflag |= O_SHLOCK;
64
65 if (options & eOpenOptionExclusiveLock)
66 oflag |= O_EXLOCK;
67
68 mode_t mode = 0;
69 if (permissions & ePermissionsUserRead) mode |= S_IRUSR;
70 if (permissions & ePermissionsUserWrite) mode |= S_IWUSR;
71 if (permissions & ePermissionsUserExecute) mode |= S_IXUSR;
72 if (permissions & ePermissionsGroupRead) mode |= S_IRGRP;
73 if (permissions & ePermissionsGroupWrite) mode |= S_IWGRP;
74 if (permissions & ePermissionsGroupExecute) mode |= S_IXGRP;
75 if (permissions & ePermissionsWorldRead) mode |= S_IROTH;
76 if (permissions & ePermissionsWorldWrite) mode |= S_IWOTH;
77 if (permissions & ePermissionsWorldExecute) mode |= S_IXOTH;
78
79 m_file_desc = ::open(path, oflag, mode);
80 if (m_file_desc == -1)
81 error.SetErrorToErrno();
Greg Clayton60a63ae2011-02-08 17:49:02 +000082
83 return error;
84}
85
86Error
87File::Close ()
88{
89 Error error;
90 if (IsValid ())
91 {
92 if (::close (m_file_desc) != 0)
93 error.SetErrorToErrno();
Greg Clayton882ef052011-02-08 19:10:53 +000094 m_file_desc = -1;
Greg Clayton60a63ae2011-02-08 17:49:02 +000095 }
96 return error;
97}
Greg Clayton882ef052011-02-08 19:10:53 +000098
99
100Error
101File::GetFileSpec (FileSpec &file_spec) const
102{
103 Error error;
104 if (IsValid ())
105 {
106 char path[PATH_MAX];
107 if (::fcntl(m_file_desc, F_GETPATH, path) == -1)
108 error.SetErrorToErrno();
109 else
110 file_spec.SetFile (path, false);
111 }
112 else
113 error.SetErrorString("invalid file handle");
114
115 if (error.Fail())
116 file_spec.Clear();
117 return error;
118}
119
120Error
121File::SeekFromStart (off_t& offset)
122{
123 Error error;
124 if (IsValid ())
125 {
126 offset = ::lseek (m_file_desc, offset, SEEK_SET);
127
128 if (offset == -1)
129 error.SetErrorToErrno();
130 }
131 else
132 {
133 error.SetErrorString("invalid file handle");
134 }
135 return error;
136}
137
138Error
139File::SeekFromCurrent (off_t& offset)
140{
141 Error error;
142 if (IsValid ())
143 {
144 offset = ::lseek (m_file_desc, offset, SEEK_CUR);
145
146 if (offset == -1)
147 error.SetErrorToErrno();
148 }
149 else
150 {
151 error.SetErrorString("invalid file handle");
152 }
153 return error;
154}
155
156Error
157File::SeekFromEnd (off_t& offset)
158{
159 Error error;
160 if (IsValid ())
161 {
162 offset = ::lseek (m_file_desc, offset, SEEK_CUR);
163
164 if (offset == -1)
165 error.SetErrorToErrno();
166 }
167 else
168 {
169 error.SetErrorString("invalid file handle");
170 }
171 return error;
172}
173
174Error
175File::Sync ()
176{
177 Error error;
178 if (IsValid ())
179 {
180 if (::fsync (m_file_desc) == -1)
181 error.SetErrorToErrno();
182 }
183 else
184 {
185 error.SetErrorString("invalid file handle");
186 }
187 return error;
188}
Greg Clayton60a63ae2011-02-08 17:49:02 +0000189Error
190File::Read (void *buf, size_t &num_bytes)
191{
192 Error error;
193 if (IsValid ())
194 {
195 ssize_t bytes_read = ::read (m_file_desc, buf, num_bytes);
196 if (bytes_read == -1)
197 {
198 error.SetErrorToErrno();
199 num_bytes = 0;
200 }
201 else
202 num_bytes = bytes_read;
203 }
204 else
205 {
206 num_bytes = 0;
207 error.SetErrorString("invalid file handle");
208 }
209 return error;
210}
211
212Error
213File::Write (const void *buf, size_t &num_bytes)
214{
215 Error error;
216 if (IsValid())
217 {
218 ssize_t bytes_written = ::write (m_file_desc, buf, num_bytes);
219 if (bytes_written == -1)
220 {
221 error.SetErrorToErrno();
222 num_bytes = 0;
223 }
224 else
225 num_bytes = bytes_written;
226 }
227 else
228 {
229 num_bytes = 0;
230 error.SetErrorString("invalid file handle");
231 }
232 return error;
233}
234