commit d1667f0dbbdff5755402fbb72c95b6940d78ad3e from: Stefan Sperling date: Mon Mar 11 19:22:28 2019 UTC introduce got_path_dirname() commit - 5e1c9f2326a6e3d33c6e0ff547f59e1ef91d62da commit + d1667f0dbbdff5755402fbb72c95b6940d78ad3e blob - f28495fa63bafdcb674db7376c3f74985866eaaa blob + d79664694d4c2376976a212524c0b51cd08c16a2 --- lib/got_lib_path.h +++ lib/got_lib_path.h @@ -89,3 +89,6 @@ void got_pathlist_free(struct got_pathlist_head *); /* Attempt to create a directory at a given path. */ const struct got_error *got_path_mkdir(const char *); + +/* dirname(3) with error handling and dynamically allocated result. */ +const struct got_error *got_path_dirname(char **, const char *); blob - 0e002c4fcedc1d9aee2f3af1c9556f2b32c4890f blob + 5da704bbed7adcc0af99dee149047248c671a50f --- lib/path.c +++ lib/path.c @@ -273,14 +273,11 @@ static const struct got_error * make_parent_dirs(const char *abspath) { const struct got_error *err = NULL; - char *p, *parent; + char *parent; - p = dirname(abspath); - if (p == NULL) - return got_error_from_errno(); - parent = strdup(p); - if (parent == NULL) - return got_error_from_errno(); + err = got_path_dirname(&parent, abspath); + if (err) + return err; if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) { if (errno == ENOENT) { @@ -317,4 +314,23 @@ got_path_mkdir(const char *abspath) done: return err; +} + +const struct got_error * +got_path_dirname(char **parent, const char *path) +{ + char *p; + + p = dirname(path); + if (p == NULL) + return got_error_from_errno(); + + if (p[0] == '.' && p[1] == '\0') + return got_error(GOT_ERR_BAD_PATH); + + *parent = strdup(p); + if (*parent == NULL) + return got_error_from_errno(); + + return NULL; } blob - 66e45886c2e46b2ad626492278f97eb1568393e7 blob + d2d3fe04e80254afe9c7056ea5e4746a2c02d9ea --- lib/reference.c +++ lib/reference.c @@ -742,19 +742,12 @@ got_ref_write(struct got_reference *ref, struct got_re err = got_opentemp_named(&tmppath, &f, path); if (err) { - char *p, *parent; + char *parent; if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT)) - goto done; - p = dirname(path); - if (p == NULL) { - err = got_error_from_errno(); goto done; - } - parent = strdup(p); - if (parent == NULL) { - err = got_error_from_errno(); + err = got_path_dirname(&parent, path); + if (err) goto done; - } err = got_path_mkdir(parent); free(parent); if (err)