commit - 5e1c9f2326a6e3d33c6e0ff547f59e1ef91d62da
commit + d1667f0dbbdff5755402fbb72c95b6940d78ad3e
blob - f28495fa63bafdcb674db7376c3f74985866eaaa
blob + d79664694d4c2376976a212524c0b51cd08c16a2
--- lib/got_lib_path.h
+++ lib/got_lib_path.h
/* 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
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) {
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
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)