blob: edb101efb0f692b5390a00b8cf1f755a9dda9991 [file] [log] [blame]
Michael J. Spencerdffde992010-11-29 22:28:51 +00001//===- llvm/Support/Unix/PathV2.cpp - Unix Path Implementation --*- 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// This file implements the Unix specific implementation of the PathV2 API.
11//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15//=== WARNING: Implementation here must contain only generic UNIX code that
16//=== is guaranteed to work on *all* UNIX variants.
17//===----------------------------------------------------------------------===//
18
19#include "Unix.h"
Michael J. Spencerbee0c382010-12-01 19:32:01 +000020#if HAVE_SYS_STAT_H
21#include <sys/stat.h>
22#endif
23#if HAVE_FCNTL_H
24#include <fcntl.h>
25#endif
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +000026#if HAVE_DIRENT_H
27# include <dirent.h>
28# define NAMLEN(dirent) strlen((dirent)->d_name)
29#else
30# define dirent direct
31# define NAMLEN(dirent) (dirent)->d_namlen
32# if HAVE_SYS_NDIR_H
33# include <sys/ndir.h>
34# endif
35# if HAVE_SYS_DIR_H
36# include <sys/dir.h>
37# endif
38# if HAVE_NDIR_H
39# include <ndir.h>
40# endif
41#endif
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000042#if HAVE_STDIO_H
43#include <stdio.h>
Michael J. Spencerbee0c382010-12-01 19:32:01 +000044#endif
Bill Wendling544e4122011-09-14 21:49:42 +000045#if HAVE_LIMITS_H
46#include <limits.h>
47#endif
Michael J. Spencerbee0c382010-12-01 19:32:01 +000048
Sylvestre Ledru6fc30c22012-04-11 15:35:36 +000049// For GNU Hurd
50#if defined(__GNU__) && !defined(PATH_MAX)
51# define PATH_MAX 4096
52#endif
53
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000054using namespace llvm;
55
Michael J. Spencerbee0c382010-12-01 19:32:01 +000056namespace {
Michael J. Spencerad8a14f2010-12-07 01:23:08 +000057 /// This class automatically closes the given file descriptor when it goes out
58 /// of scope. You can take back explicit ownership of the file descriptor by
59 /// calling take(). The destructor does not verify that close was successful.
60 /// Therefore, never allow this class to call close on a file descriptor that
61 /// has been read from or written to.
Michael J. Spencerbee0c382010-12-01 19:32:01 +000062 struct AutoFD {
63 int FileDescriptor;
64
65 AutoFD(int fd) : FileDescriptor(fd) {}
66 ~AutoFD() {
67 if (FileDescriptor >= 0)
68 ::close(FileDescriptor);
69 }
70
71 int take() {
72 int ret = FileDescriptor;
73 FileDescriptor = -1;
74 return ret;
75 }
76
77 operator int() const {return FileDescriptor;}
78 };
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000079
80 error_code TempDir(SmallVectorImpl<char> &result) {
81 // FIXME: Don't use TMPDIR if program is SUID or SGID enabled.
82 const char *dir = 0;
83 (dir = std::getenv("TMPDIR" )) ||
84 (dir = std::getenv("TMP" )) ||
85 (dir = std::getenv("TEMP" )) ||
86 (dir = std::getenv("TEMPDIR")) ||
87#ifdef P_tmpdir
88 (dir = P_tmpdir) ||
89#endif
90 (dir = "/tmp");
91
Michael J. Spencerfbd1bbd2010-12-07 01:23:19 +000092 result.clear();
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000093 StringRef d(dir);
94 result.append(d.begin(), d.end());
David Blaikie58604cd2012-02-09 19:24:12 +000095 return error_code::success();
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000096 }
Michael J. Spencerbee0c382010-12-01 19:32:01 +000097}
Michael J. Spencerdffde992010-11-29 22:28:51 +000098
99namespace llvm {
100namespace sys {
Michael J. Spencer1522fce2010-12-07 01:22:31 +0000101namespace fs {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000102
103error_code current_path(SmallVectorImpl<char> &result) {
Sylvestre Ledru6fc30c22012-04-11 15:35:36 +0000104#ifdef MAXPATHLEN
Andrew Trick0a828fd2011-03-24 16:43:37 +0000105 result.reserve(MAXPATHLEN);
Sylvestre Ledru6fc30c22012-04-11 15:35:36 +0000106#else
107// For GNU Hurd
108 result.reserve(1024);
109#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +0000110
Michael J. Spencer1522fce2010-12-07 01:22:31 +0000111 while (true) {
112 if (::getcwd(result.data(), result.capacity()) == 0) {
113 // See if there was a real error.
114 if (errno != errc::not_enough_memory)
115 return error_code(errno, system_category());
116 // Otherwise there just wasn't enough space.
117 result.reserve(result.capacity() * 2);
118 } else
119 break;
120 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000121
122 result.set_size(strlen(result.data()));
David Blaikie58604cd2012-02-09 19:24:12 +0000123 return error_code::success();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000124}
125
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000126error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
127 // Get arguments.
128 SmallString<128> from_storage;
129 SmallString<128> to_storage;
Michael J. Spencer871498e2010-12-01 20:37:42 +0000130 StringRef f = from.toNullTerminatedStringRef(from_storage);
131 StringRef t = to.toNullTerminatedStringRef(to_storage);
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000132
133 const size_t buf_sz = 32768;
134 char buffer[buf_sz];
135 int from_file = -1, to_file = -1;
136
137 // Open from.
138 if ((from_file = ::open(f.begin(), O_RDONLY)) < 0)
139 return error_code(errno, system_category());
140 AutoFD from_fd(from_file);
141
142 // Stat from.
143 struct stat from_stat;
144 if (::stat(f.begin(), &from_stat) != 0)
145 return error_code(errno, system_category());
146
147 // Setup to flags.
148 int to_flags = O_CREAT | O_WRONLY;
149 if (copt == copy_option::fail_if_exists)
150 to_flags |= O_EXCL;
151
152 // Open to.
153 if ((to_file = ::open(t.begin(), to_flags, from_stat.st_mode)) < 0)
154 return error_code(errno, system_category());
155 AutoFD to_fd(to_file);
156
157 // Copy!
158 ssize_t sz, sz_read = 1, sz_write;
159 while (sz_read > 0 &&
160 (sz_read = ::read(from_fd, buffer, buf_sz)) > 0) {
161 // Allow for partial writes - see Advanced Unix Programming (2nd Ed.),
162 // Marc Rochkind, Addison-Wesley, 2004, page 94
163 sz_write = 0;
164 do {
165 if ((sz = ::write(to_fd, buffer + sz_write, sz_read - sz_write)) < 0) {
166 sz_read = sz; // cause read loop termination.
167 break; // error.
168 }
169 sz_write += sz;
170 } while (sz_write < sz_read);
171 }
172
173 // After all the file operations above the return value of close actually
174 // matters.
175 if (::close(from_fd.take()) < 0) sz_read = -1;
176 if (::close(to_fd.take()) < 0) sz_read = -1;
177
178 // Check for errors.
179 if (sz_read < 0)
180 return error_code(errno, system_category());
181
David Blaikie58604cd2012-02-09 19:24:12 +0000182 return error_code::success();
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000183}
184
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000185error_code create_directory(const Twine &path, bool &existed) {
186 SmallString<128> path_storage;
187 StringRef p = path.toNullTerminatedStringRef(path_storage);
188
Michael J. Spencer79c3c3a2010-12-07 01:23:29 +0000189 if (::mkdir(p.begin(), S_IRWXU | S_IRWXG) == -1) {
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000190 if (errno != errc::file_exists)
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000191 return error_code(errno, system_category());
192 existed = true;
193 } else
194 existed = false;
195
David Blaikie58604cd2012-02-09 19:24:12 +0000196 return error_code::success();
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000197}
198
Michael J. Spencerd7b305f2010-12-03 05:58:41 +0000199error_code create_hard_link(const Twine &to, const Twine &from) {
200 // Get arguments.
201 SmallString<128> from_storage;
202 SmallString<128> to_storage;
203 StringRef f = from.toNullTerminatedStringRef(from_storage);
204 StringRef t = to.toNullTerminatedStringRef(to_storage);
205
206 if (::link(t.begin(), f.begin()) == -1)
207 return error_code(errno, system_category());
208
David Blaikie58604cd2012-02-09 19:24:12 +0000209 return error_code::success();
Michael J. Spencerd7b305f2010-12-03 05:58:41 +0000210}
211
Michael J. Spencer9b391c52010-12-03 07:41:25 +0000212error_code create_symlink(const Twine &to, const Twine &from) {
213 // Get arguments.
214 SmallString<128> from_storage;
215 SmallString<128> to_storage;
216 StringRef f = from.toNullTerminatedStringRef(from_storage);
217 StringRef t = to.toNullTerminatedStringRef(to_storage);
218
219 if (::symlink(t.begin(), f.begin()) == -1)
220 return error_code(errno, system_category());
221
David Blaikie58604cd2012-02-09 19:24:12 +0000222 return error_code::success();
Michael J. Spencer9b391c52010-12-03 07:41:25 +0000223}
224
Michael J. Spencer106aa732010-12-03 17:53:43 +0000225error_code remove(const Twine &path, bool &existed) {
226 SmallString<128> path_storage;
227 StringRef p = path.toNullTerminatedStringRef(path_storage);
228
229 if (::remove(p.begin()) == -1) {
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000230 if (errno != errc::no_such_file_or_directory)
Michael J. Spencer106aa732010-12-03 17:53:43 +0000231 return error_code(errno, system_category());
232 existed = false;
233 } else
234 existed = true;
235
David Blaikie58604cd2012-02-09 19:24:12 +0000236 return error_code::success();
Michael J. Spencer106aa732010-12-03 17:53:43 +0000237}
238
Michael J. Spencera50b98c2010-12-03 17:53:55 +0000239error_code rename(const Twine &from, const Twine &to) {
240 // Get arguments.
241 SmallString<128> from_storage;
242 SmallString<128> to_storage;
243 StringRef f = from.toNullTerminatedStringRef(from_storage);
244 StringRef t = to.toNullTerminatedStringRef(to_storage);
245
Michael J. Spencer283d49c2011-01-16 22:18:41 +0000246 if (::rename(f.begin(), t.begin()) == -1) {
247 // If it's a cross device link, copy then delete, otherwise return the error
248 if (errno == EXDEV) {
249 if (error_code ec = copy_file(from, to, copy_option::overwrite_if_exists))
250 return ec;
251 bool Existed;
252 if (error_code ec = remove(from, Existed))
253 return ec;
254 } else
255 return error_code(errno, system_category());
256 }
Michael J. Spencera50b98c2010-12-03 17:53:55 +0000257
David Blaikie58604cd2012-02-09 19:24:12 +0000258 return error_code::success();
Michael J. Spencera50b98c2010-12-03 17:53:55 +0000259}
260
Michael J. Spencer3920d3b2010-12-03 17:54:07 +0000261error_code resize_file(const Twine &path, uint64_t size) {
262 SmallString<128> path_storage;
263 StringRef p = path.toNullTerminatedStringRef(path_storage);
264
265 if (::truncate(p.begin(), size) == -1)
266 return error_code(errno, system_category());
267
David Blaikie58604cd2012-02-09 19:24:12 +0000268 return error_code::success();
Michael J. Spencer3920d3b2010-12-03 17:54:07 +0000269}
270
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000271error_code exists(const Twine &path, bool &result) {
272 SmallString<128> path_storage;
273 StringRef p = path.toNullTerminatedStringRef(path_storage);
274
275 struct stat status;
276 if (::stat(p.begin(), &status) == -1) {
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000277 if (errno != errc::no_such_file_or_directory)
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000278 return error_code(errno, system_category());
279 result = false;
280 } else
281 result = true;
282
David Blaikie58604cd2012-02-09 19:24:12 +0000283 return error_code::success();
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000284}
285
Michael J. Spencerd45fbe62011-12-12 06:04:28 +0000286bool equivalent(file_status A, file_status B) {
287 assert(status_known(A) && status_known(B));
288 return A.st_dev == B.st_dev &&
289 A.st_ino == B.st_ino;
290}
291
Michael J. Spencerb531f452010-12-03 18:49:13 +0000292error_code equivalent(const Twine &A, const Twine &B, bool &result) {
Michael J. Spencerd45fbe62011-12-12 06:04:28 +0000293 file_status fsA, fsB;
294 if (error_code ec = status(A, fsA)) return ec;
295 if (error_code ec = status(B, fsB)) return ec;
296 result = equivalent(fsA, fsB);
David Blaikie58604cd2012-02-09 19:24:12 +0000297 return error_code::success();
Michael J. Spencerb531f452010-12-03 18:49:13 +0000298}
299
Michael J. Spencer01a87c42010-12-04 00:31:48 +0000300error_code file_size(const Twine &path, uint64_t &result) {
301 SmallString<128> path_storage;
302 StringRef p = path.toNullTerminatedStringRef(path_storage);
303
304 struct stat status;
305 if (::stat(p.begin(), &status) == -1)
306 return error_code(errno, system_category());
307 if (!S_ISREG(status.st_mode))
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000308 return make_error_code(errc::operation_not_permitted);
Michael J. Spencer01a87c42010-12-04 00:31:48 +0000309
310 result = status.st_size;
David Blaikie58604cd2012-02-09 19:24:12 +0000311 return error_code::success();
Michael J. Spencer01a87c42010-12-04 00:31:48 +0000312}
313
Michael J. Spencer470ae132010-12-04 00:32:40 +0000314error_code status(const Twine &path, file_status &result) {
315 SmallString<128> path_storage;
316 StringRef p = path.toNullTerminatedStringRef(path_storage);
317
318 struct stat status;
319 if (::stat(p.begin(), &status) != 0) {
320 error_code ec(errno, system_category());
321 if (ec == errc::no_such_file_or_directory)
322 result = file_status(file_type::file_not_found);
323 else
324 result = file_status(file_type::status_error);
325 return ec;
326 }
327
328 if (S_ISDIR(status.st_mode))
329 result = file_status(file_type::directory_file);
330 else if (S_ISREG(status.st_mode))
331 result = file_status(file_type::regular_file);
332 else if (S_ISBLK(status.st_mode))
333 result = file_status(file_type::block_file);
334 else if (S_ISCHR(status.st_mode))
335 result = file_status(file_type::character_file);
336 else if (S_ISFIFO(status.st_mode))
337 result = file_status(file_type::fifo_file);
338 else if (S_ISSOCK(status.st_mode))
339 result = file_status(file_type::socket_file);
340 else
341 result = file_status(file_type::type_unknown);
342
Michael J. Spencerd45fbe62011-12-12 06:04:28 +0000343 result.st_dev = status.st_dev;
344 result.st_ino = status.st_ino;
345
David Blaikie58604cd2012-02-09 19:24:12 +0000346 return error_code::success();
Michael J. Spencer470ae132010-12-04 00:32:40 +0000347}
348
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000349error_code unique_file(const Twine &model, int &result_fd,
Argyrios Kyrtzidis814450a2011-07-28 00:29:20 +0000350 SmallVectorImpl<char> &result_path,
351 bool makeAbsolute) {
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000352 SmallString<128> Model;
353 model.toVector(Model);
354 // Null terminate.
355 Model.c_str();
356
Argyrios Kyrtzidis814450a2011-07-28 00:29:20 +0000357 if (makeAbsolute) {
358 // Make model absolute by prepending a temp directory if it's not already.
359 bool absolute = path::is_absolute(Twine(Model));
360 if (!absolute) {
361 SmallString<128> TDir;
362 if (error_code ec = TempDir(TDir)) return ec;
363 path::append(TDir, Twine(Model));
364 Model.swap(TDir);
365 }
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000366 }
367
368 // Replace '%' with random chars. From here on, DO NOT modify model. It may be
369 // needed if the randomly chosen path already exists.
370 SmallString<128> RandomPath;
371 RandomPath.reserve(Model.size() + 1);
372 ::srand(::time(NULL));
373
374retry_random_path:
375 // This is opened here instead of above to make it easier to track when to
376 // close it. Collisions should be rare enough for the possible extra syscalls
377 // not to matter.
378 FILE *RandomSource = ::fopen("/dev/urandom", "r");
379 RandomPath.set_size(0);
380 for (SmallVectorImpl<char>::const_iterator i = Model.begin(),
381 e = Model.end(); i != e; ++i) {
382 if (*i == '%') {
383 char val = 0;
384 if (RandomSource)
385 val = fgetc(RandomSource);
386 else
387 val = ::rand();
388 RandomPath.push_back("0123456789abcdef"[val & 15]);
389 } else
390 RandomPath.push_back(*i);
391 }
392
393 if (RandomSource)
394 ::fclose(RandomSource);
395
396 // Try to open + create the file.
397rety_open_create:
398 int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
399 if (RandomFD == -1) {
400 // If the file existed, try again, otherwise, error.
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000401 if (errno == errc::file_exists)
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000402 goto retry_random_path;
403 // The path prefix doesn't exist.
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000404 if (errno == errc::no_such_file_or_directory) {
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000405 StringRef p(RandomPath.begin(), RandomPath.size());
406 SmallString<64> dir_to_create;
407 for (path::const_iterator i = path::begin(p),
408 e = --path::end(p); i != e; ++i) {
Michael J. Spencer936671b2010-12-07 03:57:37 +0000409 path::append(dir_to_create, *i);
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000410 bool Exists;
411 if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec;
412 if (!Exists) {
413 // Don't try to create network paths.
414 if (i->size() > 2 && (*i)[0] == '/' &&
415 (*i)[1] == '/' &&
416 (*i)[2] != '/')
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000417 return make_error_code(errc::no_such_file_or_directory);
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000418 if (::mkdir(dir_to_create.c_str(), 0700) == -1)
419 return error_code(errno, system_category());
420 }
421 }
422 goto rety_open_create;
423 }
424 return error_code(errno, system_category());
425 }
426
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000427 // Make the path absolute.
Andrew Trick0a828fd2011-03-24 16:43:37 +0000428 char real_path_buff[PATH_MAX + 1];
429 if (realpath(RandomPath.c_str(), real_path_buff) == NULL) {
430 int error = errno;
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000431 ::close(RandomFD);
432 ::unlink(RandomPath.c_str());
Andrew Trick0a828fd2011-03-24 16:43:37 +0000433 return error_code(error, system_category());
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000434 }
435
Andrew Trick0a828fd2011-03-24 16:43:37 +0000436 result_path.clear();
437 StringRef d(real_path_buff);
438 result_path.append(d.begin(), d.end());
439
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000440 result_fd = RandomFD;
David Blaikie58604cd2012-02-09 19:24:12 +0000441 return error_code::success();
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000442}
443
Michael J. Spencera81ac8f2011-12-08 22:50:09 +0000444error_code detail::directory_iterator_construct(detail::DirIterState &it,
445 StringRef path){
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +0000446 SmallString<128> path_null(path);
447 DIR *directory = ::opendir(path_null.c_str());
448 if (directory == 0)
449 return error_code(errno, system_category());
450
451 it.IterationHandle = reinterpret_cast<intptr_t>(directory);
452 // Add something for replace_filename to replace.
453 path::append(path_null, ".");
454 it.CurrentEntry = directory_entry(path_null.str());
455 return directory_iterator_increment(it);
456}
457
Michael J. Spencera81ac8f2011-12-08 22:50:09 +0000458error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +0000459 if (it.IterationHandle)
460 ::closedir(reinterpret_cast<DIR *>(it.IterationHandle));
461 it.IterationHandle = 0;
462 it.CurrentEntry = directory_entry();
David Blaikie58604cd2012-02-09 19:24:12 +0000463 return error_code::success();
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +0000464}
465
Michael J. Spencera81ac8f2011-12-08 22:50:09 +0000466error_code detail::directory_iterator_increment(detail::DirIterState &it) {
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +0000467 errno = 0;
468 dirent *cur_dir = ::readdir(reinterpret_cast<DIR *>(it.IterationHandle));
469 if (cur_dir == 0 && errno != 0) {
470 return error_code(errno, system_category());
471 } else if (cur_dir != 0) {
472 StringRef name(cur_dir->d_name, NAMLEN(cur_dir));
473 if ((name.size() == 1 && name[0] == '.') ||
474 (name.size() == 2 && name[0] == '.' && name[1] == '.'))
475 return directory_iterator_increment(it);
476 it.CurrentEntry.replace_filename(name);
477 } else
478 return directory_iterator_destruct(it);
479
David Blaikie58604cd2012-02-09 19:24:12 +0000480 return error_code::success();
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +0000481}
482
Michael J. Spencerd6cdf1d2011-01-15 18:52:33 +0000483error_code get_magic(const Twine &path, uint32_t len,
484 SmallVectorImpl<char> &result) {
485 SmallString<128> PathStorage;
486 StringRef Path = path.toNullTerminatedStringRef(PathStorage);
487 result.set_size(0);
488
489 // Open path.
490 std::FILE *file = std::fopen(Path.data(), "rb");
491 if (file == 0)
492 return error_code(errno, system_category());
493
494 // Reserve storage.
495 result.reserve(len);
496
497 // Read magic!
498 size_t size = std::fread(result.data(), 1, len, file);
499 if (std::ferror(file) != 0) {
500 std::fclose(file);
501 return error_code(errno, system_category());
502 } else if (size != result.size()) {
503 if (std::feof(file) != 0) {
504 std::fclose(file);
505 result.set_size(size);
506 return make_error_code(errc::value_too_large);
507 }
508 }
509 std::fclose(file);
510 result.set_size(len);
David Blaikie58604cd2012-02-09 19:24:12 +0000511 return error_code::success();
Michael J. Spencerd6cdf1d2011-01-15 18:52:33 +0000512}
513
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000514} // end namespace fs
Michael J. Spencerdffde992010-11-29 22:28:51 +0000515} // end namespace sys
516} // end namespace llvm