Skip to content

Commit d2794b0

Browse files
authored
Merge pull request libgit2#3877 from libgit2/ethomson/paths_init
sysdir: don't assume an empty dir is uninitialized
2 parents 0d84de0 + 031d34b commit d2794b0

File tree

2 files changed

+41
-52
lines changed

2 files changed

+41
-52
lines changed

src/sysdir.c

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -83,45 +83,43 @@ static int git_sysdir_guess_template_dirs(git_buf *out)
8383
#endif
8484
}
8585

86-
typedef int (*git_sysdir_guess_cb)(git_buf *out);
87-
88-
static git_buf git_sysdir__dirs[GIT_SYSDIR__MAX] =
89-
{ GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT };
86+
struct git_sysdir__dir {
87+
git_buf buf;
88+
int (*guess)(git_buf *out);
89+
};
9090

91-
static git_sysdir_guess_cb git_sysdir__dir_guess[GIT_SYSDIR__MAX] = {
92-
git_sysdir_guess_system_dirs,
93-
git_sysdir_guess_global_dirs,
94-
git_sysdir_guess_xdg_dirs,
95-
git_sysdir_guess_programdata_dirs,
96-
git_sysdir_guess_template_dirs,
91+
static struct git_sysdir__dir git_sysdir__dirs[] = {
92+
{ GIT_BUF_INIT, git_sysdir_guess_system_dirs },
93+
{ GIT_BUF_INIT, git_sysdir_guess_global_dirs },
94+
{ GIT_BUF_INIT, git_sysdir_guess_xdg_dirs },
95+
{ GIT_BUF_INIT, git_sysdir_guess_programdata_dirs },
96+
{ GIT_BUF_INIT, git_sysdir_guess_template_dirs },
9797
};
9898

99-
static int git_sysdir__dirs_shutdown_set = 0;
99+
static void git_sysdir_global_shutdown(void)
100+
{
101+
size_t i;
102+
103+
for (i = 0; i < ARRAY_SIZE(git_sysdir__dirs); ++i)
104+
git_buf_free(&git_sysdir__dirs[i].buf);
105+
}
100106

101107
int git_sysdir_global_init(void)
102108
{
103-
git_sysdir_t i;
104-
const git_buf *path;
109+
size_t i;
105110
int error = 0;
106111

107-
for (i = 0; !error && i < GIT_SYSDIR__MAX; i++)
108-
error = git_sysdir_get(&path, i);
112+
for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++)
113+
error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf);
109114

110-
return error;
111-
}
115+
git__on_shutdown(git_sysdir_global_shutdown);
112116

113-
void git_sysdir_global_shutdown(void)
114-
{
115-
int i;
116-
for (i = 0; i < GIT_SYSDIR__MAX; ++i)
117-
git_buf_free(&git_sysdir__dirs[i]);
118-
119-
git_sysdir__dirs_shutdown_set = 0;
117+
return error;
120118
}
121119

122120
static int git_sysdir_check_selector(git_sysdir_t which)
123121
{
124-
if (which < GIT_SYSDIR__MAX)
122+
if (which < ARRAY_SIZE(git_sysdir__dirs))
125123
return 0;
126124

127125
giterr_set(GITERR_INVALID, "config directory selector out of range");
@@ -137,18 +135,7 @@ int git_sysdir_get(const git_buf **out, git_sysdir_t which)
137135

138136
GITERR_CHECK_ERROR(git_sysdir_check_selector(which));
139137

140-
if (!git_buf_len(&git_sysdir__dirs[which])) {
141-
/* prepare shutdown if we're going to need it */
142-
if (!git_sysdir__dirs_shutdown_set) {
143-
git__on_shutdown(git_sysdir_global_shutdown);
144-
git_sysdir__dirs_shutdown_set = 1;
145-
}
146-
147-
GITERR_CHECK_ERROR(
148-
git_sysdir__dir_guess[which](&git_sysdir__dirs[which]));
149-
}
150-
151-
*out = &git_sysdir__dirs[which];
138+
*out = &git_sysdir__dirs[which].buf;
152139
return 0;
153140
}
154141

@@ -183,31 +170,38 @@ int git_sysdir_set(git_sysdir_t which, const char *search_path)
183170
if (search_path != NULL)
184171
expand_path = strstr(search_path, PATH_MAGIC);
185172

186-
/* init with default if not yet done and needed (ignoring error) */
187-
if ((!search_path || expand_path) &&
188-
!git_buf_len(&git_sysdir__dirs[which]))
189-
git_sysdir__dir_guess[which](&git_sysdir__dirs[which]);
173+
/* reset the default if this path has been cleared */
174+
if (!search_path || expand_path)
175+
git_sysdir__dirs[which].guess(&git_sysdir__dirs[which].buf);
190176

191177
/* if $PATH is not referenced, then just set the path */
192-
if (!expand_path)
193-
return git_buf_sets(&git_sysdir__dirs[which], search_path);
178+
if (!expand_path) {
179+
if (search_path)
180+
git_buf_sets(&git_sysdir__dirs[which].buf, search_path);
181+
182+
goto done;
183+
}
194184

195185
/* otherwise set to join(before $PATH, old value, after $PATH) */
196186
if (expand_path > search_path)
197187
git_buf_set(&merge, search_path, expand_path - search_path);
198188

199-
if (git_buf_len(&git_sysdir__dirs[which]))
189+
if (git_buf_len(&git_sysdir__dirs[which].buf))
200190
git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR,
201-
merge.ptr, git_sysdir__dirs[which].ptr);
191+
merge.ptr, git_sysdir__dirs[which].buf.ptr);
202192

203193
expand_path += strlen(PATH_MAGIC);
204194
if (*expand_path)
205195
git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path);
206196

207-
git_buf_swap(&git_sysdir__dirs[which], &merge);
197+
git_buf_swap(&git_sysdir__dirs[which].buf, &merge);
208198
git_buf_free(&merge);
209199

210-
return git_buf_oom(&git_sysdir__dirs[which]) ? -1 : 0;
200+
done:
201+
if (git_buf_oom(&git_sysdir__dirs[which].buf))
202+
return -1;
203+
204+
return 0;
211205
}
212206

213207
static int git_sysdir_find_in_dirlist(

src/sysdir.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,4 @@ extern int git_sysdir_get_str(char *out, size_t outlen, git_sysdir_t which);
103103
*/
104104
extern int git_sysdir_set(git_sysdir_t which, const char *paths);
105105

106-
/**
107-
* Free the configuration file search paths.
108-
*/
109-
extern void git_sysdir_global_shutdown(void);
110-
111106
#endif /* INCLUDE_sysdir_h__ */

0 commit comments

Comments
 (0)