1 //===- Filesystem.cpp -----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains a few utility functions to handle files.
11 //===----------------------------------------------------------------------===//
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"
27 // Removes a given file asynchronously. This is a performance hack,
28 // so remove this when operating systems are improved.
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.
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
40 // This function spawns a background thread to remove the file.
41 // The calling thread returns almost immediately.
42 void lld::unlinkAsync(StringRef path
) {
43 if (!sys::fs::exists(path
) || !sys::fs::is_regular_file(path
))
46 // Removing a file is async on windows.
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.
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.
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.
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
68 SmallString
<128> tmpName
;
69 if (!sys::fs::createUniqueFile(path
+ "%%%%%%%%.tmp", tmpName
)) {
70 if (!sys::fs::rename(path
, tmpName
))
73 sys::fs::remove(tmpName
);
75 sys::fs::remove(path
);
77 if (parallel::strategy
.ThreadsRequested
== 1)
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.
85 std::error_code ec
= sys::fs::openFileForRead(path
, fd
);
86 sys::fs::remove(path
);
91 // close and therefore remove TempPath in background.
93 std::condition_variable cv
;
97 std::lock_guard
<std::mutex
> l(m
);
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
; });
111 // Simulate file creation to see if Path is writable.
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.
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.
123 std::error_code
lld::tryCreateFile(StringRef path
) {
125 return std::error_code();
127 return std::error_code();
128 return errorToErrorCode(FileOutputBuffer::create(path
, 1).takeError());