xref: /freebsd/contrib/llvm-project/lld/Common/Filesystem.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===- Filesystem.cpp -----------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains a few utility functions to handle files.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "lld/Common/Filesystem.h"
140b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
150b57cec5SDimitry Andric #include "llvm/Support/FileOutputBuffer.h"
160b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
17*5ffd83dbSDimitry Andric #include "llvm/Support/Parallel.h"
18*5ffd83dbSDimitry Andric #include "llvm/Support/Path.h"
190b57cec5SDimitry Andric #if LLVM_ON_UNIX
200b57cec5SDimitry Andric #include <unistd.h>
210b57cec5SDimitry Andric #endif
220b57cec5SDimitry Andric #include <thread>
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric using namespace lld;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric // Removes a given file asynchronously. This is a performance hack,
280b57cec5SDimitry Andric // so remove this when operating systems are improved.
290b57cec5SDimitry Andric //
300b57cec5SDimitry Andric // On Linux (and probably on other Unix-like systems), unlink(2) is a
310b57cec5SDimitry Andric // noticeably slow system call. As of 2016, unlink takes 250
320b57cec5SDimitry Andric // milliseconds to remove a 1 GB file on ext4 filesystem on my machine.
330b57cec5SDimitry Andric //
340b57cec5SDimitry Andric // To create a new result file, we first remove existing file. So, if
350b57cec5SDimitry Andric // you repeatedly link a 1 GB program in a regular compile-link-debug
360b57cec5SDimitry Andric // cycle, every cycle wastes 250 milliseconds only to remove a file.
370b57cec5SDimitry Andric // Since LLD can link a 1 GB binary in about 5 seconds, that waste
380b57cec5SDimitry Andric // actually counts.
390b57cec5SDimitry Andric //
400b57cec5SDimitry Andric // This function spawns a background thread to remove the file.
410b57cec5SDimitry Andric // The calling thread returns almost immediately.
420b57cec5SDimitry Andric void lld::unlinkAsync(StringRef path) {
43*5ffd83dbSDimitry Andric   if (!sys::fs::exists(path) || !sys::fs::is_regular_file(path))
44*5ffd83dbSDimitry Andric     return;
45*5ffd83dbSDimitry Andric 
460b57cec5SDimitry Andric // Removing a file is async on windows.
470b57cec5SDimitry Andric #if defined(_WIN32)
48*5ffd83dbSDimitry Andric   // On Windows co-operative programs can be expected to open LLD's
49*5ffd83dbSDimitry Andric   // output in FILE_SHARE_DELETE mode. This allows us to delete the
50*5ffd83dbSDimitry Andric   // file (by moving it to a temporary filename and then deleting
51*5ffd83dbSDimitry Andric   // it) so that we can link another output file that overwrites
52*5ffd83dbSDimitry Andric   // the existing file, even if the current file is in use.
53*5ffd83dbSDimitry Andric   //
54*5ffd83dbSDimitry Andric   // This is done on a best effort basis - we do not error if the
55*5ffd83dbSDimitry Andric   // operation fails. The consequence is merely that the user
56*5ffd83dbSDimitry Andric   // experiences an inconvenient work-flow.
57*5ffd83dbSDimitry Andric   //
58*5ffd83dbSDimitry Andric   // The code here allows LLD to work on all versions of Windows.
59*5ffd83dbSDimitry Andric   // However, at Windows 10 1903 it seems that the behavior of
60*5ffd83dbSDimitry Andric   // Windows has changed, so that we could simply delete the output
61*5ffd83dbSDimitry Andric   // file. This code should be simplified once support for older
62*5ffd83dbSDimitry Andric   // versions of Windows is dropped.
63*5ffd83dbSDimitry Andric   //
64*5ffd83dbSDimitry Andric   // Warning: It seems that the WINVER and _WIN32_WINNT preprocessor
65*5ffd83dbSDimitry Andric   // defines affect the behavior of the Windows versions of the calls
66*5ffd83dbSDimitry Andric   // we are using here. If this code stops working this is worth
67*5ffd83dbSDimitry Andric   // bearing in mind.
68*5ffd83dbSDimitry Andric   SmallString<128> tmpName;
69*5ffd83dbSDimitry Andric   if (!sys::fs::createUniqueFile(path + "%%%%%%%%.tmp", tmpName)) {
70*5ffd83dbSDimitry Andric     if (!sys::fs::rename(path, tmpName))
71*5ffd83dbSDimitry Andric       path = tmpName;
72*5ffd83dbSDimitry Andric     else
73*5ffd83dbSDimitry Andric       sys::fs::remove(tmpName);
74*5ffd83dbSDimitry Andric   }
750b57cec5SDimitry Andric   sys::fs::remove(path);
760b57cec5SDimitry Andric #else
77*5ffd83dbSDimitry Andric   if (parallel::strategy.ThreadsRequested == 1)
780b57cec5SDimitry Andric     return;
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   // We cannot just remove path from a different thread because we are now going
810b57cec5SDimitry Andric   // to create path as a new file.
820b57cec5SDimitry Andric   // Instead we open the file and unlink it on this thread. The unlink is fast
830b57cec5SDimitry Andric   // since the open fd guarantees that it is not removing the last reference.
840b57cec5SDimitry Andric   int fd;
850b57cec5SDimitry Andric   std::error_code ec = sys::fs::openFileForRead(path, fd);
860b57cec5SDimitry Andric   sys::fs::remove(path);
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   if (ec)
890b57cec5SDimitry Andric     return;
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   // close and therefore remove TempPath in background.
920b57cec5SDimitry Andric   std::mutex m;
930b57cec5SDimitry Andric   std::condition_variable cv;
940b57cec5SDimitry Andric   bool started = false;
950b57cec5SDimitry Andric   std::thread([&, fd] {
960b57cec5SDimitry Andric     {
970b57cec5SDimitry Andric       std::lock_guard<std::mutex> l(m);
980b57cec5SDimitry Andric       started = true;
990b57cec5SDimitry Andric       cv.notify_all();
1000b57cec5SDimitry Andric     }
1010b57cec5SDimitry Andric     ::close(fd);
1020b57cec5SDimitry Andric   }).detach();
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   // GLIBC 2.26 and earlier have race condition that crashes an entire process
1050b57cec5SDimitry Andric   // if the main thread calls exit(2) while other thread is starting up.
1060b57cec5SDimitry Andric   std::unique_lock<std::mutex> l(m);
1070b57cec5SDimitry Andric   cv.wait(l, [&] { return started; });
1080b57cec5SDimitry Andric #endif
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric // Simulate file creation to see if Path is writable.
1120b57cec5SDimitry Andric //
1130b57cec5SDimitry Andric // Determining whether a file is writable or not is amazingly hard,
1140b57cec5SDimitry Andric // and after all the only reliable way of doing that is to actually
1150b57cec5SDimitry Andric // create a file. But we don't want to do that in this function
1160b57cec5SDimitry Andric // because LLD shouldn't update any file if it will end in a failure.
1170b57cec5SDimitry Andric // We also don't want to reimplement heuristics to determine if a
1180b57cec5SDimitry Andric // file is writable. So we'll let FileOutputBuffer do the work.
1190b57cec5SDimitry Andric //
120480093f4SDimitry Andric // FileOutputBuffer doesn't touch a destination file until commit()
1210b57cec5SDimitry Andric // is called. We use that class without calling commit() to predict
1220b57cec5SDimitry Andric // if the given file is writable.
1230b57cec5SDimitry Andric std::error_code lld::tryCreateFile(StringRef path) {
1240b57cec5SDimitry Andric   if (path.empty())
1250b57cec5SDimitry Andric     return std::error_code();
1260b57cec5SDimitry Andric   if (path == "-")
1270b57cec5SDimitry Andric     return std::error_code();
1280b57cec5SDimitry Andric   return errorToErrorCode(FileOutputBuffer::create(path, 1).takeError());
1290b57cec5SDimitry Andric }
130