is_directory(): a generic helper function

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>
maint
Junio C Hamano 2008-09-09 01:27:07 -07:00
parent 10708a994a
commit 90b4a71c49
6 changed files with 19 additions and 20 deletions

View File

@ -1,5 +1,16 @@
#include "cache.h" #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. */ /* We allow "recursive" symbolic links. Only within reason, though. */
#define MAXDEPTH 5 #define MAXDEPTH 5


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


while (depth--) { while (depth--) {
if (stat(buf, &st) || !S_ISDIR(st.st_mode)) { if (!is_directory(buf)) {
char *last_slash = strrchr(buf, '/'); char *last_slash = strrchr(buf, '/');
if (last_slash) { if (last_slash) {
*last_slash = '\0'; *last_slash = '\0';

View File

@ -77,7 +77,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
for (i = 0; i < ARRAY_SIZE(suffix); i++) { for (i = 0; i < ARRAY_SIZE(suffix); i++) {
const char *path; const char *path;
path = mkpath("%s%s", repo, suffix[i]); path = mkpath("%s%s", repo, suffix[i]);
if (!stat(path, &st) && S_ISDIR(st.st_mode)) { if (is_directory(path)) {
*is_bundle = 0; *is_bundle = 0;
return xstrdup(make_nonrelative_path(path)); return xstrdup(make_nonrelative_path(path));
} }
@ -140,13 +140,6 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
return xstrndup(start, end - start); 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) static void strip_trailing_slashes(char *dir)
{ {
char *end = dir + strlen(dir); char *end = dir + strlen(dir);

View File

@ -533,6 +533,7 @@ static inline int is_absolute_path(const char *path)
{ {
return path[0] == '/' || has_dos_drive_prefix(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_absolute_path(const char *path);
const char *make_nonrelative_path(const char *path); const char *make_nonrelative_path(const char *path);
const char *make_relative_path(const char *abs, const char *base); const char *make_relative_path(const char *abs, const char *base);

View File

@ -1115,13 +1115,9 @@ int main(int argc, char **argv)
if (strict_paths && (!ok_paths || !*ok_paths)) if (strict_paths && (!ok_paths || !*ok_paths))
die("option --strict-paths requires a whitelist"); die("option --strict-paths requires a whitelist");


if (base_path) { if (base_path && !is_directory(base_path))
struct stat st; die("base-path '%s' does not exist or is not a directory",

base_path);
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 (inetd_mode) { if (inetd_mode) {
struct sockaddr_storage ss; struct sockaddr_storage ss;

View File

@ -319,7 +319,6 @@ static int git_rerere_config(const char *var, const char *value, void *cb)


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


@ -327,7 +326,7 @@ static int is_rerere_enabled(void)
return 0; return 0;


rr_cache = git_path("rr-cache"); 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) if (rerere_enabled < 0)
return rr_cache_exists; return rr_cache_exists;



View File

@ -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) 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(); const char *objdir = get_object_directory();
struct alternate_object_database *ent; struct alternate_object_database *ent;
struct alternate_object_database *alt; struct alternate_object_database *alt;
@ -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; ent->base[pfxlen] = ent->base[entlen-1] = 0;


/* Detect cases where alternate disappeared */ /* 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; " error("object directory %s does not exist; "
"check .git/objects/info/alternates.", "check .git/objects/info/alternates.",
ent->base); ent->base);