/*
- * Copyright (c) 2009-2016 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2009-2017 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
/*
* Create any parent directories needed by path (but not path itself).
+ * Note that path is modified but is restored before it returns.
*/
bool
sudo_mkdir_parents(char *path, uid_t uid, gid_t *gidp, mode_t mode, bool quiet)
struct stat sb;
gid_t parent_gid = 0;
char *slash = path;
- bool ret = true;
debug_decl(sudo_mkdir_parents, SUDOERS_DEBUG_UTIL)
/* If no gid specified, inherit from parent dir. */
if (errno != EEXIST) {
if (!quiet)
sudo_warn(U_("unable to mkdir %s"), path);
- ret = false;
- break;
+ goto bad;
}
/* Already exists, make sure it is a directory. */
if (stat(path, &sb) != 0) {
if (!quiet)
sudo_warn(U_("unable to stat %s"), path);
- ret = false;
- break;
+ goto bad;
}
if (!S_ISDIR(sb.st_mode)) {
if (!quiet)
sudo_warnx(U_("%s exists but is not a directory (0%o)"),
path, (unsigned int) sb.st_mode);
- ret = false;
- break;
+ goto bad;
}
/* Inherit gid of parent dir for ownership. */
if (*gidp == (gid_t)-1)
}
/* Return parent gid if none was specified by caller. */
- if (ret && *gidp == (gid_t)-1)
+ if (*gidp == (gid_t)-1)
*gidp = parent_gid;
- debug_return_bool(ret);
+ debug_return_bool(true);
+bad:
+ /* We must restore the path before we return. */
+ *slash = '/';
+ debug_return_bool(false);
}