From d55890b5047fbc363698d28c0ffb4a9c9b47e010 Mon Sep 17 00:00:00 2001 From: Alan Deutscher Date: Wed, 19 Feb 2020 12:51:54 -0800 Subject: [PATCH 1/2] Add handling for dead symbolic links. --- updog/templates/home.html | 14 +++++++++++--- updog/utils/path.py | 38 ++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/updog/templates/home.html b/updog/templates/home.html index c53a4a3..d15c2d8 100644 --- a/updog/templates/home.html +++ b/updog/templates/home.html @@ -75,14 +75,20 @@

Directory: {{ directory }}

{% for file in files %} - {% if file.is_dir %} + {% if not file.exists %} + + {% elif file.is_dir %} {% else %} {% endif %} + {% if file.exists %} {{ file.name }}{% if file.is_dir %}/{% endif %} + {% else %} + {{ file.name }}{% if file.is_dir %}/{% endif %} + {% endif %} {{ file.size }} @@ -91,7 +97,9 @@

Directory: {{ directory }}

{{ file.last_modified }} - {% if not file.is_dir %} + {% if file.is_symlink and not file.exists %} + Dead Symbolic Link + {% elif not file.is_dir %} View in browser {% endif %} @@ -115,4 +123,4 @@

Directory: {{ directory }}

- \ No newline at end of file + diff --git a/updog/utils/path.py b/updog/utils/path.py index cde0c3f..0c82b8f 100644 --- a/updog/utils/path.py +++ b/updog/utils/path.py @@ -30,21 +30,35 @@ def human_readable_file_size(size): def process_files(directory_files, base_directory): files = [] - for file in directory_files: - if file.is_dir(): - size = '--' - size_sort = -1 - else: - size = human_readable_file_size(file.stat().st_size) - size_sort = file.stat().st_size + for file_entry in directory_files: + + real_path = os.path.realpath(os.path.join(base_directory, file_entry.name)) + + # Default size + size = '--' + size_sort = -1 + + if os.path.isfile(real_path): + # Item is a file or a symlink points to a real file + size_sort = file_entry.stat().st_size + size = human_readable_file_size(size_sort) + + last_modified = '???' + last_modified_sort = -1 + if os.path.exists(real_path): + last_modified_sort = file_entry.stat().st_mtime + last_modified = ctime(last_modified_sort) + files.append({ - 'name': file.name, - 'is_dir': file.is_dir(), - 'rel_path': get_relative_path(file.path, base_directory), + 'name': file_entry.name, + 'is_dir': file_entry.is_dir(), + 'rel_path': get_relative_path(file_entry.path, base_directory), 'size': size, 'size_sort': size_sort, - 'last_modified': ctime(file.stat().st_mtime), - 'last_modified_sort': file.stat().st_mtime + 'last_modified': last_modified, + 'last_modified_sort': last_modified_sort, + 'is_symlink': file_entry.is_symlink(), + 'exists': os.path.exists(real_path) }) return files From 5dec63dbb1487c496e5dd430e2fc37e1cfcb4f5e Mon Sep 17 00:00:00 2001 From: Alan Deutscher Date: Mon, 24 Feb 2020 23:22:29 -0800 Subject: [PATCH 2/2] Correct listing of a walled-off symbolic link (fixing #19), and correct a problem in this branch with processing subdirectories --- updog/__main__.py | 2 +- updog/utils/path.py | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/updog/__main__.py b/updog/__main__.py index 63d5e06..e20c811 100644 --- a/updog/__main__.py +++ b/updog/__main__.py @@ -106,7 +106,7 @@ def home(path): if os.path.exists(requested_path): # Read the files try: - directory_files = process_files(os.scandir(requested_path), base_directory) + directory_files = process_files(os.scandir(requested_path), base_directory, requested_path) except PermissionError: abort(403, 'Read Permission Denied: ' + requested_path) diff --git a/updog/utils/path.py b/updog/utils/path.py index 0c82b8f..986d25e 100644 --- a/updog/utils/path.py +++ b/updog/utils/path.py @@ -28,37 +28,42 @@ def human_readable_file_size(size): return '{:.4g} {}'.format(size / (1 << (order * 10)), _suffixes[order]) -def process_files(directory_files, base_directory): +def process_files(directory_files, base_directory, requested_path): files = [] + for file_entry in directory_files: - real_path = os.path.realpath(os.path.join(base_directory, file_entry.name)) + path = os.path.join(requested_path, file_entry.name) + + # Default values for display/sort fields - # Default size + # Size size = '--' size_sort = -1 - if os.path.isfile(real_path): - # Item is a file or a symlink points to a real file - size_sort = file_entry.stat().st_size - size = human_readable_file_size(size_sort) - + # Last-Modified date last_modified = '???' last_modified_sort = -1 - if os.path.exists(real_path): + + if os.path.exists(path): last_modified_sort = file_entry.stat().st_mtime last_modified = ctime(last_modified_sort) + if os.path.isfile(path): + # Existing item is a file or a symlink points to a real file + size_sort = file_entry.stat().st_size + size = human_readable_file_size(size_sort) + files.append({ 'name': file_entry.name, - 'is_dir': file_entry.is_dir(), + 'is_dir': os.path.isdir(path), 'rel_path': get_relative_path(file_entry.path, base_directory), 'size': size, 'size_sort': size_sort, 'last_modified': last_modified, 'last_modified_sort': last_modified_sort, 'is_symlink': file_entry.is_symlink(), - 'exists': os.path.exists(real_path) + 'exists': os.path.exists(path) }) return files