blob: 71a6149cd614c11b728b3015757f6dbec0fbd458 [file] [log] [blame]
Greg Claytonfbb76342013-11-20 21:07:01 +00001//===-- File.cpp ------------------------------------------------*- C++ -*-===//
Greg Clayton504f89a2011-02-08 17:49:02 +00002//
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
Greg Clayton504f89a2011-02-08 17:49:02 +000010#include "lldb/Host/File.h"
11
Greg Clayton96c09682012-01-04 22:56:43 +000012#include <errno.h>
Greg Clayton504f89a2011-02-08 17:49:02 +000013#include <fcntl.h>
Stephen Wilson8acdbb82011-04-08 13:36:44 +000014#include <limits.h>
Greg Claytonf4dd5432011-02-09 21:12:40 +000015#include <stdarg.h>
Greg Clayton340b0302014-02-05 17:57:57 +000016#include <stdio.h>
Greg Clayton2b4d9b72011-04-01 18:18:34 +000017#include <sys/stat.h>
Greg Clayton504f89a2011-02-08 17:49:02 +000018
Virgile Bellob2f1fb22013-08-23 12:44:05 +000019#ifdef _WIN32
20#include "lldb/Host/windows/windows.h"
Deepak Panickal94667bc2014-02-17 17:43:39 +000021#else
22#include <sys/ioctl.h>
Virgile Bellob2f1fb22013-08-23 12:44:05 +000023#endif
24
Sean Callanan66810412015-10-19 23:11:07 +000025#include "llvm/Support/Process.h" // for llvm::sys::Process::FileDescriptorHasColors()
26
Greg Clayton96c09682012-01-04 22:56:43 +000027#include "lldb/Core/DataBufferHeap.h"
Greg Clayton504f89a2011-02-08 17:49:02 +000028#include "lldb/Core/Error.h"
Zachary Turner98688922014-08-06 18:16:26 +000029#include "lldb/Core/Log.h"
Greg Clayton925137c2011-02-09 01:16:43 +000030#include "lldb/Host/Config.h"
Greg Clayton846b64b2011-02-08 19:54:44 +000031#include "lldb/Host/FileSpec.h"
Greg Clayton504f89a2011-02-08 17:49:02 +000032
33using namespace lldb;
34using namespace lldb_private;
35
Greg Clayton51b1e2d2011-02-09 01:08:52 +000036static const char *
37GetStreamOpenModeFromOptions (uint32_t options)
38{
39 if (options & File::eOpenOptionAppend)
40 {
41 if (options & File::eOpenOptionRead)
42 {
43 if (options & File::eOpenOptionCanCreateNewOnly)
44 return "a+x";
45 else
46 return "a+";
47 }
48 else if (options & File::eOpenOptionWrite)
49 {
50 if (options & File::eOpenOptionCanCreateNewOnly)
51 return "ax";
52 else
53 return "a";
54 }
55 }
56 else if (options & File::eOpenOptionRead && options & File::eOpenOptionWrite)
57 {
58 if (options & File::eOpenOptionCanCreate)
59 {
60 if (options & File::eOpenOptionCanCreateNewOnly)
61 return "w+x";
62 else
63 return "w+";
64 }
65 else
66 return "r+";
67 }
68 else if (options & File::eOpenOptionRead)
69 {
70 return "r";
71 }
72 else if (options & File::eOpenOptionWrite)
73 {
74 return "w";
75 }
76 return NULL;
77}
78
79int File::kInvalidDescriptor = -1;
80FILE * File::kInvalidStream = NULL;
81
Greg Clayton504f89a2011-02-08 17:49:02 +000082File::File(const char *path, uint32_t options, uint32_t permissions) :
Zachary Turner98688922014-08-06 18:16:26 +000083 IOObject(eFDTypeFile, false),
Greg Clayton51b1e2d2011-02-09 01:08:52 +000084 m_descriptor (kInvalidDescriptor),
85 m_stream (kInvalidStream),
Greg Clayton44d93782014-01-27 23:43:24 +000086 m_options (),
87 m_own_stream (false),
Greg Clayton340b0302014-02-05 17:57:57 +000088 m_is_interactive (eLazyBoolCalculate),
89 m_is_real_terminal (eLazyBoolCalculate)
Greg Clayton504f89a2011-02-08 17:49:02 +000090{
91 Open (path, options, permissions);
92}
93
Daniel Maleae0f8f572013-08-26 23:57:52 +000094File::File (const FileSpec& filespec,
95 uint32_t options,
96 uint32_t permissions) :
Zachary Turner98688922014-08-06 18:16:26 +000097 IOObject(eFDTypeFile, false),
Daniel Maleae0f8f572013-08-26 23:57:52 +000098 m_descriptor (kInvalidDescriptor),
99 m_stream (kInvalidStream),
100 m_options (0),
Greg Clayton44d93782014-01-27 23:43:24 +0000101 m_own_stream (false),
Greg Clayton340b0302014-02-05 17:57:57 +0000102 m_is_interactive (eLazyBoolCalculate),
103 m_is_real_terminal (eLazyBoolCalculate)
104
Daniel Maleae0f8f572013-08-26 23:57:52 +0000105{
106 if (filespec)
107 {
108 Open (filespec.GetPath().c_str(), options, permissions);
109 }
110}
111
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000112File::File (const File &rhs) :
Zachary Turner98688922014-08-06 18:16:26 +0000113 IOObject(eFDTypeFile, false),
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000114 m_descriptor (kInvalidDescriptor),
115 m_stream (kInvalidStream),
116 m_options (0),
Greg Clayton44d93782014-01-27 23:43:24 +0000117 m_own_stream (false),
Greg Clayton340b0302014-02-05 17:57:57 +0000118 m_is_interactive (eLazyBoolCalculate),
119 m_is_real_terminal (eLazyBoolCalculate)
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000120{
121 Duplicate (rhs);
122}
123
124
125File &
126File::operator = (const File &rhs)
127{
128 if (this != &rhs)
129 Duplicate (rhs);
130 return *this;
131}
132
Greg Clayton504f89a2011-02-08 17:49:02 +0000133File::~File()
134{
135 Close ();
136}
137
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000138
139int
140File::GetDescriptor() const
141{
142 if (DescriptorIsValid())
143 return m_descriptor;
144
145 // Don't open the file descriptor if we don't need to, just get it from the
146 // stream if we have one.
147 if (StreamIsValid())
Zachary Turner9c402642015-10-15 19:35:48 +0000148 {
149#if defined(LLVM_ON_WIN32)
150 return _fileno(m_stream);
151#else
152 return fileno(m_stream);
153#endif
154 }
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000155
156 // Invalid descriptor and invalid stream, return invalid descriptor.
157 return kInvalidDescriptor;
158}
159
Zachary Turner98688922014-08-06 18:16:26 +0000160IOObject::WaitableHandle
161File::GetWaitableHandle()
162{
163 return m_descriptor;
164}
165
166
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000167void
168File::SetDescriptor (int fd, bool transfer_ownership)
169{
170 if (IsValid())
171 Close();
172 m_descriptor = fd;
Zachary Turner98688922014-08-06 18:16:26 +0000173 m_should_close_fd = transfer_ownership;
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000174}
175
176
177FILE *
178File::GetStream ()
179{
180 if (!StreamIsValid())
181 {
182 if (DescriptorIsValid())
183 {
184 const char *mode = GetStreamOpenModeFromOptions (m_options);
185 if (mode)
Greg Clayton96c09682012-01-04 22:56:43 +0000186 {
Zachary Turner98688922014-08-06 18:16:26 +0000187 if (!m_should_close_fd)
Greg Clayton44d93782014-01-27 23:43:24 +0000188 {
189 // We must duplicate the file descriptor if we don't own it because
190 // when you call fdopen, the stream will own the fd
191#ifdef _WIN32
192 m_descriptor = ::_dup(GetDescriptor());
193#else
194 m_descriptor = ::fcntl(GetDescriptor(), F_DUPFD);
195#endif
Zachary Turner98688922014-08-06 18:16:26 +0000196 m_should_close_fd = true;
Greg Clayton44d93782014-01-27 23:43:24 +0000197 }
198
Greg Clayton96c09682012-01-04 22:56:43 +0000199 do
200 {
201 m_stream = ::fdopen (m_descriptor, mode);
202 } while (m_stream == NULL && errno == EINTR);
Greg Clayton44d93782014-01-27 23:43:24 +0000203
204 // If we got a stream, then we own the stream and should no
205 // longer own the descriptor because fclose() will close it for us
206
207 if (m_stream)
208 {
209 m_own_stream = true;
Zachary Turner98688922014-08-06 18:16:26 +0000210 m_should_close_fd = false;
Greg Clayton44d93782014-01-27 23:43:24 +0000211 }
Greg Clayton96c09682012-01-04 22:56:43 +0000212 }
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000213 }
214 }
215 return m_stream;
216}
217
218
219void
220File::SetStream (FILE *fh, bool transfer_ownership)
221{
222 if (IsValid())
223 Close();
224 m_stream = fh;
Greg Clayton44d93782014-01-27 23:43:24 +0000225 m_own_stream = transfer_ownership;
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000226}
227
228Error
229File::Duplicate (const File &rhs)
230{
231 Error error;
232 if (IsValid ())
233 Close();
234
235 if (rhs.DescriptorIsValid())
236 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000237#ifdef _WIN32
238 m_descriptor = ::_dup(rhs.GetDescriptor());
239#else
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000240 m_descriptor = ::fcntl(rhs.GetDescriptor(), F_DUPFD);
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000241#endif
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000242 if (!DescriptorIsValid())
243 error.SetErrorToErrno();
244 else
245 {
246 m_options = rhs.m_options;
Zachary Turner98688922014-08-06 18:16:26 +0000247 m_should_close_fd = true;
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000248 }
249 }
250 else
251 {
252 error.SetErrorString ("invalid file to duplicate");
253 }
254 return error;
255}
256
Greg Clayton504f89a2011-02-08 17:49:02 +0000257Error
258File::Open (const char *path, uint32_t options, uint32_t permissions)
259{
260 Error error;
261 if (IsValid())
262 Close ();
263
264 int oflag = 0;
Greg Clayton96c09682012-01-04 22:56:43 +0000265 const bool read = options & eOpenOptionRead;
266 const bool write = options & eOpenOptionWrite;
267 if (write)
268 {
269 if (read)
270 oflag |= O_RDWR;
271 else
272 oflag |= O_WRONLY;
273
274 if (options & eOpenOptionAppend)
275 oflag |= O_APPEND;
276
277 if (options & eOpenOptionTruncate)
278 oflag |= O_TRUNC;
279
280 if (options & eOpenOptionCanCreate)
281 oflag |= O_CREAT;
282
283 if (options & eOpenOptionCanCreateNewOnly)
284 oflag |= O_CREAT | O_EXCL;
285 }
286 else if (read)
287 {
Greg Clayton504f89a2011-02-08 17:49:02 +0000288 oflag |= O_RDONLY;
Greg Claytonfbb76342013-11-20 21:07:01 +0000289
Colin Riley909bb7a2013-11-26 15:10:46 +0000290#ifndef _WIN32
Greg Claytonfbb76342013-11-20 21:07:01 +0000291 if (options & eOpenoptionDontFollowSymlinks)
292 oflag |= O_NOFOLLOW;
Colin Riley909bb7a2013-11-26 15:10:46 +0000293#endif
Greg Clayton96c09682012-01-04 22:56:43 +0000294 }
Greg Clayton504f89a2011-02-08 17:49:02 +0000295
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000296#ifndef _WIN32
Greg Clayton504f89a2011-02-08 17:49:02 +0000297 if (options & eOpenOptionNonBlocking)
298 oflag |= O_NONBLOCK;
Pavel Labath97a9ac12015-02-05 16:44:42 +0000299 if (options & eOpenOptionCloseOnExec)
300 oflag |= O_CLOEXEC;
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000301#else
302 oflag |= O_BINARY;
303#endif
Greg Clayton504f89a2011-02-08 17:49:02 +0000304
Greg Clayton504f89a2011-02-08 17:49:02 +0000305 mode_t mode = 0;
Greg Clayton96c09682012-01-04 22:56:43 +0000306 if (oflag & O_CREAT)
307 {
Greg Claytonfbb76342013-11-20 21:07:01 +0000308 if (permissions & lldb::eFilePermissionsUserRead) mode |= S_IRUSR;
309 if (permissions & lldb::eFilePermissionsUserWrite) mode |= S_IWUSR;
310 if (permissions & lldb::eFilePermissionsUserExecute) mode |= S_IXUSR;
311 if (permissions & lldb::eFilePermissionsGroupRead) mode |= S_IRGRP;
312 if (permissions & lldb::eFilePermissionsGroupWrite) mode |= S_IWGRP;
313 if (permissions & lldb::eFilePermissionsGroupExecute) mode |= S_IXGRP;
314 if (permissions & lldb::eFilePermissionsWorldRead) mode |= S_IROTH;
315 if (permissions & lldb::eFilePermissionsWorldWrite) mode |= S_IWOTH;
316 if (permissions & lldb::eFilePermissionsWorldExecute) mode |= S_IXOTH;
Greg Clayton96c09682012-01-04 22:56:43 +0000317 }
Greg Clayton504f89a2011-02-08 17:49:02 +0000318
Greg Clayton96c09682012-01-04 22:56:43 +0000319 do
320 {
321 m_descriptor = ::open(path, oflag, mode);
322 } while (m_descriptor < 0 && errno == EINTR);
323
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000324 if (!DescriptorIsValid())
Greg Clayton504f89a2011-02-08 17:49:02 +0000325 error.SetErrorToErrno();
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000326 else
Greg Clayton44d93782014-01-27 23:43:24 +0000327 {
Zachary Turner98688922014-08-06 18:16:26 +0000328 m_should_close_fd = true;
Greg Clayton44d93782014-01-27 23:43:24 +0000329 m_options = options;
330 }
Greg Clayton504f89a2011-02-08 17:49:02 +0000331
332 return error;
333}
334
Daniel Maleae0f8f572013-08-26 23:57:52 +0000335uint32_t
Chaoren Lind3173f32015-05-29 19:52:29 +0000336File::GetPermissions(const FileSpec &file_spec, Error &error)
Daniel Maleae0f8f572013-08-26 23:57:52 +0000337{
Chaoren Lind3173f32015-05-29 19:52:29 +0000338 if (file_spec)
Daniel Maleae0f8f572013-08-26 23:57:52 +0000339 {
340 struct stat file_stats;
Chaoren Lind3173f32015-05-29 19:52:29 +0000341 if (::stat(file_spec.GetCString(), &file_stats) == -1)
Daniel Maleae0f8f572013-08-26 23:57:52 +0000342 error.SetErrorToErrno();
343 else
344 {
345 error.Clear();
Greg Claytonc0293822013-11-22 18:46:55 +0000346 return file_stats.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000347 }
348 }
349 else
Chaoren Lind3173f32015-05-29 19:52:29 +0000350 error.SetErrorString ("empty file spec");
Daniel Maleae0f8f572013-08-26 23:57:52 +0000351 return 0;
352}
353
354uint32_t
355File::GetPermissions(Error &error) const
356{
357 int fd = GetDescriptor();
358 if (fd != kInvalidDescriptor)
359 {
360 struct stat file_stats;
361 if (::fstat (fd, &file_stats) == -1)
362 error.SetErrorToErrno();
363 else
364 {
365 error.Clear();
Greg Claytonc0293822013-11-22 18:46:55 +0000366 return file_stats.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000367 }
368 }
369 else
370 {
371 error.SetErrorString ("invalid file descriptor");
372 }
373 return 0;
374}
375
376
Greg Clayton504f89a2011-02-08 17:49:02 +0000377Error
378File::Close ()
379{
380 Error error;
Greg Clayton44d93782014-01-27 23:43:24 +0000381 if (StreamIsValid() && m_own_stream)
Greg Clayton504f89a2011-02-08 17:49:02 +0000382 {
Greg Clayton44d93782014-01-27 23:43:24 +0000383 if (::fclose (m_stream) == EOF)
384 error.SetErrorToErrno();
Greg Clayton504f89a2011-02-08 17:49:02 +0000385 }
Greg Clayton44d93782014-01-27 23:43:24 +0000386
Zachary Turner98688922014-08-06 18:16:26 +0000387 if (DescriptorIsValid() && m_should_close_fd)
Greg Clayton44d93782014-01-27 23:43:24 +0000388 {
389 if (::close (m_descriptor) != 0)
390 error.SetErrorToErrno();
391 }
392 m_descriptor = kInvalidDescriptor;
393 m_stream = kInvalidStream;
394 m_options = 0;
395 m_own_stream = false;
Zachary Turner98688922014-08-06 18:16:26 +0000396 m_should_close_fd = false;
Greg Clayton340b0302014-02-05 17:57:57 +0000397 m_is_interactive = eLazyBoolCalculate;
398 m_is_real_terminal = eLazyBoolCalculate;
Greg Clayton504f89a2011-02-08 17:49:02 +0000399 return error;
400}
Greg Clayton968fa382011-02-08 19:10:53 +0000401
402
403Error
404File::GetFileSpec (FileSpec &file_spec) const
405{
406 Error error;
Greg Clayton925137c2011-02-09 01:16:43 +0000407#ifdef LLDB_CONFIG_FCNTL_GETPATH_SUPPORTED
Greg Clayton968fa382011-02-08 19:10:53 +0000408 if (IsValid ())
409 {
410 char path[PATH_MAX];
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000411 if (::fcntl(GetDescriptor(), F_GETPATH, path) == -1)
Greg Clayton968fa382011-02-08 19:10:53 +0000412 error.SetErrorToErrno();
413 else
414 file_spec.SetFile (path, false);
415 }
416 else
Greg Clayton925137c2011-02-09 01:16:43 +0000417 {
Greg Clayton968fa382011-02-08 19:10:53 +0000418 error.SetErrorString("invalid file handle");
Greg Clayton925137c2011-02-09 01:16:43 +0000419 }
Greg Clayton94d08622011-02-09 07:19:18 +0000420#elif defined(__linux__)
421 char proc[64];
422 char path[PATH_MAX];
423 if (::snprintf(proc, sizeof(proc), "/proc/self/fd/%d", GetDescriptor()) < 0)
Greg Clayton86edbf42011-10-26 00:56:27 +0000424 error.SetErrorString ("cannot resolve file descriptor");
Greg Clayton94d08622011-02-09 07:19:18 +0000425 else
426 {
427 ssize_t len;
428 if ((len = ::readlink(proc, path, sizeof(path) - 1)) == -1)
429 error.SetErrorToErrno();
430 else
431 {
432 path[len] = '\0';
433 file_spec.SetFile (path, false);
434 }
435 }
Greg Clayton925137c2011-02-09 01:16:43 +0000436#else
Greg Clayton94d08622011-02-09 07:19:18 +0000437 error.SetErrorString ("File::GetFileSpec is not supported on this platform");
Greg Clayton925137c2011-02-09 01:16:43 +0000438#endif
Greg Clayton968fa382011-02-08 19:10:53 +0000439
440 if (error.Fail())
441 file_spec.Clear();
442 return error;
443}
444
Greg Clayton43d82792013-05-22 23:30:09 +0000445off_t
446File::SeekFromStart (off_t offset, Error *error_ptr)
Greg Clayton968fa382011-02-08 19:10:53 +0000447{
Greg Clayton43d82792013-05-22 23:30:09 +0000448 off_t result = 0;
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000449 if (DescriptorIsValid())
Greg Clayton968fa382011-02-08 19:10:53 +0000450 {
Greg Clayton43d82792013-05-22 23:30:09 +0000451 result = ::lseek (m_descriptor, offset, SEEK_SET);
Greg Clayton968fa382011-02-08 19:10:53 +0000452
Greg Clayton43d82792013-05-22 23:30:09 +0000453 if (error_ptr)
454 {
455 if (result == -1)
456 error_ptr->SetErrorToErrno();
457 else
458 error_ptr->Clear();
459 }
Greg Clayton968fa382011-02-08 19:10:53 +0000460 }
Greg Clayton43d82792013-05-22 23:30:09 +0000461 else if (StreamIsValid ())
Greg Clayton968fa382011-02-08 19:10:53 +0000462 {
Greg Clayton43d82792013-05-22 23:30:09 +0000463 result = ::fseek(m_stream, offset, SEEK_SET);
464
465 if (error_ptr)
466 {
467 if (result == -1)
468 error_ptr->SetErrorToErrno();
469 else
470 error_ptr->Clear();
471 }
Greg Clayton968fa382011-02-08 19:10:53 +0000472 }
Greg Clayton43d82792013-05-22 23:30:09 +0000473 else if (error_ptr)
474 {
475 error_ptr->SetErrorString("invalid file handle");
476 }
477 return result;
Greg Clayton968fa382011-02-08 19:10:53 +0000478}
479
Greg Clayton43d82792013-05-22 23:30:09 +0000480off_t
481File::SeekFromCurrent (off_t offset, Error *error_ptr)
Greg Clayton968fa382011-02-08 19:10:53 +0000482{
Greg Clayton43d82792013-05-22 23:30:09 +0000483 off_t result = -1;
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000484 if (DescriptorIsValid())
Greg Clayton968fa382011-02-08 19:10:53 +0000485 {
Greg Clayton43d82792013-05-22 23:30:09 +0000486 result = ::lseek (m_descriptor, offset, SEEK_CUR);
Greg Clayton968fa382011-02-08 19:10:53 +0000487
Greg Clayton43d82792013-05-22 23:30:09 +0000488 if (error_ptr)
489 {
490 if (result == -1)
491 error_ptr->SetErrorToErrno();
492 else
493 error_ptr->Clear();
494 }
Greg Clayton968fa382011-02-08 19:10:53 +0000495 }
Greg Clayton43d82792013-05-22 23:30:09 +0000496 else if (StreamIsValid ())
Greg Clayton968fa382011-02-08 19:10:53 +0000497 {
Greg Clayton43d82792013-05-22 23:30:09 +0000498 result = ::fseek(m_stream, offset, SEEK_CUR);
499
500 if (error_ptr)
501 {
502 if (result == -1)
503 error_ptr->SetErrorToErrno();
504 else
505 error_ptr->Clear();
506 }
Greg Clayton968fa382011-02-08 19:10:53 +0000507 }
Greg Clayton43d82792013-05-22 23:30:09 +0000508 else if (error_ptr)
509 {
510 error_ptr->SetErrorString("invalid file handle");
511 }
512 return result;
Greg Clayton968fa382011-02-08 19:10:53 +0000513}
514
Greg Clayton43d82792013-05-22 23:30:09 +0000515off_t
516File::SeekFromEnd (off_t offset, Error *error_ptr)
Greg Clayton968fa382011-02-08 19:10:53 +0000517{
Greg Clayton43d82792013-05-22 23:30:09 +0000518 off_t result = -1;
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000519 if (DescriptorIsValid())
Greg Clayton968fa382011-02-08 19:10:53 +0000520 {
Greg Clayton43d82792013-05-22 23:30:09 +0000521 result = ::lseek (m_descriptor, offset, SEEK_END);
Greg Clayton968fa382011-02-08 19:10:53 +0000522
Greg Clayton43d82792013-05-22 23:30:09 +0000523 if (error_ptr)
524 {
525 if (result == -1)
526 error_ptr->SetErrorToErrno();
527 else
528 error_ptr->Clear();
529 }
Greg Clayton968fa382011-02-08 19:10:53 +0000530 }
Greg Clayton43d82792013-05-22 23:30:09 +0000531 else if (StreamIsValid ())
Greg Clayton968fa382011-02-08 19:10:53 +0000532 {
Greg Clayton43d82792013-05-22 23:30:09 +0000533 result = ::fseek(m_stream, offset, SEEK_END);
534
535 if (error_ptr)
536 {
537 if (result == -1)
538 error_ptr->SetErrorToErrno();
539 else
540 error_ptr->Clear();
541 }
Greg Clayton968fa382011-02-08 19:10:53 +0000542 }
Greg Clayton43d82792013-05-22 23:30:09 +0000543 else if (error_ptr)
544 {
545 error_ptr->SetErrorString("invalid file handle");
546 }
547 return result;
Greg Clayton968fa382011-02-08 19:10:53 +0000548}
549
550Error
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000551File::Flush ()
552{
553 Error error;
554 if (StreamIsValid())
555 {
Greg Clayton96c09682012-01-04 22:56:43 +0000556 int err = 0;
557 do
558 {
559 err = ::fflush (m_stream);
560 } while (err == EOF && errno == EINTR);
561
562 if (err == EOF)
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000563 error.SetErrorToErrno();
564 }
565 else if (!DescriptorIsValid())
566 {
567 error.SetErrorString("invalid file handle");
568 }
569 return error;
570}
571
572
573Error
Greg Clayton968fa382011-02-08 19:10:53 +0000574File::Sync ()
575{
576 Error error;
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000577 if (DescriptorIsValid())
Greg Clayton968fa382011-02-08 19:10:53 +0000578 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000579#ifdef _WIN32
580 int err = FlushFileBuffers((HANDLE)_get_osfhandle(m_descriptor));
581 if (err == 0)
582 error.SetErrorToGenericError();
583#else
Greg Clayton96c09682012-01-04 22:56:43 +0000584 int err = 0;
585 do
586 {
587 err = ::fsync (m_descriptor);
588 } while (err == -1 && errno == EINTR);
589
590 if (err == -1)
Greg Clayton968fa382011-02-08 19:10:53 +0000591 error.SetErrorToErrno();
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000592#endif
Greg Clayton968fa382011-02-08 19:10:53 +0000593 }
594 else
595 {
596 error.SetErrorString("invalid file handle");
597 }
598 return error;
599}
Greg Clayton846b64b2011-02-08 19:54:44 +0000600
Greg Clayton29aac9a2015-04-09 00:06:44 +0000601#if defined (__APPLE__)
602// Darwin kernels only can read/write <= INT_MAX bytes
603#define MAX_READ_SIZE INT_MAX
604#define MAX_WRITE_SIZE INT_MAX
605#endif
606
Greg Clayton504f89a2011-02-08 17:49:02 +0000607Error
608File::Read (void *buf, size_t &num_bytes)
609{
610 Error error;
Greg Clayton29aac9a2015-04-09 00:06:44 +0000611
612#if defined (MAX_READ_SIZE)
613 if (num_bytes > MAX_READ_SIZE)
614 {
615 uint8_t *p = (uint8_t *)buf;
616 size_t bytes_left = num_bytes;
617 // Init the num_bytes read to zero
618 num_bytes = 0;
619
620 while (bytes_left > 0)
621 {
622 size_t curr_num_bytes;
623 if (bytes_left > MAX_READ_SIZE)
624 curr_num_bytes = MAX_READ_SIZE;
625 else
626 curr_num_bytes = bytes_left;
627
628 error = Read (p + num_bytes, curr_num_bytes);
629
630 // Update how many bytes were read
631 num_bytes += curr_num_bytes;
632 if (bytes_left < curr_num_bytes)
633 bytes_left = 0;
634 else
635 bytes_left -= curr_num_bytes;
636
637 if (error.Fail())
638 break;
639 }
640 return error;
641 }
642#endif
643
Greg Clayton96c09682012-01-04 22:56:43 +0000644 ssize_t bytes_read = -1;
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000645 if (DescriptorIsValid())
Greg Clayton504f89a2011-02-08 17:49:02 +0000646 {
Greg Clayton96c09682012-01-04 22:56:43 +0000647 do
648 {
649 bytes_read = ::read (m_descriptor, buf, num_bytes);
650 } while (bytes_read < 0 && errno == EINTR);
651
Greg Clayton504f89a2011-02-08 17:49:02 +0000652 if (bytes_read == -1)
653 {
654 error.SetErrorToErrno();
655 num_bytes = 0;
656 }
657 else
658 num_bytes = bytes_read;
659 }
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000660 else if (StreamIsValid())
661 {
Greg Clayton96c09682012-01-04 22:56:43 +0000662 bytes_read = ::fread (buf, 1, num_bytes, m_stream);
663
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000664 if (bytes_read == 0)
665 {
666 if (::feof(m_stream))
667 error.SetErrorString ("feof");
668 else if (::ferror (m_stream))
669 error.SetErrorString ("ferror");
670 num_bytes = 0;
671 }
672 else
673 num_bytes = bytes_read;
674 }
Greg Clayton504f89a2011-02-08 17:49:02 +0000675 else
676 {
677 num_bytes = 0;
678 error.SetErrorString("invalid file handle");
679 }
680 return error;
681}
682
683Error
684File::Write (const void *buf, size_t &num_bytes)
685{
686 Error error;
Greg Clayton29aac9a2015-04-09 00:06:44 +0000687
688#if defined (MAX_WRITE_SIZE)
689 if (num_bytes > MAX_WRITE_SIZE)
690 {
691 const uint8_t *p = (const uint8_t *)buf;
692 size_t bytes_left = num_bytes;
693 // Init the num_bytes written to zero
694 num_bytes = 0;
695
696 while (bytes_left > 0)
697 {
698 size_t curr_num_bytes;
699 if (bytes_left > MAX_WRITE_SIZE)
700 curr_num_bytes = MAX_WRITE_SIZE;
701 else
702 curr_num_bytes = bytes_left;
703
704 error = Write (p + num_bytes, curr_num_bytes);
705
706 // Update how many bytes were read
707 num_bytes += curr_num_bytes;
708 if (bytes_left < curr_num_bytes)
709 bytes_left = 0;
710 else
711 bytes_left -= curr_num_bytes;
712
713 if (error.Fail())
714 break;
715 }
716 return error;
717 }
718#endif
719
Greg Clayton96c09682012-01-04 22:56:43 +0000720 ssize_t bytes_written = -1;
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000721 if (DescriptorIsValid())
Greg Clayton504f89a2011-02-08 17:49:02 +0000722 {
Greg Clayton96c09682012-01-04 22:56:43 +0000723 do
724 {
725 bytes_written = ::write (m_descriptor, buf, num_bytes);
726 } while (bytes_written < 0 && errno == EINTR);
727
Greg Clayton504f89a2011-02-08 17:49:02 +0000728 if (bytes_written == -1)
729 {
730 error.SetErrorToErrno();
731 num_bytes = 0;
732 }
733 else
734 num_bytes = bytes_written;
735 }
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000736 else if (StreamIsValid())
737 {
Greg Clayton96c09682012-01-04 22:56:43 +0000738 bytes_written = ::fwrite (buf, 1, num_bytes, m_stream);
739
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000740 if (bytes_written == 0)
741 {
742 if (::feof(m_stream))
743 error.SetErrorString ("feof");
744 else if (::ferror (m_stream))
745 error.SetErrorString ("ferror");
746 num_bytes = 0;
747 }
748 else
749 num_bytes = bytes_written;
750
751 }
Greg Clayton504f89a2011-02-08 17:49:02 +0000752 else
753 {
754 num_bytes = 0;
755 error.SetErrorString("invalid file handle");
756 }
Zachary Turner98688922014-08-06 18:16:26 +0000757
Greg Clayton504f89a2011-02-08 17:49:02 +0000758 return error;
759}
760
Greg Clayton846b64b2011-02-08 19:54:44 +0000761
762Error
763File::Read (void *buf, size_t &num_bytes, off_t &offset)
764{
765 Error error;
Greg Clayton29aac9a2015-04-09 00:06:44 +0000766
767#if defined (MAX_READ_SIZE)
768 if (num_bytes > MAX_READ_SIZE)
769 {
770 uint8_t *p = (uint8_t *)buf;
771 size_t bytes_left = num_bytes;
772 // Init the num_bytes read to zero
773 num_bytes = 0;
774
775 while (bytes_left > 0)
776 {
777 size_t curr_num_bytes;
778 if (bytes_left > MAX_READ_SIZE)
779 curr_num_bytes = MAX_READ_SIZE;
780 else
781 curr_num_bytes = bytes_left;
782
783 error = Read (p + num_bytes, curr_num_bytes, offset);
784
785 // Update how many bytes were read
786 num_bytes += curr_num_bytes;
787 if (bytes_left < curr_num_bytes)
788 bytes_left = 0;
789 else
790 bytes_left -= curr_num_bytes;
791
792 if (error.Fail())
793 break;
794 }
795 return error;
796 }
797#endif
798
799#ifndef _WIN32
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000800 int fd = GetDescriptor();
801 if (fd != kInvalidDescriptor)
Greg Clayton846b64b2011-02-08 19:54:44 +0000802 {
Greg Clayton96c09682012-01-04 22:56:43 +0000803 ssize_t bytes_read = -1;
804 do
805 {
806 bytes_read = ::pread (fd, buf, num_bytes, offset);
807 } while (bytes_read < 0 && errno == EINTR);
808
Greg Clayton846b64b2011-02-08 19:54:44 +0000809 if (bytes_read < 0)
810 {
811 num_bytes = 0;
812 error.SetErrorToErrno();
813 }
814 else
815 {
816 offset += bytes_read;
817 num_bytes = bytes_read;
818 }
819 }
820 else
821 {
822 num_bytes = 0;
823 error.SetErrorString("invalid file handle");
824 }
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000825#else
826 long cur = ::lseek(m_descriptor, 0, SEEK_CUR);
827 SeekFromStart(offset);
Greg Clayton29aac9a2015-04-09 00:06:44 +0000828 error = Read(buf, num_bytes);
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000829 if (!error.Fail())
830 SeekFromStart(cur);
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000831#endif
Greg Clayton29aac9a2015-04-09 00:06:44 +0000832 return error;
Greg Clayton846b64b2011-02-08 19:54:44 +0000833}
834
835Error
Greg Clayton0b0b5122012-08-30 18:15:10 +0000836File::Read (size_t &num_bytes, off_t &offset, bool null_terminate, DataBufferSP &data_buffer_sp)
Greg Clayton96c09682012-01-04 22:56:43 +0000837{
838 Error error;
839
840 if (num_bytes > 0)
841 {
842 int fd = GetDescriptor();
843 if (fd != kInvalidDescriptor)
844 {
845 struct stat file_stats;
846 if (::fstat (fd, &file_stats) == 0)
847 {
848 if (file_stats.st_size > offset)
849 {
850 const size_t bytes_left = file_stats.st_size - offset;
851 if (num_bytes > bytes_left)
852 num_bytes = bytes_left;
853
Jason Molenda03176d72015-01-22 00:41:05 +0000854 size_t num_bytes_plus_nul_char = num_bytes + (null_terminate ? 1 : 0);
Greg Clayton7b0992d2013-04-18 22:45:39 +0000855 std::unique_ptr<DataBufferHeap> data_heap_ap;
Greg Clayton29aac9a2015-04-09 00:06:44 +0000856 data_heap_ap.reset(new DataBufferHeap());
857 data_heap_ap->SetByteSize(num_bytes_plus_nul_char);
Greg Clayton96c09682012-01-04 22:56:43 +0000858
859 if (data_heap_ap.get())
860 {
861 error = Read (data_heap_ap->GetBytes(), num_bytes, offset);
862 if (error.Success())
863 {
864 // Make sure we read exactly what we asked for and if we got
865 // less, adjust the array
Jason Molenda03176d72015-01-22 00:41:05 +0000866 if (num_bytes_plus_nul_char < data_heap_ap->GetByteSize())
867 data_heap_ap->SetByteSize(num_bytes_plus_nul_char);
Greg Clayton96c09682012-01-04 22:56:43 +0000868 data_buffer_sp.reset(data_heap_ap.release());
869 return error;
870 }
871 }
872 }
873 else
874 error.SetErrorString("file is empty");
875 }
876 else
877 error.SetErrorToErrno();
878 }
879 else
880 error.SetErrorString("invalid file handle");
881 }
882 else
883 error.SetErrorString("invalid file handle");
884
885 num_bytes = 0;
886 data_buffer_sp.reset();
887 return error;
888}
889
890Error
Greg Clayton846b64b2011-02-08 19:54:44 +0000891File::Write (const void *buf, size_t &num_bytes, off_t &offset)
892{
Greg Clayton7965e542015-04-09 00:12:33 +0000893 Error error;
894
Greg Clayton29aac9a2015-04-09 00:06:44 +0000895#if defined (MAX_WRITE_SIZE)
896 if (num_bytes > MAX_WRITE_SIZE)
897 {
898 const uint8_t *p = (const uint8_t *)buf;
899 size_t bytes_left = num_bytes;
900 // Init the num_bytes written to zero
901 num_bytes = 0;
902
903 while (bytes_left > 0)
904 {
905 size_t curr_num_bytes;
906 if (bytes_left > MAX_WRITE_SIZE)
907 curr_num_bytes = MAX_WRITE_SIZE;
908 else
909 curr_num_bytes = bytes_left;
910
911 error = Write (p + num_bytes, curr_num_bytes, offset);
912
913 // Update how many bytes were read
914 num_bytes += curr_num_bytes;
915 if (bytes_left < curr_num_bytes)
916 bytes_left = 0;
917 else
918 bytes_left -= curr_num_bytes;
919
920 if (error.Fail())
921 break;
922 }
923 return error;
924 }
925#endif
926
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000927 int fd = GetDescriptor();
928 if (fd != kInvalidDescriptor)
Greg Clayton846b64b2011-02-08 19:54:44 +0000929 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000930#ifndef _WIN32
Greg Clayton96c09682012-01-04 22:56:43 +0000931 ssize_t bytes_written = -1;
932 do
933 {
934 bytes_written = ::pwrite (m_descriptor, buf, num_bytes, offset);
935 } while (bytes_written < 0 && errno == EINTR);
936
Greg Clayton846b64b2011-02-08 19:54:44 +0000937 if (bytes_written < 0)
938 {
939 num_bytes = 0;
940 error.SetErrorToErrno();
941 }
942 else
943 {
944 offset += bytes_written;
945 num_bytes = bytes_written;
946 }
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000947#else
948 long cur = ::lseek(m_descriptor, 0, SEEK_CUR);
949 error = Write(buf, num_bytes);
950 long after = ::lseek(m_descriptor, 0, SEEK_CUR);
951
952 if (!error.Fail())
953 SeekFromStart(cur);
954
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000955 offset = after;
956#endif
Greg Clayton846b64b2011-02-08 19:54:44 +0000957 }
958 else
959 {
960 num_bytes = 0;
961 error.SetErrorString("invalid file handle");
962 }
963 return error;
964}
965
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000966//------------------------------------------------------------------
967// Print some formatted output to the stream.
968//------------------------------------------------------------------
Greg Claytonc7bece562013-01-25 18:06:21 +0000969size_t
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000970File::Printf (const char *format, ...)
971{
972 va_list args;
973 va_start (args, format);
Greg Claytonc7bece562013-01-25 18:06:21 +0000974 size_t result = PrintfVarArg (format, args);
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000975 va_end (args);
976 return result;
977}
Greg Clayton846b64b2011-02-08 19:54:44 +0000978
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000979//------------------------------------------------------------------
980// Print some formatted output to the stream.
981//------------------------------------------------------------------
Greg Claytonc7bece562013-01-25 18:06:21 +0000982size_t
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000983File::PrintfVarArg (const char *format, va_list args)
984{
Greg Claytonc7bece562013-01-25 18:06:21 +0000985 size_t result = 0;
Greg Clayton51b1e2d2011-02-09 01:08:52 +0000986 if (DescriptorIsValid())
987 {
988 char *s = NULL;
989 result = vasprintf(&s, format, args);
990 if (s != NULL)
991 {
992 if (result > 0)
993 {
994 size_t s_len = result;
995 Write (s, s_len);
996 result = s_len;
997 }
998 free (s);
999 }
1000 }
1001 else if (StreamIsValid())
1002 {
1003 result = ::vfprintf (m_stream, format, args);
1004 }
1005 return result;
1006}
Daniel Maleae0f8f572013-08-26 23:57:52 +00001007
1008mode_t
1009File::ConvertOpenOptionsForPOSIXOpen (uint32_t open_options)
1010{
1011 mode_t mode = 0;
1012 if (open_options & eOpenOptionRead && open_options & eOpenOptionWrite)
1013 mode |= O_RDWR;
1014 else if (open_options & eOpenOptionWrite)
1015 mode |= O_WRONLY;
1016
1017 if (open_options & eOpenOptionAppend)
1018 mode |= O_APPEND;
1019
1020 if (open_options & eOpenOptionTruncate)
1021 mode |= O_TRUNC;
1022
1023 if (open_options & eOpenOptionNonBlocking)
1024 mode |= O_NONBLOCK;
1025
1026 if (open_options & eOpenOptionCanCreateNewOnly)
1027 mode |= O_CREAT | O_EXCL;
1028 else if (open_options & eOpenOptionCanCreate)
1029 mode |= O_CREAT;
1030
1031 return mode;
1032}
Greg Clayton340b0302014-02-05 17:57:57 +00001033
1034void
1035File::CalculateInteractiveAndTerminal ()
1036{
1037 const int fd = GetDescriptor();
1038 if (fd >= 0)
1039 {
1040 m_is_interactive = eLazyBoolNo;
1041 m_is_real_terminal = eLazyBoolNo;
Shawn Best8da0bf32014-11-08 01:41:49 +00001042#if (defined(_WIN32) || defined(__ANDROID_NDK__))
Deepak Panickal6419e982014-03-03 15:53:37 +00001043 if (_isatty(fd))
1044 {
1045 m_is_interactive = eLazyBoolYes;
1046 m_is_real_terminal = eLazyBoolYes;
1047 }
1048#else
Greg Clayton340b0302014-02-05 17:57:57 +00001049 if (isatty(fd))
1050 {
1051 m_is_interactive = eLazyBoolYes;
1052 struct winsize window_size;
1053 if (::ioctl (fd, TIOCGWINSZ, &window_size) == 0)
1054 {
1055 if (window_size.ws_col > 0)
Sean Callanan66810412015-10-19 23:11:07 +00001056 {
Greg Clayton340b0302014-02-05 17:57:57 +00001057 m_is_real_terminal = eLazyBoolYes;
Sean Callanan66810412015-10-19 23:11:07 +00001058 if (llvm::sys::Process::FileDescriptorHasColors(fd))
1059 m_supports_colors = eLazyBoolYes;
1060 }
Greg Clayton340b0302014-02-05 17:57:57 +00001061 }
1062 }
Deepak Panickal94667bc2014-02-17 17:43:39 +00001063#endif
Greg Clayton340b0302014-02-05 17:57:57 +00001064 }
1065}
1066
1067bool
1068File::GetIsInteractive ()
1069{
1070 if (m_is_interactive == eLazyBoolCalculate)
1071 CalculateInteractiveAndTerminal ();
1072 return m_is_interactive == eLazyBoolYes;
1073}
1074
1075bool
1076File::GetIsRealTerminal ()
1077{
1078 if (m_is_real_terminal == eLazyBoolCalculate)
1079 CalculateInteractiveAndTerminal();
1080 return m_is_real_terminal == eLazyBoolYes;
1081}
1082
Sean Callanan66810412015-10-19 23:11:07 +00001083bool
1084File::GetIsTerminalWithColors ()
1085{
1086 if (m_supports_colors == eLazyBoolCalculate)
1087 CalculateInteractiveAndTerminal();
1088 return m_supports_colors == eLazyBoolYes;
1089}
1090