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 <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jan 6, 2014 at 14:45 UTC 0be0521b23f46dac586e1b464fafe8b97027b645
4 files changed +26 -13
builtin/init-db.c
+2 -2
@@ -515,10 +515,10 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
515 saved = shared_repository;
516 shared_repository = 0;
517 switch (safe_create_leading_directories_const(argv[0])) {
518 - case -3:
518 + case SCLD_EXISTS:
519 errno = EEXIST;
520 /* fallthru */
521 - case -1:
521 + case SCLD_FAILED:
522 die_errno(_("cannot mkdir %s"), argv[0]);
523 break;
524 default:
cache.h
+15 -2
@@ -736,8 +736,21 @@ enum sharedrepo {
736 };
737 int git_config_perm(const char *var, const char *value);
738 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 +
754 int mkdir_in_gitdir(const char *path);
755 extern void home_config_paths(char **global, char **xdg, char *file);
756 extern char *expand_user_path(const char *path);
merge-recursive.c
+1 -1
@@ -693,7 +693,7 @@ static int make_room_for_path(struct merge_options *o, const char *path)
693 /* Make sure leading directories are created */
694 status = safe_create_leading_directories_const(path);
695 if (status) {
696 - if (status == -3) {
696 + if (status == SCLD_EXISTS) {
697 /* something else exists */
698 error(msg, path, _(": perhaps a D/F conflict?"));
699 return -1;
sha1_file.c
+8 -8
@@ -105,12 +105,12 @@ int mkdir_in_gitdir(const char *path)
105 return adjust_shared_perm(path);
106 }
107
108 -int safe_create_leading_directories(char *path)
108 +enum scld_error safe_create_leading_directories(char *path)
109 {
110 char *next_component = path + offset_1st_component(path);
111 - int ret = 0;
111 + enum scld_error ret = SCLD_OK;
112
113 - while (!ret && next_component) {
113 + while (ret == SCLD_OK && next_component) {
114 struct stat st;
115 char *slash = strchr(next_component, '/');
116
@@ -127,26 +127,26 @@ int safe_create_leading_directories(char *path)
127 if (!stat(path, &st)) {
128 /* path exists */
129 if (!S_ISDIR(st.st_mode))
130 - ret = -3;
130 + ret = SCLD_EXISTS;
131 } else if (mkdir(path, 0777)) {
132 if (errno == EEXIST &&
133 !stat(path, &st) && S_ISDIR(st.st_mode))
134 ; /* somebody created it since we checked */
135 else
136 - ret = -1;
136 + ret = SCLD_FAILED;
137 } else if (adjust_shared_perm(path)) {
138 - ret = -2;
138 + ret = SCLD_PERMS;
139 }
140 *slash = '/';
141 }
142 return ret;
143 }
144
145 -int safe_create_leading_directories_const(const char *path)
145 +enum scld_error safe_create_leading_directories_const(const char *path)
146 {
147 /* path points to cache entries, so xstrdup before messing with it */
148 char *buf = xstrdup(path);
149 - int result = safe_create_leading_directories(buf);
149 + enum scld_error result = safe_create_leading_directories(buf);
150 free(buf);
151 return result;
152 }