Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 1 | //===- 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" |
| 16 | #include "Error.h" |
| 17 | #include "llvm/Support/FileSystem.h" |
| 18 | #include "llvm/Support/FileOutputBuffer.h" |
| 19 | #include <thread> |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | using namespace lld; |
| 24 | using namespace lld::elf; |
| 25 | |
| 26 | // Removes a given file asynchronously. This is a performance hack, |
| 27 | // so remove this when operating systems are improved. |
| 28 | // |
| 29 | // On Linux (and probably on other Unix-like systems), unlink(2) is a |
| 30 | // noticeably slow system call. As of 2016, unlink takes 250 |
| 31 | // milliseconds to remove a 1 GB file on ext4 filesystem on my machine. |
| 32 | // |
| 33 | // To create a new result file, we first remove existing file. So, if |
| 34 | // you repeatedly link a 1 GB program in a regular compile-link-debug |
| 35 | // cycle, every cycle wastes 250 milliseconds only to remove a file. |
| 36 | // Since LLD can link a 1 GB binary in about 5 seconds, that waste |
| 37 | // actually counts. |
| 38 | // |
| 39 | // This function spawns a background thread to call unlink. |
| 40 | // The calling thread returns almost immediately. |
| 41 | void elf::unlinkAsync(StringRef Path) { |
| 42 | if (!Config->Threads || !sys::fs::exists(Config->OutputFile)) |
| 43 | return; |
| 44 | |
| 45 | // First, rename Path to avoid race condition. We cannot remove |
| 46 | // Path from a different thread because we are now going to create |
| 47 | // Path as a new file. If we do that in a different thread, the new |
| 48 | // thread can remove the new file. |
| 49 | SmallString<128> TempPath; |
| 50 | if (sys::fs::createUniqueFile(Path + "tmp%%%%%%%%", TempPath)) |
| 51 | return; |
| 52 | if (sys::fs::rename(Path, TempPath)) { |
| 53 | sys::fs::remove(TempPath); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | // Remove TempPath in background. |
| 58 | std::thread([=] { ::remove(TempPath.str().str().c_str()); }).detach(); |
| 59 | } |
| 60 | |
| 61 | // Returns true if a given file seems to be writable. |
| 62 | // |
| 63 | // Determining whether a file is writable or not is amazingly hard, |
| 64 | // and after all the only reliable way of doing that is to actually |
| 65 | // create a file. But we don't want to do that in this function |
| 66 | // because LLD shouldn't update any file if it will end in a failure. |
| 67 | // We also don't want to reimplement heuristics. So we'll let |
| 68 | // FileOutputBuffer do the work. |
| 69 | // |
| 70 | // FileOutputBuffer doesn't touch a desitnation file until commit() |
| 71 | // is called. We use that class without calling commit() to predict |
| 72 | // if the given file is writable. |
James Henderson | b7a90ef | 2017-04-04 09:42:24 +0000 | [diff] [blame] | 73 | bool elf::isFileWritable(StringRef Path, StringRef Desc) { |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 74 | if (auto EC = FileOutputBuffer::create(Path, 1).getError()) { |
James Henderson | b7a90ef | 2017-04-04 09:42:24 +0000 | [diff] [blame] | 75 | error("cannot open " + Desc + " " + Path + ": " + EC.message()); |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | return true; |
| 79 | } |