From: Ralph Ronnquist Date: Thu, 11 Nov 2021 08:07:24 +0000 (+1100) Subject: rework sanitized_source_directory using realpath X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=a93f22e050e3bc971f341ab4580d39266713133b;p=rrq%2Ffuse_xattrs.git rework sanitized_source_directory using realpath --- diff --git a/fuse_xattrs.c b/fuse_xattrs.c index 01d4d26..3539c65 100644 --- a/fuse_xattrs.c +++ b/fuse_xattrs.c @@ -194,45 +194,20 @@ static struct fuse_operations xmp_oper = { /** * Check if the path is valid. If it's a relative path, - * prepend the working path. + * prepend the working path. Ensure it ends with / * @param path relative or absolute path to eval. * @return new string with absolute path */ const char *sanitized_source_directory(const char *path) { - char *absolute_path; - if (strlen(path) == 0) { - return NULL; + char *absolute_path = stralloc( PATH_MAX ); + if ( ( strlen( path ) == 0 ) || + ( realpath( path, absolute_path ) == 0 ) || + ( is_directory( absolute_path ) == -1 ) ) { + return NULL; // Path badness } - /* absolute path, we don't do anything */ - if (path[0] == '/') { - if (is_directory(path) == -1) { - return NULL; - } - absolute_path = strdup(path); - return absolute_path; - } - - static char cwd[MAXPATHLEN]; - char *pwd = getcwd(cwd, sizeof(cwd)); - size_t len = strlen(pwd) + 1 + strlen(path) + 1; - int has_trailing_backslash = (path[strlen(path)-1] == '/'); - if (!has_trailing_backslash) - len++; - - absolute_path = (char*) malloc(sizeof(char) * len); - memset(absolute_path, '\0', len); - sprintf(absolute_path, "%s/%s", pwd, path); - - if(!has_trailing_backslash) - absolute_path[len-2] = '/'; - - if (is_directory(absolute_path) == -1) { - strfree(absolute_path); - return NULL; - } - - return absolute_path; + // Append "/" and allocate new memory for the string + return strdup( strcat( absolute_path, "/" ) ); } enum { @@ -304,7 +279,6 @@ int main(int argc, char *argv[]) { if ( xattrs_config.sidecar_dir ) { xattrs_config.sidecar_dir = sanitized_source_directory( xattrs_config.sidecar_dir ); - return 0; } if (!xattrs_config.source_dir) {