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" |
Bob Haarman | 4f5c8c2 | 2017-10-13 18:22:55 +0000 | [diff] [blame] | 16 | #include "lld/Common/Threads.h" |
Rafael Espindola | 1501a26 | 2017-11-07 23:01:37 +0000 | [diff] [blame^] | 17 | #include "llvm/Config/config.h" |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileOutputBuffer.h" |
Rui Ueyama | 945cd64 | 2017-10-05 23:01:11 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | 1501a26 | 2017-11-07 23:01:37 +0000 | [diff] [blame^] | 20 | #if defined(HAVE_UNISTD_H) |
| 21 | #include <unistd.h> |
| 22 | #endif |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | using namespace lld; |
| 27 | using namespace lld::elf; |
| 28 | |
| 29 | // Removes a given file asynchronously. This is a performance hack, |
| 30 | // so remove this when operating systems are improved. |
| 31 | // |
| 32 | // On Linux (and probably on other Unix-like systems), unlink(2) is a |
| 33 | // noticeably slow system call. As of 2016, unlink takes 250 |
| 34 | // milliseconds to remove a 1 GB file on ext4 filesystem on my machine. |
| 35 | // |
| 36 | // To create a new result file, we first remove existing file. So, if |
| 37 | // you repeatedly link a 1 GB program in a regular compile-link-debug |
| 38 | // cycle, every cycle wastes 250 milliseconds only to remove a file. |
| 39 | // Since LLD can link a 1 GB binary in about 5 seconds, that waste |
| 40 | // actually counts. |
| 41 | // |
Rafael Espindola | 1501a26 | 2017-11-07 23:01:37 +0000 | [diff] [blame^] | 42 | // This function spawns a background thread to remove the file. |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 43 | // The calling thread returns almost immediately. |
| 44 | void elf::unlinkAsync(StringRef Path) { |
Rafael Espindola | 1501a26 | 2017-11-07 23:01:37 +0000 | [diff] [blame^] | 45 | // Removing a file is async on windows. |
| 46 | #if defined(LLVM_ON_WIN32) |
| 47 | sys::fs::remove(Path); |
| 48 | #else |
Rafael Espindola | fcd7494 | 2017-11-07 02:00:51 +0000 | [diff] [blame] | 49 | if (!ThreadsEnabled || !sys::fs::exists(Path) || |
| 50 | !sys::fs::is_regular_file(Path)) |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 51 | return; |
| 52 | |
Rafael Espindola | 1501a26 | 2017-11-07 23:01:37 +0000 | [diff] [blame^] | 53 | // We cannot just remove path from a different thread because we are now going |
| 54 | // to create path as a new file. |
| 55 | // Instead we open the file and unlink it on this thread. The unlink is fast |
| 56 | // since the open fd guarantees that it is not removing the last reference. |
| 57 | int FD; |
| 58 | if (std::error_code EC = sys::fs::openFileForRead(Path, FD)) |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 59 | return; |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 60 | |
Rafael Espindola | 1501a26 | 2017-11-07 23:01:37 +0000 | [diff] [blame^] | 61 | sys::fs::remove(Path); |
| 62 | |
| 63 | // close and therefore remove TempPath in background. |
| 64 | runBackground([=] { ::close(FD); }); |
| 65 | #endif |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Rui Ueyama | 7163027 | 2017-04-26 16:14:46 +0000 | [diff] [blame] | 68 | // Simulate file creation to see if Path is writable. |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 69 | // |
| 70 | // Determining whether a file is writable or not is amazingly hard, |
| 71 | // and after all the only reliable way of doing that is to actually |
| 72 | // create a file. But we don't want to do that in this function |
| 73 | // because LLD shouldn't update any file if it will end in a failure. |
Rui Ueyama | 7163027 | 2017-04-26 16:14:46 +0000 | [diff] [blame] | 74 | // We also don't want to reimplement heuristics to determine if a |
| 75 | // file is writable. So we'll let FileOutputBuffer do the work. |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 76 | // |
| 77 | // FileOutputBuffer doesn't touch a desitnation file until commit() |
| 78 | // is called. We use that class without calling commit() to predict |
| 79 | // if the given file is writable. |
Rui Ueyama | 7163027 | 2017-04-26 16:14:46 +0000 | [diff] [blame] | 80 | std::error_code elf::tryCreateFile(StringRef Path) { |
Rui Ueyama | d283eb7 | 2017-04-26 16:15:07 +0000 | [diff] [blame] | 81 | if (Path.empty()) |
| 82 | return std::error_code(); |
Rui Ueyama | 7163027 | 2017-04-26 16:14:46 +0000 | [diff] [blame] | 83 | return FileOutputBuffer::create(Path, 1).getError(); |
Rui Ueyama | 9b55e92 | 2017-03-24 00:15:16 +0000 | [diff] [blame] | 84 | } |