Skip to content

Commit 0be0521

Browse files
mhaggergitster
authored andcommitted
safe_create_leading_directories(): introduce enum for return values
Instead of returning magic integer values (which a couple of callers go to the trouble of distinguishing), return values from an enum. Add a docstring. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9e6f885 commit 0be0521

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

builtin/init-db.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,10 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
515515
saved = shared_repository;
516516
shared_repository = 0;
517517
switch (safe_create_leading_directories_const(argv[0])) {
518-
case -3:
518+
case SCLD_EXISTS:
519519
errno = EEXIST;
520520
/* fallthru */
521-
case -1:
521+
case SCLD_FAILED:
522522
die_errno(_("cannot mkdir %s"), argv[0]);
523523
break;
524524
default:

cache.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,21 @@ enum sharedrepo {
736736
};
737737
int git_config_perm(const char *var, const char *value);
738738
int adjust_shared_perm(const char *path);
739-
int safe_create_leading_directories(char *path);
740-
int safe_create_leading_directories_const(const char *path);
739+
740+
/*
741+
* Create the directory containing the named path, using care to be
742+
* somewhat safe against races. Return one of the scld_error values
743+
* to indicate success/failure.
744+
*/
745+
enum scld_error {
746+
SCLD_OK = 0,
747+
SCLD_FAILED = -1,
748+
SCLD_PERMS = -2,
749+
SCLD_EXISTS = -3
750+
};
751+
enum scld_error safe_create_leading_directories(char *path);
752+
enum scld_error safe_create_leading_directories_const(const char *path);
753+
741754
int mkdir_in_gitdir(const char *path);
742755
extern void home_config_paths(char **global, char **xdg, char *file);
743756
extern char *expand_user_path(const char *path);

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ static int make_room_for_path(struct merge_options *o, const char *path)
693693
/* Make sure leading directories are created */
694694
status = safe_create_leading_directories_const(path);
695695
if (status) {
696-
if (status == -3) {
696+
if (status == SCLD_EXISTS) {
697697
/* something else exists */
698698
error(msg, path, _(": perhaps a D/F conflict?"));
699699
return -1;

sha1_file.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ int mkdir_in_gitdir(const char *path)
105105
return adjust_shared_perm(path);
106106
}
107107

108-
int safe_create_leading_directories(char *path)
108+
enum scld_error safe_create_leading_directories(char *path)
109109
{
110110
char *next_component = path + offset_1st_component(path);
111-
int ret = 0;
111+
enum scld_error ret = SCLD_OK;
112112

113-
while (!ret && next_component) {
113+
while (ret == SCLD_OK && next_component) {
114114
struct stat st;
115115
char *slash = strchr(next_component, '/');
116116

@@ -127,26 +127,26 @@ int safe_create_leading_directories(char *path)
127127
if (!stat(path, &st)) {
128128
/* path exists */
129129
if (!S_ISDIR(st.st_mode))
130-
ret = -3;
130+
ret = SCLD_EXISTS;
131131
} else if (mkdir(path, 0777)) {
132132
if (errno == EEXIST &&
133133
!stat(path, &st) && S_ISDIR(st.st_mode))
134134
; /* somebody created it since we checked */
135135
else
136-
ret = -1;
136+
ret = SCLD_FAILED;
137137
} else if (adjust_shared_perm(path)) {
138-
ret = -2;
138+
ret = SCLD_PERMS;
139139
}
140140
*slash = '/';
141141
}
142142
return ret;
143143
}
144144

145-
int safe_create_leading_directories_const(const char *path)
145+
enum scld_error safe_create_leading_directories_const(const char *path)
146146
{
147147
/* path points to cache entries, so xstrdup before messing with it */
148148
char *buf = xstrdup(path);
149-
int result = safe_create_leading_directories(buf);
149+
enum scld_error result = safe_create_leading_directories(buf);
150150
free(buf);
151151
return result;
152152
}

0 commit comments

Comments
 (0)