Anonymous avatar Anonymous committed 8c5e84d Merge

Merge "ext4_utils: fix windows build by avoiding asprintf"

Comments (0)

Files changed (1)

ext4_utils/make_ext4fs.c

 	return status;
 }
 
+/* return a newly-malloc'd string that is a copy of str.  The new string
+   is guaranteed to have a trailing slash.  If absolute is true, the new string
+   is also guaranteed to have a leading slash.
+*/
+static char *canonicalize_slashes(const char *str, bool absolute)
+{
+	char *ret;
+	int len = strlen(str);
+	int newlen = len;
+	char *ptr;
+
+	if (len == 0 && absolute) {
+		return strdup("/");
+	}
+
+	if (str[0] != '/' && absolute) {
+		newlen++;
+	}
+	if (str[len - 1] != '/') {
+		newlen++;
+	}
+	ret = malloc(newlen + 1);
+	if (!ret) {
+		critical_error("malloc");
+	}
+
+	ptr = ret;
+	if (str[0] != '/' && absolute) {
+		*ptr++ = '/';
+	}
+
+	strcpy(ptr, str);
+	ptr += len;
+
+	if (str[len - 1] != '/') {
+		*ptr++ = '/';
+	}
+
+	if (ptr != ret + newlen) {
+		critical_error("assertion failed\n");
+	}
+
+	*ptr = '\0';
+
+	return ret;
+}
+
+static char *canonicalize_abs_slashes(const char *str)
+{
+	return canonicalize_slashes(str, true);
+}
+
+static char *canonicalize_rel_slashes(const char *str)
+{
+	return canonicalize_slashes(str, false);
+}
+
 int make_ext4fs_internal(int fd, const char *_directory,
                          const char *_mountpoint, fs_config_func_t fs_config_func, int gzip,
                          int sparse, int crc, int wipe, int init_itabs,
 
 	if (_mountpoint == NULL) {
 		mountpoint = strdup("");
-	} else if (_mountpoint[0] == '\0') {
-		mountpoint = strdup("/");
 	} else {
-		ret = asprintf(&mountpoint, "%s%s%s",
-			       _mountpoint[0] == '/' ? "" : "/",
-			       _mountpoint,
-			       _mountpoint[strlen(_mountpoint) - 1] == '/' ? "" : "/");
-		if (ret < 0)
-			critical_error_errno("asprintf");
+		mountpoint = canonicalize_abs_slashes(_mountpoint);
 	}
 
 	if (_directory) {
-		ret = asprintf(&directory, "%s%s",
-			       _directory,
-			       _directory[strlen(_directory) - 1] == '/' ? "" : "/");
-		if (ret < 0)
-			critical_error_errno("asprintf");
+		directory = canonicalize_rel_slashes(_directory);
 	}
 
 	if (info.len <= 0)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.