2017-03-24 08:15:16 +08:00
|
|
|
//===- Filesystem.cpp -----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-03-24 08:15:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a few utility functions to handle files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-12 00:30:55 +08:00
|
|
|
#include "lld/Common/Filesystem.h"
|
2017-10-14 02:22:55 +08:00
|
|
|
#include "lld/Common/Threads.h"
|
2017-11-18 05:40:38 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2017-03-24 08:15:16 +08:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2017-10-06 07:01:11 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2017-11-18 05:40:38 +08:00
|
|
|
#if LLVM_ON_UNIX
|
2017-11-08 07:01:37 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2017-11-08 07:09:54 +08:00
|
|
|
#include <thread>
|
2017-03-24 08:15:16 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
|
|
|
|
// Removes a given file asynchronously. This is a performance hack,
|
|
|
|
// so remove this when operating systems are improved.
|
|
|
|
//
|
|
|
|
// On Linux (and probably on other Unix-like systems), unlink(2) is a
|
|
|
|
// noticeably slow system call. As of 2016, unlink takes 250
|
|
|
|
// milliseconds to remove a 1 GB file on ext4 filesystem on my machine.
|
|
|
|
//
|
|
|
|
// To create a new result file, we first remove existing file. So, if
|
|
|
|
// you repeatedly link a 1 GB program in a regular compile-link-debug
|
|
|
|
// cycle, every cycle wastes 250 milliseconds only to remove a file.
|
|
|
|
// Since LLD can link a 1 GB binary in about 5 seconds, that waste
|
|
|
|
// actually counts.
|
|
|
|
//
|
2017-11-08 07:01:37 +08:00
|
|
|
// This function spawns a background thread to remove the file.
|
2017-03-24 08:15:16 +08:00
|
|
|
// The calling thread returns almost immediately.
|
2019-03-12 00:30:55 +08:00
|
|
|
void lld::unlinkAsync(StringRef path) {
|
2017-11-08 07:01:37 +08:00
|
|
|
// Removing a file is async on windows.
|
2018-04-10 21:15:21 +08:00
|
|
|
#if defined(_WIN32)
|
2019-07-11 14:00:40 +08:00
|
|
|
sys::fs::remove(path);
|
2017-11-08 07:01:37 +08:00
|
|
|
#else
|
2017-11-07 10:00:51 +08:00
|
|
|
if (!threadsEnabled || !sys::fs::exists(path) ||
|
|
|
|
!sys::fs::is_regular_file(path))
|
2017-03-24 08:15:16 +08:00
|
|
|
return;
|
|
|
|
|
2017-11-08 07:01:37 +08:00
|
|
|
// We cannot just remove path from a different thread because we are now going
|
|
|
|
// to create path as a new file.
|
|
|
|
// Instead we open the file and unlink it on this thread. The unlink is fast
|
|
|
|
// since the open fd guarantees that it is not removing the last reference.
|
|
|
|
int fd;
|
2017-11-08 12:22:40 +08:00
|
|
|
std::error_code ec = sys::fs::openFileForRead(path, fd);
|
2017-11-08 07:01:37 +08:00
|
|
|
sys::fs::remove(path);
|
|
|
|
|
2019-02-15 07:41:23 +08:00
|
|
|
if (ec)
|
|
|
|
return;
|
|
|
|
|
2017-11-08 07:01:37 +08:00
|
|
|
// close and therefore remove TempPath in background.
|
2019-02-15 07:41:23 +08:00
|
|
|
std::mutex m;
|
|
|
|
std::condition_variable cv;
|
|
|
|
bool started = false;
|
|
|
|
std::thread([&, fd] {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(m);
|
|
|
|
started = true;
|
|
|
|
cv.notify_all();
|
|
|
|
}
|
|
|
|
::close(fd);
|
|
|
|
}).detach();
|
|
|
|
|
|
|
|
// GLIBC 2.26 and earlier have race condition that crashes an entire process
|
|
|
|
// if the main thread calls exit(2) while other thread is starting up.
|
|
|
|
std::unique_lock<std::mutex> l(m);
|
|
|
|
cv.wait(l, [&] { return started; });
|
2017-11-08 07:01:37 +08:00
|
|
|
#endif
|
2017-03-24 08:15:16 +08:00
|
|
|
}
|
|
|
|
|
2017-04-27 00:14:46 +08:00
|
|
|
// Simulate file creation to see if Path is writable.
|
2017-03-24 08:15:16 +08:00
|
|
|
//
|
|
|
|
// Determining whether a file is writable or not is amazingly hard,
|
|
|
|
// and after all the only reliable way of doing that is to actually
|
|
|
|
// create a file. But we don't want to do that in this function
|
|
|
|
// because LLD shouldn't update any file if it will end in a failure.
|
2017-04-27 00:14:46 +08:00
|
|
|
// We also don't want to reimplement heuristics to determine if a
|
|
|
|
// file is writable. So we'll let FileOutputBuffer do the work.
|
2017-03-24 08:15:16 +08:00
|
|
|
//
|
|
|
|
// FileOutputBuffer doesn't touch a desitnation file until commit()
|
|
|
|
// is called. We use that class without calling commit() to predict
|
|
|
|
// if the given file is writable.
|
2019-03-12 00:30:55 +08:00
|
|
|
std::error_code lld::tryCreateFile(StringRef path) {
|
2017-04-27 00:15:07 +08:00
|
|
|
if (path.empty())
|
|
|
|
return std::error_code();
|
2017-11-09 07:07:32 +08:00
|
|
|
if (path == "-")
|
|
|
|
return std::error_code();
|
2017-11-08 09:05:52 +08:00
|
|
|
return errorToErrorCode(FileOutputBuffer::create(path, 1).takeError());
|
2017-03-24 08:15:16 +08:00
|
|
|
}
|