Skip to content

Commit 0539ecf

Browse files
Jojo-Schmitzgitster
authored andcommitted
compat: some mkdir() do not like a slash at the end
Introduce a compatibility helper for platforms with such a mkdir(). Signed-off-by: Joachim Schmitz <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fab4b04 commit 0539ecf

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

compat/mkdir.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "../git-compat-util.h"
2+
#undef mkdir
3+
4+
/* for platforms that can't deal with a trailing '/' */
5+
int compat_mkdir_wo_trailing_slash(const char *dir, mode_t mode)
6+
{
7+
int retval;
8+
char *tmp_dir = NULL;
9+
size_t len = strlen(dir);
10+
11+
if (len && dir[len-1] == '/') {
12+
if ((tmp_dir = strdup(dir)) == NULL)
13+
return -1;
14+
tmp_dir[len-1] = '\0';
15+
}
16+
else
17+
tmp_dir = (char *)dir;
18+
19+
retval = mkdir(tmp_dir, mode);
20+
if (tmp_dir != dir)
21+
free(tmp_dir);
22+
23+
return retval;
24+
}

git-compat-util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
#define probe_utf8_pathname_composition(a,b)
163163
#endif
164164

165+
#ifdef MKDIR_WO_TRAILING_SLASH
166+
#define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))
167+
extern int compat_mkdir_wo_trailing_slash(const char*, mode_t);
168+
#endif
169+
165170
#ifndef NO_LIBGEN_H
166171
#include <libgen.h>
167172
#else

0 commit comments

Comments
 (0)