blob: 6a97b4a778b975828dfdf077b58d56aa757ebd67 [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"
Bob Haarman4f5c8c22017-10-13 18:22:55 +000016#include "lld/Common/Threads.h"
Rafael Espindola1501a262017-11-07 23:01:37 +000017#include "llvm/Config/config.h"
Rui Ueyama9b55e922017-03-24 00:15:16 +000018#include "llvm/Support/FileOutputBuffer.h"
Rui Ueyama945cd642017-10-05 23:01:11 +000019#include "llvm/Support/FileSystem.h"
Rafael Espindola1501a262017-11-07 23:01:37 +000020#if defined(HAVE_UNISTD_H)
21#include <unistd.h>
22#endif
Rafael Espindolac7889062017-11-07 23:09:54 +000023#include <thread>
Rui Ueyama9b55e922017-03-24 00:15:16 +000024
25using namespace llvm;
26
27using namespace lld;
28using namespace lld::elf;
29
30// Removes a given file asynchronously. This is a performance hack,
31// so remove this when operating systems are improved.
32//
33// On Linux (and probably on other Unix-like systems), unlink(2) is a
34// noticeably slow system call. As of 2016, unlink takes 250
35// milliseconds to remove a 1 GB file on ext4 filesystem on my machine.
36//
37// To create a new result file, we first remove existing file. So, if
38// you repeatedly link a 1 GB program in a regular compile-link-debug
39// cycle, every cycle wastes 250 milliseconds only to remove a file.
40// Since LLD can link a 1 GB binary in about 5 seconds, that waste
41// actually counts.
42//
Rafael Espindola1501a262017-11-07 23:01:37 +000043// This function spawns a background thread to remove the file.
Rui Ueyama9b55e922017-03-24 00:15:16 +000044// The calling thread returns almost immediately.
45void elf::unlinkAsync(StringRef Path) {
Rafael Espindola1501a262017-11-07 23:01:37 +000046// Removing a file is async on windows.
47#if defined(LLVM_ON_WIN32)
48 sys::fs::remove(Path);
49#else
Rafael Espindolafcd74942017-11-07 02:00:51 +000050 if (!ThreadsEnabled || !sys::fs::exists(Path) ||
51 !sys::fs::is_regular_file(Path))
Rui Ueyama9b55e922017-03-24 00:15:16 +000052 return;
53
Rafael Espindola1501a262017-11-07 23:01:37 +000054 // We cannot just remove path from a different thread because we are now going
55 // to create path as a new file.
56 // Instead we open the file and unlink it on this thread. The unlink is fast
57 // since the open fd guarantees that it is not removing the last reference.
58 int FD;
Rafael Espindola367ca012017-11-07 23:43:01 +000059 if (sys::fs::openFileForRead(Path, FD))
Rui Ueyama9b55e922017-03-24 00:15:16 +000060 return;
Rui Ueyama9b55e922017-03-24 00:15:16 +000061
Rafael Espindola1501a262017-11-07 23:01:37 +000062 sys::fs::remove(Path);
63
64 // close and therefore remove TempPath in background.
Rafael Espindolac7889062017-11-07 23:09:54 +000065 std::thread([=] { ::close(FD); }).detach();
Rafael Espindola1501a262017-11-07 23:01:37 +000066#endif
Rui Ueyama9b55e922017-03-24 00:15:16 +000067}
68
Rui Ueyama71630272017-04-26 16:14:46 +000069// Simulate file creation to see if Path is writable.
Rui Ueyama9b55e922017-03-24 00:15:16 +000070//
71// Determining whether a file is writable or not is amazingly hard,
72// and after all the only reliable way of doing that is to actually
73// create a file. But we don't want to do that in this function
74// because LLD shouldn't update any file if it will end in a failure.
Rui Ueyama71630272017-04-26 16:14:46 +000075// We also don't want to reimplement heuristics to determine if a
76// file is writable. So we'll let FileOutputBuffer do the work.
Rui Ueyama9b55e922017-03-24 00:15:16 +000077//
78// FileOutputBuffer doesn't touch a desitnation file until commit()
79// is called. We use that class without calling commit() to predict
80// if the given file is writable.
Rui Ueyama71630272017-04-26 16:14:46 +000081std::error_code elf::tryCreateFile(StringRef Path) {
Rui Ueyamad283eb72017-04-26 16:15:07 +000082 if (Path.empty())
83 return std::error_code();
Rafael Espindolaf7a57292017-11-08 01:05:52 +000084 return errorToErrorCode(FileOutputBuffer::create(Path, 1).takeError());
Rui Ueyama9b55e922017-03-24 00:15:16 +000085}