mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-10-19 19:12:56 +02:00
Replace use of PathV1.h in Program.cpp.
llvm-svn: 183996
This commit is contained in:
parent
9aa8b92dda
commit
109393c56d
@ -342,6 +342,12 @@ inline bool exists(const Twine &path) {
|
|||||||
return !exists(path, result) && result;
|
return !exists(path, result) && result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Can we execute this file?
|
||||||
|
///
|
||||||
|
/// @param Path Input path.
|
||||||
|
/// @returns True if we can execute it, false otherwise.
|
||||||
|
bool can_execute(const Twine &Path);
|
||||||
|
|
||||||
/// @brief Do file_status's represent the same thing?
|
/// @brief Do file_status's represent the same thing?
|
||||||
///
|
///
|
||||||
/// @param A Input file_status.
|
/// @param A Input file_status.
|
||||||
|
@ -12,7 +12,6 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#include "llvm/Support/Program.h"
|
#include "llvm/Support/Program.h"
|
||||||
#include "llvm/Support/PathV1.h"
|
|
||||||
#include "llvm/Config/config.h"
|
#include "llvm/Config/config.h"
|
||||||
#include "llvm/Support/system_error.h"
|
#include "llvm/Support/system_error.h"
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
@ -23,39 +22,12 @@ using namespace sys;
|
|||||||
//=== independent code.
|
//=== independent code.
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
static bool Execute(void **Data, const Path &path, const char **args,
|
|
||||||
const char **env, const sys::Path **redirects,
|
|
||||||
unsigned memoryLimit, std::string *ErrMsg);
|
|
||||||
|
|
||||||
static int Wait(void *&Data, const Path &path, unsigned secondsToWait,
|
|
||||||
std::string *ErrMsg);
|
|
||||||
|
|
||||||
|
|
||||||
static bool Execute(void **Data, StringRef Program, const char **args,
|
static bool Execute(void **Data, StringRef Program, const char **args,
|
||||||
const char **env, const StringRef **Redirects,
|
const char **env, const StringRef **Redirects,
|
||||||
unsigned memoryLimit, std::string *ErrMsg) {
|
unsigned memoryLimit, std::string *ErrMsg);
|
||||||
Path P(Program);
|
|
||||||
if (!Redirects)
|
|
||||||
return Execute(Data, P, args, env, 0, memoryLimit, ErrMsg);
|
|
||||||
Path IO[3];
|
|
||||||
const Path *IOP[3];
|
|
||||||
for (int I = 0; I < 3; ++I) {
|
|
||||||
if (Redirects[I]) {
|
|
||||||
IO[I] = *Redirects[I];
|
|
||||||
IOP[I] = &IO[I];
|
|
||||||
} else {
|
|
||||||
IOP[I] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Execute(Data, P, args, env, IOP, memoryLimit, ErrMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int Wait(void *&Data, StringRef Program, unsigned secondsToWait,
|
static int Wait(void *&Data, StringRef Program, unsigned secondsToWait,
|
||||||
std::string *ErrMsg) {
|
std::string *ErrMsg);
|
||||||
Path P(Program);
|
|
||||||
return Wait(Data, P, secondsToWait, ErrMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
int sys::ExecuteAndWait(StringRef Program, const char **args, const char **envp,
|
int sys::ExecuteAndWait(StringRef Program, const char **args, const char **envp,
|
||||||
const StringRef **redirects, unsigned secondsToWait,
|
const StringRef **redirects, unsigned secondsToWait,
|
||||||
|
@ -292,6 +292,13 @@ error_code exists(const Twine &path, bool &result) {
|
|||||||
return error_code::success();
|
return error_code::success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool can_execute(const Twine &Path) {
|
||||||
|
SmallString<128> PathStorage;
|
||||||
|
StringRef P = Path.toNullTerminatedStringRef(PathStorage);
|
||||||
|
|
||||||
|
return ::access(P.begin(), X_OK) != -1;
|
||||||
|
}
|
||||||
|
|
||||||
bool equivalent(file_status A, file_status B) {
|
bool equivalent(file_status A, file_status B) {
|
||||||
assert(status_known(A) && status_known(B));
|
assert(status_known(A) && status_known(B));
|
||||||
return A.fs_st_dev == B.fs_st_dev &&
|
return A.fs_st_dev == B.fs_st_dev &&
|
||||||
|
@ -54,13 +54,11 @@ sys::FindProgramByName(const std::string& progName) {
|
|||||||
// Check some degenerate cases
|
// Check some degenerate cases
|
||||||
if (progName.length() == 0) // no program
|
if (progName.length() == 0) // no program
|
||||||
return "";
|
return "";
|
||||||
Path temp;
|
std::string temp = progName;
|
||||||
if (!temp.set(progName)) // invalid name
|
|
||||||
return "";
|
|
||||||
// Use the given path verbatim if it contains any slashes; this matches
|
// Use the given path verbatim if it contains any slashes; this matches
|
||||||
// the behavior of sh(1) and friends.
|
// the behavior of sh(1) and friends.
|
||||||
if (progName.find('/') != std::string::npos)
|
if (progName.find('/') != std::string::npos)
|
||||||
return temp.str();
|
return temp;
|
||||||
|
|
||||||
// At this point, the file name is valid and does not contain slashes. Search
|
// At this point, the file name is valid and does not contain slashes. Search
|
||||||
// for it through the directories specified in the PATH environment variable.
|
// for it through the directories specified in the PATH environment variable.
|
||||||
@ -77,12 +75,10 @@ sys::FindProgramByName(const std::string& progName) {
|
|||||||
const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
|
const char *Colon = std::find(PathStr, PathStr+PathLen, ':');
|
||||||
|
|
||||||
// Check to see if this first directory contains the executable...
|
// Check to see if this first directory contains the executable...
|
||||||
Path FilePath;
|
SmallString<128> FilePath(PathStr,Colon);
|
||||||
if (FilePath.set(std::string(PathStr,Colon))) {
|
sys::path::append(FilePath, progName);
|
||||||
FilePath.appendComponent(progName);
|
if (sys::fs::can_execute(Twine(FilePath)))
|
||||||
if (FilePath.canExecute())
|
return FilePath.str(); // Found the executable!
|
||||||
return FilePath.str(); // Found the executable!
|
|
||||||
}
|
|
||||||
|
|
||||||
// Nope it wasn't in this directory, check the next path in the list!
|
// Nope it wasn't in this directory, check the next path in the list!
|
||||||
PathLen -= Colon-PathStr;
|
PathLen -= Colon-PathStr;
|
||||||
@ -97,20 +93,20 @@ sys::FindProgramByName(const std::string& progName) {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool RedirectIO(const Path *Path, int FD, std::string* ErrMsg) {
|
static bool RedirectIO(const StringRef *Path, int FD, std::string* ErrMsg) {
|
||||||
if (Path == 0) // Noop
|
if (Path == 0) // Noop
|
||||||
return false;
|
return false;
|
||||||
const char *File;
|
std::string File;
|
||||||
if (Path->isEmpty())
|
if (Path->empty())
|
||||||
// Redirect empty paths to /dev/null
|
// Redirect empty paths to /dev/null
|
||||||
File = "/dev/null";
|
File = "/dev/null";
|
||||||
else
|
else
|
||||||
File = Path->c_str();
|
File = *Path;
|
||||||
|
|
||||||
// Open the file
|
// Open the file
|
||||||
int InFD = open(File, FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
|
int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666);
|
||||||
if (InFD == -1) {
|
if (InFD == -1) {
|
||||||
MakeErrMsg(ErrMsg, "Cannot open file '" + std::string(File) + "' for "
|
MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for "
|
||||||
+ (FD == 0 ? "input" : "output"));
|
+ (FD == 0 ? "input" : "output"));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -126,19 +122,20 @@ static bool RedirectIO(const Path *Path, int FD, std::string* ErrMsg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_POSIX_SPAWN
|
#ifdef HAVE_POSIX_SPAWN
|
||||||
static bool RedirectIO_PS(const Path *Path, int FD, std::string *ErrMsg,
|
static bool RedirectIO_PS(const StringRef *Path, int FD, std::string *ErrMsg,
|
||||||
posix_spawn_file_actions_t *FileActions) {
|
posix_spawn_file_actions_t *FileActions) {
|
||||||
if (Path == 0) // Noop
|
if (Path == 0) // Noop
|
||||||
return false;
|
return false;
|
||||||
const char *File;
|
std::string File;
|
||||||
if (Path->isEmpty())
|
if (Path->empty())
|
||||||
// Redirect empty paths to /dev/null
|
// Redirect empty paths to /dev/null
|
||||||
File = "/dev/null";
|
File = "/dev/null";
|
||||||
else
|
else
|
||||||
File = Path->c_str();
|
File = *Path;
|
||||||
|
|
||||||
if (int Err = posix_spawn_file_actions_addopen(FileActions, FD,
|
if (int Err = posix_spawn_file_actions_addopen(
|
||||||
File, FD == 0 ? O_RDONLY : O_WRONLY|O_CREAT, 0666))
|
FileActions, FD, File.c_str(),
|
||||||
|
FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666))
|
||||||
return MakeErrMsg(ErrMsg, "Cannot dup2", Err);
|
return MakeErrMsg(ErrMsg, "Cannot dup2", Err);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -178,8 +175,8 @@ static void SetMemoryLimits (unsigned size)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool Execute(void **Data, const Path &path, const char **args,
|
static bool Execute(void **Data, StringRef Program, const char **args,
|
||||||
const char **envp, const Path **redirects,
|
const char **envp, const StringRef **redirects,
|
||||||
unsigned memoryLimit, std::string *ErrMsg) {
|
unsigned memoryLimit, std::string *ErrMsg) {
|
||||||
// If this OS has posix_spawn and there is no memory limit being implied, use
|
// If this OS has posix_spawn and there is no memory limit being implied, use
|
||||||
// posix_spawn. It is more efficient than fork/exec.
|
// posix_spawn. It is more efficient than fork/exec.
|
||||||
@ -219,7 +216,7 @@ static bool Execute(void **Data, const Path &path, const char **args,
|
|||||||
// Explicitly initialized to prevent what appears to be a valgrind false
|
// Explicitly initialized to prevent what appears to be a valgrind false
|
||||||
// positive.
|
// positive.
|
||||||
pid_t PID = 0;
|
pid_t PID = 0;
|
||||||
int Err = posix_spawn(&PID, path.c_str(), FileActions, /*attrp*/0,
|
int Err = posix_spawn(&PID, Program.str().c_str(), FileActions, /*attrp*/0,
|
||||||
const_cast<char **>(args), const_cast<char **>(envp));
|
const_cast<char **>(args), const_cast<char **>(envp));
|
||||||
|
|
||||||
if (FileActions)
|
if (FileActions)
|
||||||
@ -270,12 +267,13 @@ static bool Execute(void **Data, const Path &path, const char **args,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Execute!
|
// Execute!
|
||||||
|
std::string PathStr = Program;
|
||||||
if (envp != 0)
|
if (envp != 0)
|
||||||
execve(path.c_str(),
|
execve(PathStr.c_str(),
|
||||||
const_cast<char **>(args),
|
const_cast<char **>(args),
|
||||||
const_cast<char **>(envp));
|
const_cast<char **>(envp));
|
||||||
else
|
else
|
||||||
execv(path.c_str(),
|
execv(PathStr.c_str(),
|
||||||
const_cast<char **>(args));
|
const_cast<char **>(args));
|
||||||
// If the execve() failed, we should exit. Follow Unix protocol and
|
// If the execve() failed, we should exit. Follow Unix protocol and
|
||||||
// return 127 if the executable was not found, and 126 otherwise.
|
// return 127 if the executable was not found, and 126 otherwise.
|
||||||
@ -297,7 +295,7 @@ static bool Execute(void **Data, const Path &path, const char **args,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int Wait(void *&Data, const sys::Path &path, unsigned secondsToWait,
|
static int Wait(void *&Data, StringRef Program, unsigned secondsToWait,
|
||||||
std::string *ErrMsg) {
|
std::string *ErrMsg) {
|
||||||
#ifdef HAVE_SYS_WAIT_H
|
#ifdef HAVE_SYS_WAIT_H
|
||||||
struct sigaction Act, Old;
|
struct sigaction Act, Old;
|
||||||
@ -356,7 +354,7 @@ static int Wait(void *&Data, const sys::Path &path, unsigned secondsToWait,
|
|||||||
// itself apparently does not), check to see if the failure was due to some
|
// itself apparently does not), check to see if the failure was due to some
|
||||||
// reason other than the file not existing, and return 126 in this case.
|
// reason other than the file not existing, and return 126 in this case.
|
||||||
bool Exists;
|
bool Exists;
|
||||||
if (result == 127 && !llvm::sys::fs::exists(path.str(), Exists) && Exists)
|
if (result == 127 && !llvm::sys::fs::exists(Program, Exists) && Exists)
|
||||||
result = 126;
|
result = 126;
|
||||||
#endif
|
#endif
|
||||||
if (result == 127) {
|
if (result == 127) {
|
||||||
|
@ -362,6 +362,17 @@ error_code exists(const Twine &path, bool &result) {
|
|||||||
return error_code::success();
|
return error_code::success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool can_execute(const Twine &Path) {
|
||||||
|
SmallString<128> PathStorage;
|
||||||
|
SmallVector<wchar_t, 128> PathUtf16;
|
||||||
|
|
||||||
|
if (UTF8ToUTF16(Path.toStringRef(PathStorage), PathUtf16))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
DWORD Attr = ::GetFileAttributesW(PathUtf16.begin());
|
||||||
|
return Attr != INVALID_FILE_ATTRIBUTES;
|
||||||
|
}
|
||||||
|
|
||||||
bool equivalent(file_status A, file_status B) {
|
bool equivalent(file_status A, file_status B) {
|
||||||
assert(status_known(A) && status_known(B));
|
assert(status_known(A) && status_known(B));
|
||||||
return A.FileIndexHigh == B.FileIndexHigh &&
|
return A.FileIndexHigh == B.FileIndexHigh &&
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#include "Windows.h"
|
#include "Windows.h"
|
||||||
|
#include "llvm/Support/FileSystem.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
@ -37,13 +38,11 @@ std::string sys::FindProgramByName(const std::string &progName) {
|
|||||||
// Check some degenerate cases
|
// Check some degenerate cases
|
||||||
if (progName.length() == 0) // no program
|
if (progName.length() == 0) // no program
|
||||||
return "";
|
return "";
|
||||||
Path temp;
|
std::string temp = progName;
|
||||||
if (!temp.set(progName)) // invalid name
|
|
||||||
return "";
|
|
||||||
// Return paths with slashes verbatim.
|
// Return paths with slashes verbatim.
|
||||||
if (progName.find('\\') != std::string::npos ||
|
if (progName.find('\\') != std::string::npos ||
|
||||||
progName.find('/') != std::string::npos)
|
progName.find('/') != std::string::npos)
|
||||||
return temp.str();
|
return temp;
|
||||||
|
|
||||||
// At this point, the file name is valid and does not contain slashes.
|
// At this point, the file name is valid and does not contain slashes.
|
||||||
// Let Windows search for it.
|
// Let Windows search for it.
|
||||||
@ -76,7 +75,7 @@ std::string sys::FindProgramByName(const std::string &progName) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) {
|
static HANDLE RedirectIO(const StringRef *path, int fd, std::string* ErrMsg) {
|
||||||
HANDLE h;
|
HANDLE h;
|
||||||
if (path == 0) {
|
if (path == 0) {
|
||||||
DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
|
DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
|
||||||
@ -85,19 +84,19 @@ static HANDLE RedirectIO(const Path *path, int fd, std::string* ErrMsg) {
|
|||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *fname;
|
std::string fname;
|
||||||
if (path->isEmpty())
|
if (path->empty())
|
||||||
fname = "NUL";
|
fname = "NUL";
|
||||||
else
|
else
|
||||||
fname = path->c_str();
|
fname = *path;
|
||||||
|
|
||||||
SECURITY_ATTRIBUTES sa;
|
SECURITY_ATTRIBUTES sa;
|
||||||
sa.nLength = sizeof(sa);
|
sa.nLength = sizeof(sa);
|
||||||
sa.lpSecurityDescriptor = 0;
|
sa.lpSecurityDescriptor = 0;
|
||||||
sa.bInheritHandle = TRUE;
|
sa.bInheritHandle = TRUE;
|
||||||
|
|
||||||
h = CreateFile(fname, fd ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ,
|
h = CreateFile(fname.c_str(), fd ? GENERIC_WRITE : GENERIC_READ,
|
||||||
&sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
|
FILE_SHARE_READ, &sa, fd == 0 ? OPEN_EXISTING : CREATE_ALWAYS,
|
||||||
FILE_ATTRIBUTE_NORMAL, NULL);
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
||||||
if (h == INVALID_HANDLE_VALUE) {
|
if (h == INVALID_HANDLE_VALUE) {
|
||||||
MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
|
MakeErrMsg(ErrMsg, std::string(fname) + ": Can't open file for " +
|
||||||
@ -171,13 +170,13 @@ static unsigned int ArgLenWithQuotes(const char *Str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool Execute(void **Data,
|
static bool Execute(void **Data,
|
||||||
const Path& path,
|
StringRef Program,
|
||||||
const char** args,
|
const char** args,
|
||||||
const char** envp,
|
const char** envp,
|
||||||
const Path** redirects,
|
const StringRef** redirects,
|
||||||
unsigned memoryLimit,
|
unsigned memoryLimit,
|
||||||
std::string* ErrMsg) {
|
std::string* ErrMsg) {
|
||||||
if (!path.canExecute()) {
|
if (!sys::fs::can_execute(Program)) {
|
||||||
if (ErrMsg)
|
if (ErrMsg)
|
||||||
*ErrMsg = "program not executable";
|
*ErrMsg = "program not executable";
|
||||||
return false;
|
return false;
|
||||||
@ -297,7 +296,8 @@ static bool Execute(void **Data,
|
|||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
BOOL rc = CreateProcess(path.c_str(), command, NULL, NULL, TRUE, 0,
|
std::string ProgramStr = Program;
|
||||||
|
BOOL rc = CreateProcess(ProgramStr.c_str(), command, NULL, NULL, TRUE, 0,
|
||||||
envblock, NULL, &si, &pi);
|
envblock, NULL, &si, &pi);
|
||||||
DWORD err = GetLastError();
|
DWORD err = GetLastError();
|
||||||
|
|
||||||
@ -311,7 +311,7 @@ static bool Execute(void **Data,
|
|||||||
if (!rc) {
|
if (!rc) {
|
||||||
SetLastError(err);
|
SetLastError(err);
|
||||||
MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
|
MakeErrMsg(ErrMsg, std::string("Couldn't execute program '") +
|
||||||
path.str() + "'");
|
ProgramStr + "'");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (Data) {
|
if (Data) {
|
||||||
@ -398,7 +398,7 @@ static int WaitAux(Win32ProcessInfo *wpi, unsigned secondsToWait,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int Wait(void *&Data, const Path &path, unsigned secondsToWait,
|
static int Wait(void *&Data, StringRef Program, unsigned secondsToWait,
|
||||||
std::string *ErrMsg) {
|
std::string *ErrMsg) {
|
||||||
Win32ProcessInfo *wpi = reinterpret_cast<Win32ProcessInfo *>(Data);
|
Win32ProcessInfo *wpi = reinterpret_cast<Win32ProcessInfo *>(Data);
|
||||||
int Ret = WaitAux(wpi, secondsToWait, ErrMsg);
|
int Ret = WaitAux(wpi, secondsToWait, ErrMsg);
|
||||||
|
Loading…
Reference in New Issue
Block a user