On 2018/02/16 4:04, Omar Sandoval wrote:
> From: Omar Sandoval <osan...@fb.com>

> +static enum btrfs_util_error openat_parent_and_name(int dirfd, const char 
> *path,
> +                                                 char *name, size_t name_len,
> +                                                 int *fd)
> +{
> +     char *tmp_path, *slash, *dirname, *basename;
> +     size_t len;
> +
> +     /* Ignore trailing slashes. */
> +     len = strlen(path);
> +     while (len > 1 && path[len - 1] == '/')
> +             len--;
> +
> +     tmp_path = malloc(len + 1);
> +     if (!tmp_path)
> +             return BTRFS_UTIL_ERROR_NO_MEMORY;
> +     memcpy(tmp_path, path, len);
> +     tmp_path[len] = '\0';
> +
> +     slash = memrchr(tmp_path, '/', len);
> +     if (slash == tmp_path) {
> +             dirname = "/";
> +             basename = tmp_path + 1;
> +     } else if (slash) {
> +             *slash = '\0';
> +             dirname = tmp_path;
> +             basename = slash + 1;
> +     } else {
> +             dirname = ".";
> +             basename = tmp_path;
> +     }
> +
> +     len = strlen(basename);
> +     if (len >= name_len) {
> +             errno = ENAMETOOLONG;

tmp_path should be also freed here.

> +             return BTRFS_UTIL_ERROR_INVALID_ARGUMENT;
> +     }
> +     memcpy(name, basename, len);
> +     name[len] = '\0';
> +
> +     *fd = openat(dirfd, dirname, O_RDONLY | O_DIRECTORY);
> +     if (*fd == -1) {
> +             free(tmp_path);
> +             return BTRFS_UTIL_ERROR_OPEN_FAILED;
> +     }
> +
> +     free(tmp_path);
> +     return BTRFS_UTIL_OK;
> +}


--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to