/**
* 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 {
if ( xattrs_config.sidecar_dir ) {
xattrs_config.sidecar_dir =
sanitized_source_directory( xattrs_config.sidecar_dir );
- return 0;
}
if (!xattrs_config.source_dir) {