17fd99fc4SRui Ueyama //===- Filesystem.cpp -----------------------------------------------------===//
27fd99fc4SRui Ueyama //
37fd99fc4SRui Ueyama // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47fd99fc4SRui Ueyama // See https://llvm.org/LICENSE.txt for license information.
57fd99fc4SRui Ueyama // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67fd99fc4SRui Ueyama //
77fd99fc4SRui Ueyama //===----------------------------------------------------------------------===//
87fd99fc4SRui Ueyama //
97fd99fc4SRui Ueyama // This file contains a few utility functions to handle files.
107fd99fc4SRui Ueyama //
117fd99fc4SRui Ueyama //===----------------------------------------------------------------------===//
127fd99fc4SRui Ueyama 
137fd99fc4SRui Ueyama #include "lld/Common/Filesystem.h"
147fd99fc4SRui Ueyama #include "llvm/Config/llvm-config.h"
157fd99fc4SRui Ueyama #include "llvm/Support/FileOutputBuffer.h"
167fd99fc4SRui Ueyama #include "llvm/Support/FileSystem.h"
17932f0276SReid Kleckner #include "llvm/Support/Parallel.h"
18f42f599dSBen Dunbobbin #include "llvm/Support/Path.h"
197fd99fc4SRui Ueyama #if LLVM_ON_UNIX
207fd99fc4SRui Ueyama #include <unistd.h>
217fd99fc4SRui Ueyama #endif
227fd99fc4SRui Ueyama #include <thread>
237fd99fc4SRui Ueyama 
247fd99fc4SRui Ueyama using namespace llvm;
257fd99fc4SRui Ueyama using namespace lld;
267fd99fc4SRui Ueyama 
277fd99fc4SRui Ueyama // Removes a given file asynchronously. This is a performance hack,
287fd99fc4SRui Ueyama // so remove this when operating systems are improved.
297fd99fc4SRui Ueyama //
307fd99fc4SRui Ueyama // On Linux (and probably on other Unix-like systems), unlink(2) is a
317fd99fc4SRui Ueyama // noticeably slow system call. As of 2016, unlink takes 250
327fd99fc4SRui Ueyama // milliseconds to remove a 1 GB file on ext4 filesystem on my machine.
337fd99fc4SRui Ueyama //
347fd99fc4SRui Ueyama // To create a new result file, we first remove existing file. So, if
357fd99fc4SRui Ueyama // you repeatedly link a 1 GB program in a regular compile-link-debug
367fd99fc4SRui Ueyama // cycle, every cycle wastes 250 milliseconds only to remove a file.
377fd99fc4SRui Ueyama // Since LLD can link a 1 GB binary in about 5 seconds, that waste
387fd99fc4SRui Ueyama // actually counts.
397fd99fc4SRui Ueyama //
407fd99fc4SRui Ueyama // This function spawns a background thread to remove the file.
417fd99fc4SRui Ueyama // The calling thread returns almost immediately.
unlinkAsync(StringRef path)42136d27abSRui Ueyama void lld::unlinkAsync(StringRef path) {
43*6bda276fSBen Dunbobbin   if (!sys::fs::exists(path) || !sys::fs::is_regular_file(path))
44*6bda276fSBen Dunbobbin     return;
45*6bda276fSBen Dunbobbin 
467fd99fc4SRui Ueyama // Removing a file is async on windows.
477fd99fc4SRui Ueyama #if defined(_WIN32)
48f42f599dSBen Dunbobbin   // On Windows co-operative programs can be expected to open LLD's
49f42f599dSBen Dunbobbin   // output in FILE_SHARE_DELETE mode. This allows us to delete the
50f42f599dSBen Dunbobbin   // file (by moving it to a temporary filename and then deleting
51f42f599dSBen Dunbobbin   // it) so that we can link another output file that overwrites
52f42f599dSBen Dunbobbin   // the existing file, even if the current file is in use.
53f42f599dSBen Dunbobbin   //
54f42f599dSBen Dunbobbin   // This is done on a best effort basis - we do not error if the
55f42f599dSBen Dunbobbin   // operation fails. The consequence is merely that the user
56f42f599dSBen Dunbobbin   // experiences an inconvenient work-flow.
57f42f599dSBen Dunbobbin   //
58f42f599dSBen Dunbobbin   // The code here allows LLD to work on all versions of Windows.
59f42f599dSBen Dunbobbin   // However, at Windows 10 1903 it seems that the behavior of
60f42f599dSBen Dunbobbin   // Windows has changed, so that we could simply delete the output
61f42f599dSBen Dunbobbin   // file. This code should be simplified once support for older
62f42f599dSBen Dunbobbin   // versions of Windows is dropped.
63f42f599dSBen Dunbobbin   //
64f42f599dSBen Dunbobbin   // Warning: It seems that the WINVER and _WIN32_WINNT preprocessor
65f42f599dSBen Dunbobbin   // defines affect the behavior of the Windows versions of the calls
66f42f599dSBen Dunbobbin   // we are using here. If this code stops working this is worth
67f42f599dSBen Dunbobbin   // bearing in mind.
68f42f599dSBen Dunbobbin   SmallString<128> tmpName;
69f42f599dSBen Dunbobbin   if (!sys::fs::createUniqueFile(path + "%%%%%%%%.tmp", tmpName)) {
70f42f599dSBen Dunbobbin     if (!sys::fs::rename(path, tmpName))
71f42f599dSBen Dunbobbin       path = tmpName;
72f42f599dSBen Dunbobbin     else
73f42f599dSBen Dunbobbin       sys::fs::remove(tmpName);
74f42f599dSBen Dunbobbin   }
7576c3f6cdSRui Ueyama   sys::fs::remove(path);
767fd99fc4SRui Ueyama #else
77*6bda276fSBen Dunbobbin   if (parallel::strategy.ThreadsRequested == 1)
787fd99fc4SRui Ueyama     return;
797fd99fc4SRui Ueyama 
807fd99fc4SRui Ueyama   // We cannot just remove path from a different thread because we are now going
817fd99fc4SRui Ueyama   // to create path as a new file.
827fd99fc4SRui Ueyama   // Instead we open the file and unlink it on this thread. The unlink is fast
837fd99fc4SRui Ueyama   // since the open fd guarantees that it is not removing the last reference.
84136d27abSRui Ueyama   int fd;
85136d27abSRui Ueyama   std::error_code ec = sys::fs::openFileForRead(path, fd);
86136d27abSRui Ueyama   sys::fs::remove(path);
877fd99fc4SRui Ueyama 
88136d27abSRui Ueyama   if (ec)
897fd99fc4SRui Ueyama     return;
907fd99fc4SRui Ueyama 
917fd99fc4SRui Ueyama   // close and therefore remove TempPath in background.
92136d27abSRui Ueyama   std::mutex m;
93136d27abSRui Ueyama   std::condition_variable cv;
94136d27abSRui Ueyama   bool started = false;
95136d27abSRui Ueyama   std::thread([&, fd] {
967fd99fc4SRui Ueyama     {
97136d27abSRui Ueyama       std::lock_guard<std::mutex> l(m);
98136d27abSRui Ueyama       started = true;
99136d27abSRui Ueyama       cv.notify_all();
1007fd99fc4SRui Ueyama     }
101136d27abSRui Ueyama     ::close(fd);
1027fd99fc4SRui Ueyama   }).detach();
1037fd99fc4SRui Ueyama 
1047fd99fc4SRui Ueyama   // GLIBC 2.26 and earlier have race condition that crashes an entire process
1057fd99fc4SRui Ueyama   // if the main thread calls exit(2) while other thread is starting up.
106136d27abSRui Ueyama   std::unique_lock<std::mutex> l(m);
107136d27abSRui Ueyama   cv.wait(l, [&] { return started; });
1087fd99fc4SRui Ueyama #endif
1097fd99fc4SRui Ueyama }
1107fd99fc4SRui Ueyama 
1117fd99fc4SRui Ueyama // Simulate file creation to see if Path is writable.
1127fd99fc4SRui Ueyama //
1137fd99fc4SRui Ueyama // Determining whether a file is writable or not is amazingly hard,
1147fd99fc4SRui Ueyama // and after all the only reliable way of doing that is to actually
1157fd99fc4SRui Ueyama // create a file. But we don't want to do that in this function
1167fd99fc4SRui Ueyama // because LLD shouldn't update any file if it will end in a failure.
1177fd99fc4SRui Ueyama // We also don't want to reimplement heuristics to determine if a
1187fd99fc4SRui Ueyama // file is writable. So we'll let FileOutputBuffer do the work.
1197fd99fc4SRui Ueyama //
1207ae3d335SKazuaki Ishizaki // FileOutputBuffer doesn't touch a destination file until commit()
1217fd99fc4SRui Ueyama // is called. We use that class without calling commit() to predict
1227fd99fc4SRui Ueyama // if the given file is writable.
tryCreateFile(StringRef path)123136d27abSRui Ueyama std::error_code lld::tryCreateFile(StringRef path) {
124136d27abSRui Ueyama   if (path.empty())
1257fd99fc4SRui Ueyama     return std::error_code();
126136d27abSRui Ueyama   if (path == "-")
1277fd99fc4SRui Ueyama     return std::error_code();
128136d27abSRui Ueyama   return errorToErrorCode(FileOutputBuffer::create(path, 1).takeError());
1297fd99fc4SRui Ueyama }
130