blob: d468ae0c618a1c9224eb82d276d9cfbbdabd7cb1 [file] [log] [blame]
Rui Ueyama9b55e922017-03-24 00:15:16 +00001//===- Filesystem.cpp -----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a few utility functions to handle files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Filesystem.h"
15#include "Config.h"
Rui Ueyama9b55e922017-03-24 00:15:16 +000016#include "llvm/Support/FileSystem.h"
17#include "llvm/Support/FileOutputBuffer.h"
18#include <thread>
19
20using namespace llvm;
21
22using namespace lld;
23using namespace lld::elf;
24
25// Removes a given file asynchronously. This is a performance hack,
26// so remove this when operating systems are improved.
27//
28// On Linux (and probably on other Unix-like systems), unlink(2) is a
29// noticeably slow system call. As of 2016, unlink takes 250
30// milliseconds to remove a 1 GB file on ext4 filesystem on my machine.
31//
32// To create a new result file, we first remove existing file. So, if
33// you repeatedly link a 1 GB program in a regular compile-link-debug
34// cycle, every cycle wastes 250 milliseconds only to remove a file.
35// Since LLD can link a 1 GB binary in about 5 seconds, that waste
36// actually counts.
37//
38// This function spawns a background thread to call unlink.
39// The calling thread returns almost immediately.
40void elf::unlinkAsync(StringRef Path) {
Tom Stellardc17a04c2017-07-04 16:03:34 +000041 if (!Config->Threads || !sys::fs::exists(Config->OutputFile) ||
42 !sys::fs::is_regular_file(Config->OutputFile))
Rui Ueyama9b55e922017-03-24 00:15:16 +000043 return;
44
45 // First, rename Path to avoid race condition. We cannot remove
46 // Path from a different thread because we are now going to create
47 // Path as a new file. If we do that in a different thread, the new
48 // thread can remove the new file.
49 SmallString<128> TempPath;
50 if (sys::fs::createUniqueFile(Path + "tmp%%%%%%%%", TempPath))
51 return;
52 if (sys::fs::rename(Path, TempPath)) {
53 sys::fs::remove(TempPath);
54 return;
55 }
56
57 // Remove TempPath in background.
58 std::thread([=] { ::remove(TempPath.str().str().c_str()); }).detach();
59}
60
Rui Ueyama71630272017-04-26 16:14:46 +000061// Simulate file creation to see if Path is writable.
Rui Ueyama9b55e922017-03-24 00:15:16 +000062//
63// Determining whether a file is writable or not is amazingly hard,
64// and after all the only reliable way of doing that is to actually
65// create a file. But we don't want to do that in this function
66// because LLD shouldn't update any file if it will end in a failure.
Rui Ueyama71630272017-04-26 16:14:46 +000067// We also don't want to reimplement heuristics to determine if a
68// file is writable. So we'll let FileOutputBuffer do the work.
Rui Ueyama9b55e922017-03-24 00:15:16 +000069//
70// FileOutputBuffer doesn't touch a desitnation file until commit()
71// is called. We use that class without calling commit() to predict
72// if the given file is writable.
Rui Ueyama71630272017-04-26 16:14:46 +000073std::error_code elf::tryCreateFile(StringRef Path) {
Rui Ueyamad283eb72017-04-26 16:15:07 +000074 if (Path.empty())
75 return std::error_code();
Rui Ueyama71630272017-04-26 16:14:46 +000076 return FileOutputBuffer::create(Path, 1).getError();
Rui Ueyama9b55e922017-03-24 00:15:16 +000077}