Symbolic links for the universal binary also work on Windows

This commit is contained in:
Marcus Holland-Moritz 2023-07-10 18:41:39 +02:00
parent b978b954e0
commit 89571de0f3
3 changed files with 31 additions and 19 deletions

View File

@ -128,24 +128,33 @@ FUSE driver) in a single executable. These executables are compressed
using [upx](https://github.com/upx/upx), so they are much smaller than using [upx](https://github.com/upx/upx), so they are much smaller than
the individual tools combined. the individual tools combined.
The universal binaries can be run either through symbolic links named The universal binaries can be run through symbolic links named after
after the proper tool. e.g.: the proper tool. e.g.:
``` ```
$ ln -s dwarfs-universal-0.7.0-RC5-Linux-aarch64 mkdwarfs $ ln -s dwarfs-universal-0.7.0-Linux-aarch64 mkdwarfs
$ ./mkdwarfs --help $ ./mkdwarfs --help
``` ```
Or you can select the tool by passing `--tool=<name>` as the first This also works on Windows ift the file system supports symbolic links:
argument on the command line:
``` ```
$ .\dwarfs-universal-0.7.0-RC5-Windows-AMD64.exe --tool=mkdwarfs --help > mklink mkdwarfs.exe dwarfs-universal-0.7.0-Windows-AMD64.exe
> .\mkdwarfs.exe --help
```
Alternatively, you can select the tool by passing `--tool=<name>` as
the first argument on the command line:
```
> .\dwarfs-universal-0.7.0-Windows-AMD64.exe --tool=mkdwarfs --help
``` ```
Note that just like the `dwarfs.exe` Windows binary, the universal Note that just like the `dwarfs.exe` Windows binary, the universal
Windows binary depends on the `winfsp-x64.dll` from the Windows binary depends on the `winfsp-x64.dll` from the
[WinFsp](https://github.com/winfsp/winfsp) project. [WinFsp](https://github.com/winfsp/winfsp) project. However, for the
universal binary, the DLL is loaded lazily, so you can still use all
other tools without the DLL.
See the [Windows Support](#windows-support) section for more details. See the [Windows Support](#windows-support) section for more details.
### Dependencies ### Dependencies

View File

@ -87,6 +87,12 @@ int dwarfs_main_helper(int argc, sys_char** argv) {
} }
#endif #endif
#ifdef _WIN32
#define EXE_EXT ".exe"
#else
#define EXE_EXT ""
#endif
std::map<std::string_view, int (*)(int, sys_char**)> const functions{ std::map<std::string_view, int (*)(int, sys_char**)> const functions{
#ifdef _WIN32 #ifdef _WIN32
{"dwarfs", &dwarfs_main_helper}, {"dwarfs", &dwarfs_main_helper},
@ -120,14 +126,13 @@ int SYS_MAIN(int argc, sys_char** argv) {
} }
} }
#ifndef _WIN32
auto path = std::filesystem::path(argv[0]); auto path = std::filesystem::path(argv[0]);
if (auto it = functions.find(path.filename().string()); if (path.extension().string() == EXE_EXT) {
it != functions.end()) { if (auto it = functions.find(path.stem().string()); it != functions.end()) {
return safe_main([&] { return it->second(argc, argv); }); return safe_main([&] { return it->second(argc, argv); });
}
} }
#endif
using namespace folly::gen; using namespace folly::gen;

View File

@ -589,9 +589,7 @@ enum class binary_mode {
std::vector<binary_mode> tools_test_modes{ std::vector<binary_mode> tools_test_modes{
binary_mode::standalone, binary_mode::standalone,
binary_mode::universal_tool, binary_mode::universal_tool,
#ifndef _WIN32
binary_mode::universal_symlink, binary_mode::universal_symlink,
#endif
}; };
class tools_test : public ::testing::TestWithParam<binary_mode> {}; class tools_test : public ::testing::TestWithParam<binary_mode> {};
@ -608,10 +606,10 @@ TEST_P(tools_test, end_to_end) {
auto image_hdr = td / "test_hdr.dwarfs"; auto image_hdr = td / "test_hdr.dwarfs";
auto fsdata_dir = td / "fsdata"; auto fsdata_dir = td / "fsdata";
auto header_data = fsdata_dir / "format.sh"; auto header_data = fsdata_dir / "format.sh";
auto universal_symlink_dwarfs_bin = td / "dwarfs"; auto universal_symlink_dwarfs_bin = td / "dwarfs" EXE_EXT;
auto universal_symlink_mkdwarfs_bin = td / "mkdwarfs"; auto universal_symlink_mkdwarfs_bin = td / "mkdwarfs" EXE_EXT;
auto universal_symlink_dwarfsck_bin = td / "dwarfsck"; auto universal_symlink_dwarfsck_bin = td / "dwarfsck" EXE_EXT;
auto universal_symlink_dwarfsextract_bin = td / "dwarfsextract"; auto universal_symlink_dwarfsextract_bin = td / "dwarfsextract" EXE_EXT;
std::vector<std::string> mkdwarfs_tool_arg; std::vector<std::string> mkdwarfs_tool_arg;
std::vector<std::string> dwarfsck_tool_arg; std::vector<std::string> dwarfsck_tool_arg;
std::vector<std::string> dwarfsextract_tool_arg; std::vector<std::string> dwarfsextract_tool_arg;
@ -897,7 +895,7 @@ TEST_P(tools_test, mutating_ops) {
auto non_empty_dir = mountpoint / "foo"; auto non_empty_dir = mountpoint / "foo";
auto name_inside_fs = mountpoint / "some_random_name"; auto name_inside_fs = mountpoint / "some_random_name";
auto name_outside_fs = td / "some_random_name"; auto name_outside_fs = td / "some_random_name";
auto universal_symlink_dwarfs_bin = td / "dwarfs"; auto universal_symlink_dwarfs_bin = td / "dwarfs" EXE_EXT;
if (mode == binary_mode::universal_symlink) { if (mode == binary_mode::universal_symlink) {
fs::create_symlink(universal_bin, universal_symlink_dwarfs_bin); fs::create_symlink(universal_bin, universal_symlink_dwarfs_bin);