Skip to content

Commit 26c8a53

Browse files
author
Linus Torvalds
committed
Add "mkpath()" helper function
I'm bored with doing it by hand all the time.
1 parent 5c5dc2f commit 26c8a53

File tree

4 files changed

+62
-29
lines changed

4 files changed

+62
-29
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ install: $(PROG) $(SCRIPTS)
5656
$(INSTALL) $(PROG) $(SCRIPTS) $(dest)$(bin)
5757

5858
LIB_OBJS=read-cache.o sha1_file.o usage.o object.o commit.o tree.o blob.o \
59-
tag.o date.o index.o diff-delta.o patch-delta.o entry.o \
59+
tag.o date.o index.o diff-delta.o patch-delta.o entry.o path.o \
6060
epoch.o refs.o csum-file.o pack-check.o pkt-line.o connect.o
6161
LIB_FILE=libgit.a
6262
LIB_H=cache.h object.h blob.h tree.h commit.h tag.h delta.h epoch.h csum-file.h \

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ extern void rollback_index_file(struct cache_file *);
158158
#define TYPE_CHANGED 0x0040
159159

160160
/* Return a statically allocated filename matching the sha1 signature */
161+
extern char *mkpath(const char *fmt, ...);
161162
extern char *git_path(const char *fmt, ...);
162163
extern char *sha1_file_name(const unsigned char *sha1);
163164

path.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* I'm tired of doing "vsnprintf()" etc just to open a
3+
* file, so here's a "return static buffer with printf"
4+
* interface for paths.
5+
*
6+
* It's obviously not thread-safe. Sue me. But it's quite
7+
* useful for doing things like
8+
*
9+
* f = open(mkpath("%s/%s.git", base, name), O_RDONLY);
10+
*
11+
* which is what it's designed for.
12+
*/
13+
#include "cache.h"
14+
15+
static char pathname[PATH_MAX];
16+
static char bad_path[] = "/bad-path/";
17+
18+
static char *cleanup_path(char *path)
19+
{
20+
/* Clean it up */
21+
if (!memcmp(path, "./", 2)) {
22+
path += 2;
23+
while (*path == '/')
24+
path++;
25+
}
26+
return path;
27+
}
28+
29+
char *mkpath(const char *fmt, ...)
30+
{
31+
va_list args;
32+
unsigned len;
33+
34+
va_start(args, fmt);
35+
len = vsnprintf(pathname, PATH_MAX, fmt, args);
36+
va_end(args);
37+
if (len >= PATH_MAX)
38+
return bad_path;
39+
return cleanup_path(pathname);
40+
}
41+
42+
char *git_path(const char *fmt, ...)
43+
{
44+
const char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
45+
va_list args;
46+
unsigned len;
47+
48+
len = strlen(git_dir);
49+
if (len > PATH_MAX-100)
50+
return bad_path;
51+
memcpy(pathname, git_dir, len);
52+
if (len && git_dir[len-1] != '/')
53+
pathname[len++] = '/';
54+
va_start(args, fmt);
55+
len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
56+
va_end(args);
57+
if (len >= PATH_MAX)
58+
return bad_path;
59+
return cleanup_path(pathname);
60+
}

sha1_file.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -102,34 +102,6 @@ char *get_index_file(void)
102102
return git_index_file;
103103
}
104104

105-
char *git_path(const char *fmt, ...)
106-
{
107-
static char pathname[PATH_MAX], *ret;
108-
va_list args;
109-
int len;
110-
111-
if (!git_dir)
112-
setup_git_env();
113-
len = strlen(git_dir);
114-
if (len > PATH_MAX-100)
115-
return "pad-path";
116-
memcpy(pathname, git_dir, len);
117-
if (len && git_dir[len-1] != '/')
118-
pathname[len++] = '/';
119-
va_start(args, fmt);
120-
vsnprintf(pathname + len, sizeof(pathname) - len, fmt, args);
121-
va_end(args);
122-
ret = pathname;
123-
124-
/* Clean it up */
125-
if (!memcmp(pathname, "./", 2)) {
126-
ret += 2;
127-
while (*ret == '/')
128-
ret++;
129-
}
130-
return ret;
131-
}
132-
133105
int safe_create_leading_directories(char *path)
134106
{
135107
char *pos = path;

0 commit comments

Comments
 (0)