blob: 4e431ca377540669d1181d038ffd3aa092c22ee3 [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"
Daniel Dunbar92bb5142012-05-05 16:36:24 +000020#include "llvm/Support/Process.h"
Michael J. Spencerbee0c382010-12-01 19:32:01 +000021#if HAVE_SYS_STAT_H
22#include <sys/stat.h>
23#endif
24#if HAVE_FCNTL_H
25#include <fcntl.h>
26#endif
Nick Kledzikca077ec2012-06-20 00:28:54 +000027#ifdef HAVE_SYS_MMAN_H
28#include <sys/mman.h>
29#endif
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +000030#if HAVE_DIRENT_H
31# include <dirent.h>
32# define NAMLEN(dirent) strlen((dirent)->d_name)
33#else
34# define dirent direct
35# define NAMLEN(dirent) (dirent)->d_namlen
36# if HAVE_SYS_NDIR_H
37# include <sys/ndir.h>
38# endif
39# if HAVE_SYS_DIR_H
40# include <sys/dir.h>
41# endif
42# if HAVE_NDIR_H
43# include <ndir.h>
44# endif
45#endif
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000046#if HAVE_STDIO_H
47#include <stdio.h>
Michael J. Spencerbee0c382010-12-01 19:32:01 +000048#endif
Bill Wendling544e4122011-09-14 21:49:42 +000049#if HAVE_LIMITS_H
50#include <limits.h>
51#endif
Michael J. Spencerbee0c382010-12-01 19:32:01 +000052
Sylvestre Ledru6fc30c22012-04-11 15:35:36 +000053// For GNU Hurd
54#if defined(__GNU__) && !defined(PATH_MAX)
55# define PATH_MAX 4096
56#endif
57
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000058using namespace llvm;
59
Michael J. Spencerbee0c382010-12-01 19:32:01 +000060namespace {
Michael J. Spencerad8a14f2010-12-07 01:23:08 +000061 /// This class automatically closes the given file descriptor when it goes out
62 /// of scope. You can take back explicit ownership of the file descriptor by
63 /// calling take(). The destructor does not verify that close was successful.
64 /// Therefore, never allow this class to call close on a file descriptor that
65 /// has been read from or written to.
Michael J. Spencerbee0c382010-12-01 19:32:01 +000066 struct AutoFD {
67 int FileDescriptor;
68
69 AutoFD(int fd) : FileDescriptor(fd) {}
70 ~AutoFD() {
71 if (FileDescriptor >= 0)
72 ::close(FileDescriptor);
73 }
74
75 int take() {
76 int ret = FileDescriptor;
77 FileDescriptor = -1;
78 return ret;
79 }
80
81 operator int() const {return FileDescriptor;}
82 };
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000083
84 error_code TempDir(SmallVectorImpl<char> &result) {
85 // FIXME: Don't use TMPDIR if program is SUID or SGID enabled.
86 const char *dir = 0;
87 (dir = std::getenv("TMPDIR" )) ||
88 (dir = std::getenv("TMP" )) ||
89 (dir = std::getenv("TEMP" )) ||
90 (dir = std::getenv("TEMPDIR")) ||
91#ifdef P_tmpdir
92 (dir = P_tmpdir) ||
93#endif
94 (dir = "/tmp");
95
Michael J. Spencerfbd1bbd2010-12-07 01:23:19 +000096 result.clear();
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000097 StringRef d(dir);
98 result.append(d.begin(), d.end());
David Blaikie58604cd2012-02-09 19:24:12 +000099 return error_code::success();
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000100 }
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000101}
Michael J. Spencerdffde992010-11-29 22:28:51 +0000102
103namespace llvm {
104namespace sys {
Michael J. Spencer1522fce2010-12-07 01:22:31 +0000105namespace fs {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000106
107error_code current_path(SmallVectorImpl<char> &result) {
Sylvestre Ledru6fc30c22012-04-11 15:35:36 +0000108#ifdef MAXPATHLEN
Andrew Trick0a828fd2011-03-24 16:43:37 +0000109 result.reserve(MAXPATHLEN);
Sylvestre Ledru6fc30c22012-04-11 15:35:36 +0000110#else
111// For GNU Hurd
112 result.reserve(1024);
113#endif
Michael J. Spencerdffde992010-11-29 22:28:51 +0000114
Michael J. Spencer1522fce2010-12-07 01:22:31 +0000115 while (true) {
116 if (::getcwd(result.data(), result.capacity()) == 0) {
117 // See if there was a real error.
118 if (errno != errc::not_enough_memory)
119 return error_code(errno, system_category());
120 // Otherwise there just wasn't enough space.
121 result.reserve(result.capacity() * 2);
122 } else
123 break;
124 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000125
126 result.set_size(strlen(result.data()));
David Blaikie58604cd2012-02-09 19:24:12 +0000127 return error_code::success();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000128}
129
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000130error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
131 // Get arguments.
132 SmallString<128> from_storage;
133 SmallString<128> to_storage;
Michael J. Spencer871498e2010-12-01 20:37:42 +0000134 StringRef f = from.toNullTerminatedStringRef(from_storage);
135 StringRef t = to.toNullTerminatedStringRef(to_storage);
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000136
137 const size_t buf_sz = 32768;
138 char buffer[buf_sz];
139 int from_file = -1, to_file = -1;
140
141 // Open from.
142 if ((from_file = ::open(f.begin(), O_RDONLY)) < 0)
143 return error_code(errno, system_category());
144 AutoFD from_fd(from_file);
145
146 // Stat from.
147 struct stat from_stat;
148 if (::stat(f.begin(), &from_stat) != 0)
149 return error_code(errno, system_category());
150
151 // Setup to flags.
152 int to_flags = O_CREAT | O_WRONLY;
153 if (copt == copy_option::fail_if_exists)
154 to_flags |= O_EXCL;
155
156 // Open to.
157 if ((to_file = ::open(t.begin(), to_flags, from_stat.st_mode)) < 0)
158 return error_code(errno, system_category());
159 AutoFD to_fd(to_file);
160
161 // Copy!
162 ssize_t sz, sz_read = 1, sz_write;
163 while (sz_read > 0 &&
164 (sz_read = ::read(from_fd, buffer, buf_sz)) > 0) {
165 // Allow for partial writes - see Advanced Unix Programming (2nd Ed.),
166 // Marc Rochkind, Addison-Wesley, 2004, page 94
167 sz_write = 0;
168 do {
169 if ((sz = ::write(to_fd, buffer + sz_write, sz_read - sz_write)) < 0) {
170 sz_read = sz; // cause read loop termination.
171 break; // error.
172 }
173 sz_write += sz;
174 } while (sz_write < sz_read);
175 }
176
177 // After all the file operations above the return value of close actually
178 // matters.
179 if (::close(from_fd.take()) < 0) sz_read = -1;
180 if (::close(to_fd.take()) < 0) sz_read = -1;
181
182 // Check for errors.
183 if (sz_read < 0)
184 return error_code(errno, system_category());
185
David Blaikie58604cd2012-02-09 19:24:12 +0000186 return error_code::success();
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000187}
188
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000189error_code create_directory(const Twine &path, bool &existed) {
190 SmallString<128> path_storage;
191 StringRef p = path.toNullTerminatedStringRef(path_storage);
192
Michael J. Spencer79c3c3a2010-12-07 01:23:29 +0000193 if (::mkdir(p.begin(), S_IRWXU | S_IRWXG) == -1) {
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000194 if (errno != errc::file_exists)
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000195 return error_code(errno, system_category());
196 existed = true;
197 } else
198 existed = false;
199
David Blaikie58604cd2012-02-09 19:24:12 +0000200 return error_code::success();
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000201}
202
Michael J. Spencerd7b305f2010-12-03 05:58:41 +0000203error_code create_hard_link(const Twine &to, const Twine &from) {
204 // Get arguments.
205 SmallString<128> from_storage;
206 SmallString<128> to_storage;
207 StringRef f = from.toNullTerminatedStringRef(from_storage);
208 StringRef t = to.toNullTerminatedStringRef(to_storage);
209
210 if (::link(t.begin(), f.begin()) == -1)
211 return error_code(errno, system_category());
212
David Blaikie58604cd2012-02-09 19:24:12 +0000213 return error_code::success();
Michael J. Spencerd7b305f2010-12-03 05:58:41 +0000214}
215
Michael J. Spencer9b391c52010-12-03 07:41:25 +0000216error_code create_symlink(const Twine &to, const Twine &from) {
217 // Get arguments.
218 SmallString<128> from_storage;
219 SmallString<128> to_storage;
220 StringRef f = from.toNullTerminatedStringRef(from_storage);
221 StringRef t = to.toNullTerminatedStringRef(to_storage);
222
223 if (::symlink(t.begin(), f.begin()) == -1)
224 return error_code(errno, system_category());
225
David Blaikie58604cd2012-02-09 19:24:12 +0000226 return error_code::success();
Michael J. Spencer9b391c52010-12-03 07:41:25 +0000227}
228
Michael J. Spencer106aa732010-12-03 17:53:43 +0000229error_code remove(const Twine &path, bool &existed) {
230 SmallString<128> path_storage;
231 StringRef p = path.toNullTerminatedStringRef(path_storage);
232
233 if (::remove(p.begin()) == -1) {
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000234 if (errno != errc::no_such_file_or_directory)
Michael J. Spencer106aa732010-12-03 17:53:43 +0000235 return error_code(errno, system_category());
236 existed = false;
237 } else
238 existed = true;
239
David Blaikie58604cd2012-02-09 19:24:12 +0000240 return error_code::success();
Michael J. Spencer106aa732010-12-03 17:53:43 +0000241}
242
Michael J. Spencera50b98c2010-12-03 17:53:55 +0000243error_code rename(const Twine &from, const Twine &to) {
244 // Get arguments.
245 SmallString<128> from_storage;
246 SmallString<128> to_storage;
247 StringRef f = from.toNullTerminatedStringRef(from_storage);
248 StringRef t = to.toNullTerminatedStringRef(to_storage);
249
Michael J. Spencer283d49c2011-01-16 22:18:41 +0000250 if (::rename(f.begin(), t.begin()) == -1) {
251 // If it's a cross device link, copy then delete, otherwise return the error
252 if (errno == EXDEV) {
253 if (error_code ec = copy_file(from, to, copy_option::overwrite_if_exists))
254 return ec;
255 bool Existed;
256 if (error_code ec = remove(from, Existed))
257 return ec;
258 } else
259 return error_code(errno, system_category());
260 }
Michael J. Spencera50b98c2010-12-03 17:53:55 +0000261
David Blaikie58604cd2012-02-09 19:24:12 +0000262 return error_code::success();
Michael J. Spencera50b98c2010-12-03 17:53:55 +0000263}
264
Michael J. Spencer3920d3b2010-12-03 17:54:07 +0000265error_code resize_file(const Twine &path, uint64_t size) {
266 SmallString<128> path_storage;
267 StringRef p = path.toNullTerminatedStringRef(path_storage);
268
269 if (::truncate(p.begin(), size) == -1)
270 return error_code(errno, system_category());
271
David Blaikie58604cd2012-02-09 19:24:12 +0000272 return error_code::success();
Michael J. Spencer3920d3b2010-12-03 17:54:07 +0000273}
274
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000275error_code exists(const Twine &path, bool &result) {
276 SmallString<128> path_storage;
277 StringRef p = path.toNullTerminatedStringRef(path_storage);
278
Benjamin Kramer1222c5c2012-06-02 16:28:09 +0000279 if (::access(p.begin(), F_OK) == -1) {
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000280 if (errno != errc::no_such_file_or_directory)
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000281 return error_code(errno, system_category());
282 result = false;
283 } else
284 result = true;
285
David Blaikie58604cd2012-02-09 19:24:12 +0000286 return error_code::success();
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000287}
288
Michael J. Spencerd45fbe62011-12-12 06:04:28 +0000289bool equivalent(file_status A, file_status B) {
290 assert(status_known(A) && status_known(B));
Sylvestre Ledru9dc06bd2012-04-23 16:37:23 +0000291 return A.fs_st_dev == B.fs_st_dev &&
292 A.fs_st_ino == B.fs_st_ino;
Michael J. Spencerd45fbe62011-12-12 06:04:28 +0000293}
294
Michael J. Spencerb531f452010-12-03 18:49:13 +0000295error_code equivalent(const Twine &A, const Twine &B, bool &result) {
Michael J. Spencerd45fbe62011-12-12 06:04:28 +0000296 file_status fsA, fsB;
297 if (error_code ec = status(A, fsA)) return ec;
298 if (error_code ec = status(B, fsB)) return ec;
299 result = equivalent(fsA, fsB);
David Blaikie58604cd2012-02-09 19:24:12 +0000300 return error_code::success();
Michael J. Spencerb531f452010-12-03 18:49:13 +0000301}
302
Michael J. Spencer01a87c42010-12-04 00:31:48 +0000303error_code file_size(const Twine &path, uint64_t &result) {
304 SmallString<128> path_storage;
305 StringRef p = path.toNullTerminatedStringRef(path_storage);
306
307 struct stat status;
308 if (::stat(p.begin(), &status) == -1)
309 return error_code(errno, system_category());
310 if (!S_ISREG(status.st_mode))
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000311 return make_error_code(errc::operation_not_permitted);
Michael J. Spencer01a87c42010-12-04 00:31:48 +0000312
313 result = status.st_size;
David Blaikie58604cd2012-02-09 19:24:12 +0000314 return error_code::success();
Michael J. Spencer01a87c42010-12-04 00:31:48 +0000315}
316
Michael J. Spencer470ae132010-12-04 00:32:40 +0000317error_code status(const Twine &path, file_status &result) {
318 SmallString<128> path_storage;
319 StringRef p = path.toNullTerminatedStringRef(path_storage);
320
321 struct stat status;
322 if (::stat(p.begin(), &status) != 0) {
323 error_code ec(errno, system_category());
324 if (ec == errc::no_such_file_or_directory)
325 result = file_status(file_type::file_not_found);
326 else
327 result = file_status(file_type::status_error);
328 return ec;
329 }
330
Nick Kledzikca077ec2012-06-20 00:28:54 +0000331 perms prms = static_cast<perms>(status.st_mode & perms_mask);
332
Michael J. Spencer470ae132010-12-04 00:32:40 +0000333 if (S_ISDIR(status.st_mode))
Nick Kledzikca077ec2012-06-20 00:28:54 +0000334 result = file_status(file_type::directory_file, prms);
Michael J. Spencer470ae132010-12-04 00:32:40 +0000335 else if (S_ISREG(status.st_mode))
Nick Kledzikca077ec2012-06-20 00:28:54 +0000336 result = file_status(file_type::regular_file, prms);
Michael J. Spencer470ae132010-12-04 00:32:40 +0000337 else if (S_ISBLK(status.st_mode))
Nick Kledzikca077ec2012-06-20 00:28:54 +0000338 result = file_status(file_type::block_file, prms);
Michael J. Spencer470ae132010-12-04 00:32:40 +0000339 else if (S_ISCHR(status.st_mode))
Nick Kledzikca077ec2012-06-20 00:28:54 +0000340 result = file_status(file_type::character_file, prms);
Michael J. Spencer470ae132010-12-04 00:32:40 +0000341 else if (S_ISFIFO(status.st_mode))
Nick Kledzikca077ec2012-06-20 00:28:54 +0000342 result = file_status(file_type::fifo_file, prms);
Michael J. Spencer470ae132010-12-04 00:32:40 +0000343 else if (S_ISSOCK(status.st_mode))
Nick Kledzikca077ec2012-06-20 00:28:54 +0000344 result = file_status(file_type::socket_file, prms);
Michael J. Spencer470ae132010-12-04 00:32:40 +0000345 else
Nick Kledzikca077ec2012-06-20 00:28:54 +0000346 result = file_status(file_type::type_unknown, prms);
Michael J. Spencer470ae132010-12-04 00:32:40 +0000347
Sylvestre Ledru9dc06bd2012-04-23 16:37:23 +0000348 result.fs_st_dev = status.st_dev;
349 result.fs_st_ino = status.st_ino;
Michael J. Spencerd45fbe62011-12-12 06:04:28 +0000350
David Blaikie58604cd2012-02-09 19:24:12 +0000351 return error_code::success();
Michael J. Spencer470ae132010-12-04 00:32:40 +0000352}
353
Nick Kledzikca077ec2012-06-20 00:28:54 +0000354// Modifies permissions on a file.
355error_code permissions(const Twine &path, perms prms) {
356 if ((prms & add_perms) && (prms & remove_perms))
357 llvm_unreachable("add_perms and remove_perms are mutually exclusive");
358
359 // Get current permissions
360 file_status info;
361 if (error_code ec = status(path, info)) {
362 return ec;
363 }
364
365 // Set updated permissions.
366 SmallString<128> path_storage;
367 StringRef p = path.toNullTerminatedStringRef(path_storage);
368 perms permsToSet;
369 if (prms & add_perms) {
370 permsToSet = (info.permissions() | prms) & perms_mask;
371 } else if (prms & remove_perms) {
372 permsToSet = (info.permissions() & ~prms) & perms_mask;
373 } else {
374 permsToSet = prms & perms_mask;
375 }
376 if (::chmod(p.begin(), static_cast<mode_t>(permsToSet))) {
377 return error_code(errno, system_category());
378 }
379
380 return error_code::success();
381}
382
Eric Christopher61aef8b2012-05-11 00:07:44 +0000383// Since this is most often used for temporary files, mode defaults to 0600.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000384error_code unique_file(const Twine &model, int &result_fd,
Eric Christopher61aef8b2012-05-11 00:07:44 +0000385 SmallVectorImpl<char> &result_path,
386 bool makeAbsolute, unsigned mode) {
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000387 SmallString<128> Model;
388 model.toVector(Model);
389 // Null terminate.
390 Model.c_str();
391
Argyrios Kyrtzidis814450a2011-07-28 00:29:20 +0000392 if (makeAbsolute) {
393 // Make model absolute by prepending a temp directory if it's not already.
394 bool absolute = path::is_absolute(Twine(Model));
395 if (!absolute) {
396 SmallString<128> TDir;
397 if (error_code ec = TempDir(TDir)) return ec;
398 path::append(TDir, Twine(Model));
399 Model.swap(TDir);
400 }
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000401 }
402
Daniel Dunbar463a7192012-05-05 16:39:22 +0000403 // From here on, DO NOT modify model. It may be needed if the randomly chosen
404 // path already exists.
Daniel Dunbar92bb5142012-05-05 16:36:24 +0000405 SmallString<128> RandomPath = Model;
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000406
407retry_random_path:
Daniel Dunbar463a7192012-05-05 16:39:22 +0000408 // Replace '%' with random chars.
Daniel Dunbar92bb5142012-05-05 16:36:24 +0000409 for (unsigned i = 0, e = Model.size(); i != e; ++i) {
410 if (Model[i] == '%')
411 RandomPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000412 }
413
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000414 // Try to open + create the file.
415rety_open_create:
Eric Christopher61aef8b2012-05-11 00:07:44 +0000416 int RandomFD = ::open(RandomPath.c_str(), O_RDWR | O_CREAT | O_EXCL, mode);
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000417 if (RandomFD == -1) {
418 // If the file existed, try again, otherwise, error.
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000419 if (errno == errc::file_exists)
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000420 goto retry_random_path;
421 // The path prefix doesn't exist.
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000422 if (errno == errc::no_such_file_or_directory) {
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000423 StringRef p(RandomPath.begin(), RandomPath.size());
424 SmallString<64> dir_to_create;
425 for (path::const_iterator i = path::begin(p),
426 e = --path::end(p); i != e; ++i) {
Michael J. Spencer936671b2010-12-07 03:57:37 +0000427 path::append(dir_to_create, *i);
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000428 bool Exists;
429 if (error_code ec = exists(Twine(dir_to_create), Exists)) return ec;
430 if (!Exists) {
431 // Don't try to create network paths.
432 if (i->size() > 2 && (*i)[0] == '/' &&
433 (*i)[1] == '/' &&
434 (*i)[2] != '/')
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000435 return make_error_code(errc::no_such_file_or_directory);
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000436 if (::mkdir(dir_to_create.c_str(), 0700) == -1)
437 return error_code(errno, system_category());
438 }
439 }
440 goto rety_open_create;
441 }
442 return error_code(errno, system_category());
443 }
444
Michael J. Spencer9d425e72010-12-04 18:45:32 +0000445 // Make the path absolute.
Andrew Trick0a828fd2011-03-24 16:43:37 +0000446 char real_path_buff[PATH_MAX + 1];
447 if (realpath(RandomPath.c_str(), real_path_buff) == NULL) {
448 int error = errno;
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000449 ::close(RandomFD);
450 ::unlink(RandomPath.c_str());
Andrew Trick0a828fd2011-03-24 16:43:37 +0000451 return error_code(error, system_category());
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000452 }
453
Andrew Trick0a828fd2011-03-24 16:43:37 +0000454 result_path.clear();
455 StringRef d(real_path_buff);
456 result_path.append(d.begin(), d.end());
457
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000458 result_fd = RandomFD;
David Blaikie58604cd2012-02-09 19:24:12 +0000459 return error_code::success();
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000460}
461
Michael J. Spencera81ac8f2011-12-08 22:50:09 +0000462error_code detail::directory_iterator_construct(detail::DirIterState &it,
463 StringRef path){
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +0000464 SmallString<128> path_null(path);
465 DIR *directory = ::opendir(path_null.c_str());
466 if (directory == 0)
467 return error_code(errno, system_category());
468
469 it.IterationHandle = reinterpret_cast<intptr_t>(directory);
470 // Add something for replace_filename to replace.
471 path::append(path_null, ".");
472 it.CurrentEntry = directory_entry(path_null.str());
473 return directory_iterator_increment(it);
474}
475
Michael J. Spencera81ac8f2011-12-08 22:50:09 +0000476error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +0000477 if (it.IterationHandle)
478 ::closedir(reinterpret_cast<DIR *>(it.IterationHandle));
479 it.IterationHandle = 0;
480 it.CurrentEntry = directory_entry();
David Blaikie58604cd2012-02-09 19:24:12 +0000481 return error_code::success();
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +0000482}
483
Michael J. Spencera81ac8f2011-12-08 22:50:09 +0000484error_code detail::directory_iterator_increment(detail::DirIterState &it) {
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +0000485 errno = 0;
486 dirent *cur_dir = ::readdir(reinterpret_cast<DIR *>(it.IterationHandle));
487 if (cur_dir == 0 && errno != 0) {
488 return error_code(errno, system_category());
489 } else if (cur_dir != 0) {
490 StringRef name(cur_dir->d_name, NAMLEN(cur_dir));
491 if ((name.size() == 1 && name[0] == '.') ||
492 (name.size() == 2 && name[0] == '.' && name[1] == '.'))
493 return directory_iterator_increment(it);
494 it.CurrentEntry.replace_filename(name);
495 } else
496 return directory_iterator_destruct(it);
497
David Blaikie58604cd2012-02-09 19:24:12 +0000498 return error_code::success();
Michael J. Spencerda7c1ca2011-01-05 16:38:57 +0000499}
500
Michael J. Spencerd6cdf1d2011-01-15 18:52:33 +0000501error_code get_magic(const Twine &path, uint32_t len,
502 SmallVectorImpl<char> &result) {
503 SmallString<128> PathStorage;
504 StringRef Path = path.toNullTerminatedStringRef(PathStorage);
505 result.set_size(0);
506
507 // Open path.
508 std::FILE *file = std::fopen(Path.data(), "rb");
509 if (file == 0)
510 return error_code(errno, system_category());
511
512 // Reserve storage.
513 result.reserve(len);
514
515 // Read magic!
516 size_t size = std::fread(result.data(), 1, len, file);
517 if (std::ferror(file) != 0) {
518 std::fclose(file);
519 return error_code(errno, system_category());
520 } else if (size != result.size()) {
521 if (std::feof(file) != 0) {
522 std::fclose(file);
523 result.set_size(size);
524 return make_error_code(errc::value_too_large);
525 }
526 }
527 std::fclose(file);
528 result.set_size(len);
David Blaikie58604cd2012-02-09 19:24:12 +0000529 return error_code::success();
Michael J. Spencerd6cdf1d2011-01-15 18:52:33 +0000530}
531
Nick Kledzikca077ec2012-06-20 00:28:54 +0000532error_code map_file_pages(const Twine &path, off_t file_offset, size_t size,
533 bool map_writable, void *&result) {
534 SmallString<128> path_storage;
535 StringRef name = path.toNullTerminatedStringRef(path_storage);
536 int oflags = map_writable ? O_RDWR : O_RDONLY;
537 int ofd = ::open(name.begin(), oflags);
538 if ( ofd == -1 )
539 return error_code(errno, system_category());
540 AutoFD fd(ofd);
541 int flags = map_writable ? MAP_SHARED : MAP_PRIVATE;
542 int prot = map_writable ? (PROT_READ|PROT_WRITE) : PROT_READ;
543#ifdef MAP_FILE
544 flags |= MAP_FILE;
545#endif
546 result = ::mmap(0, size, prot, flags, fd, file_offset);
547 if (result == MAP_FAILED) {
548 return error_code(errno, system_category());
549 }
550
551 return error_code::success();
552}
553
554error_code unmap_file_pages(void *base, size_t size) {
555 if ( ::munmap(base, size) == -1 )
556 return error_code(errno, system_category());
557
558 return error_code::success();
559}
560
561
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000562} // end namespace fs
Michael J. Spencerdffde992010-11-29 22:28:51 +0000563} // end namespace sys
564} // end namespace llvm