Skip to content

Commit

Permalink
Expand ~ in unique_filename_from_url()
Browse files Browse the repository at this point in the history
unique_filename_from_url() is used for `/url save`.
It doesn't recognize ~ by itself, we need to expand it first.

Mentioned in
#1375 (review)
  • Loading branch information
jubalh committed Dec 9, 2020
1 parent 7a319df commit a2291b3
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,19 +628,41 @@ _basename_from_url(const char* url)
return basename;
}

gchar*
get_expanded_path(const char *path)
{
GString* exp_path = g_string_new("");
gchar *result;

if (strlen(path) >= 2 && path[0] == '~' && path[1] == '/') {
g_string_printf(exp_path, "%s/%s", getenv("HOME"), path+2);
} else {
g_string_printf(exp_path, "%s", path+2);
}

result = exp_path->str;
g_string_free(exp_path, FALSE);

return result;
}

gchar*
unique_filename_from_url(const char* url, const char* path)
{
gchar *realpath;

// Default to './' as path when none has been provided.
if (path == NULL) {
path = "./";
realpath = "./";
} else {
realpath = get_expanded_path(path);
}

// Resolves paths such as './../.' for path.
GFile* target = g_file_new_for_commandline_arg(path);
GFile* target = g_file_new_for_commandline_arg(realpath);
gchar* filename = NULL;

if (_has_directory_suffix(path) || g_file_test(path, G_FILE_TEST_IS_DIR)) {
if (_has_directory_suffix(realpath) || g_file_test(realpath, G_FILE_TEST_IS_DIR)) {
// The target should be used as a directory. Assume that the basename
// should be derived from the URL.
char* basename = _basename_from_url(url);
Expand All @@ -654,11 +676,13 @@ unique_filename_from_url(const char* url, const char* path)
gchar* unique_filename = _unique_filename(filename);
if (unique_filename == NULL) {
g_free(filename);
g_free(realpath);
return NULL;
}

g_object_unref(target);
g_free(filename);
g_free(realpath);

return unique_filename;
}

0 comments on commit a2291b3

Please sign in to comment.