diff options
author | Johannes Schindelin <johannes.schindelin@gmx.de> | 2022-03-02 13:06:24 +0300 |
---|---|---|
committer | Johannes Schindelin <johannes.schindelin@gmx.de> | 2022-03-21 15:16:26 +0300 |
commit | bdc77d1d685be9c10b88abb281a42bc620548595 (patch) | |
tree | da028076656c4f292f6aa5bcbf8890f25f57c1d0 /git-compat-util.h | |
parent | 2a9a5862e516935149697f710f4fb00f14b7c40b (diff) |
Add a function to determine whether a path is owned by the current user
This function will be used in the next commit to prevent
`setup_git_directory()` from discovering a repository in a directory
that is owned by someone other than the current user.
Note: We cannot simply use `st.st_uid` on Windows just like we do on
Linux and other Unix-like platforms: according to
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions
this field is always zero on Windows (because Windows' idea of a user ID
does not fit into a single numerical value). Therefore, we have to do
something a little involved to replicate the same functionality there.
Also note: On Windows, a user's home directory is not actually owned by
said user, but by the administrator. For all practical purposes, it is
under the user's control, though, therefore we pretend that it is owned
by the user.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Diffstat (limited to 'git-compat-util.h')
-rw-r--r-- | git-compat-util.h | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/git-compat-util.h b/git-compat-util.h index 3da9f975e2..63ba89dd31 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -392,6 +392,18 @@ static inline int git_offset_1st_component(const char *path) #define is_valid_path(path) 1 #endif +#ifndef is_path_owned_by_current_user +static inline int is_path_owned_by_current_uid(const char *path) +{ + struct stat st; + if (lstat(path, &st)) + return 0; + return st.st_uid == geteuid(); +} + +#define is_path_owned_by_current_user is_path_owned_by_current_uid +#endif + #ifndef find_last_dir_sep static inline char *git_find_last_dir_sep(const char *path) { |