blob: 73d6e7f7b2b2485df898ca6e3bc594d011e4f42e [file] [log] [blame]
Ben Langmuirc8130a72014-02-20 21:59:23 +00001//===- VirtualFileSystem.cpp - Virtual File System Layer --------*- 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// This file implements the VirtualFileSystem interface.
10//===----------------------------------------------------------------------===//
11
12#include "clang/Basic/VirtualFileSystem.h"
Ben Langmuird51ba0b2014-02-21 23:39:37 +000013#include "llvm/ADT/DenseMap.h"
Ben Langmuirc8130a72014-02-20 21:59:23 +000014#include "llvm/ADT/OwningPtr.h"
Ben Langmuird51ba0b2014-02-21 23:39:37 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/Support/Atomic.h"
Ben Langmuirc8130a72014-02-20 21:59:23 +000018#include "llvm/Support/MemoryBuffer.h"
Ben Langmuirc8130a72014-02-20 21:59:23 +000019#include "llvm/Support/Path.h"
Ben Langmuird51ba0b2014-02-21 23:39:37 +000020#include "llvm/Support/YAMLParser.h"
Ben Langmuirc8130a72014-02-20 21:59:23 +000021
22using namespace clang;
23using namespace clang::vfs;
24using namespace llvm;
25using llvm::sys::fs::file_status;
26using llvm::sys::fs::file_type;
27using llvm::sys::fs::perms;
28using llvm::sys::fs::UniqueID;
29
30Status::Status(const file_status &Status)
31 : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
32 User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
33 Type(Status.type()), Perms(Status.permissions()) {}
34
35Status::Status(StringRef Name, StringRef ExternalName, UniqueID UID,
36 sys::TimeValue MTime, uint32_t User, uint32_t Group,
37 uint64_t Size, file_type Type, perms Perms)
Ben Langmuirb59cf672014-02-27 00:25:12 +000038 : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size),
39 Type(Type), Perms(Perms) {}
Ben Langmuirc8130a72014-02-20 21:59:23 +000040
41bool Status::equivalent(const Status &Other) const {
42 return getUniqueID() == Other.getUniqueID();
43}
44bool Status::isDirectory() const {
45 return Type == file_type::directory_file;
46}
47bool Status::isRegularFile() const {
48 return Type == file_type::regular_file;
49}
50bool Status::isOther() const {
51 return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
52}
53bool Status::isSymlink() const {
54 return Type == file_type::symlink_file;
55}
56bool Status::isStatusKnown() const {
57 return Type != file_type::status_error;
58}
59bool Status::exists() const {
60 return isStatusKnown() && Type != file_type::file_not_found;
61}
62
63File::~File() {}
64
65FileSystem::~FileSystem() {}
66
67error_code FileSystem::getBufferForFile(const llvm::Twine &Name,
68 OwningPtr<MemoryBuffer> &Result,
69 int64_t FileSize,
70 bool RequiresNullTerminator) {
71 llvm::OwningPtr<File> F;
72 if (error_code EC = openFileForRead(Name, F))
73 return EC;
74
75 error_code EC = F->getBuffer(Name, Result, FileSize, RequiresNullTerminator);
76 return EC;
77}
78
79//===-----------------------------------------------------------------------===/
80// RealFileSystem implementation
81//===-----------------------------------------------------------------------===/
82
Benjamin Kramer3d6220d2014-03-01 17:21:22 +000083namespace {
Ben Langmuirc8130a72014-02-20 21:59:23 +000084/// \brief Wrapper around a raw file descriptor.
85class RealFile : public File {
86 int FD;
Ben Langmuird066d4c2014-02-28 21:16:07 +000087 Status S;
Ben Langmuirc8130a72014-02-20 21:59:23 +000088 friend class RealFileSystem;
89 RealFile(int FD) : FD(FD) {
90 assert(FD >= 0 && "Invalid or inactive file descriptor");
91 }
Ben Langmuird51ba0b2014-02-21 23:39:37 +000092
Ben Langmuirc8130a72014-02-20 21:59:23 +000093public:
94 ~RealFile();
Craig Toppera798a9d2014-03-02 09:32:10 +000095 ErrorOr<Status> status() override;
Ben Langmuirc8130a72014-02-20 21:59:23 +000096 error_code getBuffer(const Twine &Name, OwningPtr<MemoryBuffer> &Result,
97 int64_t FileSize = -1,
Craig Toppera798a9d2014-03-02 09:32:10 +000098 bool RequiresNullTerminator = true) override;
99 error_code close() override;
100 void setName(StringRef Name) override;
Ben Langmuirc8130a72014-02-20 21:59:23 +0000101};
Benjamin Kramer3d6220d2014-03-01 17:21:22 +0000102} // end anonymous namespace
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000103RealFile::~RealFile() { close(); }
Ben Langmuirc8130a72014-02-20 21:59:23 +0000104
105ErrorOr<Status> RealFile::status() {
106 assert(FD != -1 && "cannot stat closed file");
Ben Langmuird066d4c2014-02-28 21:16:07 +0000107 if (!S.isStatusKnown()) {
108 file_status RealStatus;
109 if (error_code EC = sys::fs::status(FD, RealStatus))
110 return EC;
111 Status NewS(RealStatus);
112 NewS.setName(S.getName());
Chandler Carruthc72d9b32014-03-02 04:02:40 +0000113 S = std::move(NewS);
Ben Langmuird066d4c2014-02-28 21:16:07 +0000114 }
115 return S;
Ben Langmuirc8130a72014-02-20 21:59:23 +0000116}
117
118error_code RealFile::getBuffer(const Twine &Name,
119 OwningPtr<MemoryBuffer> &Result,
120 int64_t FileSize, bool RequiresNullTerminator) {
121 assert(FD != -1 && "cannot get buffer for closed file");
122 return MemoryBuffer::getOpenFile(FD, Name.str().c_str(), Result, FileSize,
123 RequiresNullTerminator);
124}
125
126// FIXME: This is terrible, we need this for ::close.
127#if !defined(_MSC_VER) && !defined(__MINGW32__)
128#include <unistd.h>
129#include <sys/uio.h>
130#else
131#include <io.h>
132#ifndef S_ISFIFO
133#define S_ISFIFO(x) (0)
134#endif
135#endif
136error_code RealFile::close() {
137 if (::close(FD))
138 return error_code(errno, system_category());
139 FD = -1;
140 return error_code::success();
141}
142
Ben Langmuird066d4c2014-02-28 21:16:07 +0000143void RealFile::setName(StringRef Name) {
144 S.setName(Name);
145}
146
Benjamin Kramer3d6220d2014-03-01 17:21:22 +0000147namespace {
Ben Langmuirc8130a72014-02-20 21:59:23 +0000148/// \brief The file system according to your operating system.
149class RealFileSystem : public FileSystem {
150public:
Craig Toppera798a9d2014-03-02 09:32:10 +0000151 ErrorOr<Status> status(const Twine &Path) override;
Ben Langmuirc8130a72014-02-20 21:59:23 +0000152 error_code openFileForRead(const Twine &Path,
Craig Toppera798a9d2014-03-02 09:32:10 +0000153 OwningPtr<File> &Result) override;
Ben Langmuirc8130a72014-02-20 21:59:23 +0000154};
Benjamin Kramer3d6220d2014-03-01 17:21:22 +0000155} // end anonymous namespace
Ben Langmuirc8130a72014-02-20 21:59:23 +0000156
157ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
158 sys::fs::file_status RealStatus;
159 if (error_code EC = sys::fs::status(Path, RealStatus))
160 return EC;
161 Status Result(RealStatus);
162 Result.setName(Path.str());
Ben Langmuirc8130a72014-02-20 21:59:23 +0000163 return Result;
164}
165
166error_code RealFileSystem::openFileForRead(const Twine &Name,
167 OwningPtr<File> &Result) {
168 int FD;
169 if (error_code EC = sys::fs::openFileForRead(Name, FD))
170 return EC;
171 Result.reset(new RealFile(FD));
Ben Langmuird066d4c2014-02-28 21:16:07 +0000172 Result->setName(Name.str());
Ben Langmuirc8130a72014-02-20 21:59:23 +0000173 return error_code::success();
174}
175
176IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
177 static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem();
178 return FS;
179}
180
181//===-----------------------------------------------------------------------===/
182// OverlayFileSystem implementation
183//===-----------------------------------------------------------------------===/
184OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
185 pushOverlay(BaseFS);
186}
187
188void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
189 FSList.push_back(FS);
190}
191
192ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
193 // FIXME: handle symlinks that cross file systems
194 for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
195 ErrorOr<Status> Status = (*I)->status(Path);
196 if (Status || Status.getError() != errc::no_such_file_or_directory)
197 return Status;
198 }
199 return error_code(errc::no_such_file_or_directory, system_category());
200}
201
202error_code OverlayFileSystem::openFileForRead(const llvm::Twine &Path,
203 OwningPtr<File> &Result) {
204 // FIXME: handle symlinks that cross file systems
205 for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
206 error_code EC = (*I)->openFileForRead(Path, Result);
207 if (!EC || EC != errc::no_such_file_or_directory)
208 return EC;
209 }
210 return error_code(errc::no_such_file_or_directory, system_category());
211}
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000212
213//===-----------------------------------------------------------------------===/
214// VFSFromYAML implementation
215//===-----------------------------------------------------------------------===/
216
217// Allow DenseMap<StringRef, ...>. This is useful below because we know all the
218// strings are literals and will outlive the map, and there is no reason to
219// store them.
220namespace llvm {
221 template<>
222 struct DenseMapInfo<StringRef> {
223 // This assumes that "" will never be a valid key.
224 static inline StringRef getEmptyKey() { return StringRef(""); }
225 static inline StringRef getTombstoneKey() { return StringRef(); }
226 static unsigned getHashValue(StringRef Val) { return HashString(Val); }
227 static bool isEqual(StringRef LHS, StringRef RHS) { return LHS == RHS; }
228 };
229}
230
231namespace {
232
233enum EntryKind {
234 EK_Directory,
235 EK_File
236};
237
238/// \brief A single file or directory in the VFS.
239class Entry {
240 EntryKind Kind;
241 std::string Name;
242
243public:
244 virtual ~Entry();
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000245 Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
246 StringRef getName() const { return Name; }
247 EntryKind getKind() const { return Kind; }
248};
249
250class DirectoryEntry : public Entry {
251 std::vector<Entry *> Contents;
252 Status S;
253
254public:
255 virtual ~DirectoryEntry();
256#if LLVM_HAS_RVALUE_REFERENCES
Ben Langmuir47ff9ab2014-02-25 04:34:14 +0000257 DirectoryEntry(StringRef Name, std::vector<Entry *> Contents, Status S)
258 : Entry(EK_Directory, Name), Contents(std::move(Contents)),
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000259 S(std::move(S)) {}
260#endif
261 DirectoryEntry(StringRef Name, ArrayRef<Entry *> Contents, const Status &S)
262 : Entry(EK_Directory, Name), Contents(Contents), S(S) {}
263 Status getStatus() { return S; }
264 typedef std::vector<Entry *>::iterator iterator;
265 iterator contents_begin() { return Contents.begin(); }
266 iterator contents_end() { return Contents.end(); }
267 static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
268};
269
270class FileEntry : public Entry {
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000271public:
Ben Langmuirb59cf672014-02-27 00:25:12 +0000272 enum NameKind {
273 NK_NotSet,
274 NK_External,
275 NK_Virtual
276 };
277private:
278 std::string ExternalContentsPath;
279 NameKind UseName;
280public:
281 FileEntry(StringRef Name, StringRef ExternalContentsPath, NameKind UseName)
282 : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
283 UseName(UseName) {}
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000284 StringRef getExternalContentsPath() const { return ExternalContentsPath; }
Ben Langmuirb59cf672014-02-27 00:25:12 +0000285 /// \brief whether to use the external path as the name for this file.
Ben Langmuird066d4c2014-02-28 21:16:07 +0000286 bool useExternalName(bool GlobalUseExternalName) const {
287 return UseName == NK_NotSet ? GlobalUseExternalName
288 : (UseName == NK_External);
289 }
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000290 static bool classof(const Entry *E) { return E->getKind() == EK_File; }
291};
292
293/// \brief A virtual file system parsed from a YAML file.
294///
295/// Currently, this class allows creating virtual directories and mapping
296/// virtual file paths to existing external files, available in \c ExternalFS.
297///
298/// The basic structure of the parsed file is:
299/// \verbatim
300/// {
301/// 'version': <version number>,
302/// <optional configuration>
303/// 'roots': [
304/// <directory entries>
305/// ]
306/// }
307/// \endverbatim
308///
309/// All configuration options are optional.
310/// 'case-sensitive': <boolean, default=true>
Ben Langmuirb59cf672014-02-27 00:25:12 +0000311/// 'use-external-names': <boolean, default=true>
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000312///
313/// Virtual directories are represented as
314/// \verbatim
315/// {
316/// 'type': 'directory',
317/// 'name': <string>,
318/// 'contents': [ <file or directory entries> ]
319/// }
320/// \endverbatim
321///
322/// The default attributes for virtual directories are:
323/// \verbatim
324/// MTime = now() when created
325/// Perms = 0777
326/// User = Group = 0
327/// Size = 0
328/// UniqueID = unspecified unique value
329/// \endverbatim
330///
331/// Re-mapped files are represented as
332/// \verbatim
333/// {
334/// 'type': 'file',
335/// 'name': <string>,
Ben Langmuirb59cf672014-02-27 00:25:12 +0000336/// 'use-external-name': <boolean> # Optional
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000337/// 'external-contents': <path to external file>)
338/// }
339/// \endverbatim
340///
341/// and inherit their attributes from the external contents.
342///
Ben Langmuir47ff9ab2014-02-25 04:34:14 +0000343/// In both cases, the 'name' field may contain multiple path components (e.g.
344/// /path/to/file). However, any directory that contains more than one child
345/// must be uniquely represented by a directory entry.
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000346class VFSFromYAML : public vfs::FileSystem {
347 std::vector<Entry *> Roots; ///< The root(s) of the virtual file system.
348 /// \brief The file system to use for external references.
349 IntrusiveRefCntPtr<FileSystem> ExternalFS;
350
351 /// @name Configuration
352 /// @{
353
354 /// \brief Whether to perform case-sensitive comparisons.
355 ///
356 /// Currently, case-insensitive matching only works correctly with ASCII.
Ben Langmuirb59cf672014-02-27 00:25:12 +0000357 bool CaseSensitive;
358
359 /// \brief Whether to use to use the value of 'external-contents' for the
360 /// names of files. This global value is overridable on a per-file basis.
361 bool UseExternalNames;
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000362 /// @}
363
364 friend class VFSFromYAMLParser;
365
366private:
367 VFSFromYAML(IntrusiveRefCntPtr<FileSystem> ExternalFS)
Ben Langmuirb59cf672014-02-27 00:25:12 +0000368 : ExternalFS(ExternalFS), CaseSensitive(true), UseExternalNames(true) {}
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000369
370 /// \brief Looks up \p Path in \c Roots.
371 ErrorOr<Entry *> lookupPath(const Twine &Path);
372
373 /// \brief Looks up the path <tt>[Start, End)</tt> in \p From, possibly
374 /// recursing into the contents of \p From if it is a directory.
375 ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start,
376 sys::path::const_iterator End, Entry *From);
377
378public:
379 ~VFSFromYAML();
380
381 /// \brief Parses \p Buffer, which is expected to be in YAML format and
382 /// returns a virtual file system representing its contents.
383 ///
384 /// Takes ownership of \p Buffer.
385 static VFSFromYAML *create(MemoryBuffer *Buffer,
386 SourceMgr::DiagHandlerTy DiagHandler,
Ben Langmuir97882e72014-02-24 20:56:37 +0000387 void *DiagContext,
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000388 IntrusiveRefCntPtr<FileSystem> ExternalFS);
389
Craig Toppera798a9d2014-03-02 09:32:10 +0000390 ErrorOr<Status> status(const Twine &Path) override;
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000391 error_code openFileForRead(const Twine &Path,
Craig Toppera798a9d2014-03-02 09:32:10 +0000392 OwningPtr<File> &Result) override;
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000393};
394
395/// \brief A helper class to hold the common YAML parsing state.
396class VFSFromYAMLParser {
397 yaml::Stream &Stream;
398
399 void error(yaml::Node *N, const Twine &Msg) {
400 Stream.printError(N, Msg);
401 }
402
403 // false on error
404 bool parseScalarString(yaml::Node *N, StringRef &Result,
405 SmallVectorImpl<char> &Storage) {
406 yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N);
407 if (!S) {
408 error(N, "expected string");
409 return false;
410 }
411 Result = S->getValue(Storage);
412 return true;
413 }
414
415 // false on error
416 bool parseScalarBool(yaml::Node *N, bool &Result) {
417 SmallString<5> Storage;
418 StringRef Value;
419 if (!parseScalarString(N, Value, Storage))
420 return false;
421
422 if (Value.equals_lower("true") || Value.equals_lower("on") ||
423 Value.equals_lower("yes") || Value == "1") {
424 Result = true;
425 return true;
426 } else if (Value.equals_lower("false") || Value.equals_lower("off") ||
427 Value.equals_lower("no") || Value == "0") {
428 Result = false;
429 return true;
430 }
431
432 error(N, "expected boolean value");
433 return false;
434 }
435
436 struct KeyStatus {
437 KeyStatus(bool Required=false) : Required(Required), Seen(false) {}
438 bool Required;
439 bool Seen;
440 };
441 typedef std::pair<StringRef, KeyStatus> KeyStatusPair;
442
443 // false on error
444 bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
445 DenseMap<StringRef, KeyStatus> &Keys) {
446 if (!Keys.count(Key)) {
447 error(KeyNode, "unknown key");
448 return false;
449 }
450 KeyStatus &S = Keys[Key];
451 if (S.Seen) {
452 error(KeyNode, Twine("duplicate key '") + Key + "'");
453 return false;
454 }
455 S.Seen = true;
456 return true;
457 }
458
459 // false on error
460 bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
461 for (DenseMap<StringRef, KeyStatus>::iterator I = Keys.begin(),
462 E = Keys.end();
463 I != E; ++I) {
464 if (I->second.Required && !I->second.Seen) {
465 error(Obj, Twine("missing key '") + I->first + "'");
466 return false;
467 }
468 }
469 return true;
470 }
471
472 Entry *parseEntry(yaml::Node *N) {
473 yaml::MappingNode *M = dyn_cast<yaml::MappingNode>(N);
474 if (!M) {
475 error(N, "expected mapping node for file or directory entry");
476 return NULL;
477 }
478
479 KeyStatusPair Fields[] = {
480 KeyStatusPair("name", true),
481 KeyStatusPair("type", true),
482 KeyStatusPair("contents", false),
Ben Langmuirb59cf672014-02-27 00:25:12 +0000483 KeyStatusPair("external-contents", false),
484 KeyStatusPair("use-external-name", false),
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000485 };
486
487 DenseMap<StringRef, KeyStatus> Keys(
488 &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));
489
490 bool HasContents = false; // external or otherwise
491 std::vector<Entry *> EntryArrayContents;
492 std::string ExternalContentsPath;
493 std::string Name;
Ben Langmuirb59cf672014-02-27 00:25:12 +0000494 FileEntry::NameKind UseExternalName = FileEntry::NK_NotSet;
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000495 EntryKind Kind;
496
497 for (yaml::MappingNode::iterator I = M->begin(), E = M->end(); I != E;
498 ++I) {
499 StringRef Key;
500 // Reuse the buffer for key and value, since we don't look at key after
501 // parsing value.
502 SmallString<256> Buffer;
503 if (!parseScalarString(I->getKey(), Key, Buffer))
504 return NULL;
505
506 if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
507 return NULL;
508
509 StringRef Value;
510 if (Key == "name") {
511 if (!parseScalarString(I->getValue(), Value, Buffer))
512 return NULL;
513 Name = Value;
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000514 } else if (Key == "type") {
515 if (!parseScalarString(I->getValue(), Value, Buffer))
516 return NULL;
517 if (Value == "file")
518 Kind = EK_File;
519 else if (Value == "directory")
520 Kind = EK_Directory;
521 else {
522 error(I->getValue(), "unknown value for 'type'");
523 return NULL;
524 }
525 } else if (Key == "contents") {
526 if (HasContents) {
527 error(I->getKey(),
528 "entry already has 'contents' or 'external-contents'");
529 return NULL;
530 }
531 HasContents = true;
532 yaml::SequenceNode *Contents =
533 dyn_cast<yaml::SequenceNode>(I->getValue());
534 if (!Contents) {
535 // FIXME: this is only for directories, what about files?
536 error(I->getValue(), "expected array");
537 return NULL;
538 }
539
540 for (yaml::SequenceNode::iterator I = Contents->begin(),
541 E = Contents->end();
542 I != E; ++I) {
543 if (Entry *E = parseEntry(&*I))
544 EntryArrayContents.push_back(E);
545 else
546 return NULL;
547 }
548 } else if (Key == "external-contents") {
549 if (HasContents) {
550 error(I->getKey(),
551 "entry already has 'contents' or 'external-contents'");
552 return NULL;
553 }
554 HasContents = true;
555 if (!parseScalarString(I->getValue(), Value, Buffer))
556 return NULL;
557 ExternalContentsPath = Value;
Ben Langmuirb59cf672014-02-27 00:25:12 +0000558 } else if (Key == "use-external-name") {
559 bool Val;
560 if (!parseScalarBool(I->getValue(), Val))
561 return NULL;
562 UseExternalName = Val ? FileEntry::NK_External : FileEntry::NK_Virtual;
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000563 } else {
564 llvm_unreachable("key missing from Keys");
565 }
566 }
567
568 if (Stream.failed())
569 return NULL;
570
571 // check for missing keys
572 if (!HasContents) {
573 error(N, "missing key 'contents' or 'external-contents'");
574 return NULL;
575 }
576 if (!checkMissingKeys(N, Keys))
577 return NULL;
578
Ben Langmuirb59cf672014-02-27 00:25:12 +0000579 // check invalid configuration
580 if (Kind == EK_Directory && UseExternalName != FileEntry::NK_NotSet) {
581 error(N, "'use-external-name' is not supported for directories");
582 return NULL;
583 }
584
Ben Langmuir47ff9ab2014-02-25 04:34:14 +0000585 // Remove trailing slash(es)
586 StringRef Trimmed(Name);
587 while (Trimmed.size() > 1 && sys::path::is_separator(Trimmed.back()))
588 Trimmed = Trimmed.slice(0, Trimmed.size()-1);
589 // Get the last component
590 StringRef LastComponent = sys::path::filename(Trimmed);
591
592 Entry *Result = 0;
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000593 switch (Kind) {
594 case EK_File:
Chandler Carruthc72d9b32014-03-02 04:02:40 +0000595 Result = new FileEntry(LastComponent, std::move(ExternalContentsPath),
Ben Langmuirb59cf672014-02-27 00:25:12 +0000596 UseExternalName);
Ben Langmuir47ff9ab2014-02-25 04:34:14 +0000597 break;
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000598 case EK_Directory:
Chandler Carruthc72d9b32014-03-02 04:02:40 +0000599 Result = new DirectoryEntry(LastComponent, std::move(EntryArrayContents),
Ben Langmuir47ff9ab2014-02-25 04:34:14 +0000600 Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
601 0, file_type::directory_file, sys::fs::all_all));
602 break;
603 }
604
605 StringRef Parent = sys::path::parent_path(Trimmed);
606 if (Parent.empty())
607 return Result;
608
609 // if 'name' contains multiple components, create implicit directory entries
610 for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
611 E = sys::path::rend(Parent);
612 I != E; ++I) {
613 Result = new DirectoryEntry(*I, Result,
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000614 Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
615 0, file_type::directory_file, sys::fs::all_all));
616 }
Ben Langmuir47ff9ab2014-02-25 04:34:14 +0000617 return Result;
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000618 }
619
620public:
621 VFSFromYAMLParser(yaml::Stream &S) : Stream(S) {}
622
623 // false on error
624 bool parse(yaml::Node *Root, VFSFromYAML *FS) {
625 yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
626 if (!Top) {
627 error(Root, "expected mapping node");
628 return false;
629 }
630
631 KeyStatusPair Fields[] = {
632 KeyStatusPair("version", true),
633 KeyStatusPair("case-sensitive", false),
Ben Langmuirb59cf672014-02-27 00:25:12 +0000634 KeyStatusPair("use-external-names", false),
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000635 KeyStatusPair("roots", true),
636 };
637
638 DenseMap<StringRef, KeyStatus> Keys(
639 &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));
640
641 // Parse configuration and 'roots'
642 for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E;
643 ++I) {
644 SmallString<10> KeyBuffer;
645 StringRef Key;
646 if (!parseScalarString(I->getKey(), Key, KeyBuffer))
647 return false;
648
649 if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
650 return false;
651
652 if (Key == "roots") {
653 yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue());
654 if (!Roots) {
655 error(I->getValue(), "expected array");
656 return false;
657 }
658
659 for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end();
660 I != E; ++I) {
661 if (Entry *E = parseEntry(&*I))
662 FS->Roots.push_back(E);
663 else
664 return false;
665 }
666 } else if (Key == "version") {
667 StringRef VersionString;
668 SmallString<4> Storage;
669 if (!parseScalarString(I->getValue(), VersionString, Storage))
670 return false;
671 int Version;
672 if (VersionString.getAsInteger<int>(10, Version)) {
673 error(I->getValue(), "expected integer");
674 return false;
675 }
676 if (Version < 0) {
677 error(I->getValue(), "invalid version number");
678 return false;
679 }
680 if (Version != 0) {
681 error(I->getValue(), "version mismatch, expected 0");
682 return false;
683 }
684 } else if (Key == "case-sensitive") {
685 if (!parseScalarBool(I->getValue(), FS->CaseSensitive))
686 return false;
Ben Langmuirb59cf672014-02-27 00:25:12 +0000687 } else if (Key == "use-external-names") {
688 if (!parseScalarBool(I->getValue(), FS->UseExternalNames))
689 return false;
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000690 } else {
691 llvm_unreachable("key missing from Keys");
692 }
693 }
694
695 if (Stream.failed())
696 return false;
697
698 if (!checkMissingKeys(Top, Keys))
699 return false;
700 return true;
701 }
702};
703} // end of anonymous namespace
704
705Entry::~Entry() {}
706DirectoryEntry::~DirectoryEntry() { llvm::DeleteContainerPointers(Contents); }
707
708VFSFromYAML::~VFSFromYAML() { llvm::DeleteContainerPointers(Roots); }
709
710VFSFromYAML *VFSFromYAML::create(MemoryBuffer *Buffer,
711 SourceMgr::DiagHandlerTy DiagHandler,
Ben Langmuir97882e72014-02-24 20:56:37 +0000712 void *DiagContext,
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000713 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
714
715 SourceMgr SM;
716 yaml::Stream Stream(Buffer, SM);
717
Ben Langmuir97882e72014-02-24 20:56:37 +0000718 SM.setDiagHandler(DiagHandler, DiagContext);
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000719 yaml::document_iterator DI = Stream.begin();
720 yaml::Node *Root = DI->getRoot();
721 if (DI == Stream.end() || !Root) {
722 SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
723 return NULL;
724 }
725
726 VFSFromYAMLParser P(Stream);
727
728 OwningPtr<VFSFromYAML> FS(new VFSFromYAML(ExternalFS));
729 if (!P.parse(Root, FS.get()))
730 return NULL;
731
732 return FS.take();
733}
734
735ErrorOr<Entry *> VFSFromYAML::lookupPath(const Twine &Path_) {
736 SmallVector<char, 256> Storage;
737 StringRef Path = Path_.toNullTerminatedStringRef(Storage);
738
739 if (Path.empty())
740 return error_code(errc::invalid_argument, system_category());
741
742 sys::path::const_iterator Start = sys::path::begin(Path);
743 sys::path::const_iterator End = sys::path::end(Path);
744 for (std::vector<Entry *>::iterator I = Roots.begin(), E = Roots.end();
745 I != E; ++I) {
746 ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
747 if (Result || Result.getError() != errc::no_such_file_or_directory)
748 return Result;
749 }
750 return error_code(errc::no_such_file_or_directory, system_category());
751}
752
753ErrorOr<Entry *> VFSFromYAML::lookupPath(sys::path::const_iterator Start,
754 sys::path::const_iterator End,
755 Entry *From) {
756 // FIXME: handle . and ..
757 if (CaseSensitive ? !Start->equals(From->getName())
758 : !Start->equals_lower(From->getName()))
759 // failure to match
760 return error_code(errc::no_such_file_or_directory, system_category());
761
762 ++Start;
763
764 if (Start == End) {
765 // Match!
766 return From;
767 }
768
769 DirectoryEntry *DE = dyn_cast<DirectoryEntry>(From);
770 if (!DE)
771 return error_code(errc::not_a_directory, system_category());
772
773 for (DirectoryEntry::iterator I = DE->contents_begin(),
774 E = DE->contents_end();
775 I != E; ++I) {
776 ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
777 if (Result || Result.getError() != errc::no_such_file_or_directory)
778 return Result;
779 }
780 return error_code(errc::no_such_file_or_directory, system_category());
781}
782
783ErrorOr<Status> VFSFromYAML::status(const Twine &Path) {
784 ErrorOr<Entry *> Result = lookupPath(Path);
785 if (!Result)
786 return Result.getError();
787
788 std::string PathStr(Path.str());
789 if (FileEntry *F = dyn_cast<FileEntry>(*Result)) {
790 ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath());
Ben Langmuirb59cf672014-02-27 00:25:12 +0000791 assert(!S || S->getName() == F->getExternalContentsPath());
Ben Langmuird066d4c2014-02-28 21:16:07 +0000792 if (S && !F->useExternalName(UseExternalNames))
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000793 S->setName(PathStr);
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000794 return S;
795 } else { // directory
796 DirectoryEntry *DE = cast<DirectoryEntry>(*Result);
797 Status S = DE->getStatus();
798 S.setName(PathStr);
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000799 return S;
800 }
801}
802
803error_code VFSFromYAML::openFileForRead(const Twine &Path,
804 OwningPtr<vfs::File> &Result) {
805 ErrorOr<Entry *> E = lookupPath(Path);
806 if (!E)
807 return E.getError();
808
809 FileEntry *F = dyn_cast<FileEntry>(*E);
810 if (!F) // FIXME: errc::not_a_file?
811 return error_code(errc::invalid_argument, system_category());
812
Ben Langmuird066d4c2014-02-28 21:16:07 +0000813 if (error_code EC = ExternalFS->openFileForRead(F->getExternalContentsPath(),
814 Result))
815 return EC;
816
817 if (!F->useExternalName(UseExternalNames))
818 Result->setName(Path.str());
819
820 return error_code::success();
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000821}
822
823IntrusiveRefCntPtr<FileSystem>
824vfs::getVFSFromYAML(MemoryBuffer *Buffer, SourceMgr::DiagHandlerTy DiagHandler,
Ben Langmuir97882e72014-02-24 20:56:37 +0000825 void *DiagContext,
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000826 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
Ben Langmuir97882e72014-02-24 20:56:37 +0000827 return VFSFromYAML::create(Buffer, DiagHandler, DiagContext, ExternalFS);
Ben Langmuird51ba0b2014-02-21 23:39:37 +0000828}
829
830UniqueID vfs::getNextVirtualUniqueID() {
831 static volatile sys::cas_flag UID = 0;
832 sys::cas_flag ID = llvm::sys::AtomicIncrement(&UID);
833 // The following assumes that uint64_t max will never collide with a real
834 // dev_t value from the OS.
835 return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
836}