1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
//===-- 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 <dirent.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef __linux__
#include <sys/statfs.h>
#include <sys/mount.h>
#include <linux/magic.h>
#endif
#if defined(__NetBSD__)
#include <sys/statvfs.h>
#endif
// lldb Includes
#include "lldb/Core/Error.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Host/Host.h"
using namespace lldb;
using namespace lldb_private;
const char *
FileSystem::DEV_NULL = "/dev/null";
FileSpec::PathSyntax
FileSystem::GetNativePathSyntax()
{
return FileSpec::ePathSyntaxPosix;
}
Error
FileSystem::MakeDirectory(const FileSpec &file_spec, uint32_t file_permissions)
{
if (file_spec)
{
Error error;
if (::mkdir(file_spec.GetCString(), file_permissions) == -1)
{
error.SetErrorToErrno();
errno = 0;
switch (error.GetError())
{
case ENOENT:
{
// Parent directory doesn't exist, so lets make it if we can
// Make the parent directory and try again
FileSpec parent_file_spec{file_spec.GetDirectory().GetCString(), false};
error = MakeDirectory(parent_file_spec, file_permissions);
if (error.Fail())
return error;
// Try and make the directory again now that the parent directory was made successfully
if (::mkdir(file_spec.GetCString(), file_permissions) == -1)
{
error.SetErrorToErrno();
}
return error;
}
break;
case EEXIST:
{
if (file_spec.IsDirectory())
return Error(); // It is a directory and it already exists
}
break;
}
}
return error;
}
return Error("empty path");
}
Error
FileSystem::DeleteDirectory(const FileSpec &file_spec, bool recurse)
{
Error error;
if (file_spec)
{
if (recurse)
{
// Save all sub directories in a list so we don't recursively call this function
// and possibly run out of file descriptors if the directory is too deep.
std::vector<FileSpec> sub_directories;
FileSpec::ForEachItemInDirectory (file_spec.GetCString(), [&error, &sub_directories](FileSpec::FileType file_type, const FileSpec &spec) -> FileSpec::EnumerateDirectoryResult {
if (file_type == FileSpec::eFileTypeDirectory)
{
// Save all directorires and process them after iterating through this directory
sub_directories.push_back(spec);
}
else
{
// Update sub_spec to point to the current file and delete it
error = FileSystem::Unlink(spec);
}
// If anything went wrong, stop iterating, else process the next file
if (error.Fail())
return FileSpec::eEnumerateDirectoryResultQuit;
else
return FileSpec::eEnumerateDirectoryResultNext;
});
if (error.Success())
{
// Now delete all sub directories with separate calls that aren't
// recursively calling into this function _while_ this function is
// iterating through the current directory.
for (const auto &sub_directory : sub_directories)
{
error = DeleteDirectory(sub_directory, recurse);
if (error.Fail())
break;
}
}
}
if (error.Success())
{
if (::rmdir(file_spec.GetCString()) != 0)
error.SetErrorToErrno();
}
}
else
{
error.SetErrorString("empty path");
}
return error;
}
Error
FileSystem::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions)
{
Error error;
struct stat file_stats;
if (::stat(file_spec.GetCString(), &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 FileSpec &file_spec, uint32_t file_permissions)
{
Error error;
if (::chmod(file_spec.GetCString(), 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::Hardlink(const FileSpec &src, const FileSpec &dst)
{
Error error;
if (::link(dst.GetCString(), src.GetCString()) == -1)
error.SetErrorToErrno();
return error;
}
int
FileSystem::GetHardlinkCount(const FileSpec &file_spec)
{
struct stat file_stat;
if (::stat(file_spec.GetCString(), &file_stat) == 0)
return file_stat.st_nlink;
return -1;
}
Error
FileSystem::Symlink(const FileSpec &src, const FileSpec &dst)
{
Error error;
if (::symlink(dst.GetCString(), src.GetCString()) == -1)
error.SetErrorToErrno();
return error;
}
Error
FileSystem::Unlink(const FileSpec &file_spec)
{
Error error;
if (::unlink(file_spec.GetCString()) == -1)
error.SetErrorToErrno();
return error;
}
Error
FileSystem::Readlink(const FileSpec &src, FileSpec &dst)
{
Error error;
char buf[PATH_MAX];
ssize_t count = ::readlink(src.GetCString(), buf, sizeof(buf) - 1);
if (count < 0)
error.SetErrorToErrno();
else
{
buf[count] = '\0'; // Success
dst.SetFile(buf, false);
}
return error;
}
Error
FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst)
{
char resolved_path[PATH_MAX];
if (!src.GetPath (resolved_path, sizeof (resolved_path)))
{
return Error("Couldn't get the canonical path for %s", src.GetCString());
}
char real_path[PATH_MAX + 1];
if (realpath(resolved_path, real_path) == nullptr)
{
Error err;
err.SetErrorToErrno();
return err;
}
dst = FileSpec(real_path, false);
return Error();
}
#if defined(__NetBSD__)
static bool IsLocal(const struct statvfs& info)
{
return (info.f_flag & MNT_LOCAL) != 0;
}
#else
static bool IsLocal(const struct statfs& info)
{
#ifdef __linux__
#define CIFS_MAGIC_NUMBER 0xFF534D42
switch ((uint32_t)info.f_type)
{
case NFS_SUPER_MAGIC:
case SMB_SUPER_MAGIC:
case CIFS_MAGIC_NUMBER:
return false;
default:
return true;
}
#else
return (info.f_flags & MNT_LOCAL) != 0;
#endif
}
#endif
#if defined(__NetBSD__)
bool
FileSystem::IsLocal(const FileSpec &spec)
{
struct statvfs statfs_info;
std::string path (spec.GetPath());
if (statvfs(path.c_str(), &statfs_info) == 0)
return ::IsLocal(statfs_info);
return false;
}
#else
bool
FileSystem::IsLocal(const FileSpec &spec)
{
struct statfs statfs_info;
std::string path (spec.GetPath());
if (statfs(path.c_str(), &statfs_info) == 0)
return ::IsLocal(statfs_info);
return false;
}
#endif
FILE *
FileSystem::Fopen(const char *path, const char *mode)
{
return ::fopen(path, mode);
}
int
FileSystem::Stat(const char *path, struct stat *stats)
{
return ::stat(path, stats);
}
|