Files
clang-p2996/lldb/source/Host/posix/FileSystem.cpp
Greg Clayton 736888c84b Avoid crashing by not mmap'ing files on network mounted file systems.
This is implemented by making a new FileSystem function:

bool
FileSystem::IsLocal(const FileSpec &spec)

Then using this in a new function:

DataBufferSP
FileSpec::MemoryMapFileContentsIfLocal(off_t file_offset, size_t file_size) const;

This function only mmaps data if the file is a local file since that means we can reliably page in data. We were experiencing crashes where people would use debug info files on network mounted file systems and that mount would go away and cause the next access to a page that wasn't paged in to crash LLDB. 

We now avoid this by just copying the data into a heap buffer and keeping a permanent copy to avoid the crash. Updated all previous users of FileSpec::MemoryMapFileContentsIfLocal() in ObjectFile subclasses over to use the new FileSpec::MemoryMapFileContentsIfLocal() function.

<rdar://problem/19470249>

llvm-svn: 230283
2015-02-23 23:47:09 +00:00

187 lines
4.6 KiB
C++

//===-- FileSystem.cpp ------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Host/FileSystem.h"
// C includes
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
// lldb Includes
#include "lldb/Core/Error.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Host/Host.h"
using namespace lldb;
using namespace lldb_private;
FileSpec::PathSyntax
FileSystem::GetNativePathSyntax()
{
return FileSpec::ePathSyntaxPosix;
}
Error
FileSystem::MakeDirectory(const char *path, uint32_t file_permissions)
{
Error error;
if (path && path[0])
{
if (::mkdir(path, file_permissions) != 0)
{
error.SetErrorToErrno();
switch (error.GetError())
{
case ENOENT:
{
// Parent directory doesn't exist, so lets make it if we can
FileSpec spec(path, false);
if (spec.GetDirectory() && spec.GetFilename())
{
// Make the parent directory and try again
Error error2 = MakeDirectory(spec.GetDirectory().GetCString(), file_permissions);
if (error2.Success())
{
// Try and make the directory again now that the parent directory was made successfully
if (::mkdir(path, file_permissions) == 0)
error.Clear();
else
error.SetErrorToErrno();
}
}
}
break;
case EEXIST:
{
FileSpec path_spec(path, false);
if (path_spec.IsDirectory())
error.Clear(); // It is a directory and it already exists
}
break;
}
}
}
else
{
error.SetErrorString("empty path");
}
return error;
}
Error
FileSystem::DeleteDirectory(const char *path, bool recurse)
{
Error error;
if (path && path[0])
{
if (recurse)
{
StreamString command;
command.Printf("rm -rf \"%s\"", path);
int status = ::system(command.GetString().c_str());
if (status != 0)
error.SetError(status, eErrorTypeGeneric);
}
else
{
if (::rmdir(path) != 0)
error.SetErrorToErrno();
}
}
else
{
error.SetErrorString("empty path");
}
return error;
}
Error
FileSystem::GetFilePermissions(const char *path, uint32_t &file_permissions)
{
Error error;
struct stat file_stats;
if (::stat(path, &file_stats) == 0)
{
// The bits in "st_mode" currently match the definitions
// for the file mode bits in unix.
file_permissions = file_stats.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
}
else
{
error.SetErrorToErrno();
}
return error;
}
Error
FileSystem::SetFilePermissions(const char *path, uint32_t file_permissions)
{
Error error;
if (::chmod(path, file_permissions) != 0)
error.SetErrorToErrno();
return error;
}
lldb::user_id_t
FileSystem::GetFileSize(const FileSpec &file_spec)
{
return file_spec.GetByteSize();
}
bool
FileSystem::GetFileExists(const FileSpec &file_spec)
{
return file_spec.Exists();
}
Error
FileSystem::Symlink(const char *src, const char *dst)
{
Error error;
if (::symlink(dst, src) == -1)
error.SetErrorToErrno();
return error;
}
Error
FileSystem::Unlink(const char *path)
{
Error error;
if (::unlink(path) == -1)
error.SetErrorToErrno();
return error;
}
Error
FileSystem::Readlink(const char *path, char *buf, size_t buf_len)
{
Error error;
ssize_t count = ::readlink(path, buf, buf_len);
if (count < 0)
error.SetErrorToErrno();
else if (static_cast<size_t>(count) < (buf_len - 1))
buf[count] = '\0'; // Success
else
error.SetErrorString("'buf' buffer is too small to contain link contents");
return error;
}
bool
FileSystem::IsLocal(const FileSpec &spec)
{
struct statfs statfs_info;
std::string path (spec.GetPath());
if (statfs(path.c_str(), &statfs_info) == 0)
return (statfs_info.f_flags & MNT_LOCAL) != 0;
return false;
}