summaryrefslogtreecommitdiff
path: root/src/basic/path-util.c
Commit message (Collapse)AuthorAgeFilesLines
...
* basic: add path_equal_filename helperLuca Boccassi2021-04-071-0/+21
|
* util: add creds-util.[ch] with helpers for dealing with credentialsLennart Poettering2021-03-261-6/+0
|
* path-util: return O_DIRECTORY from path_extract_filename() when path ends in ↵Lennart Poettering2021-03-021-5/+16
| | | | | | | | | | | | | | | | | | | | | | | | | slash Let's fine-tune the path_extract_filename() interface: on succes return O_DIRECTORY as indicator that the input path was slash-suffixed, and regular 0 otherwise. This is useful since in many cases it is useful to filter out paths that must refer to dirs early on. I opted for O_DIRECTORY instead of the following other ideas: 1. return -EISDIR: I think the function should return an extracted filename even when referring to an obvious dir, so this is not an option. 2. S_ISDIR, this was a strong contender, but I think O_DIRECTORY is a tiny bit nicer since quite likely we will go on and open the thing, maybe with openat(), and hence it's quite nice to be able to OR in the return value into the flags argument of openat(). 3. A new enum defined with two values "dont-know" and "definitely-directory". But I figured this was unnecessary, given we have other options too, that reuse existing definitions for very similar purposes.
* path-util: add path_extract_directory(), to match path_extract_filename()Lennart Poettering2021-03-021-0/+42
| | | | | | | | | | | These two together are a lot like dirname() + basename() but have the benefit that they return clear errors when one passes a special case path to them where the extraction doesn't make sense, i.e. "", "/", "foo", "foo/" and so on. Sooner or later we should probably port all our uses of dirname()/basename() over to this, to catch these special cases more safely.
* Move and rename parse_path_argument() functionZbigniew Jędrzejewski-Szmek2021-02-151-32/+0
| | | | | This fits better in shared/, and the new parse-argument.c file is a good home for it.
* path-util: tighten path_is_valid() checksLennart Poettering2021-02-031-4/+19
| | | | | This tightens the path_is_valid() checking: it now tests whether each component in the path is bound by FILENAME_MAX in its size.
* path-util: tighten path_extract_filename()Lennart Poettering2021-02-021-12/+13
| | | | | | | | | | | Let's tighten the logic behind path_extract_filename() a bit: first of all, refuse all cases of invalid paths with -EINVAL. More importantly though return a recognizable error when a valid path is specified that does not contain any filename. Specifically, "/" will now result in -EADDRNOTAVAIL. This changes API, but none of the existing callers care about the return value, hence the change should be fine.
* path-util: simplify check_x_access()Yu Watanabe2021-01-201-23/+11
| | | | | | | | | | | | | | | Follow-up for ece852c84592220c3d6bb5a055fd8b84ea097290. This addresses the following comments by the Lennart: --- hmm, so this now does two access() calls for the case where the fd is not requested, and opens things up for races (theoretically, …). now, the access() code path was in place for optimization, but if an optimization is less sexy than the original (and i think it is less sexy, since more than one syscall, and non-atomic), i think we shouldn't do the optimization. maybe we should just always use open(O_PATH) now, and then fstat() it to check if regular file, and then access_fd() it for checking if its executable.
* path-util: also check absolute path is a directory or not in find_executable()Yu Watanabe2021-01-191-29/+28
|
* Merge pull request #17079 from keszybz/late-exec-resolutionLennart Poettering2020-12-031-8/+44
|\ | | | | Resolve executable paths before execution, use fexecve()
| * basic/path-util: let find_executable_full() optionally return an fdZbigniew Jędrzejewski-Szmek2020-11-061-8/+44
| |
* | license: LGPL-2.1+ -> LGPL-2.1-or-laterYu Watanabe2020-11-091-1/+1
| |
* | macro: introduce POINTER_MAX as define for (void*) -1Lennart Poettering2020-10-221-2/+2
|/ | | | | Just add a safer, prettier way to write (void*) -1, that doesn't rely on two's complement, but uses the correct underlying C constructs.
* basic/path-util: enhance find_executable() for the fixed path caseZbigniew Jędrzejewski-Szmek2020-09-181-4/+6
|
* basic/path-util: inline two trivial functionsZbigniew Jędrzejewski-Szmek2020-09-181-8/+0
| | | | While at it, add assert() for the argument.
* Rename find_binary to find_executableZbigniew Jędrzejewski-Szmek2020-09-181-13/+7
| | | | "executable" is more correct than "binary", since scripts are OK too.
* path: Improve $PATH search directory caseChris Down2020-08-271-7/+18
| | | | | | | | | | | | Previously: 1. last_error wouldn't be updated with errors from is_dir; 2. We'd always issue a stat(), even for binaries without execute; 3. We used stat() instead of access(), which is cheaper. This change avoids all of those, by only checking inside X_OK-positive case whether access() works on the path with an extra slash appended. Thanks to Lennart for the suggestion.
* Merge pull request #16568 from poettering/creds-storeZbigniew Jędrzejewski-Szmek2020-08-261-0/+7
|\ | | | | credentials logic to pass privileged data to services
| * core: add credentials logicLennart Poettering2020-08-251-0/+7
| | | | | | | | Fixes: #15778 #16060
* | path: Skip directories when finalising $PATH searchChris Down2020-08-261-0/+3
|/ | | | | | | | | | | | | | | | | | | | | | | | Imagine $PATH /a:/b. There is an echo command at /b/echo. Under this configuration, this works fine: % systemd-run --user --scope echo . Running scope as unit: run-rfe98e0574b424d63a641644af511ff30.scope . However, if I do `mkdir /a/echo`, this happens: % systemd-run --user --scope echo . Running scope as unit: run-rcbe9369537ed47f282ee12ce9f692046.scope Failed to execute: Permission denied We check whether the resulting file is executable for the performing user, but of course, most directories are anyway, since that's needed to list within it. As such, another is_dir() check is needed prior to considering the search result final. Another approach might be to check S_ISREG, but there may be more gnarly edge cases there than just eliminating this obviously pathological example, so let's just do this for now.
* shared: introduce mkfs-util.c/.hLennart Poettering2020-08-241-12/+0
| | | | | | Let's move the "mkfs" code from homed there, plus other related code. This way we can easily reuse it from other places.
* path-util: make use of TAKE_PTR() where we canLennart Poettering2020-07-301-4/+2
|
* journalctl,elsewhere: make sure --file=foo fails with sane error msg if foo ↵Lennart Poettering2020-05-191-1/+1
| | | | | | | | | | | | | is not readable It annoyed me for quite a while that running "journalctl --file=…" on a file that is not readable failed with a "File not found" error instead of a permission error. Let's fix that. We make this work by using the GLOB_NOCHECK flag for glob() which means that files are not accessible will be returned in the array as they are instead of being filtered away. This then means that our later attemps to open the files will fail cleanly with a good error message.
* namespace: ignore prefix chars when comparing pathsTopi Miettinen2020-03-101-0/+16
| | | | | Other callers of path_strv_contains() or PATH_IN_SET() don't seem to handle paths prefixed with -+.
* path-util: introduce path_strv_contains() helperLennart Poettering2020-01-091-0/+10
| | | | | it's like strv_contains() but uses path_equal() rather than streq() to compare strings.
* tree-wide: drop stat.h or statfs.h when stat-util.h is includedYu Watanabe2019-11-041-1/+0
|
* tree-wide: drop string.h when string-util.h or friends are includedYu Watanabe2019-11-041-1/+0
|
* tree-wide: drop missing.hYu Watanabe2019-10-311-1/+0
|
* basic/fs-util: change CHASE_OPEN flag into a separate output parameterZbigniew Jędrzejewski-Szmek2019-10-241-1/+1
| | | | | | | | | | | | | chase_symlinks() would return negative on error, and either a non-negative status or a non-negative fd when CHASE_OPEN was given. This made the interface quite complicated, because dependning on the flags used, we would get two different "types" of return object. Coverity was always confused by this, and flagged every use of chase_symlinks() without CHASE_OPEN as a resource leak (because it would this that an fd is returned). This patch uses a saparate output parameter, so there is no confusion. (I think it is OK to have functions which return either an error or an fd. It's only returning *either* an fd or a non-fd that is confusing.)
* util-lib: Don't propagate EACCES from find_binary PATH lookup to callerChris Down2019-09-261-1/+3
| | | | | | | | | | | | | | | | | On one of my test machines, test-path-util was failing because the find_binary("xxxx-xxxx") was returning -EACCES instead of -ENOENT. This happens because the PATH entry on that host contains a directory which the user in question doesn't have access to. Typically applications ignore permission errors when searching through PATH, for example in bash: $ whoami cdown $ PATH=/root:/bin type sh sh is /bin/sh This behaviour is present on zsh and other shells as well, though. This patch brings our PATH search behaviour closer to other major Unix tools.
* basic/path-util: move two path_simplify* functions to be adjacentZbigniew Jędrzejewski-Szmek2019-07-191-46/+46
| | | | No functional change.
* path-util: add path_startswith_strv()Zbigniew Jędrzejewski-Szmek2019-07-191-0/+12
|
* Merge pull request #12836 from yuwata/tree-wide-replace-strjoinLennart Poettering2019-06-221-5/+2
|\ | | | | tree-wide: replace strjoin() with path_join()
| * tree-wide: replace strjoin() with path_join()Yu Watanabe2019-06-211-5/+2
| |
* | path-util: get rid of prefix_root()Lennart Poettering2019-06-211-36/+2
|/ | | | | | | | | | | | | | | | | | | prefix_root() is equivalent to path_join() in almost all ways, hence let's remove it. There are subtle differences though: prefix_root() will try shorten multiple "/" before and after the prefix. path_join() doesn't do that. This means prefix_root() might return a string shorter than both its inputs combined, while path_join() never does that. I like the path_join() semantics better, hence I think dropping prefix_root() is totally OK. In the end the strings generated by both functon should always be identical in terms of path_equal() if not streq(). This leaves prefix_roota() in place. Ideally we'd have path_joina(), but I don't think we can reasonably implement that as a macro. or maybe we can? (if so, sounds like something for a later PR) Also add in a few missing OOM checks
* util: split out nulstr related stuff to nulstr-util.[ch]Lennart Poettering2019-03-141-0/+1
|
* user-util: use SYNTHETIC_ERRNO() where we canLennart Poettering2019-03-121-28/+18
|
* path-util: minimize variable scopeLennart Poettering2019-03-121-1/+3
|
* path-util: check validity before normalization in path_simplify_and_warn()Lennart Poettering2019-03-121-6/+6
| | | | | As the normalization check includes a validation check the order matters.
* When parsing paths, reject anything above PATH_MAXZbigniew Jędrzejewski-Szmek2018-12-101-0/+7
| | | | | The check for length is done after path_simplify(), to be nice to paths which are constructed using specifiers, and have duplicate slashes and stuff.
* path-util: allow NULLs in arguments to path_join()Zbigniew Jędrzejewski-Szmek2018-11-301-17/+11
| | | | | This removes the need to remember to put strempty() in places, thus reducing the likelihood of a stupid mistake.
* tree-wide: rename path_join_many() to path_join()Zbigniew Jędrzejewski-Szmek2018-11-301-5/+5
| | | | | | $ git grep -e path_join_many -l|xargs sed -r -i 's/path_join_many/path_join/g' The two test functions are merged into one.
* tree-wide: replace path_join with path_join_manyZbigniew Jędrzejewski-Szmek2018-11-301-1/+1
|
* path-util: port path_join() over to path_join_many()Lennart Poettering2018-11-291-14/+0
| | | | | | We should probably drop path_join() entirely in the long run (and then rename path_join_many() to it?), but for now let's make one a wrapper for the other.
* path-util: add new path_join_many() APILennart Poettering2018-11-291-0/+64
|
* path-util: add helper for extracting last filename from pathLennart Poettering2018-11-261-0/+31
|
* path-util: handle NULL inputs in last_path_component()Lennart Poettering2018-11-261-0/+3
|
* tree-wide: port various places over to STARTSWITH_SET()Lennart Poettering2018-11-261-2/+1
|
* path-util: add new path_is_valid() helperLennart Poettering2018-10-171-5/+13
|
* path-util: fix path_simplify() with kill_dots and "."Thomas Haller2018-10-051-7/+15
| | | | | | | | | | Previously, together with kill_dots true, patch like ".", "./.", ".//.//" would all return an empty string. That is wrong. There must be one "." left to reference the current directory. Also, the comment with examples was wrong.