Allow .. in paths for userctl

This commit is contained in:
2026-06-17 17:34:07 +02:00
parent 48c52772cb
commit d349d77085
4 changed files with 12 additions and 12 deletions

View File

@@ -47,7 +47,7 @@ static void set_modified_time(Library_Entry *entry) {
static bool create_entry(struct dirent *dir_entry, const char *parent_path, Library_Entry *result) { static bool create_entry(struct dirent *dir_entry, const char *parent_path, Library_Entry *result) {
SV name = sv(dir_entry->d_name); SV name = sv(dir_entry->d_name);
SV parent_path_sv = sv(parent_path); SV parent_path_sv = sv(parent_path);
Noh_String full_path = build_path(false, &parent_path_sv, &name); Noh_String full_path = build_path(false, false, &parent_path_sv, &name);
int type = dir_entry->d_type; int type = dir_entry->d_type;
// Check if the entry is relevant. // Check if the entry is relevant.

View File

@@ -12,12 +12,12 @@ static void append_entry(Noh_String *response, SV url, Library_Entry entry) {
noh_string_append_cstr(response, noh_arena_sprintf(&temp, "<updated>%s</updated>", format_time(entry))); noh_string_append_cstr(response, noh_arena_sprintf(&temp, "<updated>%s</updated>", format_time(entry)));
if (entry.is_dir) { if (entry.is_dir) {
SV opds = sv("opds"); SV opds = sv("opds");
Noh_String entry_url = build_path(true, &opds, &url, &entry.name); Noh_String entry_url = build_path(true, false, &opds, &url, &entry.name);
noh_string_append_cstr(response, noh_arena_sprintf(&temp, "<link rel=\"subsection\" href=\""Nsv_Fmt"\" type=\"application/atom+xml;profile=opds-catalog;kind=navigation\"/>", Nsv_Arg(entry_url))); noh_string_append_cstr(response, noh_arena_sprintf(&temp, "<link rel=\"subsection\" href=\""Nsv_Fmt"\" type=\"application/atom+xml;profile=opds-catalog;kind=navigation\"/>", Nsv_Arg(entry_url)));
noh_string_free(&entry_url); noh_string_free(&entry_url);
} else { } else {
SV get = sv("get"); SV get = sv("get");
Noh_String entry_url = build_path(true, &get, &url, &entry.name); Noh_String entry_url = build_path(true, false, &get, &url, &entry.name);
noh_string_append_cstr(response, noh_arena_sprintf(&temp, "<link rel=\"http://opds-spec.org/acquisition\" href=\""Nsv_Fmt"\" type=\"application/epub+zip\"/>", Nsv_Arg(entry_url))); noh_string_append_cstr(response, noh_arena_sprintf(&temp, "<link rel=\"http://opds-spec.org/acquisition\" href=\""Nsv_Fmt"\" type=\"application/epub+zip\"/>", Nsv_Arg(entry_url)));
noh_string_free(&entry_url); noh_string_free(&entry_url);
} }
@@ -34,7 +34,7 @@ static Noh_String create_feed_response(Library_Entry self_path, SV url, Library_
noh_string_append_cstr(&response, noh_arena_sprintf(&temp, "<updated>%s</updated>", format_time(self_path))); noh_string_append_cstr(&response, noh_arena_sprintf(&temp, "<updated>%s</updated>", format_time(self_path)));
SV opds = sv("opds"); SV opds = sv("opds");
Noh_String self_url = build_path(true, &opds, &url); Noh_String self_url = build_path(true, false, &opds, &url);
noh_string_append_cstr(&response, noh_arena_sprintf(&temp, "<link rel=\"self\" href=\""Nsv_Fmt"\" type=\"application/atom+xml;profile=opds-catalog;kind=navigation\"/>", Nsv_Arg(self_url))); noh_string_append_cstr(&response, noh_arena_sprintf(&temp, "<link rel=\"self\" href=\""Nsv_Fmt"\" type=\"application/atom+xml;profile=opds-catalog;kind=navigation\"/>", Nsv_Arg(self_url)));
noh_string_free(&self_url); noh_string_free(&self_url);

View File

@@ -1,5 +1,5 @@
#define build_path(url, first, ...) build_path_((url), (first), __VA_ARGS__, NULL) #define build_path(url, first, ...) build_path_((url), (first), __VA_ARGS__, NULL)
static Noh_String build_path_(bool url, Noh_String_View *first, ...) { static Noh_String build_path_(bool url, bool go_up, Noh_String_View *first, ...) {
va_list args; va_list args;
Noh_String_View *elem_ = first; Noh_String_View *elem_ = first;
va_start(args, first); va_start(args, first);
@@ -11,7 +11,7 @@ static Noh_String build_path_(bool url, Noh_String_View *first, ...) {
Noh_String_View elem = *elem_; Noh_String_View elem = *elem_;
while (elem.count > 0) { while (elem.count > 0) {
Noh_String_View component = noh_sv_chop_by_delim(&elem, '/'); Noh_String_View component = noh_sv_chop_by_delim(&elem, '/');
if (noh_sv_eq(component, noh_sv_from_cstr(".."))) continue; if (noh_sv_eq(component, noh_sv_from_cstr("..")) && !go_up) continue;
if (component.count > 0) { if (component.count > 0) {
if (start) start = false; else noh_string_append_cstr(&result, "/"); if (start) start = false; else noh_string_append_cstr(&result, "/");

View File

@@ -85,7 +85,7 @@ bool register_user(Noh_String_View library_path, Noh_String_View username, Noh_S
// Read the file. // Read the file.
Noh_String_View file_name = noh_sv_from_cstr("users.conf"); Noh_String_View file_name = noh_sv_from_cstr("users.conf");
Noh_String users_file_path = build_path(false, &library_path, &file_name); Noh_String users_file_path = build_path(false, true, &library_path, &file_name);
noh_string_append_null(&users_file_path); noh_string_append_null(&users_file_path);
// Build users map. // Build users map.
@@ -114,7 +114,7 @@ bool register_user(Noh_String_View library_path, Noh_String_View username, Noh_S
if (!write_users_file(users_file_path.elems, &users, NULL)) noh_return_defer(false); if (!write_users_file(users_file_path.elems, &users, NULL)) noh_return_defer(false);
// Create user folder if needed. // Create user folder if needed.
user_folder_path = build_path(false, &library_path, &username); user_folder_path = build_path(false, true, &library_path, &username);
noh_string_append_null(&user_folder_path); noh_string_append_null(&user_folder_path);
if (!noh_mkdir_if_needed(user_folder_path.elems)) noh_return_defer(false); if (!noh_mkdir_if_needed(user_folder_path.elems)) noh_return_defer(false);
@@ -127,7 +127,7 @@ bool register_user(Noh_String_View library_path, Noh_String_View username, Noh_S
if (!write_users_file(users_file_path.elems, &users, NULL)) noh_return_defer(false); if (!write_users_file(users_file_path.elems, &users, NULL)) noh_return_defer(false);
// Create user folder if needed. // Create user folder if needed.
user_folder_path = build_path(false, &library_path, &username); user_folder_path = build_path(false, true, &library_path, &username);
noh_string_append_null(&user_folder_path); noh_string_append_null(&user_folder_path);
if (!noh_mkdir_if_needed(user_folder_path.elems)) noh_return_defer(false); if (!noh_mkdir_if_needed(user_folder_path.elems)) noh_return_defer(false);
@@ -145,7 +145,7 @@ bool delete_user(Noh_String_View library_path, Noh_String_View username) {
// Read the file. // Read the file.
Noh_String_View file_name = noh_sv_from_cstr("users.conf"); Noh_String_View file_name = noh_sv_from_cstr("users.conf");
Noh_String users_file_path = build_path(false, &library_path, &file_name); Noh_String users_file_path = build_path(false, true, &library_path, &file_name);
noh_string_append_null(&users_file_path); noh_string_append_null(&users_file_path);
// Build users map. // Build users map.
@@ -180,7 +180,7 @@ bool delete_user(Noh_String_View library_path, Noh_String_View username) {
if (!write_users_file(users_file_path.elems, &users, user_to_delete)) noh_return_defer(false); if (!write_users_file(users_file_path.elems, &users, user_to_delete)) noh_return_defer(false);
// Prompt to delete the user's library folder. // Prompt to delete the user's library folder.
user_folder_path = build_path(false, &library_path, &username); user_folder_path = build_path(false, true, &library_path, &username);
noh_string_append_null(&user_folder_path); noh_string_append_null(&user_folder_path);
if (!noh_file_exists(user_folder_path.elems)) noh_return_defer(true); if (!noh_file_exists(user_folder_path.elems)) noh_return_defer(true);
@@ -200,7 +200,7 @@ defer:
bool list_users(Noh_String_View library_path) { bool list_users(Noh_String_View library_path) {
// Read the file. // Read the file.
Noh_String_View file_name = noh_sv_from_cstr("users.conf"); Noh_String_View file_name = noh_sv_from_cstr("users.conf");
Noh_String users_file_path = build_path(false, &library_path, &file_name); Noh_String users_file_path = build_path(false, true, &library_path, &file_name);
noh_string_append_null(&users_file_path); noh_string_append_null(&users_file_path);
// Build users map. // Build users map.