From fa96aa8d70eb1dfd7b3550fab417a43b3a507a6d Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 5 Jan 2019 14:16:50 +0000 Subject: busybox: add --uninstall option Add an option to allow hard links to be removed. busybox --uninstall file removes all hard links to the given file (including the file itself.) Since Microsoft Windows refuses to delete a running executable a BusyBox binary is unable to remove links to itself. busybox --uninstall -n file displays the names of all hard links to the given file. Although this feature is couched in terms of uninstalling BusyBox it's actually quite general: it can be used to delete or display hard links to any file. --- win32/mingw.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'win32') diff --git a/win32/mingw.c b/win32/mingw.c index c420992d5..8d1da5199 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -1289,3 +1289,42 @@ ULONGLONG CompatGetTickCount64(void) return GetTickCount64(); } #endif + +#if ENABLE_FEATURE_INSTALLER +/* + * Enumerate the names of all hard links to a file. The first call + * provides the file name as the first argument; subsequent calls must + * set the first argument to NULL. Returns 0 on error or when there are + * no more links. + */ +int enumerate_links(const char *file, char *name) +{ + static HANDLE h = INVALID_HANDLE_VALUE; + char aname[PATH_MAX]; + wchar_t wname[PATH_MAX]; + DWORD length = PATH_MAX; + DECLARE_PROC_ADDR(HANDLE, FindFirstFileNameW, LPCWSTR, DWORD, LPDWORD, + PWSTR); + DECLARE_PROC_ADDR(BOOL, FindNextFileNameW, HANDLE, LPDWORD, PWSTR); + + if (!INIT_PROC_ADDR(kernel32.dll, FindFirstFileNameW) || + !INIT_PROC_ADDR(kernel32.dll, FindNextFileNameW)) + return 0; + + if (file != NULL) { + wchar_t wfile[PATH_MAX]; + MultiByteToWideChar(CP_ACP, 0, file, -1, wfile, PATH_MAX); + h = FindFirstFileNameW(wfile, 0, &length, wname); + if (h == INVALID_HANDLE_VALUE) + return 0; + } + else if (!FindNextFileNameW(h, &length, wname)) { + FindClose(h); + h = INVALID_HANDLE_VALUE; + return 0; + } + WideCharToMultiByte(CP_ACP, 0, wname, -1, aname, PATH_MAX, NULL, NULL); + realpath(aname, name); + return 1; +} +#endif -- cgit v1.2.3-55-g6feb