Skip to content

Commit

Permalink
is_directory(): a generic helper function
Browse files Browse the repository at this point in the history
A simple "grep -e stat --and -e S_ISDIR" revealed there are many
open-coded implementations of this function.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
gitster committed Sep 9, 2008
1 parent 10708a9 commit 90b4a71
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
13 changes: 12 additions & 1 deletion abspath.c
@@ -1,5 +1,16 @@
#include "cache.h"

/*
* Do not use this for inspecting *tracked* content. When path is a
* symlink to a directory, we do not want to say it is a directory when
* dealing with tracked content in the working tree.
*/
int is_directory(const char *path)
{
struct stat st;
return (!stat(path, &st) && S_ISDIR(st.st_mode));
}

/* We allow "recursive" symbolic links. Only within reason, though. */
#define MAXDEPTH 5

Expand All @@ -17,7 +28,7 @@ const char *make_absolute_path(const char *path)
die ("Too long path: %.*s", 60, path);

while (depth--) {
if (stat(buf, &st) || !S_ISDIR(st.st_mode)) {
if (!is_directory(buf)) {
char *last_slash = strrchr(buf, '/');
if (last_slash) {
*last_slash = '\0';
Expand Down
9 changes: 1 addition & 8 deletions builtin-clone.c
Expand Up @@ -77,7 +77,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
for (i = 0; i < ARRAY_SIZE(suffix); i++) {
const char *path;
path = mkpath("%s%s", repo, suffix[i]);
if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
if (is_directory(path)) {
*is_bundle = 0;
return xstrdup(make_nonrelative_path(path));
}
Expand Down Expand Up @@ -140,13 +140,6 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
return xstrndup(start, end - start);
}

static int is_directory(const char *path)
{
struct stat buf;

return !stat(path, &buf) && S_ISDIR(buf.st_mode);
}

static void strip_trailing_slashes(char *dir)
{
char *end = dir + strlen(dir);
Expand Down
1 change: 1 addition & 0 deletions cache.h
Expand Up @@ -533,6 +533,7 @@ static inline int is_absolute_path(const char *path)
{
return path[0] == '/' || has_dos_drive_prefix(path);
}
int is_directory(const char *);
const char *make_absolute_path(const char *path);
const char *make_nonrelative_path(const char *path);
const char *make_relative_path(const char *abs, const char *base);
Expand Down
10 changes: 3 additions & 7 deletions daemon.c
Expand Up @@ -1115,13 +1115,9 @@ int main(int argc, char **argv)
if (strict_paths && (!ok_paths || !*ok_paths))
die("option --strict-paths requires a whitelist");

if (base_path) {
struct stat st;

if (stat(base_path, &st) || !S_ISDIR(st.st_mode))
die("base-path '%s' does not exist or "
"is not a directory", base_path);
}
if (base_path && !is_directory(base_path))
die("base-path '%s' does not exist or is not a directory",
base_path);

if (inetd_mode) {
struct sockaddr_storage ss;
Expand Down
3 changes: 1 addition & 2 deletions rerere.c
Expand Up @@ -319,15 +319,14 @@ static int git_rerere_config(const char *var, const char *value, void *cb)

static int is_rerere_enabled(void)
{
struct stat st;
const char *rr_cache;
int rr_cache_exists;

if (!rerere_enabled)
return 0;

rr_cache = git_path("rr-cache");
rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
rr_cache_exists = is_directory(rr_cache);
if (rerere_enabled < 0)
return rr_cache_exists;

Expand Down
3 changes: 1 addition & 2 deletions sha1_file.c
Expand Up @@ -250,7 +250,6 @@ static void read_info_alternates(const char * alternates, int depth);
*/
static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth)
{
struct stat st;
const char *objdir = get_object_directory();
struct alternate_object_database *ent;
struct alternate_object_database *alt;
Expand Down Expand Up @@ -281,7 +280,7 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
ent->base[pfxlen] = ent->base[entlen-1] = 0;

/* Detect cases where alternate disappeared */
if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) {
if (!is_directory(ent->base)) {
error("object directory %s does not exist; "
"check .git/objects/info/alternates.",
ent->base);
Expand Down

0 comments on commit 90b4a71

Please sign in to comment.