10127ef0fSEd Maste //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===//
20127ef0fSEd Maste //
30127ef0fSEd Maste // The LLVM Compiler Infrastructure
40127ef0fSEd Maste //
50127ef0fSEd Maste // This file is distributed under the University of Illinois Open Source
60127ef0fSEd Maste // License. See LICENSE.TXT for details.
70127ef0fSEd Maste //
80127ef0fSEd Maste //===----------------------------------------------------------------------===//
90127ef0fSEd Maste
100127ef0fSEd Maste #include "lldb/Host/FileSystem.h"
110127ef0fSEd Maste
120127ef0fSEd Maste // C includes
131c3bbb01SEd Maste #include <dirent.h>
14*b5893f02SDimitry Andric #include <fcntl.h>
151c3bbb01SEd Maste #include <sys/mount.h>
161c3bbb01SEd Maste #include <sys/param.h>
170127ef0fSEd Maste #include <sys/stat.h>
180127ef0fSEd Maste #include <sys/types.h>
19b40b48b8SDimitry Andric #include <unistd.h>
209f2f44ceSEd Maste #if defined(__NetBSD__)
219f2f44ceSEd Maste #include <sys/statvfs.h>
229f2f44ceSEd Maste #endif
230127ef0fSEd Maste
240127ef0fSEd Maste // lldb Includes
250127ef0fSEd Maste #include "lldb/Host/Host.h"
265517e702SDimitry Andric #include "lldb/Utility/Status.h"
27f678e45dSDimitry Andric #include "lldb/Utility/StreamString.h"
28f678e45dSDimitry Andric
29f678e45dSDimitry Andric #include "llvm/Support/FileSystem.h"
300127ef0fSEd Maste
310127ef0fSEd Maste using namespace lldb;
320127ef0fSEd Maste using namespace lldb_private;
330127ef0fSEd Maste
34435933ddSDimitry Andric const char *FileSystem::DEV_NULL = "/dev/null";
359f2f44ceSEd Maste
Symlink(const FileSpec & src,const FileSpec & dst)365517e702SDimitry Andric Status FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) {
375517e702SDimitry Andric Status error;
381c3bbb01SEd Maste if (::symlink(dst.GetCString(), src.GetCString()) == -1)
390127ef0fSEd Maste error.SetErrorToErrno();
400127ef0fSEd Maste return error;
410127ef0fSEd Maste }
420127ef0fSEd Maste
Readlink(const FileSpec & src,FileSpec & dst)435517e702SDimitry Andric Status FileSystem::Readlink(const FileSpec &src, FileSpec &dst) {
445517e702SDimitry Andric Status error;
451c3bbb01SEd Maste char buf[PATH_MAX];
461c3bbb01SEd Maste ssize_t count = ::readlink(src.GetCString(), buf, sizeof(buf) - 1);
470127ef0fSEd Maste if (count < 0)
480127ef0fSEd Maste error.SetErrorToErrno();
49435933ddSDimitry Andric else {
501c3bbb01SEd Maste buf[count] = '\0'; // Success
51*b5893f02SDimitry Andric dst.SetFile(buf, FileSpec::Style::native);
521c3bbb01SEd Maste }
530127ef0fSEd Maste return error;
540127ef0fSEd Maste }
550127ef0fSEd Maste
ResolveSymbolicLink(const FileSpec & src,FileSpec & dst)565517e702SDimitry Andric Status FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) {
579f2f44ceSEd Maste char resolved_path[PATH_MAX];
58435933ddSDimitry Andric if (!src.GetPath(resolved_path, sizeof(resolved_path))) {
595517e702SDimitry Andric return Status("Couldn't get the canonical path for %s", src.GetCString());
609f2f44ceSEd Maste }
619f2f44ceSEd Maste
629f2f44ceSEd Maste char real_path[PATH_MAX + 1];
63435933ddSDimitry Andric if (realpath(resolved_path, real_path) == nullptr) {
645517e702SDimitry Andric Status err;
659f2f44ceSEd Maste err.SetErrorToErrno();
669f2f44ceSEd Maste return err;
679f2f44ceSEd Maste }
689f2f44ceSEd Maste
69*b5893f02SDimitry Andric dst = FileSpec(real_path);
709f2f44ceSEd Maste
715517e702SDimitry Andric return Status();
729f2f44ceSEd Maste }
739f2f44ceSEd Maste
Fopen(const char * path,const char * mode)74435933ddSDimitry Andric FILE *FileSystem::Fopen(const char *path, const char *mode) {
754bb0738eSEd Maste return ::fopen(path, mode);
764bb0738eSEd Maste }
77*b5893f02SDimitry Andric
Open(const char * path,int flags,int mode)78*b5893f02SDimitry Andric int FileSystem::Open(const char *path, int flags, int mode) {
79*b5893f02SDimitry Andric return ::open(path, flags, mode);
80*b5893f02SDimitry Andric }
81