summaryrefslogtreecommitdiff
path: root/win32/mingw.c (follow)
Commit message (Collapse)AuthorAgeFilesLines
* win32: rmdir(2) shouldn't delete symlinksFRP-4487-gd239d2d52Ron Yorston2021-10-171-1/+8
| | | | | | | | On Linux rmdir(2) refuses to delete a symlink to a directory on the obvious grounds that a symlink isn't a directory. Windows' rmdir() is less discriminating. Make our implementation of rmdir(2) behave more like Linux.
* win32: code shrinkRon Yorston2021-10-161-9/+16
| | | | | | | | | There are a few places in mingw.c where we want to determine if a file is a symbolic link. Previously these called mingw_lstat() which collects far more information than is actually needed. Create a new is_symlink() function which does the minimum work necessary.
* realpath: improved support for Windows pathsRon Yorston2021-10-131-3/+2
| | | | | | | | | | | | | | | | Upstream commit 94eb1c4dc (libbb: better coreutils compatibility for realpath) made some changes to xmalloc_realpath_coreutils(). This now needs to be updated to handle Windows paths. - Expose the macro is_unc_path() and part of the recent change to bb_get_last_path_component_nostrip() as a separate funtion, get_last_slash(); - Convert a couple of errors relating to network filesystems to ENOENT; - Adjust xmalloc_realpath_coreutils() to handle Windows directory separators, relative paths and UNC paths.
* win32: fix creation of relative symlinksRon Yorston2021-10-121-2/+5
| | | | | | | | Symlinks containing '.' or '..' (such as '../target', './target' or 'dir/./target') were successfully created but couldn't be accessed. It turns out Windows requires paths of that form to use backslashes rather than forward slashes.
* win32: rename is_absolute_path()Ron Yorston2021-10-121-8/+8
| | | | | As the comment pointed out is_absolute_path() was misnamed. Rename it to is_relative_path() and change the sense of all tests.
* win32: use is_dir_sep() everywhereRon Yorston2021-10-121-4/+3
| | | | | The is_dir_sep() macro, which has been around since the start of busybox-w32, can be used instead of is_path_sep().
* ash: improve signal handlingRon Yorston2021-09-211-0/+9
| | | | | | | | | | | | Allow waitpid() to detect SIGTERM/SIGKILL by checking the (Windows) status returned by GetExitCodeProcess() and updating the Unix status to suit. This allows ash to detect when a process has been 'signalled'. Provide our own implementation of strsignal(3) which returns expanded text for SIGTERM/SIGKILL. Costs 192 bytes.
* win32: code shrinkRon Yorston2021-08-271-0/+2
| | | | | | | | Turn off the 'if-conversion' optimisation for err_win_to_posix(). My tests actually have this being slightly faster than with the optimisation enabled, though within the variation of the measurement. Saves 288 bytes.
* win32: implement utimes(2) using utimensat(2)Ron Yorston2021-08-151-38/+18
| | | | Saves 176 bytes.
* win32: tidy up fetching of system directoryRon Yorston2021-08-141-14/+17
| | | | | | | | | | | | Add a new function, getsysdir(), to fetch and cache the system directory. This avoids the non-intuitive use of getpwuid() in get_system_drive(). The call to GetSystemDirectory() in get_proc_addr() can't be replaced because getsysdir() calls realpath() which requires a call to get_proc_addr(). No change in the size of the binary.
* win32: code shrinkRon Yorston2021-08-141-14/+14
| | | | | | | | | There doesn't seem to be any need to call OpenThreadToken() in file_owner(): OpenProcessToken() should suffice. Also, tidy up gethomedir() without any change in functionality. Saves 56 bytes.
* win32: treat devices as character special filesRon Yorston2021-08-131-1/+3
| | | | | | | | | | | | | Commit 15fcbd19c (win32: special case for devices files in stat(2)) added special treatment in stat(2) for device files. As a result of this change device files appeared to be regular files which broke the use of /dev/null with noclobber in the shell. Device files now appear as character special files (as they do on Unix). GitHub issue #225.
* win32: better handling of nested symlinksRon Yorston2021-08-121-0/+8
| | | | | | | | | | | | | | Our realpath(3) implementation uses xmalloc_follow_symlinks() to expand symlinks. This detects when symlinks are too deeply nested but didn't set errno, so anything calling realpath(3) was unable to say what had gone wrong. (For example, 'ls -L' or 'stat -L'.) Set errno to ELOOP. This then leads to the problem that Windows doesn't know about ELOOP so reports 'Unknown error'. Add a replacement for strerror(3) which returns a sensible message. Costs 96 bytes.
* win32: tidy up time conversionsRon Yorston2021-08-091-7/+3
| | | | | | | Remove filetime_to_time_t(): it's no longer used. Align style of time{spec,val}_to_filetime() to make it easier to compare what they do.
* win32: code shrink using is_prefixed_with()Ron Yorston2021-07-281-16/+18
| | | | | | | Use is_prefixed_with() rather than strncmp() in a few places, and the case-insensitive analogues. Saves 96 bytes in 64-bit build, 192 bytes in 32-bit.
* win32: code shrink has_exec_format()Ron Yorston2021-07-281-13/+11
| | | | | | | | | | | | | | In the code to detect binaries: - use '|' rather than '+' to combine bytes; - fix the test that the PE header is within the buffer; - once we have the offset to the PE header make a pointer to it; - cosmetic changes. Saves 96 bytes.
* win32: special case for devices files in stat(2)Ron Yorston2021-07-251-5/+20
| | | | | | | | | | | | | | | | | | | | | | The diff applet calls stat(2) on the files it's asked to process. This includes /dev/null when the -N flag is used and /dev/fd/* files when process substitution is used. Treat device files as a special case in get_file_attr(), returning a fake set of attributes with FILE_ATTRIBUTE_DEVICE set. This value is unused elsewhere in busybox-w32. Ensure it's unset in other cases. When FILE_ATTRIBUTE_DEVICE is set: - adjust some permissions; - avoid calling has_exec_format() as this opens/closes the file which breaks process substitution. These changes improve the behaviour of diff but they also have other effects. For example, the stat and ls applets now report details of device files. There may be unintended consequences.
* win32: partial implementation of sync(2)Ron Yorston2021-06-071-0/+23
| | | | | | | | | | Provide a partial implementation of sync(2), so sync(1) can actually do something in some circumstances: - Only logical drives are handled. - Flushing buffers requires administrative privileges. If run as a normal user nothing will happen.
* win32: let virtual /dev/fd/<num> files be openedRon Yorston2021-06-071-0/+20
| | | | Add support for virtual /dev/fd files to represent file descriptors.
* win32: rename update_dev_fd() as update_special_fd()Ron Yorston2021-06-071-2/+2
| | | | Avoid confusion between special devices and /dev/fd.
* win32: implement futimens(2)/utimensat(2)Ron Yorston2021-05-141-1/+81
| | | | | The touch applet has been changed to use futimens(2)/utimensat(2). Provide implementations of these for WIN32.
* win32: fix creation of symlinks in 64-bit Windows 7Ron Yorston2021-05-131-1/+1
| | | | | | | | The 64-bit build of busybox-w32 failed to create symbolic links on Windows 7 but claimed to have succeeded. The declaration of CreateSymbolicLinkA had the wrong return value. See GitHub issue #217.
* libbb: fix detection of relative paths in xreadlink.csymlinkRon Yorston2021-03-011-1/+1
| | | | | | In xmalloc_follow_symlinks() the code to detect relative paths needs to be altered for WIN32. We don't want C:/path to be treated as a relative path.
* Allow `rename()` to work across drivesJohannes Schindelin2021-03-011-1/+2
| | | | Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
* Do not use `rename()` for symlinksJohannes Schindelin2021-03-011-5/+8
| | | | | | That function would rename the _target_, not the link. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
* win32: let `resolve_symlinks()` _really_ resolve symbolic linksJohannes Schindelin2021-03-011-1/+9
| | | | | | | | | When one tries to call `CreateFile()` with a reparse point, it will trigger an `ERROR_INVALID_NAME` unless `FILE_FLAG_OPEN_REPARSE_POINT` is passed. However, _when_ that flag is passed, it does not open a handle to the symlink _target_, but to the symlink itself. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
* readlink(): do `NUL`-terminate the resultJohannes Schindelin2021-03-011-2/+3
| | | | | | Otherwise we're provoking buffer overruns. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
* win32(symlink): fix logic to determine the type of the symlinkJohannes Schindelin2021-03-011-1/+10
| | | | | | | | | | | | | | | | | | | | On Windows, there are file symlinks and directory symlinks. When trying to `opendir()` a symlink marked as `file`, it will fail. Even if the target is a directory. Because it's the wrong symlink type. To address this, our `symlink()` function calls `stat(target, ...)` to see whether the target exists and is a directory. The problem is that this `target` can be a relative path, and the link path can _also_ be a relative path. Example: `symlink("dir", "uh/oh")`. In this example, the target might say `dir`, but it is relative to `uh/oh`, i.e. we need to `stat("uh/dir", ...)`. This is necessary to pass the `cp` tests because they first create such a directory symlink and then try to copy it while dereferencing symlinks, i.e. calling `opendir()` on the symlink. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
* win32: move is_absolute_path()Ron Yorston2021-03-011-0/+9
| | | | | Make is_absolute_path() a function rather than a macro and move it from ash.c into mingw.c.
* win32: workaround for lazy loading issue on Windows 7Ron Yorston2021-03-011-0/+14
| | | | | | | | Investigating why free(1) wasn't working on Windows 7 I found it's possible for LoadLibraryEx to fail with exactly the flags we're using. Work around this. This probably also explains GitHub issue #204.
* win32: update sysinfo(2) implementation (again)Ron Yorston2021-02-231-14/+31
| | | | | | | | | | Use another API call: EnumPageFiles(). This seems to provide more reliable information about page file usage than the previous ad hoc method. It also allows the call to GlobalMemoryStatusEx() to be removed. With these changes free(1) works sensibly on Windows XP, though not ReactOS.
* win32: update sysinfo(2) implementationRon Yorston2021-02-191-13/+23
| | | | | | | | | | Add a call to GetPerformanceInfo. Treat the SystemCache member of the PERFORMANCE_INFORMATION structure as buffer RAM. Deduct it from available RAM. The numbers reported by 'free' move about in vaguely sensible ways when I start and stop programs, though I still don't know if they're in any way accurate.
* win32: add uptime to sysinfo(2), enable uptime appletRon Yorston2021-02-191-3/+2
| | | | | | | | | | Fill the uptime member of the sysinfo structure. With this change we can: - use sysinfo(2) in the 'ps' applet; - enable the 'uptime' applet (though without useful support for load averages).
* free: implement sysinfo(2) and enable free(1)Ron Yorston2021-02-181-0/+24
| | | | | | | | | | | | | This is an experimental implementation of sysinfo(2)/free(1). It uses the WIN32 API GlobalMemoryStatusEx() to obtain information about memory. It seems that the 'total pagefile' value includes total RAM as well as pagefile and 'available pagefile' includes available RAM. So the RAM values are deducted. I've no idea what corresponds to Linux buffers and cache.
* win32: code shrinkRon Yorston2021-02-131-1/+1
| | | | | | | | Rewrite the recent change to tab completion so it only needs one call to sprintf. Then replace sprintf with strcpy/stpcpy, both there and in a couple of other places. Saves 40 bytes.
* win32: implement symlink(2)Ron Yorston2021-02-121-0/+35
| | | | | | | | | | | | | | | | | | | | | Provide an implementation of symlink(2). Calls to symlink(2) will fail in default Windows installations unless running with elevated privileges. Failure to create a symlink when extracting files from an archive is therefore treated as a non-fatal error. There are two ways to permit the creation of symlinks: - Edit security policy to give users the 'Create symbolic links' privilege. Unfortunately this doesn't work for users who are an Administrator. - Enable developer mode, which is available in later versions of Windows 10. The ability to create symlinks is not available in Windows XP or ReactOS.
* win32: allow symlinks to directories to be unlinkedRon Yorston2021-02-121-1/+13
| | | | | | Windows distinguishes between symlinks to directories and files. A symlink to a directory must be deleted by calling rmdir(2) rather than unlink(2).
* win32: make readlink(2) implementation unconditionalRon Yorston2021-02-121-4/+0
| | | | | | | There doesn't seem to be much advantage in having readlink(2) as a configuration option. Making it unconditional reduces divergence from upstream and allows the removal of a check for ENOSYS that's been in busybox-w32 since the start.
* tls: avoid unnecessary changes to POSIX build, part 2Ron Yorston2021-01-251-0/+9
| | | | | | | | | | | | | | On reflection, the previous commit may have been ill-advised. There are many calls to open_read_close() and most shouldn't be able to access special devices. (Though in practice only a few are enabled in busybox-w32.) Nonetheless, I've implemented a new mechanism which uses the macro MINGW_SPECIAL() to mark calls to functions that are allowed to access special devices. An unrelated change is to avoid compiling fputs_stdout() in coreutils/printf.c for the POSIX build.
* win32: make realpath() return an error if necessaryRon Yorston2020-12-121-1/+0
| | | | | | | | | | | | resolve_symlinks() uses GetFinalPathNameByHandleA which is loaded at runtime because it isn't available in all versions of Windows. If GetFinalPathNameByHandleA isn't available resolve_symlinks() (and hence realpath()) should return a NULL pointer to indicate an error, not the original path. Not returning NULL causes an infinite loop in do_lstat(). See GitHub issue #204.
* win32: make location of Unix-style absolute paths configurableRon Yorston2020-08-311-1/+1
| | | | | | | | | | | | | | | | | Previously busybox-w32 was enhanced so certain Unix-style absolute paths of the form '/dir/file' were treated as being relative to the system drive. (Notionally as reported by %SYSTEMDRIVE% but in fact derived from the API call GetSystemDirectory().) Make the location of such files configurable by the BB_SYSTEMROOT environment variable. - BB_SYSTEMROOT should probably only refer to a Windows-style absolute path, but this isn't checked. - Set BB_SYSTEMROOT using the Windows Control Panel or setx, not as a shell variable: the shell itself won't see the environment variable.
* ash, ls: improve support for 'c:path'Ron Yorston2020-08-281-15/+0
| | | | | | | | | | Revert commit 249f68e3c (win32: append '/' to bare drive name in opendir). Instead add better handling for paths of the form 'c:path' to ls and expmeta() in ash. Adds 64 bytes.
* win32: code shrink Unix-style path handlingRon Yorston2020-08-231-23/+7
| | | | | | | | Replace auto_add_system_drive() with alloc_system_drive() which leaves space for a possible filename extension. This makes it possible to drop alloc_win32_extension() and auto_win32_extension(). Saves 144 bytes.
* win32: use built-in applets for non-existent binaries with Unix-style pathsRon Yorston2020-08-131-0/+13
| | | | | | | | | | | | | | | | | | | | Shell scripts moved from Unix may contain hard-coded paths to binaries such as /bin/sh. A recent commit made it possible to execute such binaries reliably, but that does require them to be installed. As an alternative solution: if a binary with a standard Unix path prefix can't be found but is available as a built-in applet, run the applet. Add the function unix_path() to detect paths starting with /bin, /usr/bin, /sbin or /usr/sbin. Use this function in: - the 'which' applet - shellexec(), describe_command() and find_command() in ash - mingw_spawn_1() See GitHub issue #195.
* win32: handle Unix-style absolute paths for executablesRon Yorston2020-08-131-0/+15
| | | | | | | | | | | | | | | | | | | | | | As noted in commit 548ec7045 (win32: interpret absolute paths as relative to %SYSTEMDRIVE%) a path starting with a '/' in the Unix world is treated as relative to the current drive by Windows. To avoid ambiguity that commit considered certain such paths to be relative to %SYSTEMDRIVE%. Extend this to paths representing executables. Add the functions need_system_drive() and auto_add_system_drive() to detect the need for a system drive prefix and to add it if necessary. Use these functions in: - the 'which' applet - the find_executable() function - tab-completion code - PATH look-up, shellexec(), describe_command() and find_command() in ash - parse_interpreter() and mingw_spawn_1() With these changes executable paths starting with a slash are handled consistently, whatever the current drive.
* win32: use a static buffer in get_system_drive()Ron Yorston2020-08-131-14/+13
| | | | | | | Allocate static storage for the system drive string instead of making a new allocation on every call. This is easier to manage. Adds 16 bytes.
* win32: code shrinkRon Yorston2020-08-131-0/+8
| | | | | | | | | | Add a new function, has_path(), to detect that an executable name doesn't require a path look-up. Also, since is_absolute_path() is now only used in shell/ash.c move its definition there from include/mingw.h. Saves 128 bytes.
* win32: return raw file attributes in struct mingw_statRon Yorston2020-07-091-0/+4
| | | | | | Until now the emulated stat(2) system calls have only returned a synthesised Unix-style mode value. Also return the raw Windows file attributes.
* ash: improve handling of UNC pathsRon Yorston2020-06-301-1/+2
| | | | | | | | | | | | | | Be more strict about identifying UNC paths in unc_root_len(). In updatepwd() in ash: - Skip duplicate leading slashes unless the directory is a UNC path. - Rewrite detection and handling of the five possible types of path. This improves cases like 'cd ///' and 'cd /xyz' when the current directory is a UNC path. See GitHub issue #192.
* win32: enable globbing by defaultRon Yorston2020-06-141-1/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Change how busybox.exe expands wildcards on the command line. When globbing is enabled at compile time provide an implementation of _setargv(), which is run early during startup of C programs. This: - enables globbing by setting _dowildcard to -1 - checks for the presence of the environment BB_GLOBBING - if it exists and is set to 0 disables globbing - if it doesn't exist sets BB_GLOBBING=0 but continues to apply Windows' globbing in the current process The consequences of this are: - When busybox.exe is initially run from a Command Prompt Windows' globbing is applied; - Windows' globbing is turned off for future child processes, thus allowing the shell re-execute busybox.exe without it interfering with wildcards; - this behaviour can be overridden by setting BB_GLOBBING explicitly. Globbing can still be disabled at compile time if required. In that case BB_GLOBBING has no effect. With these changes globbing can be enabled by default and BusyBox will do the right thing in most circumstances. (See GitHub issues #172 and #189.)