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>
Junio C Hamano committed
Sep 9, 2008 at 01:27 UTC
90b4a71c493bf24f11b5edee8a519110624a6bea
6 files changed
+19
-20
abspath.c
+12
-1
@@ -1,5 +1,16 @@
1
#include "cache.h"
2
3
+/*
4
+ * Do not use this for inspecting *tracked* content. When path is a
5
+ * symlink to a directory, we do not want to say it is a directory when
6
+ * dealing with tracked content in the working tree.
7
+ */
8
+int is_directory(const char *path)
9
+{
10
+ struct stat st;
11
+ return (!stat(path, &st) && S_ISDIR(st.st_mode));
12
+}
13
+
14
/* We allow "recursive" symbolic links. Only within reason, though. */
15
#define MAXDEPTH 5
16
@@ -17,7 +28,7 @@ const char *make_absolute_path(const char *path)
28
die ("Too long path: %.*s", 60, path);
29
30
while (depth--) {
20
- if (stat(buf, &st) || !S_ISDIR(st.st_mode)) {
31
+ if (!is_directory(buf)) {
32
char *last_slash = strrchr(buf, '/');
33
if (last_slash) {
34
*last_slash = '\0';
builtin-clone.c
+1
-8
@@ -77,7 +77,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
77
for (i = 0; i < ARRAY_SIZE(suffix); i++) {
78
const char *path;
79
path = mkpath("%s%s", repo, suffix[i]);
80
- if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
80
+ if (is_directory(path)) {
81
*is_bundle = 0;
82
return xstrdup(make_nonrelative_path(path));
83
}
@@ -140,13 +140,6 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
140
return xstrndup(start, end - start);
141
}
142
143
-static int is_directory(const char *path)
144
-{
145
- struct stat buf;
146
-
147
- return !stat(path, &buf) && S_ISDIR(buf.st_mode);
148
-}
149
-
143
static void strip_trailing_slashes(char *dir)
144
{
145
char *end = dir + strlen(dir);
cache.h
+1
@@ -533,6 +533,7 @@ static inline int is_absolute_path(const char *path)
533
{
534
return path[0] == '/' || has_dos_drive_prefix(path);
535
}
536
+int is_directory(const char *);
537
const char *make_absolute_path(const char *path);
538
const char *make_nonrelative_path(const char *path);
539
const char *make_relative_path(const char *abs, const char *base);
daemon.c
+3
-7
@@ -1115,13 +1115,9 @@ int main(int argc, char **argv)
1115
if (strict_paths && (!ok_paths || !*ok_paths))
1116
die("option --strict-paths requires a whitelist");
1117
1118
- if (base_path) {
1119
- struct stat st;
1120
-
1121
- if (stat(base_path, &st) || !S_ISDIR(st.st_mode))
1122
- die("base-path '%s' does not exist or "
1123
- "is not a directory", base_path);
1124
- }
1118
+ if (base_path && !is_directory(base_path))
1119
+ die("base-path '%s' does not exist or is not a directory",
1120
+ base_path);
1121
1122
if (inetd_mode) {
1123
struct sockaddr_storage ss;
rerere.c
+1
-2
@@ -319,7 +319,6 @@ static int git_rerere_config(const char *var, const char *value, void *cb)
319
320
static int is_rerere_enabled(void)
321
{
322
- struct stat st;
322
const char *rr_cache;
323
int rr_cache_exists;
324
@@ -327,7 +326,7 @@ static int is_rerere_enabled(void)
326
return 0;
327
328
rr_cache = git_path("rr-cache");
330
- rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
329
+ rr_cache_exists = is_directory(rr_cache);
330
if (rerere_enabled < 0)
331
return rr_cache_exists;
332
sha1_file.c
+1
-2
@@ -250,7 +250,6 @@ static void read_info_alternates(const char * alternates, int depth);
250
*/
251
static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth)
252
{
253
- struct stat st;
253
const char *objdir = get_object_directory();
254
struct alternate_object_database *ent;
255
struct alternate_object_database *alt;
@@ -281,7 +280,7 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
280
ent->base[pfxlen] = ent->base[entlen-1] = 0;
281
282
/* Detect cases where alternate disappeared */
284
- if (stat(ent->base, &st) || !S_ISDIR(st.st_mode)) {
283
+ if (!is_directory(ent->base)) {
284
error("object directory %s does not exist; "
285
"check .git/objects/info/alternates.",
286
ent->base);