1 //===- Filesystem.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a few utility functions to handle files.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "lld/Common/Filesystem.h"
14 #include "llvm/Config/llvm-config.h"
15 #include "llvm/Support/FileOutputBuffer.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/Parallel.h"
18 #include "llvm/Support/Path.h"
19 #if LLVM_ON_UNIX
20 #include <unistd.h>
21 #endif
22 #include <thread>
23
24 using namespace llvm;
25 using namespace lld;
26
27 // Removes a given file asynchronously. This is a performance hack,
28 // so remove this when operating systems are improved.
29 //
30 // On Linux (and probably on other Unix-like systems), unlink(2) is a
31 // noticeably slow system call. As of 2016, unlink takes 250
32 // milliseconds to remove a 1 GB file on ext4 filesystem on my machine.
33 //
34 // To create a new result file, we first remove existing file. So, if
35 // you repeatedly link a 1 GB program in a regular compile-link-debug
36 // cycle, every cycle wastes 250 milliseconds only to remove a file.
37 // Since LLD can link a 1 GB binary in about 5 seconds, that waste
38 // actually counts.
39 //
40 // This function spawns a background thread to remove the file.
41 // The calling thread returns almost immediately.
unlinkAsync(StringRef path)42 void lld::unlinkAsync(StringRef path) {
43 if (!sys::fs::exists(path) || !sys::fs::is_regular_file(path))
44 return;
45
46 // Removing a file is async on windows.
47 #if defined(_WIN32)
48 // On Windows co-operative programs can be expected to open LLD's
49 // output in FILE_SHARE_DELETE mode. This allows us to delete the
50 // file (by moving it to a temporary filename and then deleting
51 // it) so that we can link another output file that overwrites
52 // the existing file, even if the current file is in use.
53 //
54 // This is done on a best effort basis - we do not error if the
55 // operation fails. The consequence is merely that the user
56 // experiences an inconvenient work-flow.
57 //
58 // The code here allows LLD to work on all versions of Windows.
59 // However, at Windows 10 1903 it seems that the behavior of
60 // Windows has changed, so that we could simply delete the output
61 // file. This code should be simplified once support for older
62 // versions of Windows is dropped.
63 //
64 // Warning: It seems that the WINVER and _WIN32_WINNT preprocessor
65 // defines affect the behavior of the Windows versions of the calls
66 // we are using here. If this code stops working this is worth
67 // bearing in mind.
68 SmallString<128> tmpName;
69 if (!sys::fs::createUniqueFile(path + "%%%%%%%%.tmp", tmpName)) {
70 if (!sys::fs::rename(path, tmpName))
71 path = tmpName;
72 else
73 sys::fs::remove(tmpName);
74 }
75 sys::fs::remove(path);
76 #else
77 if (parallel::strategy.ThreadsRequested == 1)
78 return;
79
80 // We cannot just remove path from a different thread because we are now going
81 // to create path as a new file.
82 // Instead we open the file and unlink it on this thread. The unlink is fast
83 // since the open fd guarantees that it is not removing the last reference.
84 int fd;
85 std::error_code ec = sys::fs::openFileForRead(path, fd);
86 sys::fs::remove(path);
87
88 if (ec)
89 return;
90
91 // close and therefore remove TempPath in background.
92 std::mutex m;
93 std::condition_variable cv;
94 bool started = false;
95 std::thread([&, fd] {
96 {
97 std::lock_guard<std::mutex> l(m);
98 started = true;
99 cv.notify_all();
100 }
101 ::close(fd);
102 }).detach();
103
104 // GLIBC 2.26 and earlier have race condition that crashes an entire process
105 // if the main thread calls exit(2) while other thread is starting up.
106 std::unique_lock<std::mutex> l(m);
107 cv.wait(l, [&] { return started; });
108 #endif
109 }
110
111 // Simulate file creation to see if Path is writable.
112 //
113 // Determining whether a file is writable or not is amazingly hard,
114 // and after all the only reliable way of doing that is to actually
115 // create a file. But we don't want to do that in this function
116 // because LLD shouldn't update any file if it will end in a failure.
117 // We also don't want to reimplement heuristics to determine if a
118 // file is writable. So we'll let FileOutputBuffer do the work.
119 //
120 // FileOutputBuffer doesn't touch a destination file until commit()
121 // is called. We use that class without calling commit() to predict
122 // if the given file is writable.
tryCreateFile(StringRef path)123 std::error_code lld::tryCreateFile(StringRef path) {
124 if (path.empty())
125 return std::error_code();
126 if (path == "-")
127 return std::error_code();
128 return errorToErrorCode(FileOutputBuffer::create(path, 1).takeError());
129 }
130